Ejemplo n.º 1
0
        /// <summary>
        /// Caclulate the king's legal moves
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public override ICollection <Move> calculateLegalMoves(Board board)
        {
            IList <Move> legalMoves = new List <Move>();

            foreach (int currentCandidateOffset in CANDIDATE_MOVE_COORDINATES)
            {
                //Check for first column exclusion for the king
                if (isFirstColumnExclusion(this.piecePosition, currentCandidateOffset) ||
                    isEighthColumnExclusion(this.piecePosition, currentCandidateOffset))
                {
                    continue;
                }
                int candidateDestinationCoordinate = this.piecePosition + currentCandidateOffset;
                if (BoardUtils.IsValidTileCoordinate(candidateDestinationCoordinate))
                {
                    Tile candidateDestinationTile = board.getTile(candidateDestinationCoordinate);
                    //If the destination tile is not occupied
                    if (!candidateDestinationTile.isTileOccupied())
                    {
                        legalMoves.Add(new MajorMove(board, this, candidateDestinationCoordinate));
                    }
                    //If the destination tile is occupied
                    else
                    {
                        Piece    pieceAtDestination = candidateDestinationTile.getPiece();
                        Alliance pieceAlliance      = pieceAtDestination.getPieceAlliance();
                        if (this.pieceAlliance != pieceAlliance)
                        {
                            legalMoves.Add(new MajorAttackMove(board, this, candidateDestinationCoordinate, pieceAtDestination));
                        }
                    }
                }
            }
            return(legalMoves.ToList());
        }
