From 983ab4ff653357e882abcb80bc366bce4e4e59b4 Mon Sep 17 00:00:00 2001 From: Falko Habel Date: Tue, 23 Apr 2024 11:34:35 +0200 Subject: [PATCH] Added functions to check if a ploayer has won and one to check if the board is full. --- gameControl/check.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/gameControl/check.go b/gameControl/check.go index 3b5eea1..610f04e 100644 --- a/gameControl/check.go +++ b/gameControl/check.go @@ -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 +}