void DrawBookMoves() { ClearArrows(); arrowIndex = 0; if (book.HasPosition(board.ZobristKey)) { BookPosition bookPosition = book.GetBookPosition(board.ZobristKey); int mostPlayed = 0; int leastPlayed = int.MaxValue; foreach (var moveInfo in bookPosition.numTimesMovePlayed) { int numTimesPlayed = moveInfo.Value; mostPlayed = System.Math.Max(mostPlayed, numTimesPlayed); leastPlayed = System.Math.Min(leastPlayed, numTimesPlayed); } foreach (var moveInfo in bookPosition.numTimesMovePlayed) { Move move = new Move(moveInfo.Key); int numTimesPlayed = moveInfo.Value; Vector2 startPos = boardUI.PositionFromCoord(BoardRepresentation.CoordFromIndex(move.StartSquare)); Vector2 endPos = boardUI.PositionFromCoord(BoardRepresentation.CoordFromIndex(move.TargetSquare)); float t = Mathf.InverseLerp(leastPlayed, mostPlayed, numTimesPlayed); if (mostPlayed == leastPlayed) { t = 1; } Color col = Color.Lerp(rarestCol, mostCommonCol, t); DrawArrow2D(startPos, endPos, arrowWidth, arrowHeadSize, col, zPos: -1 - t); } } }
void CreateBook() { var sw = System.Diagnostics.Stopwatch.StartNew(); Book book = new Book(); var reader = new StringReader(gamesFile.text); string pgn; Board board = new Board(); while (!string.IsNullOrEmpty(pgn = reader.ReadLine())) { Move[] moves = PGNLoader.MovesFromPGN(pgn, maxPlyCount: maxPlyToRecord); board.LoadStartPosition(); for (int i = 0; i < moves.Length; i++) { book.Add(board.ZobristKey, moves[i]); board.MakeMove(moves[i]); } } string bookString = ""; foreach (var bookPositionsByZobristKey in book.bookPositions) { ulong key = bookPositionsByZobristKey.Key; BookPosition bookPosition = bookPositionsByZobristKey.Value; string line = key + ":"; bool isFirstMoveEntry = true; foreach (var moveCountByMove in bookPosition.numTimesMovePlayed) { ushort moveValue = moveCountByMove.Key; int moveCount = moveCountByMove.Value; if (moveCount >= minMovePlayCount) { if (isFirstMoveEntry) { isFirstMoveEntry = false; } else { line += ","; } line += $" {moveValue} ({moveCount})"; } } bool hasRecordedAnyMoves = !isFirstMoveEntry; if (hasRecordedAnyMoves) { bookString += line + System.Environment.NewLine; } } //string s = fastJSON.JSON.ToJSON (book); FileWriter.WriteToTextAsset_EditorOnly(bookFile, bookString, append); Debug.Log("Created book: " + sw.ElapsedMilliseconds + " ms."); //Book loadedBook = fastJSON.JSON.ToObject<Book> (s); }