Пример #1
0
    public void HighReadWriteTest()
    {
        var bb = new BitsBoard();

        bb[0] = true;
        Assert.IsTrue(bb[0]);
        Assert.IsFalse(bb[64]);
        Assert.IsFalse(bb[65]);

        bb[64] = true;
        Assert.IsTrue(bb[0]);
        Assert.IsTrue(bb[64]);
        Assert.IsFalse(bb[65]);

        bb[65] = true;
        Assert.IsTrue(bb[0]);
        Assert.IsTrue(bb[64]);
        Assert.IsTrue(bb[65]);

        bb[0] = false;
        Assert.IsFalse(bb[0]);
        Assert.IsTrue(bb[64]);
        Assert.IsTrue(bb[65]);

        bb[64] = false;
        Assert.IsFalse(bb[0]);
        Assert.IsFalse(bb[64]);
        Assert.IsTrue(bb[65]);

        bb[65] = false;
        Assert.IsFalse(bb[0]);
        Assert.IsFalse(bb[64]);
        Assert.IsFalse(bb[65]);
    }
Пример #2
0
    static PrecomputedMoveData()
    {
        BitsBoard threats;

        for (byte b = 0; b < NUM_HEXES; b++)
        {
            var start = FastIndex.FromByte(b);
            kingMoves[b]   = GenerateAllPossibleKingMoves(start).ToArray();
            knightMoves[b] = GenerateAllPossibleKnightMoves(start).ToArray();
            squireMoves[b] = GenerateAllPossibleSquireMoves(start).ToArray();
            rookRays[b]    = GenerateAllPossibleRookRays(start).ToArray();
            bishopRays[b]  = GenerateAllPossibleBishopRays(start).ToArray();

            threats = new BitsBoard();
            foreach (var move in kingMoves[b])
            {
                threats[move] = true;
            }
            kingThreats[b] = threats;

            threats = new BitsBoard();
            foreach (var move in squireMoves[b])
            {
                threats[move] = true;
            }
            squireThreats[b] = threats;

            threats = new BitsBoard();
            foreach (var move in knightMoves[b])
            {
                threats[move] = true;
            }
            knightThreats[b] = threats;
        }
    }
Пример #3
0
    public void HighCountTest()
    {
        var bb = new BitsBoard();

        Assert.AreEqual(0, bb.Count);
        bb[79] = true;
        Assert.AreEqual(1, bb.Count);
    }
Пример #4
0
 public void Clear()
 {
     Pieces        = default;
     Pawns         = default;
     Threats       = default;
     PawnThreats   = default;
     MaterialValue = 0;
 }
Пример #5
0
    public void CountDoubleUnset()
    {
        var bb = new BitsBoard();

        Assert.AreEqual(0, bb.Count);
        bb[0] = false;
        bb[0] = false;
        Assert.AreEqual(0, bb.Count);
    }
Пример #6
0
    void AssertSetBits(BitsBoard board, params byte[] expected)
    {
        var actual = GetSetBits(board);

        if (!actual.SequenceEqual(expected))
        {
            Assert.Fail($"Expected bits ({string.Join(", ", expected)}) got ({string.Join(", ", actual)})");
        }
    }
Пример #7
0
    public void PackSoloTest()
    {
        var unpacked = new BitsBoard();

        unpacked[82] = true;
        var packed = BitsBoard.Pack(unpacked);

        Assert.AreEqual(new byte[] { 81 }, GetSetBits(packed));
    }
Пример #8
0
    public void UnpackSoloTest()
    {
        var packed = new BitsBoard();

        packed[81] = true;
        var unpacked = BitsBoard.Unpack(packed);

        Assert.AreEqual(new byte[] { 82 }, GetSetBits(unpacked));
    }
Пример #9
0
    public void CountDoubleSet()
    {
        var bb = new BitsBoard();

        Assert.AreEqual(0, bb.Count);
        bb[0] = true;
        bb[0] = true;
        Assert.AreEqual(1, bb.Count);
    }
Пример #10
0
    public void AllEmptyAtStartTest()
    {
        var bb = new BitsBoard();

        for (byte b = 0; b < 128; b++)
        {
            Assert.False(bb[b]);
        }
    }
