Пример #1
0
        public void RookPosition4()
        {
            var coords = Square.H1;

            string otherFiguresBoard =
                "00100001" +
                "10001001" +
                "00000001" +
                "00001010" +
                "00000000" +
                "10000000" +
                "00010000" +
                "10100000";

            string resultBoard =
                "00000000" +
                "00000000" +
                "00000001" +
                "00000001" +
                "00000001" +
                "00000001" +
                "00000001" +
                "00111110";

            ulong otherFigures = BitBoardHelper.FromString(otherFiguresBoard);
            ulong result       = BitBoardHelper.FromString(resultBoard);

            Assert.AreEqual(result,
                            provider.GetAttacks(coords, otherFigures));
        }
Пример #2
0
        public void FromStringComplexTest()
        {
            int count = 100;
            var rand  = new Random(DateTime.Now.Millisecond);

            while ((count--) > 0)
            {
                var board = new BitBoard();
                var str   = new StringBuilder(64);
                str.Append('0', 64);

                var indices = GetRandomBits(rand.Next(64));
                foreach (var point in indices)
                {
                    board.SetBit((Square)(point.Item1 * 8 + point.Item2));

                    int index = (7 - point.Item1) * 8 + point.Item2;
                    str[index] = '1';
                }

                Assert.AreEqual(
                    board.GetInnerValue(),
                    BitBoardHelper.FromString(str.ToString()));
            }
        }
Пример #3
0
        public void TestDoubleUpPushes()
        {
            string boardString =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "01011100" +
                "00000000" +
                "00000000" +
                "00000000";
            //	 abcdefgh
            int   rankIndex = 3;
            ulong board     = BitBoardHelper.FromString(boardString);
            byte  rank      = (byte)(board >> (rankIndex * 8) & 0xff);
            var   realMoves = new Move[] {
                new Move(Square.B2, Square.B4),
                new Move(Square.D2, Square.D4),
                new Move(Square.E2, Square.E4),
                new Move(Square.F2, Square.F4)
            };

            var moves = PawnBitBoardHelper.DoublePushes[0][rankIndex][rank];

            Assert.IsTrue(this.AreMovesEqual(realMoves, moves));
        }
Пример #4
0
        public ulong PawnsAbleToDoublePushDown(ulong emptySquares)
        {
            ulong rank5      = 0x000000FF00000000UL;
            ulong emptyRank4 = BitBoardHelper.ShiftNorthOne(emptySquares & rank5) & emptySquares;

            return(this.PawnsAbleToPushUp(emptyRank4));
        }
Пример #5
0
        public void TestSingleUpLeftAttacks1()
        {
            string boardString =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "01011100" +
                "00000000" +
                "00000000";
            //	 abcdefgh
            int   rankIndex = 2;
            ulong board     = BitBoardHelper.FromString(boardString);
            byte  rank      = (byte)(board >> (rankIndex * 8) & 0xff);
            var   realMoves = new Move[] {
                new Move(Square.C2, Square.B3),
                new Move(Square.E2, Square.D3),
                new Move(Square.F2, Square.E3),
                new Move(Square.G2, Square.F3)
            };

            var moves = PawnBitBoardHelper.AttacksLeftMoves[0][rankIndex][rank];

            Assert.IsTrue(this.AreMovesEqual(realMoves, moves));
        }
Пример #6
0
        public void TestDoubleUpPush()
        {
            string boardString =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "10000010" +
                "00101000" +
                "00000000";

            var board = new PawnBitBoard(BitBoardHelper.FromString(boardString));

            string moves =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00101000" +
                "00000000" +
                "00000000" +
                "00000000";

            ulong movesBoard   = BitBoardHelper.FromString(moves);
            ulong emptySquares = 0;

            emptySquares = ~emptySquares;

            var attacks = board.DoubleUpPushTargets(emptySquares);

            Assert.AreEqual(movesBoard, attacks);
        }
Пример #7
0
        public ulong DoubleDownPushTargets(ulong emptySquares)
        {
            ulong rank5       = 0x000000FF00000000UL;
            ulong singlePushs = this.SingleDownPushTargets(emptySquares);

            return(BitBoardHelper.ShiftSouthOne(singlePushs) & emptySquares & rank5);
        }
Пример #8
0
        public ulong PawnsAbleToDoublePushUp(ulong emptySquares)
        {
            ulong rank4      = 0x00000000FF000000UL;
            ulong emptyRank3 = BitBoardHelper.ShiftSouthOne(emptySquares & rank4) & emptySquares;

            return(this.PawnsAbleToPushUp(emptyRank3));
        }
Пример #9
0
        public void TestUpRightAttacks()
        {
            string boardString =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "10000010" +
                "00101000" +
                "00000000";

            var board = new PawnBitBoard(BitBoardHelper.FromString(boardString));

            string moves =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "01000001" +
                "00010100" +
                "00000000" +
                "00000000";

            ulong movesBoard   = BitBoardHelper.FromString(moves);
            ulong emptySquares = 0;

            emptySquares = ~emptySquares;

            var attacks = board.NorthEastAttacks();

            Assert.AreEqual(movesBoard, attacks);
        }
