summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--makefile6
-rw-r--r--score/main.cpp91
2 files changed, 96 insertions, 1 deletions
diff --git a/makefile b/makefile
index 62e2684..07109bf 100644
--- a/makefile
+++ b/makefile
@@ -2,7 +2,7 @@ CPP_ARGS = -std=c++20 -O2 -Wall -Wextra -Iinclude
GTKMM_CPP_ARGS = $(shell pkg-config --cflags gtkmm-4.0)
GTKMM_LD_ARGS = $(shell pkg-config --libs gtkmm-4.0)
-default: bin/bench
+default: bin/bench bin/score
clean:
rm -r obj bin
@@ -11,6 +11,10 @@ bin/bench: obj/bench/main.o obj/bench/bench_window.o obj/bench/core_widget.o obj
@mkdir -p $(dir $@)
g++ ${CPP_ARGS} $^ ${GTKMM_LD_ARGS} -o $@
+bin/score: obj/score/main.o obj/lib94.o
+ @mkdir -p $(dir $@)
+ g++ ${CPP_ARGS} $^ -o $@
+
obj/lib94.o: obj/lib94/core.o obj/lib94/executors.o obj/lib94/warrior.o
ld -r $^ -o $@
diff --git a/score/main.cpp b/score/main.cpp
new file mode 100644
index 0000000..1b29107
--- /dev/null
+++ b/score/main.cpp
@@ -0,0 +1,91 @@
+#include <lib94/lib94.hpp>
+#include <iostream>
+#include <fstream>
+
+[[noreturn]] void usage(const char *argv0) {
+ std::cerr << "usage: " << argv0 << " <first warrior> <second warrior> <number of rounds> <steps until tie>\n";
+ exit(1);
+}
+
+int main(int argc, const char **argv) {
+ if (argc != 5)
+ usage(argv[0]);
+
+ unsigned round_count, steps_until_tie;
+
+ try {
+ round_count = std::stoul(argv[3]);
+ steps_until_tie = std::stoul(argv[4]);
+ }
+ catch (const std::exception &e) {
+ usage(argv[0]);
+ }
+
+ std::ifstream file1(argv[1]);
+ std::ifstream file2(argv[2]);
+
+ if (!file1 || !file2)
+ usage(argv[0]);
+
+ std::string source1(std::istreambuf_iterator<char>(file1), {});
+ std::string source2(std::istreambuf_iterator<char>(file2), {});
+
+ file1.close();
+ file2.close();
+
+ auto w1 = lib94::compile_warrior(source1);
+ auto w2 = lib94::compile_warrior(source2);
+
+ if (std::holds_alternative<std::string>(w1)) {
+ std::cerr << "error compiling " << argv[1] << ": " << std::get<std::string>(w1) << '\n';
+ return 1;
+ }
+
+ if (std::holds_alternative<std::string>(w2)) {
+ std::cerr << "error compiling " << argv[2] << ": " << std::get<std::string>(w2) << '\n';
+ return 1;
+ }
+
+ const lib94::warrior *ws[2];
+ ws[0] = std::get<lib94::warrior *>(w1);
+ ws[1] = std::get<lib94::warrior *>(w2);
+
+ unsigned w1_wins = 0;
+ unsigned w2_wins = 0;
+
+ lib94::seed_prng(time(NULL));
+
+ for (unsigned round_number = 0; round_number < round_count; ++round_number) {
+
+ lib94::clear_core({
+ .op = lib94::DAT, .mod = lib94::F,
+ .amode = lib94::DIRECT, .bmode = lib94::DIRECT,
+ .anumber = 0, .bnumber = 0
+ });
+
+ lib94::init_round(ws, 2);
+
+ for (unsigned step_number = 0; step_number < steps_until_tie; ++step_number) {
+
+ const lib94::warrior *w = lib94::single_step();
+
+ if (w == ws[0]) {
+ ++w2_wins;
+ break;
+ }
+
+ if (w == ws[1]) {
+ ++w1_wins;
+ break;
+ }
+
+ }
+
+ }
+
+ std::cout << round_count << " rounds completed\n";
+ std::cout << w1_wins << " wins, " << w2_wins << " losses\n";
+ std::cout << "score " << (int)w1_wins - (int)w2_wins << '\n';
+
+ return 0;
+}