public double Evaluate(Position position)
        {
            double score = (position.Board.WhiteKing.PopCount() - position.Board.BlackKing.PopCount()) * 200
                           + (position.Board.WhiteQueens.PopCount() - position.Board.BlackQueens.PopCount()) * 9
                           + (position.Board.WhiteRooks.PopCount() - position.Board.BlackRooks.PopCount()) * 5
                           + (position.Board.WhiteBishops.PopCount() - position.Board.BlackBishops.PopCount()) * 3
                           + (position.Board.WhiteKnights.PopCount() - position.Board.BlackKnights.PopCount()) * 3
                           + (position.Board.WhitePawns.PopCount() - position.Board.BlackPawns.PopCount()) * 1;

            int totalMovesAvailable = MoveGenerator.GetAllMoves(position, onlyLegal: true).Count;
            int enemyMovesAvailable = MoveGenerator.GetAllMoves(position with {
                SideToMove = position.OpposingSide
            }, onlyLegal: true).Count;

            score += 0.1 * (totalMovesAvailable - enemyMovesAvailable);

            if (position.SideToMove == Side.White)
            {
                return(score);
            }
            else
            {
                return(-1 * score);
            }
        }
Beispiel #2
0
        public void GeneratesCorrectNumberOfMovesForPositionWithPossiblePromotions()
        {
            // Assemble
            var position = Position.FromFENString("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8");

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            moves.Should().HaveCount(44);
        }
Beispiel #3
0
        public void GeneratesCorrectNumberOfMovesForPositionWithPossibleIllegalMoves()
        {
            // Assemble
            var position = Position.FromFENString("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -");

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            moves.Should().HaveCount(14);
            moves.Where(m => m.IsCapture).Should().HaveCount(1);
        }
Beispiel #4
0
        public void GeneratesCorrectNumberOfMovesForKiwiPetePosition()
        {
            // Assemble
            var position = Position.FromFENString("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -");

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            moves.Should().HaveCount(48);
            moves.Where(m => m.IsCapture).Should().HaveCount(8);
        }
Beispiel #5
0
        public void CanIdentifyAllCastleMovesBlack()
        {
            // Assemble
            var position = Position.FromFENString("r3k2r/8/8/8/8/8/8/R3K2R b KQkq - 0 1 ");

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            var castlingMoves = new[]
            {
                new Move(Square.e8, Square.g8, MoveFlags.KingCastle),
                new Move(Square.e8, Square.c8, MoveFlags.QueenCastle)
            };

            moves.Should().Contain(castlingMoves);
        }
Beispiel #6
0
        public void CanGetMovesForPositionWithEnpassantWhite()
        {
            // Assemble
            var board = new Board
            {
                WhitePawns = 0x00_00_00_04_00_00_00_00,
                BlackPawns = 0x00_00_00_08_00_00_00_00
            };

            var position = new Position(board, Side.White, Square.d6, CastlingRights.None, 0);

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            moves.Should().HaveCount(2);
            moves.Where(m => m.Flags == MoveFlags.EnPassantCapture).Should().HaveCount(1);
        }
Beispiel #7
0
        public void CanGeneratePawnMovesForPositionBlack()
        {
            // Assemble
            var board = new Board
            {
                WhitePawns = 0x00_00_00_01_04_0a_f0_00,
                BlackPawns = 0x00_00_1d_82_40_20_00_00
            };

            var position = new Position(board, Side.Black, null, CastlingRights.None, 0);

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            moves.Should().HaveCount(9);
            moves.Where(m => (m.Flags & MoveFlags.Capture) != 0).Should().HaveCount(3);
        }
Beispiel #8
0
        public void CanGeneratePawnMovesForBasicPosition()
        {
            // Assemble
            var board = new Board
            {
                WhitePawns = 0x00_00_00_00_00_00_ff_00,
                BlackPawns = 0x00_00_00_00_00_14_00_00
            };

            var position = new Position(board, Side.White, null, CastlingRights.None, 0);

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            moves.Should().HaveCount(16);
            moves.Where(m => (m.Flags & MoveFlags.Capture) != 0).Should().HaveCount(4);
        }
