Added functions to check if a ploayer has won and one to check if the board is full.
This commit is contained in:
parent
ac198ec6a8
commit
983ab4ff65
|
@ -1 +1,35 @@
|
|||
package gamecontrol
|
||||
|
||||
// checks if a player has won the game
|
||||
func PlayerHasWon(board [3][3]string, player string) bool {
|
||||
for i := 0; i < 3; i++ {
|
||||
// check rows for a win
|
||||
if board[i][0] == player && board[i][1] == player && board[i][2] == player {
|
||||
return true
|
||||
}
|
||||
// check columns for a win
|
||||
if board[0][i] == player && board[1][i] == player && board[2][i] == player {
|
||||
return true
|
||||
}
|
||||
}
|
||||
// check diagonals for a win
|
||||
if board[0][0] == player && board[1][1] == player && board[2][2] == player {
|
||||
return true
|
||||
}
|
||||
if board[0][2] == player && board[1][1] == player && board[2][0] == player {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// iterating through each field, checking if it is empty
|
||||
func BoardisFull(board [3][3]string) bool {
|
||||
for i := 0; i < 3; i++ {
|
||||
for j := 0; j < 3; j++ {
|
||||
if board[i][j] == " " {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue