Example #1
0
        public override string Solve(string input, bool part2)
        {
            long answerCnt = 0;

            foreach (string answerGroup in GetGroupedLines(input))
            {//Get the group answerkey for each group.
                //Initialize the group key for the right operation below.
                int groupKey = part2 ? -1 : 0;
                foreach (string personAnswer in GetLines(answerGroup))
                {
                    //Get a single persons answers
                    int answerKey = 0;
                    foreach (Match answer in Regex.Matches(personAnswer.ToLower(), @"\w"))
                    {
                        answerKey = Bitwise.SetBit(answerKey, (byte)answer.Value[0] - (byte)'a', true);
                    }
                    //Add Persons answers to its group Key
                    if (part2)
                    {
                        groupKey &= answerKey;
                    }
                    else
                    {
                        groupKey |= answerKey;
                    }
                }
                //count the answers in this group
                answerCnt += Bitwise.CountSetBits(groupKey);
            }
            return("Sum of all Answers: " + answerCnt);
        }
Example #2
0
        public void TestStartPosition()
        {
            var uciStream = new UciStream();
            var board     = uciStream.Board;

            board.SetPosition(Board.StartPositionFen);
            uciStream.WriteMessageLine(board.ToString());
            // Validate integrity of board and occupancy of every square.
            board.AssertIntegrity();
            Assert.That(board.CurrentPosition.GetPiece(Square.a8), Is.EqualTo(Piece.BlackRook));
            Assert.That(board.CurrentPosition.GetPiece(Square.b8), Is.EqualTo(Piece.BlackKnight));
            Assert.That(board.CurrentPosition.GetPiece(Square.c8), Is.EqualTo(Piece.BlackBishop));
            Assert.That(board.CurrentPosition.GetPiece(Square.d8), Is.EqualTo(Piece.BlackQueen));
            Assert.That(board.CurrentPosition.GetPiece(Square.e8), Is.EqualTo(Piece.BlackKing));
            Assert.That(board.CurrentPosition.GetPiece(Square.f8), Is.EqualTo(Piece.BlackBishop));
            Assert.That(board.CurrentPosition.GetPiece(Square.g8), Is.EqualTo(Piece.BlackKnight));
            Assert.That(board.CurrentPosition.GetPiece(Square.h8), Is.EqualTo(Piece.BlackRook));
            var square = Square.a7;

            do
            {
                Assert.That(board.CurrentPosition.GetPiece(square), Is.EqualTo(Piece.BlackPawn));
                square++;
            } while (square <= Square.h7);
            do
            {
                Assert.That(board.CurrentPosition.GetPiece(square), Is.EqualTo(Piece.None));
                square++;
            } while (square <= Square.h3);
            do
            {
                Assert.That(board.CurrentPosition.GetPiece(square), Is.EqualTo(Piece.WhitePawn));
                square++;
            } while (square <= Square.h2);
            Assert.That(board.CurrentPosition.GetPiece(Square.a1), Is.EqualTo(Piece.WhiteRook));
            Assert.That(board.CurrentPosition.GetPiece(Square.b1), Is.EqualTo(Piece.WhiteKnight));
            Assert.That(board.CurrentPosition.GetPiece(Square.c1), Is.EqualTo(Piece.WhiteBishop));
            Assert.That(board.CurrentPosition.GetPiece(Square.d1), Is.EqualTo(Piece.WhiteQueen));
            Assert.That(board.CurrentPosition.GetPiece(Square.e1), Is.EqualTo(Piece.WhiteKing));
            Assert.That(board.CurrentPosition.GetPiece(Square.f1), Is.EqualTo(Piece.WhiteBishop));
            Assert.That(board.CurrentPosition.GetPiece(Square.g1), Is.EqualTo(Piece.WhiteKnight));
            Assert.That(board.CurrentPosition.GetPiece(Square.h1), Is.EqualTo(Piece.WhiteRook));
            // Validate piece counts.
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhitePawns), Is.EqualTo(8));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteKnights), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteBishops), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteRooks), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteQueens), Is.EqualTo(1));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteKing), Is.EqualTo(1));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackPawns), Is.EqualTo(8));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackKnights), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackBishops), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackRooks), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackQueens), Is.EqualTo(1));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackKing), Is.EqualTo(1));
        }
