// Adds all possible king moves to the given moves list private void addKingsMovesToList(Player i_Player, Board i_Board, List <Move> i_MovesList, ref bool capturePossible) { if (MoveValidator.IsSimpleMove(i_Player, i_Board, new Move(this.Location.Y, this.Location.X, this.Location.Y + 1, this.Location.X + 1))) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y + 1, this.Location.X + 1)); } if (MoveValidator.IsSimpleMove(i_Player, i_Board, new Move(this.Location.Y, this.Location.X, this.Location.Y - 1, this.Location.X + 1))) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y - 1, this.Location.X + 1)); } if (checkKingCapture(i_Board, -1, new Move(this.Location.Y, this.Location.X, this.Location.Y - 2, this.Location.X + 2))) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y - 2, this.Location.X + 2)); capturePossible = true; } if (checkKingCapture(i_Board, 1, new Move(this.Location.Y, this.Location.X, this.Location.Y + 2, this.Location.X + 2))) { i_MovesList.Add(new Move(this.Location.Y, this.Location.X, this.Location.Y + 2, this.Location.X + 2)); capturePossible = true; } }
// Execute the given move, returns true if it was executed successfully public void ExecuteMove(Move i_Move) { eMoveFeedback moveFeedback = eMoveFeedback.Failed; Piece piece = m_Board.GameBoard[i_Move.FromRow, i_Move.FromCol].CurrentPiece; if (piece != null) { // User chose simple move if (MoveValidator.IsSimpleMove(CurrentPlayer, m_Board, i_Move)) { // If the player can capture- the move fails (he must choose to capture) if (MoveValidator.IsPlayerHasCapture(CurrentPlayer, m_Board)) { moveFeedback = eMoveFeedback.FailedCouldCapture; } else { // If the moving piece is NOT a king if (piece.IsKing == false) { // If tries to move in the opposite direction- the move fails if (MoveValidator.IsMovingInValidDirection(CurrentPlayer, i_Move, piece) == false) { moveFeedback = eMoveFeedback.Failed; } else { m_Board.MovePiece(CurrentPlayer, piece, new Point(i_Move.ToRow, i_Move.ToCol)); moveFeedback = eMoveFeedback.Success; } } // moving piece is a king else { m_Board.MovePiece(CurrentPlayer, piece, new Point(i_Move.ToRow, i_Move.ToCol)); moveFeedback = eMoveFeedback.Success; } } } // User chose capture move else if (MoveValidator.IsCaptureMovePossible(CurrentPlayer, m_Board, i_Move)) { Player enemyPlayer = CurrentPlayer == PlayerOne ? PlayerTwo : PlayerOne; m_Board.MoveCapturingPiece(CurrentPlayer, enemyPlayer, piece, new Point(i_Move.ToRow, i_Move.ToCol)); moveFeedback = eMoveFeedback.Success; // check double capture option if (MoveValidator.CanPieceCapture(m_Board, m_Board.GameBoard[i_Move.ToRow, i_Move.ToCol].CurrentPiece)) { moveFeedback = eMoveFeedback.CanDoubleCapture; } } } if (moveFeedback == eMoveFeedback.Success) { m_TurnCount++; } // Notify listeners of move execution completed MoveExecuted?.Invoke(moveFeedback); }