Exemple #1
0
 private void UpdateThisPicture(object sender, EventArgs e)
 {
     if (selectedPiece != Pieces.NONE)
     {
         PictureBox squarePicture = (PictureBox)sender;
         if (squarePicture != null)
         {
             SquareItem squareItem = squarePicture.Tag as SquareItem;
             if (squareItem != null)
             {
                 if (squareItem.Piece == selectedPiece)
                 {
                     squareItem.Piece    = Pieces.NONE;
                     squarePicture.Image = null;
                 }
                 else
                 {
                     squareItem.Piece    = selectedPiece;
                     squarePicture.Image = GetImageByPiece(squareItem.Piece);
                 }
                 SetupCastlingControls();
                 Fen = GetBoardFEN();
                 if (OnBoardFenSet != null)
                 {
                     OnBoardFenSet(this, null);
                 }
                 //ValidateFen();
             }
         }
     }
 }
 private void UpdateThisPicture(object sender, EventArgs e)
 {
     if (selectedPiece != Pieces.NONE)
     {
         PictureBox squarePicture = (PictureBox)sender;
         if (squarePicture != null)
         {
             SquareItem squareItem = squarePicture.Tag as SquareItem;
             if (squareItem != null)
             {
                 if (squareItem.Piece == selectedPiece)
                 {
                     squareItem.Piece    = Pieces.NONE;
                     squarePicture.Image = null;
                 }
                 else
                 {
                     squareItem.Piece    = selectedPiece;
                     squarePicture.Image = GetImageByPiece(squareItem.Piece);
                 }
                 isUpdatePicture = true;
                 SetupCastlingControls();
                 ValidateFen();
             }
         }
     }
 }
Exemple #3
0
        private void SetupCastlingControls()
        {
            SquareItem whiteKing      = BoardSquares["e1"];
            SquareItem whiteKingRook  = BoardSquares["h1"];
            SquareItem whiteQueenRook = BoardSquares["a1"];

            SquareItem blackKing      = BoardSquares["e8"];
            SquareItem blackKingRook  = BoardSquares["h8"];
            SquareItem blackQueenRook = BoardSquares["a8"];
        }
Exemple #4
0
        private SquareItem GetSquareItem(int row, int col)
        {
            SquareItem item = null;

            foreach (SquareItem s in BoardSquares.Values)
            {
                if (s.Row == row && s.Column == col)
                {
                    item = s;
                    break;
                }
            }
            return(item);
        }
Exemple #5
0
        private void PlacePiece(int square, Pieces piece)
        {
            int col = square % 8;
            int row = square / 8;

            string notation = mapcol[col].ToString() + maprow[row].ToString();

            if (BoardSquares.ContainsKey(notation))
            {
                SquareItem boardSquare = BoardSquares[notation];
                boardSquare.PictureBox.Image = GetImageByPiece(piece);
                boardSquare.Piece            = piece;
            }
        }
        public SquareItem Copy()
        {
            SquareItem item = new SquareItem();

            item.Name            = this.Name;
            item.Piece           = this.Piece;
            item.Rectangle       = this.Rectangle;
            item.Column          = this.Column;
            item.Row             = this.Row;
            item.BackgroundImage = this.BackgroundImage;
            item.PictureBox      = this.PictureBox;

            return(item);
        }
Exemple #7
0
        private void MirroVertical()
        {
            foreach (SquareItem s in BoardSquares.Values)
            {
                if (s.Row <= 4)
                {
                    SquareItem thisSquare     = s.Copy();
                    int        newRow         = MapBoardRow(thisSquare.Row);
                    int        newColumn      = thisSquare.Column; //MapBoardColumn(thisSquare.Column);
                    SquareItem mirroredSquare = GetSquareItem(newRow, newColumn);

                    thisSquare.Piece            = GetOppositeSidePiece(mirroredSquare.Piece);
                    thisSquare.PictureBox.Image = GetImageByPiece(thisSquare.Piece);

                    mirroredSquare.Piece            = GetOppositeSidePiece(s.Piece);
                    mirroredSquare.PictureBox.Image = GetImageByPiece(mirroredSquare.Piece);

                    s.Piece            = thisSquare.Piece;
                    s.PictureBox.Image = thisSquare.PictureBox.Image;
                }
            }
        }
