Esempio n. 1
0
        public bool makeMove(string col, Move move)
        {
            uint x1 = move.FromX;
            uint y1 = move.FromY;
            uint x2 = move.ToX;
            uint y2 = move.ToY;

            Piece  p         = getPieceAt(x1, y1);
            Square s1        = getSquareAt(x1, y1);
            Square s2        = getSquareAt(x2, y2);
            bool   castling  = false;
            bool   enPassant = false;

            // Check if squares are ok.
            if (s1 == null || s2 == null)
            {
                return(false);
            }

            // Check if piece is ok.
            if (p == null)
            {
                return(false);
            }
            if (p.getColour() != col)
            {
                return(false);
            }
            // Move is legal

            //Check if this is a castling
            if ((p is King) && (((x1 - x2) == 2) || ((x2 - x1) == 2)))
            {
                castling = true;
            }

            // Special handling for pawns
            if ((p is Pawn))
            {
                // Check double step
                if (Math.Abs((int)move.FromY - (int)move.ToY) == 2)
                {
                    ((Pawn)p).setDoubleStepTurn(getTurn());
                }

                //Check if en passant
                if ((getSquareAt(x2, y2).getPiece() == null) && (x1 != x2))
                {
                    enPassant = true;
                }
            }

            // Remove from old position
            s1.removePiece();
            // Add to new position
            if ((p is Pawn && p.getColour() == "white" && y2 == 7) ||
                (p is Pawn && p.getColour() == "black" && y2 == 0))
            {
                p = new Queen(x1, y1, p.getColour());
            }
            s2.setPiece(p);
            p.move(x2, y2);

            // Handles the special castling case
            if (castling)
            {
                uint x2rook;
                uint x1rook;
                if (x2 > x1)// Castling right
                {
                    x1rook = 7;
                    x2rook = x2 - 1;
                }
                else
                {
                    x1rook = 0;
                    x2rook = x2 + 1;
                }
                Square s3 = getSquareAt(x1rook, y1);
                Piece  p3 = s3.getPiece();
                Square s4 = getSquareAt(x2rook, y1);
                s3.removePiece();
                s4.setPiece(p3);
                p3.move(x2rook, y2);
            }

            // Handles the special en passant case
            if (enPassant)
            {
                int yMod;
                if (p.getColour() == "white")
                {
                    yMod = -1;
                }
                else
                {
                    yMod = 1;
                }
                Square s3 = getSquareAt(x2, (uint)(y2 + yMod));
                Piece  p3 = s3.getPiece();
                s3.removePiece();
            }

            updateCover();
            return(true);
        }