Exemplo n.º 1
0
Arquivo: rps.go Projeto: 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
}
Exemplo n.º 2
0
Arquivo: ttt.go Projeto: 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
}