private void HandleStart() { // Fire start event if (OnGameStart != null) { GameBoardEventArgs gameBoardEventArgs = new GameBoardEventArgs(board, boardState); OnGameStart(this, gameBoardEventArgs); } if (boardState.CurrentPlayer == PieceType.Red) { // Red player is first gameState = GameState.RedToMove; } else { // Black player is first gameState = GameState.BlackToMove; } // Fire player event if (OnPlayerChanged != null) { if (boardState.CurrentPlayer == PieceType.Red) { PlayerEventArgs playerEventArgs = new PlayerEventArgs(board, boardState, redPlayer); OnPlayerChanged(this, playerEventArgs); } else { PlayerEventArgs playerEventArgs = new PlayerEventArgs(board, boardState, blackPlayer); OnPlayerChanged(this, playerEventArgs); } } }
private void run() { while (gameState != GameState.End) { if (gameState == GameState.Start) { HandleStart(); } else if (gameState == GameState.RedToMove) { // Ask the player for a move Move move = redPlayer.GetMove(board, boardState); boardState = boardState.PlayMove(move); // Fire the event player moved if (OnPlayerMove != null) { PlayerMoveEventArgs playerMoveEventArgs = new PlayerMoveEventArgs(board, boardState, redPlayer, move); OnPlayerMove(this, playerMoveEventArgs); } gameState = GameState.RedMoved; } else if (gameState == GameState.RedMoved) { // Check if there are any possible moves Move[] moves = board.GetPossibleMoves(boardState); if (moves.Length == 0) { if (OnGameEnd != null) { GameBoardEventArgs gameBoardEventArgs = new GameBoardEventArgs(board, boardState); OnGameEnd(this, gameBoardEventArgs); } gameState = GameState.End; } else { if (OnPlayerChanged != null) { PlayerEventArgs playerEventArgs = new PlayerEventArgs(board, boardState, blackPlayer); OnPlayerChanged(this, playerEventArgs); } gameState = GameState.BlackToMove; } } else if (gameState == GameState.BlackToMove) { // Ask the player for a move Move move = blackPlayer.GetMove(board, boardState); boardState = boardState.PlayMove(move); // Fire the event player moved if (OnPlayerMove != null) { PlayerMoveEventArgs playerMoveEventArgs = new PlayerMoveEventArgs(board, boardState, blackPlayer, move); OnPlayerMove(this, playerMoveEventArgs); } gameState = GameState.BlackMoved; } else if (gameState == GameState.BlackMoved) { // Check if there are any possible moves Move[] moves = board.GetPossibleMoves(boardState); if (moves.Length == 0) { if (OnGameEnd != null) { GameBoardEventArgs gameBoardEventArgs = new GameBoardEventArgs(board, boardState); OnGameEnd(this, gameBoardEventArgs); } gameState = GameState.End; } else { if (OnPlayerChanged != null) { PlayerEventArgs playerEventArgs = new PlayerEventArgs(board, boardState, redPlayer); OnPlayerChanged(this, playerEventArgs); } gameState = GameState.RedToMove; } } } }