Пример #1
0
Файл: rps.go Проект: qrush/go
// Checks which player won the game, or if a draw happened.
// * If the moves are the same, draw
// * Otherwise depending on player 1's move
// ** look up the outcome
// ** reverse for the other player
func (this *rpsRef) Done(player1, player2 games.View) bool {
	p1out := games.Draw
	p2out := games.Draw

	if this.p1move != this.p2move {
		switch this.p1move {
		case rock:
			p1out = rockOutcome[this.p2move]
		case scissors:
			p1out = scissorsOutcome[this.p2move]
		case paper:
			p1out = paperOutcome[this.p2move]
		}

		if p1out == games.Win {
			p2out = games.Lose
		} else {
			p2out = games.Win
		}
	}

	player1.Done(p1out)
	player2.Done(p2out)
	return false
}
Пример #2
0
Файл: ttt.go Проект: 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
}
Пример #3
0
Файл: rps.go Проект: 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)
}
Пример #4
0
Файл: ttt.go Проект: qrush/go
// Determines if the game is over. Game is over if:
// * All board elements are marked (draw)
// * A winning outcome is on the board and is owned by one player
func (this *tttRef) Done(player1, player2 games.View) (done bool) {
	draw := true

	for _, value := range this.board {
		if value == 0 {
			draw = false
			break
		}
	}

	if draw {
		player1.Done(games.Draw)
		player2.Done(games.Draw)
		return draw
	}

	for _, outcome := range outcomes {
		first := this.board[outcome[0]]
		if math.Fabs(float64(first+this.board[outcome[1]]+this.board[outcome[2]])) == 3 {
			if first == p1mark {
				player1.Done(games.Win)
				player2.Done(games.Lose)
			} else {
				player1.Done(games.Lose)
				player2.Done(games.Win)
			}

			done = true
		}
	}

	return done
}