Exemple #8
0
        private void MirroHorizontal()
        {
            foreach (SquareItem s in BoardSquares.Values)
            {
                if (s.Column <= 4)
                {
                    SquareItem thisSquare     = s.Copy();
                    int        newRow         = thisSquare.Row; //MapBoardRow(thisSquare.Row);
                    int        newColumn      = MapBoardColumn(thisSquare.Column);
                    SquareItem mirroredSquare = GetSquareItem(newRow, newColumn);

                    thisSquare.Piece            = mirroredSquare.Piece;
                    thisSquare.PictureBox.Image = mirroredSquare.PictureBox.Image;

                    mirroredSquare.Piece            = s.Piece;
                    mirroredSquare.PictureBox.Image = GetImageByPiece(s.Piece);


                    s.Piece            = thisSquare.Piece;
                    s.PictureBox.Image = thisSquare.PictureBox.Image;
                }
            }
        }
Exemple #9
0
        public string GetBoardFEN()
        {
            System.Text.StringBuilder fen = new StringBuilder();
            int empty = 0;

            for (int row = 7; row >= 0; row--)
            {
                if (empty > 0)
                {
                    fen.Append(empty.ToString());
                    empty = 0;
                }
                if (row != 7)
                {
                    fen.Append('/');
                }
                for (int col = 0; col < 8; col++)
                {
                    string notation = mapcol[col].ToString() + maprow[row].ToString();
                    //SquareItem squareItem = GetSquareItem(row, col);
                    SquareItem squareItem = BoardSquares[notation];
                    switch (squareItem.Piece)
                    {
                    case Pieces.BKING:
                    case Pieces.BQUEEN:
                    case Pieces.BROOK:
                    case Pieces.BBISHOP:
                    case Pieces.BKNIGHT:
                    case Pieces.BPAWN:
                    case Pieces.WKING:
                    case Pieces.WQUEEN:
                    case Pieces.WROOK:
                    case Pieces.WBISHOP:
                    case Pieces.WKNIGHT:
                    case Pieces.WPAWN:
                        if (empty > 0)
                        {
                            fen.Append(empty.ToString());
                            empty = 0;
                        }
                        switch (squareItem.Piece)
                        {
                        case Pieces.BKING:
                            fen.Append('k');
                            break;

                        case Pieces.BQUEEN:
                            fen.Append('q');
                            break;

                        case Pieces.BROOK:
                            fen.Append('r');
                            break;

                        case Pieces.BBISHOP:
                            fen.Append('b');
                            break;

                        case Pieces.BKNIGHT:
                            fen.Append('n');
                            break;

                        case Pieces.BPAWN:
                            fen.Append('p');
                            break;

                        case Pieces.WKING:
                            fen.Append('K');
                            break;

                        case Pieces.WQUEEN:
                            fen.Append('Q');
                            break;

                        case Pieces.WROOK:
                            fen.Append('R');
                            break;

                        case Pieces.WBISHOP:
                            fen.Append('B');
                            break;

                        case Pieces.WKNIGHT:
                            fen.Append('N');
                            break;

                        case Pieces.WPAWN:
                            fen.Append('P');
                            break;
                        }
                        break;

                    case Pieces.NONE:
                        empty++;
                        break;
                    }
                }
            }
            if (empty > 0)
            {
                fen.Append(empty.ToString());
                empty = 0;
            }
            return(fen.ToString());
        }
