Exemplo n.º 1
0
        private static bool IsPawnInStartingPosition(ushort squareIndex, Color color)
        {
            var bv             = MovingPieceService.GetBoardValueOfIndex(squareIndex);
            var secondRankMask = MovingPieceService.GetPawnStartRankMask(color);

            return((bv & secondRankMask) != 0);
        }
Exemplo n.º 2
0
        public override string ToString()
        {
            var pieceFile         = MovingPieceService.FileFromIdx(SquareIndex);
            var pawnDescription   = $"{Color.AsString()}";
            var pieceDescription  = $"Piece";
            var pawnPlacement     = $"on {DisplayHelpers.ToSquareString(SquareIndex)} ";
            var expectedIndexes   = MovingPieceService.GetSetBits(Expected);
            var playerOccupancy   = MovingPieceService.GetSetBits(PlayerObstructions);
            var opponentOccupancy = MovingPieceService.GetSetBits(OpponentObstructions);
            var strMoves          =
                expectedIndexes.Any()
                    ? $"move to {string.Join(", ", expectedIndexes.Select(DisplayHelpers.ToSquareString))}"
                    : "[no moves expected]";
            var strPlayerOccupancy =
                playerOccupancy.Any()
                    ? string.Join(", ", playerOccupancy.Select(DisplayHelpers.ToSquareString))
                    : "[no player pieces]";
            var strOpponentOccupancy =
                opponentOccupancy.Any()
                    ? string.Join(", ", opponentOccupancy.Select(DisplayHelpers.ToSquareString))
                    : "[no opponent pieces]";
            var attackedOpponentPieces = opponentOccupancy.Where(x => MovingPieceService.FileFromIdx(x) != pieceFile);
            var strAttack = attackedOpponentPieces.Any()
                ? $"- Attack pieces on {string.Join(", ", attackedOpponentPieces.Select(DisplayHelpers.ToSquareString))}"
                : "[no attacked pieces]";

            return($"{pawnDescription} {pieceDescription} on {pawnPlacement} should be able to:{Environment.NewLine}" +
                   $"{strMoves}{Environment.NewLine}" +
                   $"{strAttack}{Environment.NewLine}" +
                   $"when players pieces are at {strPlayerOccupancy} and opponent pieces are at {strOpponentOccupancy}.");
        }
        public static IEnumerable <TestCaseData> GetCastlingMoves(Color color, Move move)
        {
            const string emptyFenWhiteToMove    = "4k3/8/8/8/8/8/8/4K3 w - - 0 1";
            const string emptyFenBlackToMove    = "4k3/8/8/8/8/8/8/4K3 b - - 0 1";
            var          rookInitialSquareIndex = MoveHelpers.GetRookMoveForCastleMove(move).SourceIndex;
            var          between             = BoardHelpers.InBetween(rookInitialSquareIndex, move.SourceIndex);
            var          blockerPermutations = MovingPieceService.GetAllPermutationsOfSetBits(between.GetSetBits(), 0, 0)
                                               .Where(x => x != 0);
            var fen   = color == Color.Black ? emptyFenBlackToMove : emptyFenWhiteToMove;
            var board = fenTextToBoard.Translate(fen);

            foreach (var permutation in blockerPermutations)
            {
                var editedBoard = (Board)board.Clone();
                editedBoard.Occupancy[(int)color][(int)Piece.King] = permutation;
                var strBlockers = string.Join(", ", permutation.GetSetBits());
                yield return(new TestCaseData(editedBoard, move)
                             .SetName($"Blockers on indices {strBlockers}")
                             .Returns(MoveError.CastleOccupancyBetween));
            }

            yield return(new TestCaseData(board, move)
                         .SetName("No Blockers")
                         .Returns(MoveError.NoneSet));
        }
