Exemple #1
0
        /// <summary>
        /// Checks if the king of the specified color is in check
        /// </summary>
        /// <param name="board"></param>
        /// <param name="kingColor"></param>
        /// <returns></returns>
        public bool IsChecked(Color kingColor)
        {
            Board board = this;

            int kingLocation  = board.KingLocation(kingColor);
            var attackerColor = (kingColor == Color.White) ? Color.Black : Color.White;
            var attacks       = board.GetAttackBoard(attackerColor);

            // king is not in check
            if (attacks[kingLocation] == 0)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemple #2
0
        private static void GetKingMoves(Board board, int square, int[] moves, ref int count)
        {
            int   x      = Board.X(square);
            int   y      = Board.Y(square);
            Color color  = board.GetColor(square);
            int   target = 0;

            if (y < 7)
            {
                target = square + 7;
                if (x > 0 && board.GetColor(target) != color)
                {
                    moves[count] = target;
                    count++;
                }

                target = square + 8;
                if (board.GetColor(target) != color)
                {
                    moves[count] = target;
                    count++;
                }

                target = square + 9;
                if (x < 7 && board.GetColor(target) != color)
                {
                    moves[count] = target;
                    count++;
                }
            }

            target = square - 1;
            if (x > 0 && board.GetColor(target) != color)
            {
                moves[count] = target;
                count++;
            }

            target = square + 1;
            if (x < 7 && board.GetColor(target) != color)
            {
                moves[count] = target;
                count++;
            }

            if (y > 0)
            {
                target = square - 9;
                if (x > 0 && board.GetColor(target) != color)
                {
                    moves[count] = target;
                    count++;
                }

                target = square - 8;
                if (board.GetColor(target) != color)
                {
                    moves[count] = target;
                    count++;
                }

                target = square - 7;
                if (x < 7 && board.GetColor(target) != color)
                {
                    moves[count] = target;
                    count++;
                }
            }

            // castling
            // no quares between rook and king can be occupied, the king cannot pass through any checked attacked squares
            if (color == Color.White && square == 4)
            {
                if (board.CanCastleKWhite && board.State[5] == 0 && board.State[6] == 0)
                {
                    var attacks = board.GetAttackBoard(Color.Black);
                    if (attacks[4] == 0 && attacks[5] == 0 && attacks[6] == 0)
                    {
                        moves[count] = 6;
                        count++;
                    }
                }
                if (board.CanCastleQWhite && board.State[3] == 0 && board.State[2] == 0 && board.State[1] == 0)
                {
                    var attacks = board.GetAttackBoard(Color.Black);
                    if (attacks[2] == 0 && attacks[3] == 0 && attacks[4] == 0)
                    {
                        moves[count] = 2;
                        count++;
                    }
                }
            }
            else if (color == Color.Black && square == 60)
            {
                if (board.CanCastleKBlack && board.State[61] == 0 && board.State[62] == 0)
                {
                    var attacks = board.GetAttackBoard(Color.White);
                    if (attacks[60] == 0 && attacks[61] == 0 && attacks[62] == 0)
                    {
                        moves[count] = 62;
                        count++;
                    }
                }
                if (board.CanCastleQBlack && board.State[59] == 0 && board.State[58] == 0 && board.State[57] == 0)
                {
                    var attacks = board.GetAttackBoard(Color.White);
                    if (attacks[58] == 0 && attacks[59] == 0 && attacks[60] == 0)
                    {
                        moves[count] = 58;
                        count++;
                    }
                }
            }
        }