/// <summary> /// Function called to declare a winner and end the game /// </summary> private void EndGame() { aiThoughtTimer1.Enabled = false; aiThoughtTimer2.Enabled = false; playing = false; if (playerinfo1.Tokens == playerinfo2.Tokens) { Speak($"Game Ended, It's a draw"); MessageBox.Show( $"Game Ended, It's a draw.", "Game Ended", MessageBoxButtons.OK, MessageBoxIcon.Information ); } else { Playerinfo winner = (playerinfo1.Tokens > playerinfo2.Tokens) ? ref playerinfo1 : ref playerinfo2; Speak($"Game Ended, {winner.PlayerName} won with {winner.Tokens} tokens!"); MessageBox.Show( $"Game Ended, {winner.PlayerName} won with {winner.Tokens} tokens!", "Game Ended", MessageBoxButtons.OK, MessageBoxIcon.Information ); } }
/// <summary> /// Call this function to switch which player's currently playing. /// </summary> private void SwitchPlayer() { if (!playing) { return; } currentPlayer.PlayerTurn = false; currentPlayer = (currentPlayer == playerinfo1) ? playerinfo2 : playerinfo1; currentPlayer.PlayerTurn = true; if (currentPlayer.CellValue == CellValues.black) { this.Icon = Properties.Resources.Icon_GridBlack; } else if (currentPlayer.CellValue == CellValues.white) { this.Icon = Properties.Resources.Icon_GridWhite; } else { this.Icon = Properties.Resources.Icon_GridBase; } if (!AnyLegalMoves() && playing) { if (!currentPlayer.isAI) { Speak($"No Legal moves found, Skipping {currentPlayer.PlayerName}'s turn", false, true); } if (previousPlayerPassed == true) { EndGame(); } else { previousPlayerPassed = true; SwitchPlayer(); } } if (currentPlayer.isAI) { currentPlayer.AItimer.Enabled = true; } }
/// <summary> /// Function called to setup the board and start a new game. /// </summary> private void StartGame(int rows, int columns) { gameGrid1.Rows = rows; gameGrid1.Columns = columns; gameGrid1.InitializeGrid(); gameVals = new CellValues[gameGrid1.Columns, gameGrid1.Rows]; for (int x = 0; x < gameGrid1.Columns; x++) { for (int y = 0; y < gameGrid1.Rows; y++) { gameVals[x, y] = CellValues.blank; } } int cx = gameGrid1.Columns / 2; int cy = gameGrid1.Rows / 2; SetCell(cx - 1, cy - 1, CellValues.black); SetCell(cx, cy, CellValues.black); SetCell(cx, cy - 1, CellValues.white); SetCell(cx - 1, cy, CellValues.white); playerinfo1.Tokens = 2; playerinfo1.PlayerTurn = true; playerinfo2.Tokens = 2; playerinfo2.PlayerTurn = false; Speak($"Starting Game, {playerinfo1.PlayerName} as black versus {playerinfo2.PlayerName} as white.", true); currentPlayer = playerinfo1; this.Icon = Properties.Resources.Icon_GridBlack; aiThoughtTimer1.Enabled = true; playing = true; }