// ModernCpu tests: beats the placeholder CPU, survives a long solo run, finds a T-spin double, plans quickly. #include #include #include #include #include #include "check.hpp" #include "core/ai.hpp" #include "core/match.hpp" namespace { using namespace tbg; const CharacterDef* chr(std::string_view name) { const Roster& r = Roster::original(); return &r.characters[static_cast(r.find(name))]; } // A VS match as the app sets it up for the Modern ruleset (real timings, 7-bag, own previews). MatchConfig modern_vs(std::uint32_t seed, bool guideline) { MatchConfig cfg; cfg.seed = seed; cfg.timing = Timing::modern(cfg.timing); cfg.timing.guideline_attack = guideline; cfg.generator = GeneratorKind::Bag7; cfg.queue_mode = QueueMode::Mirrored; return cfg; } void test_modern_cpu_beats_simple_cpu() { int wins = 0; for (std::uint32_t seed = 1; seed <= 3; ++seed) { Match m(modern_vs(seed, seed % 2 == 1), {chr("Halloween"), chr("Mirurun")}); const int me = static_cast(seed % 2); // swap seats between matches ModernCpu modern(3, seed); SimpleCpu simple(3); std::array in{}; for (int f = 0; f < 200000 && !m.match_winner(); ++f) { in[static_cast(me)] = modern.next(m, me); in[static_cast(1 - me)] = simple.next(m, 1 - me); m.step(in); } wins += m.match_winner() && *m.match_winner() == me; } CHECK(wins == 3); } void test_modern_cpu_survives_solo() { // One player, nobody to attack: the speed level still rises with time (minutes 3+). Match m(modern_vs(5, true), {chr("Halloween")}); ModernCpu cpu(2, 5); std::array in{}; for (int f = 0; f < 20000 && !m.player(0).dead(); ++f) { in[0] = cpu.next(m, 0); m.step(in); } CHECK(!m.player(0).dead()); CHECK(m.player(0).total_lines() > 120); } void test_modern_cpu_easy_levels_are_easy() { // Level 0 (beginner) loses to level 1 (casual) and tops out on its own within 10 minutes; level 1 places // pieces at a casual pace (well under 60 a minute). Match vs(modern_vs(101, true), {chr("Halloween"), chr("Mirurun")}); ModernCpu beginner(0, 1), casual(1, 2); std::array in{}; for (int f = 0; f < 60 * 60 * 20 && !vs.match_winner(); ++f) { in[0] = beginner.next(vs, 0); in[1] = casual.next(vs, 1); vs.step(in); } CHECK(vs.match_winner() && *vs.match_winner() == 1); Match solo(modern_vs(3, true), {chr("Halloween")}); ModernCpu cpu(0, 3); std::array one{}; int f = 0; for (; f < 60 * 60 * 10 && !solo.player(0).dead(); ++f) { one[0] = cpu.next(solo, 0); solo.step(one); } CHECK(solo.player(0).dead()); Match pace(modern_vs(4, true), {chr("Halloween")}); ModernCpu c1(1, 4); int pieces = 0; for (int k = 0; k < 60 * 60 * 2 && !pace.player(0).dead(); ++k) { one[0] = c1.next(pace, 0); pace.step(one); for (const MatchEvent& e : pace.events()) pieces += e.type == MatchEvent::Type::Lock; } CHECK(pieces < 2 * 60); // < 60 pieces per minute } void test_modern_cpu_tspin_double() { MatchConfig cfg = MatchConfig::instant(); cfg.timing = Timing::modern(Timing::instant()); cfg.timing.guideline_attack = true; cfg.generator = GeneratorKind::Weighted; cfg.generator_weights = {0, 0, 0, 0, 0, 0, 1}; // only T pieces cfg.crystal_interval = 0; Match m(cfg, {chr("Halloween")}); // TSD slot at columns 3-5 under an overhang at (5, 21). const char* rows[] = {".....#####", "###...####", "####.#####"}; Board& b = m.player(0).board(); for (int r = 0; r < 3; ++r) for (int x = 0; x < 10; ++x) if (rows[r][x] == '#') b.set(x, 21 + r, cell::kBlock); std::array in{}; for (int k = 0; k < 4 && !m.player(0).active(); ++k) m.step(in); ModernCpu cpu(3, 1); const auto d = cpu.decide(m, 0); CHECK(d && d->tspin == 2 && d->rot == 2 && d->x == 3 && d->y == 21); int bonus = 0; for (int f = 0; f < 600 && m.player(0).total_lines() == 0; ++f) { in[0] = cpu.next(m, 0); m.step(in); for (const MatchEvent& e : m.events()) if (e.type == MatchEvent::Type::Bonus) bonus = e.value; } CHECK(m.player(0).total_lines() == 2); CHECK((bonus & 7) == 2 && ((bonus >> 3) & 3) == 2); // a full T-spin double } void test_modern_cpu_plans_quickly() { Match m(modern_vs(9, true), {chr("Halloween")}); ModernCpu cpu(3, 9), probe(3, 10); std::array in{}; double total = 0, worst = 0; int calls = 0; bool had = false; for (int f = 0; f < 6000; ++f) { const bool has = m.player(0).active().has_value(); if (has && !had) { const auto t0 = std::chrono::steady_clock::now(); probe.decide(m, 0); const double ms = std::chrono::duration(std::chrono::steady_clock::now() - t0).count(); total += ms; worst = std::max(worst, ms); ++calls; } had = has; in[0] = cpu.next(m, 0); m.step(in); } CHECK(calls > 50); const double avg = calls ? total / calls : 0; std::printf("ModernCpu decide: %d calls, avg %.2f ms, worst %.2f ms\n", calls, avg, worst); CHECK(avg < 8.0); // generous: debug builds included (an optimized build is ~0.4 ms) } } // namespace void run_modern_cpu_tests() { test_modern_cpu_tspin_double(); test_modern_cpu_plans_quickly(); test_modern_cpu_survives_solo(); test_modern_cpu_beats_simple_cpu(); test_modern_cpu_easy_levels_are_easy(); }