Exemple #1
0
Fichier : ttt.go Projet : qrush/go
// Runs a round of the tic tac toe game.
// * Get move from player 1
// * Update game board
// * Notify player 2
// * Check if the game is done
// * If not, get move from player 2
// * Update game board
// * Notify player 1
// * Check if the game is done
func (this *tttRef) Turn(player1, player2 games.View) (done bool) {
	p1d := make(chan string)
	p2d := make(chan string)

	go games.Listen(this, player1, p1d)
	p1m := <-p1d
	//fmt.Println("derp got p1move")
	this.board[p1m] = p1mark
	player2.Set(p1m)
	player2.Display()
	//fmt.Println("did display on p2")

	if done = this.Done(player1, player2); !done {

		go games.Listen(this, player2, p2d)
		p2m := <-p2d
		//fmt.Println("derp got p2move")
		this.board[p2m] = p2mark
		player1.Set(p2m)
		player1.Display()
		//fmt.Println("did display on p1")
		done = this.Done(player1, player2)

	}

	return
}
Exemple #2
0
Fichier : rps.go Projet : qrush/go
// A round of rock paper scissors:
// * Wait for input from both players at the same time
// * Let the other players know the result
func (this *rpsRef) Turn(player1, player2 games.View) bool {
	p1d := make(chan string)
	p2d := make(chan string)

	go games.Listen(this, player1, p1d)
	go games.Listen(this, player2, p2d)

	this.p1move = <-p1d
	player2.Set(this.p1move)
	this.p2move = <-p2d
	player1.Set(this.p2move)

	player1.Display()
	player2.Display()

	return this.Done(player1, player2)
}