Example #3
0
    public void TestStartPosition()
    {
        var board = new Board(WriteMessageLine, long.MaxValue);

        board.SetPosition(Board.StartPositionFen);
        WriteMessageLine(board.ToString());
        // Validate integrity of board and occupancy of every square.
        board.AssertIntegrity();
        Assert.That(board.CurrentPosition.GetPiece(Square.A8), Is.EqualTo(Piece.BlackRook));
        Assert.That(board.CurrentPosition.GetPiece(Square.B8), Is.EqualTo(Piece.BlackKnight));
        Assert.That(board.CurrentPosition.GetPiece(Square.C8), Is.EqualTo(Piece.BlackBishop));
        Assert.That(board.CurrentPosition.GetPiece(Square.D8), Is.EqualTo(Piece.BlackQueen));
        Assert.That(board.CurrentPosition.GetPiece(Square.E8), Is.EqualTo(Piece.BlackKing));
        Assert.That(board.CurrentPosition.GetPiece(Square.F8), Is.EqualTo(Piece.BlackBishop));
        Assert.That(board.CurrentPosition.GetPiece(Square.G8), Is.EqualTo(Piece.BlackKnight));
        Assert.That(board.CurrentPosition.GetPiece(Square.H8), Is.EqualTo(Piece.BlackRook));
        var square = Square.A7;

        do
        {
            Assert.That(board.CurrentPosition.GetPiece(square), Is.EqualTo(Piece.BlackPawn));
            square++;
        } while (square <= Square.H7);
        do
        {
            Assert.That(board.CurrentPosition.GetPiece(square), Is.EqualTo(Piece.None));
            square++;
        } while (square <= Square.H3);
        do
        {
            Assert.That(board.CurrentPosition.GetPiece(square), Is.EqualTo(Piece.WhitePawn));
            square++;
        } while (square <= Square.H2);
        Assert.That(board.CurrentPosition.GetPiece(Square.A1), Is.EqualTo(Piece.WhiteRook));
        Assert.That(board.CurrentPosition.GetPiece(Square.B1), Is.EqualTo(Piece.WhiteKnight));
        Assert.That(board.CurrentPosition.GetPiece(Square.C1), Is.EqualTo(Piece.WhiteBishop));
        Assert.That(board.CurrentPosition.GetPiece(Square.D1), Is.EqualTo(Piece.WhiteQueen));
        Assert.That(board.CurrentPosition.GetPiece(Square.E1), Is.EqualTo(Piece.WhiteKing));
        Assert.That(board.CurrentPosition.GetPiece(Square.F1), Is.EqualTo(Piece.WhiteBishop));
        Assert.That(board.CurrentPosition.GetPiece(Square.G1), Is.EqualTo(Piece.WhiteKnight));
        Assert.That(board.CurrentPosition.GetPiece(Square.H1), Is.EqualTo(Piece.WhiteRook));
        // Validate piece counts.
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhitePawn]), Is.EqualTo(8));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteKnight]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteBishop]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteRook]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteQueen]), Is.EqualTo(1));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteKing]), Is.EqualTo(1));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackPawn]), Is.EqualTo(8));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackKnight]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackBishop]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackRook]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackQueen]), Is.EqualTo(1));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackKing]), Is.EqualTo(1));
    }
Example #4
0
    public void FindPinnedPieces()
    {
        var ownKingSquare          = Bitwise.FirstSetSquare(GetKing(ColorToMove));
        var ownPieces              = ColorOccupancy[(int)ColorToMove];
        var enemyRankFileAttackers = GetRooks(ColorLastMoved) | GetQueens(ColorLastMoved);
        var enemyDiagonalAttackers = GetBishops(ColorLastMoved) | GetQueens(ColorLastMoved);
        var enemyPieces            = ColorOccupancy[(int)ColorLastMoved];

        // Find pieces pinned to own king by enemy rank / file attackers.
        PinnedPieces = 0;
        Square attackerSquare;

        while ((attackerSquare = Bitwise.PopFirstSetSquare(ref enemyRankFileAttackers)) != Square.Illegal)
        {
            var betweenSquares = Board.RankFileBetweenSquares[(int)attackerSquare][(int)ownKingSquare];
            if (betweenSquares == 0)
            {
                continue;
            }
            if ((betweenSquares & enemyPieces) == 0)
            {
                // No enemy pieces between enemy attacker and own king.
                var pinnedPieces = betweenSquares & ownPieces;
                if (Bitwise.CountSetBits(pinnedPieces) == 1)
                {
                    // Exactly one own piece between enemy attacker and own king.
                    // Piece is pinned to own king.
                    PinnedPieces |= pinnedPieces;
                }
            }
        }
        // Find pieces pinned to own king by enemy diagonal attackers.
        while ((attackerSquare = Bitwise.PopFirstSetSquare(ref enemyDiagonalAttackers)) != Square.Illegal)
        {
            var betweenSquares = Board.DiagonalBetweenSquares[(int)attackerSquare][(int)ownKingSquare];
            if (betweenSquares == 0)
            {
                continue;
            }
            if ((betweenSquares & enemyPieces) == 0)
            {
                // No enemy pieces between enemy attacker and own king.
                var pinnedPieces = betweenSquares & ownPieces;
                if (Bitwise.CountSetBits(pinnedPieces) == 1)
                {
                    // Exactly one own piece between enemy attacker and own king.
                    // Piece is pinned to own king.
                    PinnedPieces |= pinnedPieces;
                }
            }
        }
    }
