Ejemplo n.º 1
0
        public object Clone()
        {
            Piece newPiece;

            switch (this.GetType().FullName)
            {
            case "King":
                newPiece = new King(this.Color, this.Id);
                break;

            case "Queen":
                newPiece = new Queen(this.Color, this.Id);
                break;

            case "Rook":
                newPiece = new Rook(this.Color, this.Id);
                break;

            case "Bishop":
                newPiece = new Bishop(this.Color, this.Id);
                break;

            case "Knight":
                newPiece = new Knight(this.Color, this.Id);
                break;

            case "Pawn":
                newPiece = new Pawn(this.Color, this.Id);
                break;

            default:
                return(null);
            }

            newPiece.Location = this.Location;
            newPiece.Moved    = this.Moved;
            newPiece.Captured = this.Captured;

            return(newPiece);
        }
Ejemplo n.º 2
0
        public override List <Move> getLegalMoves(Board board)
        {
            List <Move> legalMoves = new List <Move>();

            foreach (int argument in Rook.legalMoveArguments)
            {
                int unCheckedPosition = this.piecePosition;

                while (BoardUtils.checkedForLegalPosition(unCheckedPosition))
                {
                    if (Rook.firstColumnViolation(unCheckedPosition, argument) ||
                        Rook.eightColumnViolation(unCheckedPosition, argument))
                    {
                        break;
                    }

                    unCheckedPosition += argument;
                    if (BoardUtils.checkedForLegalPosition(unCheckedPosition))
                    {
                        Cell currentCell = board.getCell(unCheckedPosition);
                        if (!currentCell.isCellOccupied())
                        {
                            legalMoves.Add(new NormalMove(board, this, unCheckedPosition));
                        }
                        else
                        {
                            if (this.pieceSide != currentCell.getPiece().getSide())
                            {
                                legalMoves.Add(new AttackMove(board, this, unCheckedPosition, currentCell.getPiece()));
                            }
                            break;
                        }
                    }
                }
            }
            return(legalMoves);
        }
Ejemplo n.º 3
0
        public void SetupBoard()
        {
            piece[0] = new Rook(false, "rbk");
            piece[1] = new Knight(false, "nbk");
            piece[2] = new Bishop(false, "bbk");
            piece[3] = new Queen(false, "qb");
            piece[4] = new King(false, "kb");
            piece[5] = new Bishop(false, "bbq");
            piece[6] = new Knight(false, "nbq");
            piece[7] = new Rook(false, "rbq");

            piece[8]  = new Pawn(false, "pba");
            piece[9]  = new Pawn(false, "pbb");
            piece[10] = new Pawn(false, "pbc");
            piece[11] = new Pawn(false, "pbd");
            piece[12] = new Pawn(false, "pbe");
            piece[13] = new Pawn(false, "pbf");
            piece[14] = new Pawn(false, "pbg");
            piece[15] = new Pawn(false, "pbh");

            piece[16] = new Rook(true, "rwk");
            piece[17] = new Knight(true, "nwk");
            piece[18] = new Bishop(true, "bwk");
            piece[19] = new Queen(true, "qw");
            piece[20] = new King(true, "kw");
            piece[21] = new Bishop(true, "bbw");
            piece[22] = new Knight(true, "nbw");
            piece[23] = new Rook(true, "rbw");

            piece[24] = new Pawn(true, "pba");
            piece[25] = new Pawn(true, "pbb");
            piece[26] = new Pawn(true, "pbc");
            piece[27] = new Pawn(true, "pbd");
            piece[28] = new Pawn(true, "pbe");
            piece[29] = new Pawn(true, "pbf");
            piece[30] = new Pawn(true, "pbg");
            piece[31] = new Pawn(true, "pbh");

            // set the first turn to white
            turn = true;

            // Adds the black home row to the board
            for (int i = 0; i < 8; i++)
            {
                piece[i].Location = new Point(i, 0);
            }

            // Adds the black pawns to the board
            for (int i = 8; i < 16; i++)
            {
                piece[i].Location = new Point(i - 8, 1);
            }

            // Adds the white home row to the board
            for (int i = 16; i < 24; i++)
            {
                piece[i].Location = new Point(i - 16, 7);
            }

            // Adds the White pawns to the board
            for (int i = 24; i < 32; i++)
            {
                piece[i].Location = new Point(i - 24, 6);
            }
        }
