コード例 #1
0
        /// <summary>
        /// Moves the the given piece in the bitboards.  May want to consider moving all of
        /// the movePiece logic into the ChessPosition.removePiece function.  This routine
        /// basically mimics the ChessBitboard.removePiece and ChessBitboard.addPiece so maybe
        /// simply call those routines to to simplify the logic.  Currenty only being done
        /// for a small optimization gain.
        /// </summary>
        /// <param name="piece">Piece we are deleting, note that the ChessPostion
        /// class ignores the color of the piece as basically it is color blind.</param>
        /// <param name="square"></param>
        /// <param name="Pieces">Current bitboards to update.</param>
        public void movePiece(Chess.Pieces piece, BitboardSquare from, BitboardSquare to)
        {
            // Move the piece.
            removePiece(piece, from.getSquareMask());
            addPiece(piece, to.getSquareMask());
            // Remove any prevoious Enpasant.
            Enpasant = 0x00;
            // See if this move puts a pawn in danger of Enpasant
            if (piece == Chess.Pieces.BPAWN || piece == Chess.Pieces.WPAWN)
            {
                if (isPotentialEnpasant(from.getSquareMask(), to.getSquareMask()))
                {
                    // Set square of pawn susceptible to enpasant attack.
                    Enpasant = to.getSquareMask();
                }
            }
            Board = (Board & (~from.getSquareMask())) | to.getSquareMask();
            // Update attacker rotated boards:
            Bitboard maskFrom = from.getA8H1Mask(); // getBitSquare(coFrom.A8H1Square);
            Bitboard maskTo   = to.getA8H1Mask();   //getBitSquare(coTo.A8H1Square);

            RotatedA8H1 = (RotatedA8H1 & (~maskFrom)) | maskTo;
            maskFrom    = from.getA1H8Mask(); //getBitSquare(coFrom.A1H8Square);
            maskTo      = to.getA1H8Mask();   //getBitSquare(coTo.A1H8Square);
            RotatedA1H8 = (RotatedA1H8 & (~maskFrom)) | maskTo;
            maskFrom    = from.getR90Mask();  //getBitSquare(coFrom.R90Square);
            maskTo      = to.getR90Mask();    //getBitSquare(coTo.R90Square);
            RotatedR90  = (RotatedR90 & (~maskFrom)) | maskTo;
        }
コード例 #2
0
 /// <summary>
 /// This routine will grab ahold of a piece and allow us to move it around
 /// the chess board.
 /// </summary>
 /// <param name="mouse"></param>
 public void pickupPiece(MouseEventArgs mouse)
 {
     if (!isDragging)
     {
         coSourceSquare = coChessBitmap.findSquare(mouse.X, mouse.Y);
         // Found a source square.
         if (coSourceSquare != null)
         {
             // Do we have a hold of one of the little dudes?
             if (coSourceSquare.Piece != Chess.Pieces.NONE)
             {
                 // Let everyone know that we are dragging the little dude around.
                 isDragging           = true;
                 coMovingSquare       = coSourceSquare.Square;
                 coPoint              = new Point(coMovingSquare.X + coMovingSquare.Width / 2, coMovingSquare.Y + coMovingSquare.Height / 2);
                 coPiece              = coSourceSquare.Piece;
                 coSourceSquare.Piece = Chess.Pieces.NONE;
                 Graphics offScreenDC = Graphics.FromImage(coChessBitmap.coBmpBoard);
                 offScreenDC.DrawImage(coSourceSquare.Background, coSourceSquare.Square);
                 Cursor.Current = (Cursor)coChessCursors[Chess.Pieces.CLOSEDHAND];
                 // If we don't do this then resources do not get released.  Memory leaks...
                 offScreenDC.Dispose();
             }
         }
     }
 }
コード例 #3
0
        public void placePiece(string notation, Chess.Pieces piece)
        {
            BoardSquare sq = (BoardSquare)coChessBoard[notation];

            sq.PieceImage = (Bitmap)imagePieces.Images[((int)piece - 1)];
            sq.Piece      = piece;
        }
コード例 #4
0
        /// <summary>
        /// Removes the requested piece from the bitboards.
        /// </summary>
        /// <param name="piece"></param>
        /// <param name="mask"></param>
        public void removePiece(Chess.Pieces piece, Bitboard mask)
        {
            switch (piece)
            {
            case Chess.Pieces.WKING:
            case Chess.Pieces.BKING:
                this.King &= ~mask;
                break;

            case Chess.Pieces.WQUEEN:
            case Chess.Pieces.BQUEEN:
                this.Queens &= ~mask;
                break;

            case Chess.Pieces.WROOK:
            case Chess.Pieces.BROOK:
                this.Rooks &= ~mask;
                break;

            case Chess.Pieces.WBISHOP:
            case Chess.Pieces.BBISHOP:
                this.Bishops &= ~mask;
                break;

            case Chess.Pieces.WKNIGHT:
            case Chess.Pieces.BKNIGHT:
                this.Knights &= ~mask;
                break;

            case Chess.Pieces.WPAWN:
            case Chess.Pieces.BPAWN:
                this.Pawns &= ~mask;
                break;
            }
        }