Example #5
0
    public void TestWac11Position()
    {
        var board = new Board(WriteMessageLine, long.MaxValue);

        board.SetPosition("r1b1kb1r/3q1ppp/pBp1pn2/8/Np3P2/5B2/PPP3PP/R2Q1RK1 w kq -");
        WriteMessageLine(board.ToString());
        // Validate integrity of board and occupancy of every square.
        board.AssertIntegrity();
        Assert.That(board.CurrentPosition.GetPiece(Square.A8), Is.EqualTo(Piece.BlackRook));
        Assert.That(board.CurrentPosition.GetPiece(Square.B8), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.C8), Is.EqualTo(Piece.BlackBishop));
        Assert.That(board.CurrentPosition.GetPiece(Square.D8), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.E8), Is.EqualTo(Piece.BlackKing));
        Assert.That(board.CurrentPosition.GetPiece(Square.F8), Is.EqualTo(Piece.BlackBishop));
        Assert.That(board.CurrentPosition.GetPiece(Square.G8), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.H8), Is.EqualTo(Piece.BlackRook));
        Assert.That(board.CurrentPosition.GetPiece(Square.A7), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.B7), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.C7), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.D7), Is.EqualTo(Piece.BlackQueen));
        Assert.That(board.CurrentPosition.GetPiece(Square.E7), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.F7), Is.EqualTo(Piece.BlackPawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.G7), Is.EqualTo(Piece.BlackPawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.H7), Is.EqualTo(Piece.BlackPawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.A6), Is.EqualTo(Piece.BlackPawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.B6), Is.EqualTo(Piece.WhiteBishop));
        Assert.That(board.CurrentPosition.GetPiece(Square.C6), Is.EqualTo(Piece.BlackPawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.D6), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.E6), Is.EqualTo(Piece.BlackPawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.F6), Is.EqualTo(Piece.BlackKnight));
        Assert.That(board.CurrentPosition.GetPiece(Square.G6), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.H6), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.A5), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.B5), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.C5), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.D5), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.E5), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.F5), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.G5), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.H5), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.A4), Is.EqualTo(Piece.WhiteKnight));
        Assert.That(board.CurrentPosition.GetPiece(Square.B4), Is.EqualTo(Piece.BlackPawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.C4), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.D4), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.E4), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.F4), Is.EqualTo(Piece.WhitePawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.G4), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.H4), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.A3), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.B3), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.C3), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.D3), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.E3), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.F3), Is.EqualTo(Piece.WhiteBishop));
        Assert.That(board.CurrentPosition.GetPiece(Square.G3), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.H3), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.A2), Is.EqualTo(Piece.WhitePawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.B2), Is.EqualTo(Piece.WhitePawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.C2), Is.EqualTo(Piece.WhitePawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.D2), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.E2), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.F2), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.G2), Is.EqualTo(Piece.WhitePawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.H2), Is.EqualTo(Piece.WhitePawn));
        Assert.That(board.CurrentPosition.GetPiece(Square.A1), Is.EqualTo(Piece.WhiteRook));
        Assert.That(board.CurrentPosition.GetPiece(Square.B1), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.C1), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.D1), Is.EqualTo(Piece.WhiteQueen));
        Assert.That(board.CurrentPosition.GetPiece(Square.E1), Is.EqualTo(Piece.None));
        Assert.That(board.CurrentPosition.GetPiece(Square.F1), Is.EqualTo(Piece.WhiteRook));
        Assert.That(board.CurrentPosition.GetPiece(Square.G1), Is.EqualTo(Piece.WhiteKing));
        Assert.That(board.CurrentPosition.GetPiece(Square.H1), Is.EqualTo(Piece.None));
        // Validate piece counts.
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhitePawn]), Is.EqualTo(6));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteKnight]), Is.EqualTo(1));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteBishop]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteRook]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteQueen]), Is.EqualTo(1));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.WhiteKing]), Is.EqualTo(1));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackPawn]), Is.EqualTo(7));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackKnight]), Is.EqualTo(1));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackBishop]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackRook]), Is.EqualTo(2));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackQueen]), Is.EqualTo(1));
        Assert.That(Bitwise.CountSetBits(board.CurrentPosition.PieceBitboards[(int)Piece.BlackKing]), Is.EqualTo(1));
    }
