diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index 6ef9b6086..7971f179e 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -52,7 +52,7 @@ public enum PlayerType // Other readonly BoardUI boardUI; readonly MoveGenerator moveGenerator; - readonly int tokenCount; + readonly TokenCount tokenCount; readonly StringBuilder pgns; public ChallengeController() @@ -215,7 +215,7 @@ ChessPlayer CreatePlayer(PlayerType type) }; } - static int GetTokenCount() + static TokenCount GetTokenCount() { string path = Path.Combine(Directory.GetCurrentDirectory(), "src", "My Bot", "MyBot.cs"); diff --git a/Chess-Challenge/src/Framework/Application/Helpers/Token Counter/TokenCounter.cs b/Chess-Challenge/src/Framework/Application/Helpers/Token Counter/TokenCounter.cs index c79b36daa..e916d4373 100644 --- a/Chess-Challenge/src/Framework/Application/Helpers/Token Counter/TokenCounter.cs +++ b/Chess-Challenge/src/Framework/Application/Helpers/Token Counter/TokenCounter.cs @@ -4,6 +4,10 @@ namespace ChessChallenge.Application { + public class TokenCount { + public int total; + public int excludingLogs; + } public static class TokenCounter { @@ -20,22 +24,26 @@ public static class TokenCounter SyntaxKind.CloseParenToken }); - public static int CountTokens(string code) + public static TokenCount CountTokens(string code) { SyntaxTree tree = CSharpSyntaxTree.ParseText(code); SyntaxNode root = tree.GetRoot(); return CountTokens(root); } - static int CountTokens(SyntaxNodeOrToken syntaxNode) + static TokenCount CountTokens(SyntaxNodeOrToken syntaxNode) { SyntaxKind kind = syntaxNode.Kind(); int numTokensInChildren = 0; + int numTokensExcludingLogs = 0; foreach (var child in syntaxNode.ChildNodesAndTokens()) { - numTokensInChildren += CountTokens(child); + if (!(child.ToString().StartsWith("System.Console.Write") || child.ToString().StartsWith("Console.Write"))) { + numTokensExcludingLogs += CountTokens(child).excludingLogs; + } + numTokensInChildren += CountTokens(child).total; } if (syntaxNode.IsToken && !tokensToIgnore.Contains(kind)) @@ -45,14 +53,15 @@ static int CountTokens(SyntaxNodeOrToken syntaxNode) // String literals count for as many chars as are in the string if (kind is SyntaxKind.StringLiteralToken or SyntaxKind.InterpolatedStringTextToken) { - return syntaxNode.ToString().Length; + int length = syntaxNode.ToString().Length; + return new TokenCount{total = length, excludingLogs = length}; } // Regular tokens count as just one token - return 1; + return new TokenCount{total = 1, excludingLogs = 1}; } - return numTokensInChildren; + return new TokenCount{total = numTokensInChildren, excludingLogs = numTokensExcludingLogs}; } } diff --git a/Chess-Challenge/src/Framework/Application/UI/BotBrainCapacityUI.cs b/Chess-Challenge/src/Framework/Application/UI/BotBrainCapacityUI.cs index c72deaaed..6f46775e2 100644 --- a/Chess-Challenge/src/Framework/Application/UI/BotBrainCapacityUI.cs +++ b/Chess-Challenge/src/Framework/Application/UI/BotBrainCapacityUI.cs @@ -10,7 +10,7 @@ public static class BotBrainCapacityUI static readonly Color red = new(219, 9, 9, 255); static readonly Color background = new Color(40, 40, 40, 255); - public static void Draw(int numTokens, int tokenLimit) + public static void Draw(TokenCount tokenCount, int tokenLimit) { int screenWidth = Raylib.GetScreenWidth(); @@ -20,7 +20,7 @@ public static void Draw(int numTokens, int tokenLimit) // Bg Raylib.DrawRectangle(0, screenHeight - height, screenWidth, height, background); // Bar - double t = (double)numTokens / tokenLimit; + double t = (double)tokenCount.total / tokenLimit; Color col; if (t <= 0.7) @@ -34,8 +34,11 @@ public static void Draw(int numTokens, int tokenLimit) Raylib.DrawRectangle(0, screenHeight - height, (int)(screenWidth * t), height, col); var textPos = new System.Numerics.Vector2(screenWidth / 2, screenHeight - height / 2); - string text = $"Bot Brain Capacity: {numTokens}/{tokenLimit}"; - if (numTokens > tokenLimit) + string text = $"Bot Brain Capacity: {tokenCount.total}/{tokenLimit}"; + if (tokenCount.total != tokenCount.excludingLogs) { + text += $" (Excluding Logs: {tokenCount.excludingLogs})"; + } + if (tokenCount.total > tokenLimit) { text += " [LIMIT EXCEEDED]"; }