Ejemplo n.º 4
0
        public bool CheckMove(Point position, int index)
        {
            if (index != -1)
            {
                if (piece[index].Color == turn)
                {
                    Point newLoc = position;
                    newLoc = Piece.boardToPiece(newLoc);
                    bool valid    = piece[index].movePiece(newLoc);
                    bool nBlocked = Rules.checkPiecePath(piece[index], newLoc, piece);

                    if (check.isCheck && check.checkColor == turn)
                    {
                        Piece tempPiece = (Piece)piece[index].Clone();
                        tempPiece.Location = newLoc;
                        if (Rules.isCheck(piece, piece[index].AttackedSquares(piece), turn))
                        {
                            return(false);
                        }
                    }

                    if (valid && nBlocked)
                    {
                        if (Rules.CheckCapture(newLoc, piece[index], piece, out int capIndex))
                        {
                            piece[capIndex].Captured = true;
                        }
                        piece[index].Location = newLoc;
                        piece[index].Moved    = true;
                        bool ischeck = Rules.isCheck(piece, piece[index].AttackedSquares(piece), turn);
                        check.isCheck    = ischeck;
                        check.checkColor = turn;

                        pieceMove move;
                        move.piece       = piece[index];
                        move.newLocation = newLoc;
                        move.capture     = piece[capIndex].Captured;
                        move.check       = ischeck;
                        file.updatePgn(move);

                        return(true);
                    }
                    else if (!valid && nBlocked && piece[index].GetType().Equals(typeof(King)))
                    {
                        Piece rook  = new Rook();
                        bool  check = Rules.checkCastle(piece[index], newLoc, piece, out int location);

                        if (check)
                        {
                            Point rLoc = piece[location].Location;
                            if (rLoc.X - piece[index].Location.X < 0)
                            {
                                rLoc.X = newLoc.X + 1;
                            }
                            else if (rLoc.X - piece[index].Location.X > 0)
                            {
                                rLoc.X = newLoc.X - 1;
                            }

                            pieceMove move;
                            move.piece       = piece[index];
                            move.newLocation = newLoc;
                            move.capture     = false;
                            move.check       = true;
                            file.updatePgn(move);

                            piece[location].Location = rLoc;
                            piece[index].Location    = newLoc;
                            piece[location].Moved    = true;
                            piece[index].Moved       = true;

                            return(true);
                        }
                    }
                    else if (!valid && piece[index].GetType().Equals(typeof(Pawn)))
                    {
                        bool pawnCap = Rules.CheckPawnCapture(newLoc, piece, index, out int capIndex);

                        if (pawnCap)
                        {
                            piece[capIndex].Captured = true;
                            pieceMove move;

                            bool ischeck = Rules.isCheck(piece, piece[index].AttackedSquares(piece), turn);
                            check.isCheck    = ischeck;
                            check.checkColor = turn;

                            move.piece       = piece[index];
                            move.newLocation = newLoc;
                            move.capture     = false;
                            move.check       = false;
                            file.updatePgn(move);

                            piece[index].Location = newLoc;

                            return(true);
                        }
                    }

                    index = -1;
                }
            }

            return(false);
        }
Ejemplo n.º 5
0
        private Chessman movePieceOnBoard(BoardSpace fromSpace, BoardSpace toSpace, String extraArgs = "")
        {
            // This is kust updating the data model. Validation Should occur prior to this
            // This function Assumes that the moves are valid.

            Chessman fromPiece   = fromSpace.piece;
            Chessman killedPiece = null;

            if (toSpace.piece != null)
            {
                // We killed piece remove it.
                killedPiece = toSpace.piece;
            }
            fromPiece.position = toSpace;
            toSpace.piece      = fromPiece;
            fromSpace.piece    = null;

            if (toSpace.piece.GetType() == typeof(King))
            {
                King kp = (King)toSpace.piece;
                if (kp.canCastle)
                {
                    kp.canCastle = false;
                }
                if (kp.color == ChessmanColor.white)
                {
                    whiteKingPos = toSpace;
                }
                else
                {
                    blackKingPos = toSpace;
                }
            }
            else if (toSpace.piece.GetType() == typeof(Rook))
            {
                Rook rp = (Rook)toSpace.piece;
                if (rp.canCastle)
                {
                    rp.canCastle = false;
                }
            }
            else if (toSpace.piece.GetType() == typeof(Pawn))
            {
                ((Pawn)toSpace.piece).canMoveTwice = false;  // Pawns Can only Move Twice on the first move
                if ((toSpace.piece.color == ChessmanColor.black && toSpace.position.Item2 == 1) || (toSpace.piece.color == ChessmanColor.white && toSpace.position.Item2 == 8))
                {
                    Char promotionType = 'q';
                    if (extraArgs.Length > 0)
                    {
                        promotionType = extraArgs.ToLower()[0];
                    }

                    switch (promotionType)
                    {
                    case 'q':
                        // Promote to Queen
                        Queen q = new Queen(toSpace, toSpace.piece.color);
                        toSpace.piece = q;
                        break;

                    case 'b':
                        // Promote to Queen
                        Bishop b = new Bishop(toSpace, toSpace.piece.color);
                        toSpace.piece = b;
                        break;

                    case 'n':
                        // Promote to Queen
                        Knight kn = new Knight(toSpace, toSpace.piece.color);
                        toSpace.piece = kn;
                        break;

                    case 'r':
                        // Promote to Queen
                        Rook r = new Rook(toSpace, toSpace.piece.color);
                        toSpace.piece = r;
                        break;
                    }
                }
            }

            updateAttacksOnKing(toSpace.piece.color);

            return(killedPiece);
        }
Ejemplo n.º 6
0
 public QueenSideCastleMove(Board board, Piece piece, int desCoordinate, Rook castleRook, int start, int des) :
     base(board, piece, desCoordinate, castleRook, start, des)
 {
 }