Пример #1
0
        //
        // Basic check
        //
        private bool[] isClear(ChessPiece piece) // Returns [rank, file], [Col, row]
        {
            bool colB = true, rowB = true;
            int  col = piece.pos.Column, row = piece.pos.Row;

            for (int i = 0; i < 8; i++)
            {
                PictureBox boxC = (PictureBox)playBoard.GetControlFromPosition(col, i);
                PictureBox boxR = (PictureBox)playBoard.GetControlFromPosition(i, row);
                ChessPiece pcC  = Calcs.CheckPiece(boxC);
                ChessPiece pcR  = Calcs.CheckPiece(boxR);
                if (boxC != null && pcC.pieceRank == piece.pieceRank &&
                    pcC.isWhite == piece.isWhite &&
                    pcC != piece)
                {
                    colB = false;
                }
                if (boxR != null && pcR.pieceRank == piece.pieceRank &&
                    pcR.isWhite == piece.isWhite &&
                    pcR != piece)
                {
                    rowB = false;
                }
            }
            return(new bool[2] {
                colB, rowB
            });
        }
Пример #2
0
        //
        // Handle piece clicks
        //
        internal void PieceClick(object sender, MouseEventArgs e)
        {
            //
            // Discard moves/wrong clicks
            //
            if (isOver)
            {
                return;
            }
            PictureBox box      = (PictureBox)sender;
            ChessPiece selpiece = Calcs.CheckPiece(box);

            if (isMoving)
            {
                AttackMove(sender, e);
                return;
            }
            else if (whiteTurn != selpiece.isWhite)
            {
                return;
            }
            isMoving = true;
            //
            // Get moves + Calculate
            // Variables
            selectedPiece = selpiece;
            List <Point> moves   = Calcs.CalcMovesG(selpiece, checkingPieces);
            List <Point> discard = new List <Point>();

            foreach (Point pt in moves)
            {
                int        col   = pt.X;
                int        row   = pt.Y;
                PictureBox cont  = (PictureBox)playBoard.GetControlFromPosition(pt.X, pt.Y);
                ChessPiece contp = Calcs.CheckPiece(cont);
                //
                // Discard unusable moves
                //
                if (col < 0 || col > 7 || row < 0 || row > 7)
                {
                    discard.Add(pt);
                }
                else if (cont != null &&
                         cont.BackColor != Color.DarkGray && contp.isWhite == selpiece.isWhite)
                {
                    discard.Add(pt);
                }
                else
                {
                    AddTempBox(row, col);
                }
            }
            if (moves.Count == discard.Count)
            {
                isMoving = false;
                return;
            }
        }
Пример #3
0
 public Board(Main Mn)
 {
     mn = Mn;
     InitializeComponent();
     Calcs.board = playBoard; // Set calc variables
     for (int i = 0; i < 8; i++)
     {
         for (int j = 0; j < 8; j++)
         {
             PictureBox box = (PictureBox)playBoard.GetControlFromPosition(i, j);
             if (box != null)
             {
                 ChessPiece piece = new ChessPiece(box, playBoard);
                 Calcs.pieces.Add(piece); // Set the "pieces" var in Calcs
                 if (piece.isWhite)
                 {
                     Calcs.wP.Add(piece);
                 }
                 else
                 {
                     Calcs.bP.Add(piece);
                 }
             }
         }
     }
     Calcs.BK = Calcs.CheckPiece(bKe);
     Calcs.WK = Calcs.CheckPiece(wKe);
     WPr      = new List <Button>()
     {
         WbuttonR, WbuttonK, WbuttonB, WbuttonQ
     };
     BPr = new List <Button>()
     {
         BbuttonR, BbuttonK, BbuttonB, BbuttonQ
     };
 }
Пример #4
0
        //
        // Main move function
        //
        internal void MovePiece(object sender, MouseEventArgs e, bool attack = false)
        {
            isCheck = false;
            //
            // Get vars + Positions
            //
            PictureBox box = (PictureBox)sender;
            TableLayoutPanelCellPosition pos    = playBoard.GetPositionFromControl(box);
            TableLayoutPanelCellPosition selpos = selectedPiece.pos;
            //
            // Passant check before move
            //
            bool ep = false;

            if (box.Name.Contains("DEL") &&
                selectedPiece.pieceRank == Rank.PAWN &&
                pos.Column != selpos.Column)
            {
                ep     = true;
                attack = true;
                playBoard.Controls.Remove
                    (playBoard.GetControlFromPosition
                        (pos.Column, selpos.Row));
            }
            //
            // Move the piece.
            //
            playBoard.Controls.Remove(box);
            playBoard.Controls.Add(selectedPiece.box, pos.Column, pos.Row);
            //
            // Pawn checks
            //
            foreach (ChessPiece pc in Calcs.pieces)
            {
                if (pc.isWhite == selectedPiece.isWhite)
                {
                    pc.PassElig = false;
                }
            }
            if (selectedPiece.pieceRank == Rank.PAWN)
            {
                if (Math.Abs(selpos.Row - pos.Row) == 2)
                {
                    selectedPiece.PassElig = true;
                }
                selectedPiece.canDouble = false;

                if (selectedPiece.CheckPromote())
                {
                    promPiece = selectedPiece;
                    Spr       = selectedPiece.isWhite ? WPr : BPr;
                    foreach (Button b in Spr)
                    {
                        b.Visible = true;
                    }
                    notifBoard.Visible = true;
                }
            }
            //
            // Remove temp boxes
            //
            ClearTempBoxes();
            //
            // Change turn text
            //
            whiteTurn = !whiteTurn;
            if (whiteTurn)
            {
                turnbox.Text = "WHITE";
            }
            else
            {
                turnbox.Text = "BLACK";
            }
            isMoving = false;
            //
            // Get check
            //
            List <ChessPiece> isCheckL = Calcs.CheckCheck(whiteTurn);

            checkingPieces = isCheckL;
            bool isCheckB = isCheckL.Count != 0;

            isCheck = isCheckB;
            //
            // Get CM
            //
            bool ifMoves = Calcs.NMCheck(whiteTurn, isCheckL);

            if (!ifMoves)
            {
                // Game is over
                isOver             = true;
                notifBoard.Visible = true;
                winText.Visible    = true;
                turnbox.Visible    = false;
                turntext.Visible   = false;
                replay.Visible     = true;

                // Find Checkmate/Stalemate
                if (isCheckB)
                {
                    winText.Text = whiteTurn ? "BLACK WINS!" : "WHITE WINS!";
                }
                else
                {
                    winText.Text = "STALEMATE!";
                }
            }

            // Update moves
            string[]      lines    = GetMoveText(attack, selpos, selectedPiece, isCheckB, !ifMoves, ep);
            List <string> boxLines = moves.Lines.ToList();

            if (boxLines.Count != 0)
            {
                string last = boxLines[boxLines.Count - 1];
                if (!last.Contains(' '))
                {
                    last += (' ' + lines[0]);
                    boxLines[boxLines.Count - 1] = last;
                }
                else
                {
                    boxLines = boxLines.Concat(lines).ToList();
                }
                moves.Lines = boxLines.ToArray();
            }
            else
            {
                moves.Lines = lines;
            }

            //
            // Clear selected piece
            //
            selectedPiece = null;
        }