コード例 #5
0
        public void placePiece(Chess.Pieces piece, int square)
        {
            int    col      = square % 8;
            int    row      = square / 8;
            string notation = mapcol[col].ToString() + maprow[row].ToString();

            placePiece(notation, piece);
        }
コード例 #6
0
        /// <summary>
        /// Adds the given piece to the bitboards.  May want to consider moving all of
        /// the addPiece logic into the ChessPosition.removePiece function.
        /// </summary>
        /// <param name="piece">Piece we are deleting, note that the ChessPostion
        /// class ignores the color of the piece as basically it is color blind.</param>
        /// <param name="Pieces">Current bitboards to update.</param>
        public void addPiece(Chess.Pieces piece, BitboardSquare to)
        {
            // Add the piece to the piece board.
            addPiece(piece, to.getSquareMask());
            // Update main board.
            Board = Board | to.getSquareMask();
            // Update rotated boards:
            Bitboard maskTo = to.getA8H1Mask(); // getBitSquare(coTo.A8H1Square);

            RotatedA8H1 = RotatedA8H1 | maskTo;
            maskTo      = to.getA1H8Mask(); // getBitSquare(coTo.A1H8Square);
            RotatedA1H8 = RotatedA1H8 | maskTo;
            maskTo      = to.getR90Mask();  // getBitSquare(coTo.R90Square);
            RotatedR90  = RotatedR90 | maskTo;
        }
コード例 #7
0
        /// <summary>
        /// Removes the given piece from the bitboards.  May want to consider moving all of
        /// the removePiece logic into the ChessPosition.removePiece function.
        /// </summary>
        /// <param name="piece">Piece we are deleting, note that the ChessPostion
        /// class ignores the color of the piece as basically it is color blind.</param>
        /// <param name="square"></param>
        /// <param name="Pieces"></param>
        public void removePiece(Chess.Pieces piece, BitboardSquare square)
        {
            // Get the squares.
            // BitboardSquare bbSquare = new BitboardSquare(square);
            Bitboard mask = square.getSquareMask(); // getBitSquare(bbSquare.Square);

            removePiece(piece, mask);
            Board = Board & ~mask;

            // Update rotated boards:
            mask        = square.getA8H1Mask(); // getBitSquare(bbSquare.A8H1Square);
            RotatedA8H1 = RotatedA8H1 & ~mask;
            mask        = square.getA1H8Mask(); // getBitSquare(bbSquare.A1H8Square);
            RotatedA1H8 = RotatedA1H8 & ~mask;
            mask        = square.getR90Mask();  // getBitSquare(bbSquare.R90Square);
            RotatedR90  = RotatedR90 & ~mask;
        }
コード例 #8
0
        public static char pieceToNotation(Chess.Pieces piece)
        {
            char aPiece = ' ';

            switch (piece)
            {
            case Chess.Pieces.WKING:
            case Chess.Pieces.BKING:
                aPiece = 'K';
                break;

            case Chess.Pieces.WQUEEN:
            case Chess.Pieces.BQUEEN:
                aPiece = 'Q';
                break;

            case Chess.Pieces.WROOK:
            case Chess.Pieces.BROOK:
                aPiece = 'R';
                break;

            case Chess.Pieces.WBISHOP:
            case Chess.Pieces.BBISHOP:
                aPiece = 'B';
                break;

            case Chess.Pieces.WKNIGHT:
            case Chess.Pieces.BKNIGHT:
                aPiece = 'N';
                break;

            case Chess.Pieces.WPAWN:
            case Chess.Pieces.BPAWN:
                aPiece = 'P';
                break;
            }
            return(aPiece);
        }