Beispiel #9
0
        public void CanGeneratePawnMovesForBasicPositionWithPromotions()
        {
            // Assemble
            var board = new Board
            {
                WhitePawns  = 0x00_12_20_00_00_00_00_00,
                BlackQueens = 0x08_00_00_00_00_00_00_00
            };

            var position = new Position(board, Side.White, null, CastlingRights.None, 0);

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            moves.Should().HaveCount(13);
            moves.Where(m => m.IsCapture).Should().HaveCount(4);
            moves.Where(m => m.IsPromotion).Should().HaveCount(12);
        }
Beispiel #10
0
        public void GeneratesAllStandardPromotions()
        {
            // Assemble
            var position = Position.FromFENString("rnb2k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w - - 0 1 ");

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            var promotionMoves = new[]
            {
                new Move(Square.d7, Square.d8, MoveFlags.KnightPromotion),
                new Move(Square.d7, Square.d8, MoveFlags.BishopPromotion),
                new Move(Square.d7, Square.d8, MoveFlags.QueenPromotion),
                new Move(Square.d7, Square.d8, MoveFlags.RookPromotion),
            };

            moves.Should().Contain(promotionMoves);
        }
Beispiel #11
0
        public void GeneratesAllCapturePromotions()
        {
            // Assemble
            var position = Position.FromFENString("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8");

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            var promotionMoves = new[]
            {
                new Move(Square.d7, Square.c8, MoveFlags.Capture | MoveFlags.KnightPromotion, Piece.Bishop),
                new Move(Square.d7, Square.c8, MoveFlags.Capture | MoveFlags.BishopPromotion, Piece.Bishop),
                new Move(Square.d7, Square.c8, MoveFlags.Capture | MoveFlags.QueenPromotion, Piece.Bishop),
                new Move(Square.d7, Square.c8, MoveFlags.Capture | MoveFlags.RookPromotion, Piece.Bishop),
            };

            moves.Should().Contain(promotionMoves);
        }
Beispiel #12
0
        public void GeneratesCorrectMovesForStartingPosition()
        {
            // Assemble
            var position = new Position(Board.StartingPosition, Side.White, null, CastlingRights.All, 0);

            // Act
            var moves = MoveGenerator.GetAllMoves(position);

            // Assert
            var expectedMoves = new List <Move>
            {
                new Move(Square.a2, Square.a3, MoveFlags.QuietMove),
                new Move(Square.b2, Square.b3, MoveFlags.QuietMove),
                new Move(Square.c2, Square.c3, MoveFlags.QuietMove),
                new Move(Square.d2, Square.d3, MoveFlags.QuietMove),
                new Move(Square.e2, Square.e3, MoveFlags.QuietMove),
                new Move(Square.f2, Square.f3, MoveFlags.QuietMove),
                new Move(Square.g2, Square.g3, MoveFlags.QuietMove),
                new Move(Square.h2, Square.h3, MoveFlags.QuietMove),

                new Move(Square.a2, Square.a4, MoveFlags.DoublePawnPush),
                new Move(Square.b2, Square.b4, MoveFlags.DoublePawnPush),
                new Move(Square.c2, Square.c4, MoveFlags.DoublePawnPush),
                new Move(Square.d2, Square.d4, MoveFlags.DoublePawnPush),
                new Move(Square.e2, Square.e4, MoveFlags.DoublePawnPush),
                new Move(Square.f2, Square.f4, MoveFlags.DoublePawnPush),
                new Move(Square.g2, Square.g4, MoveFlags.DoublePawnPush),
                new Move(Square.h2, Square.h4, MoveFlags.DoublePawnPush),

                new Move(Square.b1, Square.a3, MoveFlags.QuietMove),
                new Move(Square.b1, Square.c3, MoveFlags.QuietMove),
                new Move(Square.g1, Square.f3, MoveFlags.QuietMove),
                new Move(Square.g1, Square.h3, MoveFlags.QuietMove),
            };

            moves.Should().BeEquivalentTo(expectedMoves);
        }