Пример #10
0
        public void TestAntiDiagonalMoves3()
        {
            string boardString =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000";

            ulong board  = BitBoardHelper.FromString(boardString);
            var   coords = Square.H8;

            string moves =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000";

            ulong movesBoard          = BitBoardHelper.FromString(moves);
            var   antiDiagonalAttacks = provider.AntiDiagonalAttacks(coords, board);

            Assert.AreEqual(movesBoard, antiDiagonalAttacks);
        }
Пример #11
0
        public void TestByteMirror4()
        {
            byte b = Convert.ToByte("10000000", 2);
            byte r = Convert.ToByte("00000001", 2);

            Assert.AreEqual(r, BitBoardHelper.GetMirroredByte(b));
        }
Пример #12
0
        public static int GetDeltaX(this Move move)
        {
            int fileStart = (int)BitBoardHelper.GetFileFromSquare(move.From);
            int fileEnd   = (int)BitBoardHelper.GetFileFromSquare(move.To);

            return(fileEnd - fileStart);
        }
Пример #13
0
        public void TestSingleDownPushes1()
        {
            string boardString =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "01011100" +
                "00000000" +
                "00000000";
            //	 abcdefgh
            int   rankIndex = 2;
            ulong board     = BitBoardHelper.FromString(boardString);
            byte  rank      = (byte)(board >> (rankIndex * 8) & 0xff);
            var   realMoves = new Move[] {
                new Move(Square.B4, Square.B3),
                new Move(Square.D4, Square.D3),
                new Move(Square.E4, Square.E3),
                new Move(Square.F4, Square.F3)
            };

            var moves = PawnBitBoardHelper.QuietMoves[1][rankIndex][rank];

            Assert.IsTrue(this.AreMovesEqual(realMoves, moves));
        }
Пример #14
0
        private ulong GetKnightMoves(int i, int j)
        {
            /*
             *   noNoWe    noNoEa
             +15  +17
             |     |
             | noWeWe  +6 __|     |__+10  noEaEa
             \   /
             \     >0<
             \ __ /   \ __
             \ soWeWe -10   |     |   -6  soEaEa
             |     |
             |  -17  -15
             | soSoWe    soSoEa
             */
            int   index      = i * 8 + j;
            ulong initialPos = 1UL << index;

            ulong board = 0;

            board |= BitBoardHelper.ShiftNorthNorthWest(initialPos);
            board |= BitBoardHelper.ShiftNorthNorthEast(initialPos);
            board |= BitBoardHelper.ShiftNorthWestWest(initialPos);
            board |= BitBoardHelper.ShiftNorthEastEast(initialPos);
            board |= BitBoardHelper.ShiftSouthWestWest(initialPos);
            board |= BitBoardHelper.ShiftSouthEastEast(initialPos);
            board |= BitBoardHelper.ShiftSouthSouthWest(initialPos);
            board |= BitBoardHelper.ShiftSouthSouthEast(initialPos);

            return(board);
        }
Пример #15
0
        public static int GetDeltaY(this Move move)
        {
            int rankStart = BitBoardHelper.GetRankFromSquare(move.From);
            int rankEnd   = BitBoardHelper.GetRankFromSquare(move.To);

            return(rankEnd - rankStart);
        }
Пример #16
0
        public void AttacksTest3()
        {
            string boardString =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000";

            ulong board  = BitBoardHelper.FromString(boardString);
            var   coords = Square.C4;

            string moves =
                "00100010" +
                "00100100" +
                "10101000" +
                "01110000" +
                "11011111" +
                "01110000" +
                "10101000" +
                "00100100";

            ulong movesBoard = BitBoardHelper.FromString(moves);
            var   attacks    = provider.GetAttacks(coords, board);

            Assert.AreEqual(movesBoard, attacks);
        }
Пример #17
0
        public void TestDiagonalMoves4()
        {
            string boardString =
                "00000000" +
                "00001000" +
                "00000001" +
                "00010010" +
                "10100100" +
                "00000001" +
                "00011000" +
                "10100011";

            ulong board  = BitBoardHelper.FromString(boardString);
            var   coords = Square.E3;

            string moves =
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000100" +
                "00000000" +
                "00010000" +
                "00000000";
            ulong movesBoard      = BitBoardHelper.FromString(moves);
            var   diagonalAttacks = provider.DiagonalAttacks(coords, board);

            Assert.AreEqual(movesBoard, diagonalAttacks);
        }
Пример #18
0
        public static int GetRealIndex(this Square sq)
        {
            int file = (int)BitBoardHelper.GetFileFromSquare(sq);
            int rank = BitBoardHelper.GetRankFromSquare(sq);

            return((7 - rank) * 8 + file);
        }