コード例 #9
0
        /// <summary>
        /// Removes the requested piece bitboard.  Note you may have multiple bits set for all but
        /// the kings bitboard.
        /// </summary>
        /// <param name="piece"></param>
        /// <returns></returns>
        public Bitboard getPiece(Chess.Pieces piece)
        {
            Bitboard mask = 0;

            switch (piece)
            {
            case Chess.Pieces.WKING:
            case Chess.Pieces.BKING:
                mask = this.King;
                break;

            case Chess.Pieces.WQUEEN:
            case Chess.Pieces.BQUEEN:
                mask = this.Queens;
                break;

            case Chess.Pieces.WROOK:
            case Chess.Pieces.BROOK:
                mask = this.Rooks;
                break;

            case Chess.Pieces.WBISHOP:
            case Chess.Pieces.BBISHOP:
                mask = this.Bishops;
                break;

            case Chess.Pieces.WKNIGHT:
            case Chess.Pieces.BKNIGHT:
                mask = this.Knights;
                break;

            case Chess.Pieces.WPAWN:
            case Chess.Pieces.BPAWN:
                mask = this.Pawns;
                break;
            }
            return(mask);
        }
コード例 #10
0
        /// <summary>
        /// Used to signal that we are ready to drop a chess piece on it's respective
        /// target square.
        /// </summary>
        /// <param name="mouse"></param>
        public void dropPiece(MouseEventArgs mouse)
        {
            // Only if we were moving a piece.
            if (isDragging)
            {
                // Clear out coordinates used during the drag.
                coMovingSquare.X      = 0;
                coMovingSquare.Y      = 0;
                coMovingSquare.Width  = 0;
                coMovingSquare.Height = 0;
                coPoint.X             = 0;
                coPoint.Y             = 0;

                // Reset our source square to the original piece.
                coSourceSquare.Piece = coPiece;
                // Clear our temporary variable.
                coPiece = Chess.Pieces.NONE;
                // Locate where we want to drop the piece.
                coTargetSquare = coChessBitmap.findSquare(mouse.X, mouse.Y);
                Graphics offScreenDC = Graphics.FromImage(coChessBitmap.coBmpBoard);
                // Found a target square
                if (coTargetSquare != null)
                {
                    // Make sure we had a chess piece in hand.
                    if (coSourceSquare.PieceImage != null)
                    {
                        // Used to see if hosting window says we can move the piece.
                        bool okMove = true;
                        if (EventPieceMoved != null)
                        {
                            okMove = EventPieceMoved(coSourceSquare.Piece, coSourceSquare.Name, coTargetSquare.Name);
                        }
                        if (okMove)
                        {
                            bool bValidMove = true;
                            // Make sure we are not moving to the same square the piece originally occupied.
                            //  Then validate the move with the bitboard class.
                            if (coTargetSquare != coSourceSquare)
                            {
                                if (coValidateMoves)
                                {
                                    bValidMove = coBitBoard.move(coSourceSquare.Piece, coSourceSquare.Name, coTargetSquare.Name);
                                }

                                if (bValidMove)
                                {
                                    if (!promotion)
                                    {
                                        // Everything ok so now move the piece into the new square.
                                        coTargetSquare.PieceImage = coSourceSquare.PieceImage;
                                        coTargetSquare.Piece      = coSourceSquare.Piece;
                                    }
                                    else
                                    {
                                        promotion = false;
                                    }

                                    // Clear chess image and piece.
                                    coSourceSquare.PieceImage = null;
                                    coSourceSquare.Piece      = Chess.Pieces.NONE;

                                    // Just draw the new background and image.
                                    offScreenDC.DrawImage(coTargetSquare.Background, coTargetSquare.Square);
                                    offScreenDC.DrawImage(coTargetSquare.PieceImage, coTargetSquare.Square);
                                }
                                else
                                {
                                    // Redraw the original pieces.
                                    offScreenDC.DrawImage(coSourceSquare.Background, coSourceSquare.Square);
                                    offScreenDC.DrawImage(coSourceSquare.PieceImage, coSourceSquare.Square);
                                }
                            }
                            else
                            {
                                // Redraw the original pieces.
                                offScreenDC.DrawImage(coSourceSquare.Background, coSourceSquare.Square);
                                offScreenDC.DrawImage(coSourceSquare.PieceImage, coSourceSquare.Square);
                            }
                        }
                    }
                }
                else                 //Dragging a piece off board
                {
                    // Redraw the original pieces.
                    offScreenDC.DrawImage(coSourceSquare.Background, coSourceSquare.Square);
                    offScreenDC.DrawImage(coSourceSquare.PieceImage, coSourceSquare.Square);
                }
                offScreenDC.Dispose();
                Invalidate();
                isDragging     = false;
                Cursor.Current = (Cursor)coChessCursors[Chess.Pieces.OPENHAND];
            }
        }
コード例 #11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="piece"></param>
 /// <returns></returns>
 public Cursor getCursor(Chess.Pieces piece)
 {
     return((Cursor)coChessCursors[piece]);
 }