add: new tictactoe game
add: testing to tictactoe rename: tictactoe add: tictactoe steps refactor: tictactoe const names refactor: tictactoe loop add: tictactoe bad switch example (fallthrough) refactor: tictactoe loop skin remove: tictactoe changable skin refactor: tictactoe all remove: tictactoe unnecessary base dir add: tictactoe slices add: tictactoe slices remove: tictactoe fallthrough rename: tictactoe slices 10 -> 09 update: loops skin tictactoe add: tictactoe randomization add: tictactoe infinite loop and labeled break refactor: tictactoe rand and infinite loop add: tictactoe buggy winning algo add: tictactoe more tests rename: tictactoe wrongPlay to wrongMove add: tictactoe even more tests fix: tictactoe rename: tictactoe waitForInput to wait add: tictactoe os.args gameSpeed remove: tictactoe unnecessary files rename: tictactoe game messages refactor: tictactoe main loop add: types and arrays
This commit is contained in:
55
x-tba/tictactoe/13-detect-winning/ending.go
Normal file
55
x-tba/tictactoe/13-detect-winning/ending.go
Normal file
@@ -0,0 +1,55 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user