public GameEngine(HumanPlayer humanPlayer, PCPlayer pcPlayer, Player firstPlayerTurn) { if (humanPlayer.XOSymbol == pcPlayer.XOSymbol) throw new Exception("Invalid Players.You can not give the PC and the Human player the same XO symbol"); HumanPlayer = humanPlayer; PCPlayer = pcPlayer; this.currentPlayerTurn = firstPlayerTurn; gamePad = new XOSymbol[3, 3]; winnerPlayer = null; isGameOver = false; }
/// <summary> /// Rules to be validated /// 1- Game is not over AND /// 2- The player own this turn AND /// 3- The XOSymbol position is valid /// </summary> /// <param name="player"></param> /// <param name="gameMove"></param> /// <returns></returns> private bool validateGameMove(Player player, GameMove gameMove) { return (!IsGameOver && player.Equals(currentPlayerTurn) && validatePosition(gameMove)); }
/// <summary> /// Switch between players. /// For instnace if the Human played, it will give the turn to PC /// </summary> private void switchPlayers() { if (currentPlayerTurn.Equals(PCPlayer)) currentPlayerTurn = HumanPlayer; else currentPlayerTurn = PCPlayer; }
/// <summary> /// 1- Set the gameover flag /// 2- Find the winner /// </summary> /// <param name="winnerXOSymbol"></param> private void endGame( XOSymbol winnerXOSymbol) { isGameOver = true; if (humanPlayer.XOSymbol == winnerXOSymbol) { winnerPlayer = humanPlayer; return; } if (pcPlayer.XOSymbol == winnerXOSymbol) { winnerPlayer = pcPlayer; return; } }
public bool MakeMove(Player player, GameMove gameMove) { if (!validateGameMove(player, gameMove)) { return false; } //Write the move in the GamePad int rowIndex, columnIndex; rowIndex = getRowIndex(gameMove.Position); columnIndex = getColumnIndex(gameMove.Position); gamePad.SetValue(gameMove.XOSymbol, rowIndex, columnIndex); //Switch the player turns switchPlayers(); //Process Game status to see whether there is a Winner, or the game ends processGame(); //Drive the PCPlayer to Play if (currentPlayerTurn.Equals(PCPlayer) && !IsGameOver) pcPlayer.Play(); return true; }
private void showWinner(Player player) { if (player == null) { MessageBox.Show("Game finished with no winner"); return; } if (player is HumanPlayer) { MessageBox.Show("You win :)"); return; } if (player is PCPlayer) { MessageBox.Show("You lose :("); return; } }