Пример #11
0
    public void ShiftDownInvalidTest()
    {
        var index = new FastIndex(1, 'A');
        var board = new BitsBoard();

        board[index] = true;
        var shifted = board.Shift(HexNeighborDirection.Down);

        AssertNoIndicesSet(shifted);
    }
Пример #12
0
    public void CanSetAllTest()
    {
        var bb = new BitsBoard();

        for (byte b = 0; b < 128; b++)
        {
            Assert.IsFalse(bb[b]);
            bb[b] = true;
            Assert.IsTrue(bb[b]);
        }
    }
Пример #13
0
    public void ShiftRight65()
    {
        var bb = new BitsBoard();

        bb[65] = true;
        AssertSetBits(bb, 65);

        var shifted = bb >> 65;

        AssertSetBits(shifted, 0);
    }
Пример #14
0
    public void ShiftLeft65()
    {
        var bb = new BitsBoard();

        bb[0] = true;
        AssertSetBits(bb, 0);

        var shifted = bb << 65;

        AssertSetBits(shifted, 65);
    }
Пример #15
0
    public void UnpackMultiTest()
    {
        var packed = new BitsBoard();

        packed[81] = true;
        packed[82] = true;
        packed[83] = true;
        packed[84] = true;
        var unpacked = BitsBoard.Unpack(packed);

        Assert.AreEqual(new byte[] { 82, 84, 86, 88 }, GetSetBits(unpacked));
    }
Пример #16
0
    public void ShiftDownAcrossBoundaryTest()
    {
        var index    = FastIndex.FromByte(64);
        var expected = index[HexNeighborDirection.Down];
        var board    = new BitsBoard();

        board[index] = true;

        var shifted = board.Shift(HexNeighborDirection.Down);

        AssertOnlyIndexSet(shifted, expected);
    }
Пример #17
0
    public void PackMultiTest()
    {
        var unpacked = new BitsBoard();

        unpacked[82] = true;
        unpacked[84] = true;
        unpacked[86] = true;
        unpacked[88] = true;
        var packed = BitsBoard.Pack(unpacked);

        Assert.AreEqual(new byte[] { 81, 82, 83, 84 }, GetSetBits(packed));
    }
Пример #18
0
    public void SetSingleTest()
    {
        var bb = new BitsBoard();

        bb[0] = true;
        Assert.IsTrue(bb[0]);
        Assert.IsFalse(bb[1]);

        bb[0] = false;
        Assert.IsFalse(bb[0]);
        Assert.IsFalse(bb[1]);
    }
Пример #19
0
    public void ShiftDownTest()
    {
        var index = new FastIndex(5, 'E');
        var board = new BitsBoard();

        board[index] = true;
        AssertOnlyIndexSet(board, index);

        var shifted = board.Shift(HexNeighborDirection.Down);

        AssertOnlyIndexSet(board, index);
        AssertOnlyIndexSet(shifted, new FastIndex(4, 'E'));
    }
Пример #20
0
 static void AddThreatRays(ref BitsBoard threats, FastBoardNode node, FastIndex[][] rays)
 {
     foreach (var ray in rays)
     {
         foreach (var move in ray)
         {
             threats[move] = true;
             if (node[move].team != Team.None)
             {
                 break;
             }
         }
     }
 }
Пример #21
0
    private byte[] GetSetBits(BitsBoard board)
    {
        var set = new System.Collections.Generic.List <byte>();

        for (byte b = 0; b < 128; b++)
        {
            if (board[b])
            {
                set.Add(b);
            }
        }

        return(set.ToArray());
    }
Пример #22
0
    private void AssertOnlyIndexSet(BitsBoard board, FastIndex expected)
    {
        if (board.Count == 0)
        {
            Assert.Fail($"Expected {expected}, but no indices were set");
        }

        if (!board[expected])
        {
            Assert.Fail($"Index {expected} was not set");
        }

        board[expected] = false;
        AssertNoIndicesSet(board);
    }
Пример #23
0
    private void AssertNoIndicesSet(BitsBoard board)
    {
        if (board.Count > 0)
        {
            System.Collections.Generic.List <string> setIndices = new System.Collections.Generic.List <string>();
            for (byte b = 0; b < 85; b++)
            {
                if (board[b])
                {
                    UnityEngine.Debug.LogError($"Byte {b} unexpectedly set");
                    setIndices.Add(FastIndex.FromByte(b).ToString());
                }
            }

            Assert.Fail($"Unexpectedly set indices: {string.Join(", ", setIndices)}");
        }
        Assert.AreEqual(0, board.Count);
    }
