Files
learngo/x-tba/tictactoe/14-more-tests/ending.go

59 lines
1.3 KiB
Go
Raw Normal View History

package main
// -------------------------------------------------
// IS THERE A WINNER? OR IS IT A TIE?
// -------------------------------------------------
// /---+---+---\
// | 0 | 1 | 2 |
// +---+---+---+
// | 3 | 4 | 5 |
// +---+---+---+
// | 6 | 7 | 8 |
// \---+---+---/
func checkWinOrTie() {
// intentional bug: tie shouldn't happen before winning detection
// if tie = turn == maxTurns; tie {
// // if tie don't check for the winning
// return
// }
// loop over all the players
for i := 1; i <= 2; i++ {
// check for the next player
p := player2
if i == 1 {
p = player1
}
/* check horizontals */
hor := (cells[0] == p && cells[1] == p && cells[2] == p) ||
(cells[3] == p && cells[4] == p && cells[5] == p) ||
(cells[6] == p && cells[7] == p && cells[8] == p)
/* check verticals */
ver := (cells[0] == p && cells[3] == p && cells[6] == p) ||
(cells[1] == p && cells[4] == p && cells[7] == p) ||
(cells[2] == p && cells[5] == p && cells[8] == p)
/* check diagonals */
diag := (cells[0] == p && cells[4] == p && cells[8] == p) ||
(cells[2] == p && cells[4] == p && cells[6] == p)
// any winner?
if hor || ver || diag {
won = true
// this player wins
player = p
// there is a winner so don't check for tie!
return
}
}
// check for tie
tie = turn == maxTurns
}