diff --git a/Mahjong/Table.cpp b/Mahjong/Table.cpp index 69c326e..2a41681 100644 --- a/Mahjong/Table.cpp +++ b/Mahjong/Table.cpp @@ -143,13 +143,9 @@ void Table::init_before_playing() players[2].score, players[3].score, }; - FILE* fp = fopen(write_log_filename.c_str(), "w+"); - fprintf(fp, "Table table;\ntable.game_init_for_replay(%s, %s, %d, %d, %d, %d);\n", - vec2str(yama_log).c_str(), - vec2str(init_score).c_str(), - n立直棒, n本场, 场风, 庄家); - - fclose(fp); + log_buffer_prefix = fmt::format("Table table;\ntable.game_init_for_replay({}, {}, {}, {}, {}, {});\n", + vec2str(yama_log), vec2str(init_score), n立直棒, n本场, 场风, 庄家); + selection_log.reserve(512); // avoid reallocation } from_beginning(); @@ -746,9 +742,10 @@ void Table::make_selection(int selection) #endif // 这个地方控制了游戏流转 if (write_log) { - FILE* fp = fopen(write_log_filename.c_str(), "a+"); + /*FILE* fp = fopen(write_log_filename.c_str(), "a+"); fprintf(fp, "\ntable.make_selection(%d);", selection); - fclose(fp); + fclose(fp);*/ + selection_log.push_back(selection); } // 分为两种情况,如果是ACTION阶段 switch (phase) { diff --git a/Mahjong/Table.h b/Mahjong/Table.h index 943654e..a6893c9 100644 --- a/Mahjong/Table.h +++ b/Mahjong/Table.h @@ -7,6 +7,7 @@ #include "GameResult.h" #include "macro.h" #include "Player.h" +#include "fmt/os.h" #include namespace_mahjong @@ -34,14 +35,18 @@ class Table int turn = 0; BaseAction last_action = BaseAction::出牌; Wind 场风 = Wind::East; - int 庄家 = 0; // 庄家 + int 庄家 = 0; int n本场 = 0; int n立直棒 = 0; GameLog fullGameLog; + /* For debug */ bool write_log = false; - std::string write_log_filename = "replay.log"; std::vector yama_log; + std::string log_buffer_prefix; + std::vector selection_log; + + /* Set seed (set_seed) before init*/ bool use_seed = false; int seed = 0; @@ -85,6 +90,22 @@ class Table use_seed = true; } // inline int get_seed() { return seed; } + inline std::string get_debug_replay() + { + std::string ret; + ret += fmt::format("{}", log_buffer_prefix); + for (auto selection : selection_log) { + ret += fmt::format("table.make_selection({});\n", selection); + } + return ret; + } + inline void print_debug_replay() + { + fmt::print("{}", log_buffer_prefix); + for (auto selection : selection_log) { + fmt::print("table.make_selection({});\n", selection); + } + } enum ToStringOption : int { YAMA = 1 << 0, diff --git a/MahjongPyWrapper/MahjongPy.cpp b/MahjongPyWrapper/MahjongPy.cpp index 911aa74..ae082ad 100644 --- a/MahjongPyWrapper/MahjongPy.cpp +++ b/MahjongPyWrapper/MahjongPy.cpp @@ -184,8 +184,10 @@ PYBIND11_MODULE(MahjongPyWrapper, m) .def("get_result", &Table::get_result) .def("get_self_actions", &Table::get_self_actions) .def("get_response_actions", &Table::get_response_actions) - .def("set_write_log", &Table::set_write_log) .def("set_seed", &Table::set_seed) + .def("set_write_log", &Table::set_write_log) + .def("print_debug_replay", &Table::print_debug_replay) + .def("get_debug_replay", &Table::get_debug_replay) // 成员变量们 .def_readonly("dora_spec", &Table::dora_spec)