Пример #24
0
    public void NotOperatorTest()
    {
        var board = new BitsBoard();

        board[1]  = true;
        board[20] = true;
        board[64] = true;
        board[80] = true;

        BitsBoard mixed = ~board;

        Assert.IsFalse(mixed[1]);
        Assert.IsFalse(mixed[20]);
        Assert.IsFalse(mixed[64]);
        Assert.IsFalse(mixed[80]);

        Assert.IsTrue(mixed[0]);
        Assert.IsTrue(mixed[65]);
    }
Пример #25
0
    public void ShiftComprehensivetest([ValueSource(typeof(PrecomputedMoveData), nameof(PrecomputedMoveData.AllDirections))] HexNeighborDirection direction)
    {
        for (byte b = 0; b < 85; b++)
        {
            FastIndex index    = FastIndex.FromByte(b);
            var       original = new BitsBoard();
            original[index] = true;

            var expected = index[direction];
            var shifted  = original.Shift(direction);
            if (expected.IsInBounds)
            {
                AssertOnlyIndexSet(shifted, expected);
            }
            else
            {
                AssertNoIndicesSet(shifted);
            }
        }
    }
Пример #26
0
    public void CountAddRemoveTest()
    {
        var bb = new BitsBoard();

        Assert.AreEqual(0, bb.Count);
        bb[0] = true;
        Assert.AreEqual(1, bb.Count);
        bb[0] = false;
        Assert.AreEqual(0, bb.Count);

        Assert.AreEqual(0, bb.Count);
        bb[80] = true;
        Assert.AreEqual(1, bb.Count);
        bb[80] = false;
        Assert.AreEqual(0, bb.Count);

        bb[20] = true;
        bb[80] = true;
        Assert.AreEqual(2, bb.Count);
    }
Пример #27
0
    public void CountTest()
    {
        var bb = new BitsBoard();

        Assert.AreEqual(0, bb.Count);
        bb[0] = true;
        Assert.AreEqual(1, bb.Count);
        bb[64] = true;
        Assert.AreEqual(2, bb.Count);
        bb[0] = false;
        Assert.AreEqual(1, bb.Count);
        bb[64] = false;
        Assert.AreEqual(0, bb.Count);

        for (byte b = 0; b < 128; b++)
        {
            Assert.AreEqual(b, bb.Count);
            bb[b] = true;
            Assert.AreEqual(b + 1, bb.Count);
        }
    }
Пример #28
0
    public void OrOperatorTest()
    {
        var left  = new BitsBoard();
        var right = new BitsBoard();

        left[1]   = true;
        left[20]  = true;
        right[64] = true;
        right[80] = true;

        BitsBoard mixed = left | right;

        Assert.AreEqual(4, mixed.Count);
        Assert.IsTrue(mixed[1]);
        Assert.IsTrue(mixed[20]);
        Assert.IsTrue(mixed[64]);
        Assert.IsTrue(mixed[80]);

        Assert.IsFalse(mixed[0]);
        Assert.IsFalse(mixed[65]);
    }
Пример #29
0
    static void AddThreats(ref BitsBoard threats, FastBoardNode node, byte index)
    {
        var piece = node[index];

        switch (piece.piece)
        {
        case FastPiece.King:
            threats = threats | PrecomputedMoveData.kingThreats[index];
            return;

        case FastPiece.Knight:
            threats = threats | PrecomputedMoveData.knightThreats[index];
            return;

        case FastPiece.Squire:
            threats = threats | PrecomputedMoveData.squireThreats[index];
            return;

        case FastPiece.Bishop:
            AddThreatRays(ref threats, node, PrecomputedMoveData.bishopRays[index]);
            return;

        case FastPiece.Rook:
            AddThreatRays(ref threats, node, PrecomputedMoveData.rookRays[index]);
            return;

        case FastPiece.Queen:
            AddThreatRays(ref threats, node, PrecomputedMoveData.bishopRays[index]);
            AddThreatRays(ref threats, node, PrecomputedMoveData.rookRays[index]);
            return;

        case FastPiece.None:
        case FastPiece.Pawn:     // Pawn handled by caller
        default:
            return;
        }
    }