tictactoe-in-go/gameControl/check.go

36 lines
897 B
Go
Raw Normal View History

2024-04-24 19:28:48 +00:00
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
}