Ejemplo n.º 2
0
        /// <summary>
        /// calculate all legal moves possible for the bishop
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public override ICollection <Move> calculateLegalMoves(Board board)
        {
            IList <Move> legalMoves = new List <Move>();

            foreach (int currentCandidateOffset in CANDIDATE_MOVE_COORDINATES)
            {
                int candidateDestinationCoordinate = this.piecePosition;
                while (true)
                {
                    //First or Last column exclusion break
                    if (IsFirstColumnExclusion(candidateDestinationCoordinate, currentCandidateOffset) ||
                        IsEighthColumnExclusion(candidateDestinationCoordinate, currentCandidateOffset))
                    {
                        break;
                    }
                    candidateDestinationCoordinate += currentCandidateOffset;
                    if (!BoardUtils.IsValidTileCoordinate(candidateDestinationCoordinate))
                    {
                        break;
                    }
                    else
                    {
                        //Set the tile as a valid candidate
                        Tile candidateDestinationTile = board.getTile(candidateDestinationCoordinate);

                        //If the candidate Tile is not Occupied
                        if (!candidateDestinationTile.isTileOccupied())
                        {
                            //Add move as legal
                            legalMoves.Add(new MajorMove(board, this, candidateDestinationCoordinate));
                        }
                        else
                        {
                            //Tile is occupied
                            Piece    pieceAtDestination         = candidateDestinationTile.getPiece();
                            Alliance pieceAtDestinationAlliance = pieceAtDestination.getPieceAlliance();
                            //If the piece alliance of the piece at the target tile
                            //is not the same as the piece alliace of this object...
                            if (this.pieceAlliance != pieceAtDestinationAlliance)
                            {
                                //Add the move into the list of legal moves
                                legalMoves.Add(new MajorAttackMove(board, this, candidateDestinationCoordinate, pieceAtDestination));
                            }
                            break;
                        }
                    }
                }
            }
            return(legalMoves.ToList());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Calculates the leagal moves for a knight and returns a list of
        /// all possible moves
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public override ICollection <Move> calculateLegalMoves(Board board)
        {
            int          candidateDestinationCoordinate;
            IList <Move> legalMoves = new List <Move>();

            foreach (int currentCandidateOffset in CANDIDATE_MOVE_COORDINATES)
            {
                candidateDestinationCoordinate = this.piecePosition + currentCandidateOffset;
                //If the tile is a valid tile coordinate for this piece...
                if (BoardUtils.IsValidTileCoordinate(candidateDestinationCoordinate))
                {
                    //Check Column exclusions for the knight
                    if (isFirstColumnExclusion(this.piecePosition, currentCandidateOffset) ||
                        isSecondColumExclusion(this.piecePosition, currentCandidateOffset) ||
                        isSeventhColumExclusion(this.piecePosition, currentCandidateOffset) ||
                        isEighthColumExclusion(this.piecePosition, currentCandidateOffset))
                    {
                        continue;
                    }
                    //Set the tile as a valid candidate
                    Tile candidateDestinationTile = board.getTile(candidateDestinationCoordinate);

                    //If the candidate Tile is not Occupied
                    if (!candidateDestinationTile.isTileOccupied())
                    {
                        //Add move as legal
                        legalMoves.Add(new MajorMove(board, this, candidateDestinationCoordinate));
                    }
                    else
                    {
                        //Tile is occupied
                        Piece    pieceAtDestination = candidateDestinationTile.getPiece();
                        Alliance pieceAlliance      = pieceAtDestination.getPieceAlliance();
                        //If the piece alliance of the piece at the target tile
                        //is not the same as the piece alliace of this object...
                        if (this.pieceAlliance != pieceAlliance)
                        {
                            //Add the move into the list of legal moves
                            legalMoves.Add(new MajorAttackMove(board, this, candidateDestinationCoordinate, pieceAtDestination));
                        }
                    }
                }
            }
            //Return the calculated legal moves
            return(legalMoves.ToList());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Pawn legal moves calculations
        /// </summary>
        /// <param name="board"></param>
        /// <returns></returns>
        public override ICollection <Move> calculateLegalMoves(Board board)
        {
            //List of all possible legal moves
            IList <Move> legalMoves = new List <Move>();

            foreach (int currentCandidateOffset in CANDIDATE_MOVE_COORDINATES)
            {
                int candidateDestinationCoordinate = this.piecePosition + (this.getPieceAlliance().getDirection() * currentCandidateOffset);
                //Check if the candidateCoordinate is a valid board coordinate
                if (!BoardUtils.IsValidTileCoordinate(candidateDestinationCoordinate))
                {
                    continue;
                }
                //Pawns can only move up or down in direction based on alliance
                if (currentCandidateOffset == 8 && !board.getTile(candidateDestinationCoordinate).isTileOccupied())
                {
                    //Check for pawn promotion
                    if (this.pieceAlliance.isPawnPromotionSquare(candidateDestinationCoordinate))
                    {
                        //if the pawn is eligible for promotion then promote it
                        legalMoves.Add(new PawnPromotion(new PawnMove(board, this, candidateDestinationCoordinate)));
                    }
                    else
                    {
                        //Add this pawn move as a legal move
                        legalMoves.Add(new PawnMove(board, this, candidateDestinationCoordinate));
                    }
                }
                //if the pawns are on starting position then pawns can travel two tiles up or down
                else if (currentCandidateOffset == 16 && this.IsFirstMove() &&
                         ((BoardUtils.SECOND_RANK[this.piecePosition] && this.getPieceAlliance().isWhite()) ||
                          (BoardUtils.SEVENTH_RANK[this.piecePosition] && this.getPieceAlliance().isBlack())))
                {
                    int behindCandidateDestinationCoordinate = this.piecePosition + (this.pieceAlliance.getDirection() * 8);
                    //Check if the candidate tile is occupied
                    if (!board.getTile(behindCandidateDestinationCoordinate).isTileOccupied() &&
                        !board.getTile(candidateDestinationCoordinate).isTileOccupied())
                    {
                        //Add this pawn move as a legal move
                        legalMoves.Add(new PawnJump(board, this, candidateDestinationCoordinate));
                    }
                }
                //Attack move on 7 tile move
                else if (currentCandidateOffset == 7 &&
                         !((BoardUtils.EIGHTH_COLUMN[this.piecePosition] && this.getPieceAlliance().isWhite()) ||
                           (BoardUtils.FIRST_COLUMN[this.piecePosition] && this.getPieceAlliance().isBlack())))
                {
                    if (board.getTile(candidateDestinationCoordinate).isTileOccupied())
                    {
                        Piece pieceOnCandidate = board.getTile(candidateDestinationCoordinate).getPiece();
                        //Opponent piece alliance check
                        if (this.pieceAlliance != pieceOnCandidate.getPieceAlliance())
                        {
                            //Check for pawn promotion
                            if (this.pieceAlliance.isPawnPromotionSquare(candidateDestinationCoordinate))
                            {
                                //if the pawn is eligible for promotion then promote it
                                legalMoves.Add(new PawnPromotion(new PawnAttackMove(board, this, candidateDestinationCoordinate, pieceOnCandidate)));
                            }
                            else
                            {
                                //Attack Move
                                legalMoves.Add(new PawnAttackMove(board, this, candidateDestinationCoordinate, pieceOnCandidate));
                            }
                        }
                    }
                    //En Passant Condition
                    else if (board.getEnPassantPawn() != null)
                    {
                        if (board.getEnPassantPawn().getPiecePosition() == this.piecePosition + (this.pieceAlliance.getOppositeDirection()))
                        {
                            Piece pieceOnCandidate = board.getEnPassantPawn();
                            if (this.pieceAlliance != pieceOnCandidate.getPieceAlliance())
                            {
                                legalMoves.Add(new PawnEnPassantAttackMove(board, this, candidateDestinationCoordinate, pieceOnCandidate));
                            }
                        }
                    }
                }
                //Attack move on 9 tile move
                else if (currentCandidateOffset == 9 &&
                         !((BoardUtils.FIRST_COLUMN[this.piecePosition] && this.getPieceAlliance().isWhite()) ||
                           (BoardUtils.EIGHTH_COLUMN[this.piecePosition] && this.getPieceAlliance().isBlack())))
                {
                    if (board.getTile(candidateDestinationCoordinate).isTileOccupied())
                    {
                        Piece pieceOnCandidate = board.getTile(candidateDestinationCoordinate).getPiece();
                        if (this.pieceAlliance != pieceOnCandidate.getPieceAlliance())
                        {
                            //Check for pawn promotion
                            if (this.pieceAlliance.isPawnPromotionSquare(candidateDestinationCoordinate))
                            {
                                //if the pawn is eligible for promotion then promote it
                                legalMoves.Add(new PawnPromotion(new PawnAttackMove(board, this, candidateDestinationCoordinate, pieceOnCandidate)));
                            }
                            else
                            {
                                //Attack Move
                                legalMoves.Add(new PawnAttackMove(board, this, candidateDestinationCoordinate, pieceOnCandidate));
                            }
                        }
                    }
                    //En Passant Condition
                    else if (board.getEnPassantPawn() != null)
                    {
                        if (board.getEnPassantPawn().getPiecePosition() == this.piecePosition - (this.pieceAlliance.getOppositeDirection()))
                        {
                            Piece pieceOnCandidate = board.getEnPassantPawn();
                            if (this.pieceAlliance != pieceOnCandidate.getPieceAlliance())
                            {
                                legalMoves.Add(new PawnEnPassantAttackMove(board, this, candidateDestinationCoordinate, pieceOnCandidate));
                            }
                        }
                    }
                }
            }
            return(legalMoves.ToList());
        }