Exemplo n.º 4
0
        private static IEnumerable <TestCaseData> SetupPathSquareValidation(Board board, Move castlingMove)
        {
            var totalOccupancy  = board.Occupancy.Occupancy();
            var enPassantSquare = board.EnPassantIndex;

            var inBetweenSquares = BoardHelpers.InBetween(castlingMove.SourceIndex, castlingMove.DestinationIndex) |
                                   castlingMove.DestinationValue;
            var inBetweenSquareArr = inBetweenSquares.GetSetBits().ToArray();
            var permutations       =
                MovingPieceService.GetAllPermutationsOfSetBits(inBetweenSquareArr, 0, 0)
                .Distinct()
                .ToList();

            TestContext.WriteLine(permutations);
            TestContext.WriteLine("----------------------------------------");
            foreach (var attackedSquaresValue in permutations)
            {
                var bb = new Mock <IBitboard>();
                var attackedSquares = attackedSquaresValue.GetSetBits();

                foreach (var squareBetween in inBetweenSquareArr)
                {
                    var isAttacked = attackedSquares.Contains(squareBetween);
                    SetupMock(board, squareBetween, totalOccupancy, enPassantSquare, bb, isAttacked);
                }

                var strBlockers = attackedSquares.Any() ? string.Join(", ", attackedSquares) : "[None]";
                TestContext.WriteLine($"Returning test case for {attackedSquaresValue}");
                var expectedReturnValue = attackedSquares.Any() ? MoveError.CastleThroughCheck : MoveError.NoneSet;
                yield return(new TestCaseData(bb, board, castlingMove)
                             .SetName($"Indices Attacked: {strBlockers}")
                             .Returns(expectedReturnValue));
            }
        }
Exemplo n.º 5
0
        private static ulong GetAllCapturesFromSquare(ushort squareIndex, Color color)
        {
            var shiftNE     = GetRelativeNEShift(color);
            var shiftNW     = GetRelativeNWShift(color);
            var squareValue = MovingPieceService.GetBoardValueOfIndex(squareIndex);

            return(shiftNE(squareValue) | shiftNW(squareValue));
        }
Exemplo n.º 6
0
        private static ulong GetPawnShift(ushort squareIndex, Color color)
        {
            var startingRank  = MovingPieceService.GetPawnStartRankMask(color);
            var squareValue   = MovingPieceService.GetBoardValueOfIndex(squareIndex);
            var shift1XMethod = GetShift(color);
            var shift2XMethod = GetDoubleShift(color);

            return(((startingRank & squareValue) != 0 ? shift2XMethod(squareValue) : 0) | shift1XMethod(squareValue));
        }
Exemplo n.º 7
0
 private void InitializeWhitePawnMovesAndAttacks()
 {
     for (ushort square = 0; square < 64; square++)
     {
         var squareValue = MovingPieceService.GetBoardValueOfIndex(square);
         MoveMask[(int)Color.White][square] =
             MovingPieceService.ShiftN(squareValue) | ((squareValue & BoardConstants.Rank2) << 16);
         AttackMask[(int)Color.White][square] =
             MovingPieceService.ShiftNW(squareValue) | MovingPieceService.ShiftNE(squareValue);
     }
 }
Exemplo n.º 8
0
 private void InitializeBlackPawnMovesAndAttacks()
 {
     foreach (var square in BoardConstants.AllSquares)
     {
         var squareValue = MovingPieceService.GetBoardValueOfIndex(square);
         MoveMask[(int)Color.Black][square] =
             MovingPieceService.ShiftS(squareValue) | ((squareValue & BoardConstants.Rank7) >> 16);
         AttackMask[(int)Color.Black][square] =
             MovingPieceService.ShiftSW(squareValue) | MovingPieceService.ShiftSE(squareValue);
     }
 }
Exemplo n.º 9
0
        private static bool IsStartingRank(Color color, ushort squareIndex)
        {
            var rank = MovingPieceService.RankFromIdx(squareIndex);

            if (color == Color.White)
            {
                return(rank == 1);
            }

            return(rank == 6);
        }
