refactor: Rename cmdlist to gameRecords

This commit is contained in:
Calcitem 2021-01-01 00:00:23 +08:00
parent 5d8c93fe41
commit 778d366edb
8 changed files with 105 additions and 105 deletions

View File

@ -558,12 +558,12 @@ bool Position::reset()
break;
}
if (sprintf(cmdline, "r%1u s%03u t%02u",
if (sprintf(record, "r%1u s%03u t%02u",
r + 1, rule.maxStepsLedToDraw, 0) > 0) {
return true;
}
cmdline[0] = '\0';
record[0] = '\0';
return false;
}
@ -587,7 +587,7 @@ bool Position::start()
}
}
bool Position::put_piece(Square s, bool updateCmdlist)
bool Position::put_piece(Square s, bool updateRecord)
{
Piece piece = NO_PIECE;
int us = sideToMove;
@ -613,8 +613,8 @@ bool Position::put_piece(Square s, bool updateCmdlist)
update_key(s);
if (updateCmdlist) {
sprintf(cmdline, "(%1u,%1u)", file_of(s), rank_of(s));
if (updateRecord) {
sprintf(record, "(%1u,%1u)", file_of(s), rank_of(s));
}
currentSquare = s;
@ -666,8 +666,8 @@ bool Position::put_piece(Square s, bool updateCmdlist)
}
}
if (updateCmdlist) {
sprintf(cmdline, "(%1u,%1u)->(%1u,%1u)",
if (updateRecord) {
sprintf(record, "(%1u,%1u)->(%1u,%1u)",
file_of(currentSquare), rank_of(currentSquare),
file_of(s), rank_of(s));
st.rule50++;
@ -712,7 +712,7 @@ bool Position::put_piece(Square s, bool updateCmdlist)
return true;
}
bool Position::remove_piece(Square s, bool updateCmdlist)
bool Position::remove_piece(Square s, bool updateRecord)
{
if (phase == Phase::ready || phase == Phase::gameOver)
return false;
@ -757,8 +757,8 @@ bool Position::remove_piece(Square s, bool updateCmdlist)
pc = board[s] = NO_PIECE;
}
if (updateCmdlist) {
sprintf(cmdline, "-(%1u,%1u)", file_of(s), rank_of(s));
if (updateRecord) {
sprintf(record, "-(%1u,%1u)", file_of(s), rank_of(s));
st.rule50 = 0; // TODO: Need to move out?
}
@ -833,7 +833,7 @@ bool Position::resign(Color loser)
set_gameover(~loser, GameOverReason::loseReasonResign);
//sprintf(cmdline, "Player%d give up!", loser);
//sprintf(record, "Player%d give up!", loser);
update_score();
return true;
@ -888,7 +888,7 @@ bool Position::command(const char *cmd)
winner = DRAW;
score_draw++;
gameOverReason = GameOverReason::drawReasonThreefoldRepetition;
//sprintf(cmdline, "Threefold Repetition. Draw!");
//sprintf(record, "Threefold Repetition. Draw!");
return true;
}
#endif /* THREEFOLD_REPETITION */
@ -1312,7 +1312,7 @@ void Position::reset_bb()
}
}
void Position::mirror(vector <string> &cmdlist, bool cmdChange /*= true*/)
void Position::mirror(vector <string> &moveRecords, bool cmdChange /*= true*/)
{
Piece ch;
int f, r;
@ -1377,27 +1377,27 @@ void Position::mirror(vector <string> &cmdlist, bool cmdChange /*= true*/)
unsigned r1, s1, r2, s2;
int args = 0;
args = sscanf(cmdline, "(%1u,%1u)->(%1u,%1u)", &r1, &s1, &r2, &s2);
args = sscanf(record, "(%1u,%1u)->(%1u,%1u)", &r1, &s1, &r2, &s2);
if (args >= 4) {
s1 = (RANK_NB - s1 + 1) % RANK_NB;
s2 = (RANK_NB - s2 + 1) % RANK_NB;
cmdline[3] = '1' + static_cast<char>(s1);
cmdline[10] = '1' + static_cast<char>(s2);
record[3] = '1' + static_cast<char>(s1);
record[10] = '1' + static_cast<char>(s2);
} else {
args = sscanf(cmdline, "-(%1u,%1u)", &r1, &s1);
args = sscanf(record, "-(%1u,%1u)", &r1, &s1);
if (args >= 2) {
s1 = (RANK_NB - s1 + 1) % RANK_NB;
cmdline[4] = '1' + static_cast<char>(s1);
record[4] = '1' + static_cast<char>(s1);
} else {
args = sscanf(cmdline, "(%1u,%1u)", &r1, &s1);
args = sscanf(record, "(%1u,%1u)", &r1, &s1);
if (args >= 2) {
s1 = (RANK_NB - s1 + 1) % RANK_NB;
cmdline[3] = '1' + static_cast<char>(s1);
record[3] = '1' + static_cast<char>(s1);
}
}
}
for (auto &iter : cmdlist) {
for (auto &iter : moveRecords) {
args = sscanf(iter.c_str(), "(%1u,%1u)->(%1u,%1u)", &r1, &s1, &r2, &s2);
if (args >= 4) {
s1 = (RANK_NB - s1 + 1) % RANK_NB;
@ -1421,7 +1421,7 @@ void Position::mirror(vector <string> &cmdlist, bool cmdChange /*= true*/)
}
}
void Position::turn(vector <string> &cmdlist, bool cmdChange /*= true*/)
void Position::turn(vector <string> &gameRecords, bool cmdChange /*= true*/)
{
Piece ch;
int f, r;
@ -1504,7 +1504,7 @@ void Position::turn(vector <string> &cmdlist, bool cmdChange /*= true*/)
unsigned r1, s1, r2, s2;
int args = 0;
args = sscanf(cmdline, "(%1u,%1u)->(%1u,%1u)",
args = sscanf(record, "(%1u,%1u)->(%1u,%1u)",
&r1, &s1, &r2, &s2);
if (args >= 4) {
@ -1518,29 +1518,29 @@ void Position::turn(vector <string> &cmdlist, bool cmdChange /*= true*/)
else if (r2 == FILE_NB)
r2 = 1;
cmdline[1] = '0' + static_cast<char>(r1);
cmdline[8] = '0' + static_cast<char>(r2);
record[1] = '0' + static_cast<char>(r1);
record[8] = '0' + static_cast<char>(r2);
} else {
args = sscanf(cmdline, "-(%1u,%1u)", &r1, &s1);
args = sscanf(record, "-(%1u,%1u)", &r1, &s1);
if (args >= 2) {
if (r1 == 1)
r1 = FILE_NB;
else if (r1 == FILE_NB)
r1 = 1;
cmdline[2] = '0' + static_cast<char>(r1);
record[2] = '0' + static_cast<char>(r1);
} else {
args = sscanf(cmdline, "(%1u,%1u)", &r1, &s1);
args = sscanf(record, "(%1u,%1u)", &r1, &s1);
if (args >= 2) {
if (r1 == 1)
r1 = FILE_NB;
else if (r1 == FILE_NB)
r1 = 1;
cmdline[1] = '0' + static_cast<char>(r1);
record[1] = '0' + static_cast<char>(r1);
}
}
}
for (auto &iter : cmdlist) {
for (auto &iter : gameRecords) {
args = sscanf(iter.c_str(),
"(%1u,%1u)->(%1u,%1u)",
&r1, &s1, &r2, &s2);
@ -1583,7 +1583,7 @@ void Position::turn(vector <string> &cmdlist, bool cmdChange /*= true*/)
}
}
void Position::rotate(vector <string> &cmdlist, int degrees, bool cmdChange /*= true*/)
void Position::rotate(vector <string> &gameRecords, int degrees, bool cmdChange /*= true*/)
{
degrees = degrees % 360;
@ -1685,29 +1685,29 @@ void Position::rotate(vector <string> &cmdlist, int degrees, bool cmdChange /*=
unsigned r1, s1, r2, s2;
int args = 0;
args = sscanf(cmdline, "(%1u,%1u)->(%1u,%1u)", &r1, &s1, &r2, &s2);
args = sscanf(record, "(%1u,%1u)->(%1u,%1u)", &r1, &s1, &r2, &s2);
if (args >= 4) {
s1 = (s1 - 1 + RANK_NB - degrees) % RANK_NB;
s2 = (s2 - 1 + RANK_NB - degrees) % RANK_NB;
cmdline[3] = '1' + static_cast<char>(s1);
cmdline[10] = '1' + static_cast<char>(s2);
record[3] = '1' + static_cast<char>(s1);
record[10] = '1' + static_cast<char>(s2);
} else {
args = sscanf(cmdline, "-(%1u,%1u)", &r1, &s1);
args = sscanf(record, "-(%1u,%1u)", &r1, &s1);
if (args >= 2) {
s1 = (s1 - 1 + RANK_NB - degrees) % RANK_NB;
cmdline[4] = '1' + static_cast<char>(s1);
record[4] = '1' + static_cast<char>(s1);
} else {
args = sscanf(cmdline, "(%1u,%1u)", &r1, &s1);
args = sscanf(record, "(%1u,%1u)", &r1, &s1);
if (args >= 2) {
s1 = (s1 - 1 + RANK_NB - degrees) % RANK_NB;
cmdline[3] = '1' + static_cast<char>(s1);
record[3] = '1' + static_cast<char>(s1);
}
}
}
for (auto &iter : cmdlist) {
for (auto &iter : gameRecords) {
args = sscanf(iter.c_str(), "(%1u,%1u)->(%1u,%1u)", &r1, &s1, &r2, &s2);
if (args >= 4) {

View File

@ -99,7 +99,7 @@ public:
Square current_square() const;
enum Phase get_phase() const;
enum Action get_action() const;
const char *cmd_line() const;
const char *get_record() const;
int get_mobility_diff(bool includeBanned);
@ -116,9 +116,9 @@ public:
Color get_winner() const;
void set_gameover(Color w, GameOverReason reason);
void mirror(std::vector <std::string> &cmdlist, bool cmdChange = true);
void turn(std::vector <std::string> &cmdlist, bool cmdChange = true);
void rotate(std::vector <std::string> &cmdlist, int degrees, bool cmdChange = true);
void mirror(std::vector <std::string> &gameRecords, bool cmdChange = true);
void turn(std::vector <std::string> &gameRecords, bool cmdChange = true);
void rotate(std::vector <std::string> &gameRecords, int degrees, bool cmdChange = true);
void reset_bb();
@ -150,10 +150,10 @@ public:
void put_piece(Piece pc, Square s);
bool put_piece(File file, Rank rank);
bool put_piece(Square s, bool updateCmdlist = false);
bool put_piece(Square s, bool updategameRecords = false);
bool remove_piece(File file, Rank rank);
bool remove_piece(Square s, bool updateCmdlist = false);
bool remove_piece(Square s, bool updategameRecords = false);
bool move_piece(File f1, Rank r1, File f2, Rank r2);
bool move_piece(Square from, Square to);
@ -189,7 +189,7 @@ public:
Square currentSquare;
int gamesPlayedCount { 0 };
char cmdline[64] { '\0' };
char record[64] { '\0' };
/*
0x 00 00 00 00 00 00 00 00
@ -360,9 +360,9 @@ inline enum Action Position::get_action() const
return action;
}
inline const char *Position::cmd_line() const
inline const char *Position::get_record() const
{
return cmdline;
return record;
}
inline int Position::piece_on_board_count(Color c)

View File

@ -160,21 +160,21 @@ void Test::detach()
}
}
void Test::writeToMemory(const QString &cmdline)
void Test::writeToMemory(const QString &record)
{
if (!isTestMode) {
return;
}
if (cmdline == readStr) {
if (record == readStr) {
return;
}
char from[BUFSIZ] = { 0 };
#ifdef _WIN32
strcpy_s(from, cmdline.toStdString().c_str());
strcpy_s(from, record.toStdString().c_str());
#else
strcpy(from, cmdline.toStdString().c_str());
strcpy(from, record.toStdString().c_str());
#endif
while (true) {

View File

@ -556,8 +556,8 @@ string Thread::nextMove()
if (gameOptions.getResignIfMostLose() == true) {
if (root->value <= -VALUE_MATE) {
gameOverReason = loseReasonResign;
//sprintf(cmdline, "Player%d give up!", position->sideToMove);
return cmdline;
//sprintf(record, "Player%d give up!", position->sideToMove);
return record;
}
}

View File

@ -138,7 +138,7 @@ signals:
void emitCommand();
#endif // QT_GUI_LIB
void command(const string &cmdline, bool update = true);
void command(const string &record, bool update = true);
};

View File

@ -1,7 +1,7 @@
name: sanmill
description: A new Flutter project.
version: 0.13.1+1226
version: 0.14.0+1294
environment:
sdk: ">=2.1.0 <3.0.0"

View File

@ -111,7 +111,7 @@ Game::Game(
}
#endif
cmdlist.reserve(256);
gameRecords.reserve(256);
// 安装事件过滤器监视scene的各个事件
// 由于我重载了QGraphicsScene相关事件在重载函数中已设定不必安装监视器。
@ -134,7 +134,7 @@ Game::~Game()
}
#endif /* ENDGAME_LEARNING */
cmdlist.clear();
gameRecords.clear();
}
const map<int, QStringList> Game::getActions()
@ -163,7 +163,7 @@ extern deque<int> openingBookDequeBak;
void Game::gameStart()
{
//cmdlist.clear();
//gameRecords.clear();
position.start();
startTime = time(nullptr);
@ -201,10 +201,10 @@ void Game::gameReset()
// 重置游戏
// WAR
if (cmdlist.size() > 1) {
string bak = cmdlist[0];
cmdlist.clear();
cmdlist.emplace_back(bak);
if (gameRecords.size() > 1) {
string bak = gameRecords[0];
gameRecords.clear();
gameRecords.emplace_back(bak);
}
position.reset();
@ -272,7 +272,7 @@ void Game::gameReset()
// 更新棋谱
manualListModel.removeRows(0, manualListModel.rowCount());
manualListModel.insertRow(0);
manualListModel.setData(manualListModel.index(0), position.cmd_line());
manualListModel.setData(manualListModel.index(0), position.get_record());
currentRow = 0;
// 发出信号通知主窗口更新LCD显示
@ -359,13 +359,13 @@ void Game::setRule(int ruleNo, int stepLimited /*= -1*/, int timeLimited /*= -1*
int r = ruleNo;
elapsedSeconds[BLACK] = elapsedSeconds[WHITE] = 0;
char cmdline[64] = { 0 };
if (sprintf(cmdline, "r%1u s%03u t%02u", r + 1, rule.maxStepsLedToDraw, 0) <= 0) {
char record[64] = { 0 };
if (sprintf(record, "r%1u s%03u t%02u", r + 1, rule.maxStepsLedToDraw, 0) <= 0) {
assert(0);
}
string cmd(cmdline);
cmdlist.clear();
cmdlist.emplace_back(cmd);
string cmd(record);
gameRecords.clear();
gameRecords.emplace_back(cmd);
// 重置游戏
gameReset();
@ -588,12 +588,12 @@ void Game::flip()
#ifndef TRAINING_MODE
stopAndWaitAiThreads();
position.mirror(cmdlist);
position.rotate(cmdlist, 180);
position.mirror(gameRecords);
position.rotate(gameRecords, 180);
// 更新棋谱
int row = 0;
for (const auto &str : *(cmd_list())) {
for (const auto &str : *(game_records())) {
manualListModel.setData(manualListModel.index(row++), str.c_str());
}
@ -614,12 +614,12 @@ void Game::mirror()
#ifndef TRAINING_MODE
stopAndWaitAiThreads();
position.mirror(cmdlist);
position.mirror(gameRecords);
// 更新棋谱
int row = 0;
for (const auto &str : *(cmd_list())) {
for (const auto &str : *(game_records())) {
manualListModel.setData(manualListModel.index(row++), str.c_str());
}
@ -642,12 +642,12 @@ void Game::turnRight()
#ifndef TRAINING_MODE
stopAndWaitAiThreads();
position.rotate(cmdlist, -90);
position.rotate(gameRecords, -90);
// 更新棋谱
int row = 0;
for (const auto &str : *(cmd_list())) {
for (const auto &str : *(game_records())) {
manualListModel.setData(manualListModel.index(row++), str.c_str());
}
@ -668,11 +668,11 @@ void Game::turnLeft()
#ifndef TRAINING_MODE
stopAndWaitAiThreads();
position.rotate(cmdlist, 90);
position.rotate(gameRecords, 90);
// 更新棋谱
int row = 0;
for (const auto &str : *(cmd_list())) {
for (const auto &str : *(game_records())) {
manualListModel.setData(manualListModel.index(row++), str.c_str());
}
@ -820,7 +820,7 @@ bool Game::actionPiece(QPointF pos)
manualListModel.removeRows(currentRow + 1, rowCount - currentRow - 1);
for (int i = 0; i < removeCount; i++) {
cmdlist.pop_back();
gameRecords.pop_back();
}
// 如果再决出胜负后悔棋,则重新启动计时
@ -898,7 +898,7 @@ bool Game::actionPiece(QPointF pos)
}
if (result) {
cmdlist.emplace_back(position.cmdline);
gameRecords.emplace_back(position.record);
// 发信号更新状态栏
updateScence();
@ -910,7 +910,7 @@ bool Game::actionPiece(QPointF pos)
int k = 0;
// 输出命令行
for (const auto & i : *(cmd_list())) {
for (const auto & i : *(game_records())) {
// 跳过已添加的因标准list容器没有下标
if (k++ <= currentRow)
continue;
@ -961,7 +961,7 @@ bool Game::resign()
int k = 0;
// 输出命令行
for (const auto & i : *(cmd_list())) {
for (const auto & i : *(game_records())) {
// 跳过已添加的因标准list容器没有下标
if (k++ <= currentRow)
continue;
@ -1020,7 +1020,7 @@ bool Game::command(const string &cmd, bool update /* = true */)
loggerDebug("Computer: %s\n\n", cmd.c_str());
cmdlist.emplace_back(cmd);
gameRecords.emplace_back(cmd);
if (!position.command(cmd.c_str()))
return false;
@ -1043,23 +1043,23 @@ bool Game::command(const string &cmd, bool update /* = true */)
emit statusBarChanged(message);
// 对于新开局
if (cmd_list()->size() <= 1) {
if (game_records()->size() <= 1) {
manualListModel.removeRows(0, manualListModel.rowCount());
manualListModel.insertRow(0);
manualListModel.setData(manualListModel.index(0), position.cmd_line());
manualListModel.setData(manualListModel.index(0), position.get_record());
currentRow = 0;
}
// 对于当前局
else {
currentRow = manualListModel.rowCount() - 1;
// 跳过已添加行,迭代器不支持+运算符,只能一个个++
auto i = (cmd_list()->begin());
for (int r = 0; i != (cmd_list())->end(); i++) {
auto i = (game_records()->begin());
for (int r = 0; i != (game_records())->end(); i++) {
if (r++ > currentRow)
break;
}
// 将新增的棋谱行插入到ListModel
while (i != cmd_list()->end()) {
while (i != game_records()->end()) {
manualListModel.insertRow(++currentRow);
manualListModel.setData(manualListModel.index(currentRow), (*i++).c_str());
}
@ -1480,45 +1480,45 @@ inline std::string Game::char_to_string(char ch)
}
}
void Game::appendGameOverReasonToCmdlist()
void Game::appendGameOverReasonTogameRecords()
{
if (position.phase != Phase::gameOver) {
return;
}
char cmdline[64] = { 0 };
char record[64] = { 0 };
switch (position.gameOverReason) {
case GameOverReason::loseReasonNoWay:
sprintf(cmdline, "Player%d no way to go. Player%d win!", position.sideToMove, position.winner);
sprintf(record, "Player%d no way to go. Player%d win!", position.sideToMove, position.winner);
break;
case GameOverReason::loseReasonTimeOver:
sprintf(cmdline, "Time over. Player%d win!", position.winner);
sprintf(record, "Time over. Player%d win!", position.winner);
break;
case GameOverReason::drawReasonThreefoldRepetition:
sprintf(cmdline, "Threefold Repetition. Draw!");
sprintf(record, "Threefold Repetition. Draw!");
break;
case GameOverReason::drawReasonRule50:
sprintf(cmdline, "Steps over. In draw!");
sprintf(record, "Steps over. In draw!");
break;
case GameOverReason::loseReasonBoardIsFull:
sprintf(cmdline, "Player2 win!");
sprintf(record, "Player2 win!");
break;
case GameOverReason::drawReasonBoardIsFull:
sprintf(cmdline, "Full. In draw!");
sprintf(record, "Full. In draw!");
break;
case GameOverReason::loseReasonlessThanThree:
sprintf(cmdline, "Player%d win!", position.winner);
sprintf(record, "Player%d win!", position.winner);
break;
case GameOverReason::loseReasonResign:
sprintf(cmdline, "Player%d give up!", ~position.winner);
sprintf(record, "Player%d give up!", ~position.winner);
break;
default:
loggerDebug("No Game Over Reason");
break;
}
loggerDebug("%s\n", cmdline);
cmdlist.emplace_back(cmdline);
loggerDebug("%s\n", record);
gameRecords.emplace_back(record);
}
void Game::setTips()
@ -1558,7 +1558,7 @@ void Game::setTips()
break;
case Phase::gameOver:
appendGameOverReasonToCmdlist();
appendGameOverReasonTogameRecords();
scoreStr = "比分 " + to_string(p.score[BLACK]) + " : " + to_string(p.score[WHITE]) + ", 和棋 " + to_string(p.score_draw);

View File

@ -138,12 +138,12 @@ public:
char color_to_char(Color color);
std::string char_to_string(char ch);
void appendGameOverReasonToCmdlist();
void appendGameOverReasonTogameRecords();
void setTips();
inline const std::vector<std::string> *cmd_list() const
inline const std::vector<std::string> *game_records() const
{
return &cmdlist;
return &gameRecords;
}
time_t get_elapsed_time(int us);
@ -485,7 +485,7 @@ private:
// 提示语
string tips;
std::vector <std::string> cmdlist;
std::vector <std::string> gameRecords;
};
inline time_t Game::start_timeb() const