コード例 #1
0
        public static void kingInCheck(Position position)  // checks if the piece in the provided position poses a check to the opposite king.
        {
            ChessPiece piece = squares[position.Row, position.Col].getPiece(); // the piece which had been moved now.
            ChessPiece enemyKing;
            Position enemyKingPosition = null ;
            if (getPlayerSide(piece) == Player.White)
                enemyKing = ChessPiece.BlackKing;
            else
                enemyKing = ChessPiece.WhiteKing;

            // getting position of enemy king.
            foreach(Position p in pieceMap.Keys)
            {
                if(pieceMap[p] == enemyKing)
                {
                    Debug.Print("Position : "+p);
                    enemyKingPosition = p;
                    break;
                }
            }

            if(enemyKingPosition == null)
            {
                setErrorHighlight();
                Debug.Print("cidjf");
                setStatus("[Debug] FATAL: Enemy King position cannot be determined");
                return;
            }

            foreach(Position possiblePosition in getPossiblePositions(position))
            {
                Debug.Print("Position : " + possiblePosition);
                if(possiblePosition == enemyKingPosition)
                {
                    setStatus("King in check!");
                    // setting check info - required later for checking. (and ofcourse, removing checkHighlight)
                    if (info == null)
                        info = new CheckInfo();
                    info.threatenedBy = position;
                    info.pieceInCheck = enemyKingPosition;
                    //TODO: while making move check both if the move leads to a check or if the king is in check, the move should clear the check.
                    // ---
                    isKingInCheck = true;
                    squares[enemyKingPosition.Row, enemyKingPosition.Col].setCheckHighlight();
                }
            }
        }
コード例 #2
0
        public Form1()
        {
            InitializeComponent();
            isWhitesTurn = true;
            isKingInCheck = false;
            currentHighlight = null;
            possiblePositions = new List<Position>();
            pieceMap = new Dictionary<Position, ChessPiece>();
            statusLabel = new ToolStripLabel();
            info = null;
            lastMove = null;    // a reference for the last Position.

            //adding status label to the status strip, cant be added via the designer because this is a static object.
            statusStrip1.Items.Add(statusLabel);

            tableLayoutPanel1.RowCount = 8;
            tableLayoutPanel1.ColumnCount = 8;
            for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
            {
                for (int j = 0; j < tableLayoutPanel1.ColumnCount; j++)
                {
                    squares[i, j] = new ChessBoardSquare(i, j);
                    tableLayoutPanel1.Controls.Add(squares[i,j],j,i);
                }
            }

            // defining the resource dictionary
            resourceMap = new Dictionary<ChessPiece, Icon>();
            resourceMap.Add(ChessPiece.WhiteRook, WindowsFormsApplication2.Properties.Resources.White_Rook);
            resourceMap.Add(ChessPiece.WhiteHorse, WindowsFormsApplication2.Properties.Resources.White_Horse);
            resourceMap.Add(ChessPiece.WhiteBishop, WindowsFormsApplication2.Properties.Resources.White_Bishop);
            resourceMap.Add(ChessPiece.WhiteKing, WindowsFormsApplication2.Properties.Resources.White_King);
            resourceMap.Add(ChessPiece.WhiteQueen, WindowsFormsApplication2.Properties.Resources.White_Queen);
            resourceMap.Add(ChessPiece.WhitePawn, WindowsFormsApplication2.Properties.Resources.White_Pawn);
            resourceMap.Add(ChessPiece.BlackRook, WindowsFormsApplication2.Properties.Resources.Black_Rook);
            resourceMap.Add(ChessPiece.BlackHorse, WindowsFormsApplication2.Properties.Resources.Black_Horse);
            resourceMap.Add(ChessPiece.BlackBishop, WindowsFormsApplication2.Properties.Resources.Black_Bishop);
            resourceMap.Add(ChessPiece.BlackKing, WindowsFormsApplication2.Properties.Resources.Black_King);
            resourceMap.Add(ChessPiece.BlackQueen, WindowsFormsApplication2.Properties.Resources.Black_Queen);
            resourceMap.Add(ChessPiece.BlackPawn, WindowsFormsApplication2.Properties.Resources.Black_Pawn);

            // setting initial positions for pieces
            squares[0, 0].setPiece(ChessPiece.WhiteRook); 
            squares[0, 1].setPiece(ChessPiece.WhiteHorse); 
            squares[0, 2].setPiece(ChessPiece.WhiteBishop);
            squares[0, 3].setPiece(ChessPiece.WhiteKing);
            squares[0, 4].setPiece(ChessPiece.WhiteQueen);
            squares[0, 5].setPiece(ChessPiece.WhiteBishop);
            squares[0, 6].setPiece(ChessPiece.WhiteHorse);
            squares[0, 7].setPiece(ChessPiece.WhiteRook);

            squares[7, 0].setPiece(ChessPiece.BlackRook);
            squares[7, 1].setPiece(ChessPiece.BlackHorse);
            squares[7, 2].setPiece(ChessPiece.BlackBishop);
            squares[7, 3].setPiece(ChessPiece.BlackKing);
            squares[7, 4].setPiece(ChessPiece.BlackQueen);
            squares[7, 5].setPiece(ChessPiece.BlackBishop);
            squares[7, 6].setPiece(ChessPiece.BlackHorse);
            squares[7, 7].setPiece(ChessPiece.BlackRook);

            for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
            {
                    squares[1, i].setPiece(ChessPiece.WhitePawn);
                    squares[6, i].setPiece(ChessPiece.BlackPawn);
            }

            setStatus("White's Move now");
        }