Exemple #10
0
        private void LoadBoardSquares()
        {
            pnlBoard.Controls.Clear();

            int squareHeight = 60;
            int squareWidth  = 60;

            Rectangle  squareRectangle;
            SquareItem boardSquare;
            string     notation;

            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    notation = mapcol[col].ToString() + maprow[7 - row].ToString();
                    if (BoardSquares.ContainsKey(notation))
                    {
                        boardSquare        = BoardSquares[notation];
                        boardSquare.Row    = row + 1;
                        boardSquare.Column = col + 1;

                        PictureBox pieceImage = boardSquare.PictureBox;
                        pieceImage.Name            = notation;
                        pieceImage.BackgroundImage = GetSquareImage(row, col);
                        pieceImage.Height          = squareHeight;
                        pieceImage.Width           = squareWidth;
                        pieceImage.Top             = row * squareHeight;
                        pieceImage.Left            = col * squareWidth;

                        squareRectangle       = new Rectangle(pieceImage.Left, pieceImage.Top, pieceImage.Width, pieceImage.Height);
                        boardSquare.Rectangle = squareRectangle;

                        if (pnlBoard.Controls.Contains(pieceImage))
                        {
                            pnlBoard.Controls.Remove(pieceImage);
                        }
                    }
                    else
                    {
                        boardSquare                 = new SquareItem();
                        boardSquare.Name            = notation;
                        boardSquare.Row             = row + 1;
                        boardSquare.Column          = col + 1;
                        boardSquare.BackgroundImage = GetSquareImage(row, col);
                        BoardSquares.Add(notation, boardSquare);

                        PictureBox pieceImage = new PictureBox();
                        pieceImage.Name            = notation;
                        pieceImage.BackgroundImage = GetSquareImage(row, col);
                        pieceImage.Height          = squareHeight;
                        pieceImage.Width           = squareWidth;
                        pieceImage.Top             = row * squareHeight;
                        pieceImage.Left            = col * squareWidth;
                        pieceImage.Click          += new EventHandler(pieceImage_Click);
                        pieceImage.Tag             = boardSquare;

                        boardSquare.PictureBox = pieceImage;

                        pnlBoard.Controls.Add(pieceImage);
                    }
                }
            }
        }
        private void SetupCastlingControls()
        {
            chkWhiteCastlingShort.Checked = false;
            chkWhiteCastlingLong.Checked  = false;
            chkBlackCastlingShort.Checked = false;
            chkBlackCastlingLong.Checked  = false;

            SquareItem whiteKing      = BoardSquares["e1"];
            SquareItem whiteKingRook  = BoardSquares["h1"];
            SquareItem whiteQueenRook = BoardSquares["a1"];

            SquareItem blackKing      = BoardSquares["e8"];
            SquareItem blackKingRook  = BoardSquares["h8"];
            SquareItem blackQueenRook = BoardSquares["a8"];

            if (whiteKing != null && whiteKing.Piece == Pieces.WKING)
            {
                if (whiteKingRook != null && whiteKingRook.Piece == Pieces.WROOK)
                {
                    if (isWhiteCastlingShort == null)
                    {
                        chkWhiteCastlingShort.Checked = true;
                    }
                    else
                    {
                        if ((bool)isWhiteCastlingShort || isUpdatePicture)
                        {
                            chkWhiteCastlingShort.Checked = true;
                        }
                        else
                        {
                            chkWhiteCastlingShort.Checked = false;
                        }
                    }
                }
                if (whiteQueenRook != null && whiteQueenRook.Piece == Pieces.WROOK)
                {
                    if (isWhiteCastlingLong == null)
                    {
                        chkWhiteCastlingLong.Checked = true;
                    }
                    else
                    {
                        if ((bool)isWhiteCastlingLong || isUpdatePicture)
                        {
                            chkWhiteCastlingLong.Checked = true;
                        }
                        else
                        {
                            chkWhiteCastlingLong.Checked = false;
                        }
                    }
                }
            }
            if (blackKing != null && blackKing.Piece == Pieces.BKING)
            {
                if (blackKingRook != null && blackKingRook.Piece == Pieces.BROOK)
                {
                    if (isBlackCastlingShort == null)
                    {
                        chkBlackCastlingShort.Checked = true;
                    }
                    else
                    {
                        if ((bool)isBlackCastlingShort || isUpdatePicture)
                        {
                            chkBlackCastlingShort.Checked = true;
                        }
                        else
                        {
                            chkBlackCastlingShort.Checked = false;
                        }
                    }
                }
                if (blackQueenRook != null && blackQueenRook.Piece == Pieces.BROOK)
                {
                    if (isBlackCastlingLong == null)
                    {
                        chkBlackCastlingLong.Checked = true;
                    }
                    else
                    {
                        if ((bool)isBlackCastlingLong || isUpdatePicture)
                        {
                            chkBlackCastlingLong.Checked = true;
                        }
                        else
                        {
                            chkBlackCastlingLong.Checked = false;
                        }
                    }
                }
            }
            isUpdatePicture = false;
        }