static void ProcessFiles(string[] files, int minGameLengthPly, int recordToPly)
    {
        Board.SetPositionFromFen(Definitions.startFen);

        // Read pgns and convert to opening book dictionary
        for (int fileIndex = 0; fileIndex < files.Length; fileIndex++)
        {
            StreamReader  reader = new StreamReader(files[fileIndex]);
            List <string> pgns   = new List <string>();

            // split text into array of pgn strings
            bool readingPGN         = false;
            int  pgnIndex           = -1;
            bool finishedReadingPGN = false;
            while (reader.Peek() != -1)
            {
                string line = reader.ReadLine() + " ";
                if (line.Contains("["))                   // comment line
                {
                    finishedReadingPGN = false;
                    readingPGN         = false;
                    continue;
                }
                else if (!finishedReadingPGN)
                {
                    for (int charIndex = 0; charIndex < line.Length; charIndex++)
                    {
                        if (!readingPGN && line[charIndex] == '1')
                        {
                            readingPGN = true;
                            pgns.Add("");
                            pgnIndex++;
                        }
                        if (readingPGN)
                        {
                            pgns[pgnIndex] += line[charIndex] + "";
                            if (pgns[pgnIndex].Split('.').Length * 2 > recordToPly)                               // only record the first n moves for opening book
                            {
                                finishedReadingPGN = true;
                                break;
                            }
                        }
                    }
                }
            }

            reader.Close();

            // get moves from pgn files
            for (int i = 0; i < pgns.Count; i++)
            {
                string pgn = pgns[i];

                if (pgn.Split('.').Length * 2 < minGameLengthPly)                   // don't record games that were shorter than minGameLengthPly. This is to avoid games where an opening distaster occured
                {
                    continue;
                }

                List <string> moveStrings = PGNReader.MoveStringsFromPGN(pgn);
                List <ushort> moves       = PGNReader.MovesFromPGN(pgn);

                for (int j = 0; j < moves.Count; j++)
                {
                    if (!book.ContainsKey(Board.zobristKey))
                    {
                        keys.Add(Board.zobristKey);
                        book.Add(Board.zobristKey, new List <ushort>());
                    }
                    if (!book[Board.zobristKey].Contains(moves[j]))
                    {
                        book[Board.zobristKey].Add(moves[j]);
                    }
                    Board.MakeMove(moves[j]);
                }
                for (int k = moves.Count - 1; k >= 0; k--)
                {
                    Board.UnmakeMove(moves[k]);
                }
            }
        }
    }
Exemplo n.º 2
0
    public bool ValidateEntry()
    {
        if (currentInput.Length > 0)
        {
            List <ushort> inputtedMoves = PGNReader.MovesFromPGN(currentInput);
            if (inputtedMoves.Count > 0)
            {
                string cleanedInput = PGNReader.MoveStringsFromPGN(currentInput) [0];

                ushort   inputMove     = inputtedMoves [0];
                int      movePieceCode = PieceTypeCodeFromNotation(cleanedInput) + ((Board.IsWhiteToPlay()) ? 1 : 0);
                ushort[] legalMoves    = moveGen.GetMoves(false, false).moves;

                bool ambiguousCaseSpecified = false;                 // does the inputted string describe an ambiguous case (e.g Rad1)
                if (cleanedInput.Length >= 3)
                {
                    ambiguousCaseSpecified = Definitions.fileNames.Contains(cleanedInput [2] + "");
                }
                if ((movePieceCode & ~1) == Board.kingCode || (movePieceCode & ~1) == Board.pawnCode)                   // king and pawn moves can't be ambiguous
                {
                    ambiguousCaseSpecified = true;
                }

                bool moveIsAmbiguous = false;

                if (!ambiguousCaseSpecified)                   // check if case is ambiguous if no specification has been given in input
                {
                    int movesFoundFollowingInput = 0;

                    for (int i = 0; i < legalMoves.Length; i++)
                    {
                        int moveFromIndex = legalMoves [i] & 127;
                        int moveToIndex   = (legalMoves [i] >> 7) & 127;
                        if (Board.boardArray [moveFromIndex] == movePieceCode && moveToIndex == ((inputMove >> 7) & 127))                           // is move as described by input string
                        {
                            movesFoundFollowingInput++;
                            if (movesFoundFollowingInput == 2)
                            {
                                moveIsAmbiguous = true;
                                break;
                            }
                        }
                    }
                }

                if (moveIsAmbiguous)
                {
                    uiManager.SetMessage("move is ambiguous. please specify file/rank of piece you wish to move", 3, true);
                }
                else
                {
                    if (player != null)
                    {
                        player.TryMakeMove(inputMove);
                    }
                    return(true);
                }
            }
            else
            {
                print("Move illegal/incorrect format");
            }
        }

        return(false);
    }