public void Play(int index) { // Check if he can play if (IsGameOver) { throw new GameException(GameErrorType.GameOver); } if (index <= 0 && index >= 8) { throw new GameException(GameErrorType.IndexOutOfRange); } if (plays[index] != GamePlay.None) { throw new GameException(GameErrorType.IndexPlayedOn); } // Play plays[index] = CurrentPlayer; // Chack if current player wins foreach (var winLine in Winners) { if (plays[winLine[0]] == GamePlay.None || plays[winLine[1]] == GamePlay.None || plays[winLine[2]] == GamePlay.None) { continue; } if (plays[winLine[0]] == plays[winLine[1]] && plays[winLine[1]] == plays[winLine[2]]) { Winning = new GameWin(CurrentPlayer, winLine); IsGameOver = true; Played?.Invoke(this, EventArgs.Empty); GameOver?.Invoke(this, new GameOverEventArgs(Winning)); return; } } // Chack if game is over with no winner if (!plays.Contains(GamePlay.None) || (!plays.Select((value, i) => new { value, index = i }) .Where((play) => Array.IndexOf(new int[] { 0, 1, 2, 6, 7, 8 }, play.index) > -1) .Select(p => p.value).Contains(GamePlay.None) && plays[0] == plays[2] && plays[2] == plays[7] && plays[1] == plays[6] && plays[6] == plays[8]) || (!plays.Select((value, i) => new { value, index = i }) .Where((play) => Array.IndexOf(new int[] { 0, 2, 3, 5, 6, 8 }, play.index) > -1) .Select(p => p.value).Contains(GamePlay.None) && plays[0] == plays[5] && plays[5] == plays[6] && plays[2] == plays[3] && plays[3] == plays[8])) { Winning = new GameWin(GamePlay.None, null); IsGameOver = true; Played?.Invoke(this, EventArgs.Empty); GameOver?.Invoke(this, new GameOverEventArgs(Winning)); return; } Played?.Invoke(this, EventArgs.Empty); // If not over get ready for next round CurrentPlayer = (GamePlay)((int)CurrentPlayer % 2 + 1); // Auto play if only one left if (plays.Where((p) => p == GamePlay.None).Count() == 1) { Play(plays.IndexOf(GamePlay.None)); } }
void FindWinner(Occupier Occupier) { var isWon = false; for (var n = 0; n < 8; ++n) { if (_waysToWin[n, 0].Occupier != Occupier || _waysToWin[n, 1].Occupier != Occupier || _waysToWin[n, 2].Occupier != Occupier) { continue; } isWon = true; break; } if (isWon) { if (Occupier == Occupier.Player1) { if (GameType == GameType.SinglePlayer) { Stats.SinglePlayer.Player.Increment(Level); } else { Stats.TwoPlayer.Player1++; } _playerStarts = true; GameOver?.Invoke(Occupier.Player1); } else { if (GameType == GameType.SinglePlayer) { Stats.SinglePlayer.Computer.Increment(Level); } else { Stats.TwoPlayer.Player2++; } _playerStarts = false; GameOver?.Invoke(Occupier.ComputerOrPlayer2); } } else { if (_done > 8) { if (GameType == GameType.SinglePlayer) { Stats.SinglePlayer.Draws.Increment(Level); } else { Stats.TwoPlayer.Draws++; } _playerStarts = !_playerStarts; GameOver?.Invoke(Occupier.None); } else if (Occupier == Occupier.Player1 && GameType == GameType.SinglePlayer) { ComputerTurn(); } } }