Skip to content

Commit

Permalink
update write debug replay
Browse files Browse the repository at this point in the history
  • Loading branch information
Agony5757 committed Mar 17, 2022
1 parent 057c26a commit cc45e72
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
15 changes: 6 additions & 9 deletions Mahjong/Table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 23 additions & 2 deletions Mahjong/Table.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "GameResult.h"
#include "macro.h"
#include "Player.h"
#include "fmt/os.h"
#include <array>

namespace_mahjong
Expand Down Expand Up @@ -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<int> yama_log;
std::string log_buffer_prefix;
std::vector<int> selection_log;

/* Set seed (set_seed) before init*/
bool use_seed = false;
int seed = 0;

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion MahjongPyWrapper/MahjongPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cc45e72

Please sign in to comment.