Exemple #1
0
        public void FanRankFileAmbiguationPositiveTest()
        {
            // Tests both knights moving to same square for Rank ambiguity

            const string        fen      = "8/6k1/8/8/8/8/1K1N1N2/8 w - - 0 1";
            const MoveNotations notation = MoveNotations.Fan;

            var movingPiece   = new Piece(Pieces.WhiteKnight);
            var fromOneSquare = new Square(Squares.d2);
            var fromTwoSquare = new Square(Squares.f2);
            var toSquare      = new Square(Squares.e4);

            var uniChar        = movingPiece.GetUnicodeChar();
            var toSquareString = toSquare.ToString();

            var expectedPrimary   = $"{uniChar}{fromOneSquare.FileChar}{toSquareString}";
            var expectedSecondary = $"{uniChar}{fromTwoSquare.FileChar}{toSquareString}";

            var board      = new Board();
            var pieceValue = new PieceValue();
            var pos        = new Position(board, pieceValue);
            var g          = GameFactory.Create(pos);

            g.NewGame(fen);

            var w1 = new Move(fromOneSquare, toSquare);
            var w2 = new Move(fromTwoSquare, toSquare);

            var ambiguity = new MoveAmbiguity(pos);

            var actualPrimary   = ambiguity.ToNotation(w1, notation);
            var actualSecondary = ambiguity.ToNotation(w2, notation);

            Assert.Equal(expectedPrimary, actualPrimary);
            Assert.Equal(expectedSecondary, actualSecondary);
        }
Exemple #2
0
        /// <summary>
        /// Flatten a game into its indexed form and a list of the positions.
        /// </summary>
        /// <param name="pgnGame">The parsed PGN game object to flatten.</param>
        /// <param name="datasetId">The dataset for this import job.</param>
        /// <returns>A 2-tuple with the indexed game and a list of positions.</returns>
        public (IndexedGame Game, List <GamePositionRow> Positions) FlattenPgnGame(
            PgnGame pgnGame,
            DatasetId datasetId)
        {
            var indexedGame = this.CreateGameFromPgnGame(pgnGame, datasetId);

            var board      = new Board();
            var pieceValue = new PieceValue();
            var pos        = new Position(board, pieceValue);
            var game       = GameFactory.Create(pos);

            if (pgnGame.AllTags.ContainsKey("FEN"))
            {
                var fen = pgnGame.AllTags["FEN"].Value;
                game.NewGame(fen);
            }
            else
            {
                game.NewGame();
            }

            var positions = new List <GamePositionRow>();
            var pgnPlies  = pgnGame.MainLineAsList;

            int             plyCount        = 0;
            GamePly         prevPly         = null;
            var             pliesSoFar      = new List <GamePly>();
            string          gameId          = Guid.NewGuid().ToString();
            OpeningPosition openingPosition = null;

            foreach (var ply in pgnPlies)
            {
                var position = new GamePositionRow();
                position.GameUniqueId   = gameId;
                position.GamePlyNumber  = plyCount;
                position.GameMoveNumber = (plyCount / 2) + 1;

                var fen      = game.GetFen().ToString();
                var fenParts = fen.Split(' ');

                if (!ply !.SanIsNullMove)
                {
                    Rudz.Chess.Types.Move move;
                    try
                    {
                        move = this.PlyToChessMove(game, ply);
                    }
                    catch (FlattenNoMatchException e)
                    {
                        string posFen = game.Pos.GenerateFen().ToString();
                        Console.WriteLine(e);
                        move = this.PlyToChessMove(game, ply);
                        throw;
                    }

                    var movePrinter = new MoveAmbiguity(game.Pos);
                    position.NextMoveSan = ply !.San;
                    position.NextMoveUci = movePrinter.ToNotation(move, MoveNotations.Uci);

                    var state = new State();
                    game.Pos.MakeMove(move, state);
                }