Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a token count that excludes "Console.Write"s, making it easier to work within the token constraint while debugging #281

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace ChessChallenge.Application
{
public class TokenCount {
public int total;
public int excludingLogs;
}
public static class TokenCounter
{

Expand All @@ -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))
Expand All @@ -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};
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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]";
}
Expand Down