// Returns a capture move from all possible moves of all pieces private Move findCaptureMove(Board i_Board, List <List <Move> > i_AllMovesList) { foreach (List <Move> list in i_AllMovesList) { foreach (Move move in list) { if (MoveValidator.IsCaptureMovePossible(this, i_Board, move)) { return(move); } } } return(null); }
// 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); }