Exemplo n.º 10
0
        internal ulong GetMoves(ushort square)
        {
            var   squareValue = MovingPieceService.GetBoardValueOfIndex(square);
            ulong squares     = 0;

            foreach (var shift in _knightDirectionMethods)
            {
                squares |= shift(squareValue);
            }

            return(squares);
        }
Exemplo n.º 11
0
        protected static IEnumerable <TestCase <CastlingAvailability, CastlingAvailability> > GetCastlingAbilityTestCases()
        {
            var max = 15ul;

            var permutations      = MovingPieceService.GetAllPermutationsOfSetBits(max.GetSetBits(), 0, 0);
            var castlingCartesian = permutations.Distinct()
                                    .Select(x => (CastlingAvailability)x)
                                    .Select(x =>
                                            new TestCase <CastlingAvailability, CastlingAvailability>(x,
                                                                                                      x, $"{FENHelpers.MakeCastlingAvailabilityStringFromBitFlags(x)}")).ToArray();

            return(castlingCartesian);
        }
Exemplo n.º 12
0
        public void RookMovesShouldBeInitialized(ushort squareIndex)
        {
            var rank     = MovingPieceService.RankFromIdx(squareIndex);
            var file     = MovingPieceService.FileFromIdx(squareIndex);
            var rankFill = (ulong)0xff << (rank * 8);
            var fileFill = (ulong)0x101010101010101 << file;
            var boardVal = MovingPieceService.GetBoardValueOfIndex(squareIndex);

            var mask   = (rankFill | fileFill) ^ boardVal;
            var actual = BitBoard.GetPseudoLegalMoves(squareIndex, Piece.Rook, Color.Black, 0);

            Assert.AreEqual(mask, actual);
        }
Exemplo n.º 13
0
        private ulong GetMoves(ushort square, ulong occupancy)
        {
            ulong result      = 0;
            var   squareValue = MovingPieceService.GetBoardValueOfIndex(square);

            foreach (var shiftDirection in MoveShifts)
            {
                if (shiftDirection == null)
                {
                    continue;
                }
                result |= Traverse(shiftDirection, squareValue, occupancy);
            }

            return(result);
        }
Exemplo n.º 14
0
        private static IEnumerable <MoveTestCase> GetTestCases(Color color, ushort squareIndex)
        {
            var moveSet      = GetPawnShift(squareIndex, color);
            var setSquares   = MovingPieceService.GetSetBits(moveSet);
            var permutations = MovingPieceService.GetAllPermutationsOfSetBits(setSquares, 0, 0);
            var testCases    = new List <MoveTestCase>();

            foreach (var block in permutations)
            {
                var obstructionBoards = GetMovesFromObstructions(color, block, squareIndex);

                testCases.Add(new MoveTestCase(squareIndex, color, 0, obstructionBoards.Occupancy,
                                               obstructionBoards.MoveBoard));
            }

            return(testCases);
        }
Exemplo n.º 15
0
        private static MoveObstructionBoard GetMovesFromObstructions(Color color, ulong obstructions,
                                                                     ushort squareIndex)
        {
            ulong moves        = 0;
            var   sqValue      = MovingPieceService.GetBoardValueOfIndex(squareIndex);
            var   shiftN       = GetShift(color)(sqValue);
            var   shiftDoubleN = IsStartingRank(color, squareIndex) ? GetDoubleShift(color)(sqValue) : (ulong?)null;
            var   attacks      = GetAllCapturesFromSquare(squareIndex, color);

            if ((shiftN & obstructions) == 0)
            {
                moves |= shiftN;
                if (shiftDoubleN.HasValue && (shiftDoubleN.Value & obstructions) == 0)
                {
                    moves |= shiftDoubleN.Value;
                }
            }

            moves |= attacks & obstructions;
            return(new MoveObstructionBoard(obstructions, moves));
        }
Exemplo n.º 16
0
 protected MovingPiece()
 {
     MovingPieceSvc = new MovingPieceService();
 }