From f83185b0d95793b64913ff02eea1c5aeac77f547 Mon Sep 17 00:00:00 2001 From: Hasan Haq Date: Mon, 10 Mar 2025 11:57:02 -0500 Subject: [PATCH] trace min max params --- src/snake.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/snake.cpp b/src/snake.cpp index e510ed1..5f9581f 100644 --- a/src/snake.cpp +++ b/src/snake.cpp @@ -94,6 +94,8 @@ struct Config { std::string json_file; bool json_compact = true; RNG rng = global_rng; + int min_trace_turn = 0; // Minimum turn to start tracing from + int max_trace_turn = INT_MAX; // Maximum turn to trace until void parse_optional_args(int argc, const char** argv); }; @@ -191,8 +193,10 @@ void print_help(const char* name, std::ostream& out = std::cout) { out << " -n, --n Run the given number of rounds (default: " << def.num_rounds << ")." << endl; out << " -s, --size Size of the (square) board (default: " << def.board_size.w << ")." << endl; out << " --seed Random seed." << endl; - out << " -T, --trace-all Print the game state after each move." << endl; - out << " -t, --trace Print the game state each time the snake eats an apple." << endl; + out << " -T, --trace-all [min max] Print the game state after each move." << endl; + out << " When min and max are provided, only print turns between min and max (inclusive)." << endl; + out << " -t, --trace [min max] Print the game state each time the snake eats an apple." << endl; + out << " When min and max are provided, only print turns between min and max (inclusive)." << endl; out << " --no-color Don't use ANSI color codes in trace output" << endl; out << " -q, --quiet Don't print extra output." << endl; out << " --json Write log of one run a json file." << endl; @@ -203,6 +207,13 @@ void print_help(const char* name, std::ostream& out = std::cout) { } void Config::parse_optional_args(int argc, const char** argv) { + auto parse_min_max_turns = [&](int& i) { + if (i+2 < argc && isdigit(argv[i+1][0]) && isdigit(argv[i+2][0])) { + min_trace_turn = std::stoi(argv[++i]); + max_trace_turn = std::stoi(argv[++i]); + } + }; + for (int i=0; i void play(Game& game, Agent& agent, Config const& config, AgentLog* log = nullptr) { while (!game.done()) { - if (config.trace == Trace::all) std::cout << game; + if (config.trace == Trace::all && game.turn >= config.min_trace_turn && game.turn <= config.max_trace_turn) std::cout << game; auto event = game.move(agent(game,log)); - if (event == Game::Event::eat && config.trace == Trace::eat) std::cout << game; + if (event == Game::Event::eat && config.trace == Trace::eat && game.turn >= config.min_trace_turn && game.turn <= config.max_trace_turn) std::cout << game; } - if (config.trace == Trace::all) std::cout << game; + if (config.trace == Trace::all && game.turn >= config.min_trace_turn && game.turn <= config.max_trace_turn) std::cout << game; } template