Пример #19
0
        public void LeftTest()
        {
            ulong black = 1 << 2;
            ulong white = 1 << 1;

            ulong expected = 1;
            var   actual   = Play.ValidateOneDirection(Play.Left, black, white, BitBoardHelper.EmptySquares(black, white));

            Assert.AreEqual(expected, actual);
        }
Пример #20
0
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Square start       = (Square)values[0];
            Square end         = (Square)values[1];
            double actualWidth = (double)values[2];

            int fileStart = (int)BitBoardHelper.GetFileFromSquare(start);
            int fileEnd   = (int)BitBoardHelper.GetFileFromSquare(end);

            return((fileEnd - fileStart) * actualWidth);
        }
Пример #21
0
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Square start        = (Square)values[0];
            Square end          = (Square)values[1];
            double actualHeight = (double)values[2];

            int rankStart = (int)BitBoardHelper.GetRankFromSquare(start);
            int rankEnd   = (int)BitBoardHelper.GetRankFromSquare(end);

            return((rankEnd - rankStart) * actualHeight);
        }
Пример #22
0
        public void BitScanForwardTest()
        {
            var index = BitBoardHelper.BitScanForward(1);

            Assert.AreEqual(0, index);
            index = BitBoardHelper.BitScanForward(128);
            Assert.AreEqual(7, index);
            index = BitBoardHelper.BitScanForward((ulong)Math.Pow(2, 27));
            Assert.AreEqual(27, index);
            index = BitBoardHelper.BitScanForward((ulong)Math.Pow(2, 64));
            Assert.AreEqual(63, index);
        }
Пример #23
0
        public override void WriteResults(System.IO.TextWriter tw)
        {
            var arr = this.data as ulong[];

            foreach (var item in arr)
            {
                tw.WriteLine(BitBoardHelper.ToString(item, "\n"));
                tw.WriteLine();
            }

            //tw.WriteLine(BitBoardHelper.ToString(0x0000000080402000UL, "\n"));
        }
Пример #24
0
        protected ulong GetFileMask(int file)
        {
            ulong board = 1UL << file;
            int   count = 8;

            while (--count > 0)
            {
                board |= BitBoardHelper.ShiftNorthOne(board);
            }

            return(board);
        }
Пример #25
0
        private ulong GetKingMoves(int i, int j)
        {
            int   index      = i * 8 + j;
            ulong initialPos = 1UL << index;

            ulong board = initialPos;

            ulong attacks = BitBoardHelper.ShiftEastOne(board) | BitBoardHelper.ShiftWestOne(board);

            board    = initialPos | attacks;
            attacks |= BitBoardHelper.ShiftNorthOne(board) | BitBoardHelper.ShiftSouthOne(board);
            return(attacks);
        }
Пример #26
0
        public void SetBackgroundColor(Square square)
        {
            int file = (int)BitBoardHelper.GetFileFromSquare(square);
            int rank = BitBoardHelper.GetRankFromSquare(square);

            if (0 == ((7 - file) + rank) % 2)
            {
                this.SquareColor = Color.White;
            }
            else
            {
                this.SquareColor = Color.Black;
            }
        }
Пример #27
0
        public void KnightTest5()
        {
            string knightAttacksBoard =
                "00000000" +
                "00000101" +
                "00001000" +
                "00000000" +
                "00001000" +
                "00000101" +
                "00000000" +
                "00000000";
            ulong attacks = BitBoardHelper.FromString(knightAttacksBoard);

            Assert.AreEqual(attacks, provider.GetAttacks(Square.G5, 0));
        }
Пример #28
0
        public void TestReverseUlong()
        {
            int count = 10000;

            while ((count--) > 0)
            {
                string str        = Get64CharsString();
                ulong  board      = BitBoardHelper.FromString(str);
                ulong  reversed   = Int64Helper.GetReversedUlong(board);
                ulong  rereversed = Int64Helper.GetReversedUlong(reversed);

                Assert.AreEqual(str.MyReverse(), BitBoardHelper.ToString(reversed, string.Empty).LJust(64, '0'));
                Assert.AreEqual(board, rereversed);
            }
        }
Пример #29
0
        public void TestFileToRank3()
        {
            string boardString =
                "10000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000" +
                "00000000";
            byte  b     = Convert.ToByte("00000001", 2);
            ulong board = BitBoardHelper.FromString(boardString);

            Assert.AreEqual(b, BitBoardHelper.GetRankFromAFile(board));
        }
Пример #30
0
        public void FromStringTest()
        {
            string boardString =
                "00000000" +
                "00101110" +
                "00001000" +
                "00010000" +
                "00010000" +
                "01000000" +
                "00100010" +
                "00100010";

            var board     = BitBoardHelper.FromString(boardString);
            var newString = BitBoardHelper.ToString(board, string.Empty);

            Assert.AreEqual(boardString, newString);
        }