Example #6
0
    public void FindMagicMultipliers(ColorlessPiece colorlessPiece, Delegates.WriteMessageLine writeMessageLine = null)
    {
        Direction[] directions;
        ulong[]     unoccupiedMoveMasks;
        ulong[]     relevantOccupancyMasks;
        ulong[]     magicMultipliers;
        int[]       shifts;
        ulong[][]   moveMasks;
        // ReSharper disable SwitchStatementHandlesSomeKnownEnumValuesWithDefault
        switch (colorlessPiece)
        {
        case ColorlessPiece.Bishop:
            directions             = new[] { Direction.NorthEast, Direction.SouthEast, Direction.SouthWest, Direction.NorthWest };
            unoccupiedMoveMasks    = Board.BishopMoveMasks;
            relevantOccupancyMasks = _bishopRelevantOccupancyMasks;
            magicMultipliers       = _bishopMagicMultipliers;
            shifts    = _bishopShifts;
            moveMasks = _bishopMoveMasks;
            break;

        case ColorlessPiece.Rook:
            directions             = new[] { Direction.North, Direction.East, Direction.South, Direction.West };
            unoccupiedMoveMasks    = Board.RookMoveMasks;
            relevantOccupancyMasks = _rookRelevantOccupancyMasks;
            magicMultipliers       = _rookMagicMultipliers;
            shifts    = _rookShifts;
            moveMasks = _rookMoveMasks;
            break;

        default:
            throw new ArgumentException($"{colorlessPiece} piece not supported.");
        }
        // ReSharper restore SwitchStatementHandlesSomeKnownEnumValuesWithDefault
        // Generate moves mask on each square.
        var occupancyToMovesMask = new Dictionary <ulong, ulong>();
        var uniqueMovesMasks     = new HashSet <ulong>();

        for (var square = Square.A8; square < Square.Illegal; square++)
        {
            occupancyToMovesMask.Clear();
            uniqueMovesMasks.Clear();
            var moveDestinations         = unoccupiedMoveMasks[(int)square];
            var relevantMoveDestinations = moveDestinations & relevantOccupancyMasks[(int)square];
            var uniqueOccupancies        = (int)Math.Pow(2, Bitwise.CountSetBits(relevantMoveDestinations));
            occupancyToMovesMask.EnsureCapacity(uniqueOccupancies);
            // Generate moves mask for every permutation of occupancy of the relevant destination squares.
            var occupancyPermutations = Bitwise.GetAllPermutations(relevantMoveDestinations);
            for (var occupancyIndex = 0; occupancyIndex < occupancyPermutations.Count; occupancyIndex++)
            {
                var occupancy = occupancyPermutations[occupancyIndex];
                var movesMask = Board.CreateMoveDestinationsMask(square, occupancy, directions);
                occupancyToMovesMask.Add(occupancy, movesMask);
                if (!uniqueMovesMasks.Contains(movesMask))
                {
                    uniqueMovesMasks.Add(movesMask);
                }
            }
            Debug.Assert(occupancyToMovesMask.Count == uniqueOccupancies);
            // Determine bit shift that produces number >= unique occupancies.
            // A stricter condition is number >= unique moves but this requires more computing time to find magic multipliers.
            var shift = 64 - (int)Math.Ceiling(Math.Log(uniqueOccupancies, 2d));
            shifts[(int)square] = shift;
            var magicMultiplier      = magicMultipliers[(int)square];
            var knownMagicMultiplier = magicMultiplier == 0 ? null : (ulong?)magicMultiplier;
            (magicMultipliers[(int)square], moveMasks[(int)square]) = FindMagicMultiplier(occupancyToMovesMask, shift, knownMagicMultiplier);
            writeMessageLine?.Invoke($"{Board.SquareLocations[(int)square],6}  {PieceHelper.GetName(colorlessPiece),6}  {shift,5}  {occupancyToMovesMask.Count,18}  {uniqueMovesMasks.Count,12}  {magicMultipliers[(int)square],16:X16}");
        }
    }
Example #7
0
        public void TestWac11Position()
        {
            var uciStream = new UciStream();
            var board     = uciStream.Board;

            board.SetPosition("r1b1kb1r/3q1ppp/pBp1pn2/8/Np3P2/5B2/PPP3PP/R2Q1RK1 w kq -");
            uciStream.WriteMessageLine(board.ToString());
            // Validate integrity of board and occupancy of every square.
            board.AssertIntegrity();
            Assert.That(board.CurrentPosition.GetPiece(Square.a8), Is.EqualTo(Piece.BlackRook));
            Assert.That(board.CurrentPosition.GetPiece(Square.b8), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.c8), Is.EqualTo(Piece.BlackBishop));
            Assert.That(board.CurrentPosition.GetPiece(Square.d8), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.e8), Is.EqualTo(Piece.BlackKing));
            Assert.That(board.CurrentPosition.GetPiece(Square.f8), Is.EqualTo(Piece.BlackBishop));
            Assert.That(board.CurrentPosition.GetPiece(Square.g8), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.h8), Is.EqualTo(Piece.BlackRook));
            Assert.That(board.CurrentPosition.GetPiece(Square.a7), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.b7), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.c7), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.d7), Is.EqualTo(Piece.BlackQueen));
            Assert.That(board.CurrentPosition.GetPiece(Square.e7), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.f7), Is.EqualTo(Piece.BlackPawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.g7), Is.EqualTo(Piece.BlackPawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.h7), Is.EqualTo(Piece.BlackPawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.a6), Is.EqualTo(Piece.BlackPawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.b6), Is.EqualTo(Piece.WhiteBishop));
            Assert.That(board.CurrentPosition.GetPiece(Square.c6), Is.EqualTo(Piece.BlackPawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.d6), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.e6), Is.EqualTo(Piece.BlackPawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.f6), Is.EqualTo(Piece.BlackKnight));
            Assert.That(board.CurrentPosition.GetPiece(Square.g6), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.h6), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.a5), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.b5), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.c5), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.d5), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.e5), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.f5), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.g5), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.h5), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.a4), Is.EqualTo(Piece.WhiteKnight));
            Assert.That(board.CurrentPosition.GetPiece(Square.b4), Is.EqualTo(Piece.BlackPawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.c4), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.d4), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.e4), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.f4), Is.EqualTo(Piece.WhitePawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.g4), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.h4), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.a3), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.b3), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.c3), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.d3), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.e3), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.f3), Is.EqualTo(Piece.WhiteBishop));
            Assert.That(board.CurrentPosition.GetPiece(Square.g3), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.h3), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.a2), Is.EqualTo(Piece.WhitePawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.b2), Is.EqualTo(Piece.WhitePawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.c2), Is.EqualTo(Piece.WhitePawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.d2), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.e2), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.f2), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.g2), Is.EqualTo(Piece.WhitePawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.h2), Is.EqualTo(Piece.WhitePawn));
            Assert.That(board.CurrentPosition.GetPiece(Square.a1), Is.EqualTo(Piece.WhiteRook));
            Assert.That(board.CurrentPosition.GetPiece(Square.b1), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.c1), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.d1), Is.EqualTo(Piece.WhiteQueen));
            Assert.That(board.CurrentPosition.GetPiece(Square.e1), Is.EqualTo(Piece.None));
            Assert.That(board.CurrentPosition.GetPiece(Square.f1), Is.EqualTo(Piece.WhiteRook));
            Assert.That(board.CurrentPosition.GetPiece(Square.g1), Is.EqualTo(Piece.WhiteKing));
            Assert.That(board.CurrentPosition.GetPiece(Square.h1), Is.EqualTo(Piece.None));
            // Validate piece counts.
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhitePawns), Is.EqualTo(6));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteKnights), Is.EqualTo(1));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteBishops), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteRooks), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteQueens), Is.EqualTo(1));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.WhiteKing), Is.EqualTo(1));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackPawns), Is.EqualTo(7));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackKnights), Is.EqualTo(1));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackBishops), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackRooks), Is.EqualTo(2));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackQueens), Is.EqualTo(1));
            Assert.That(Bitwise.CountSetBits(board.CurrentPosition.BlackKing), Is.EqualTo(1));
        }