-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_main.cpp
More file actions
251 lines (232 loc) · 8.66 KB
/
Copy pathcli_main.cpp
File metadata and controls
251 lines (232 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// 命令行版入口。
// - 无参数:进入交互式 REPL。
// - --seed <N>:指定随机种子(可复现同一局)。
// - --exec "命令;命令":非交互执行若干命令后退出。
// - --file <路径>:从脚本文件逐行执行命令后退出(UTF-8,支持 # 注释与空行)。
// - --replay <路径>:加载回放文件并打印最终局面(可与 --exec/--file 继续)。
#include "cli.h"
#include "netcli.h"
#include <QCoreApplication>
#include <QElapsedTimer>
#include <QEventLoop>
#include <QFile>
#include <QStringList>
#include <QTextStream>
#include <QTimer>
#include <iostream>
#include <string>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
static void printUsage()
{
std::cout
<< "用法:ZhanJianCli [选项]\n"
<< " 无参数 进入交互式命令行\n"
<< " --seed <N> 指定随机种子(可复现同一局)\n"
<< " --exec \"命令;命令\" 非交互执行若干命令后退出\n"
<< " --file <路径> 从脚本文件逐行执行命令后退出\n"
<< " --replay <路径> 加载回放文件并打印最终局面\n"
<< " --step 与 --replay 连用:逐步打印每一步后的棋盘\n"
<< " --ai AI 自动对局(双方均 AI)直至分出胜负\n"
<< " --connect 主机:端口 联网对战(连接 ZhanJianServer)\n"
<< " --help / -h 显示本帮助\n";
}
int main(int argc, char* argv[])
{
#ifdef Q_OS_WIN
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#endif
QCoreApplication app(argc, argv);
quint32 seed = 0;
QString connectArg;
bool hasSeed = false;
QStringList execCmds;
QString filePath;
QString replayPath;
bool aiMode = false;
bool stepMode = false;
for (int i = 1; i < argc; i++) {
const QString a = QString::fromLocal8Bit(argv[i]);
if ((a == QLatin1String("--seed") || a == QLatin1String("-s")) && i + 1 < argc) {
hasSeed = true;
seed = QString::fromLocal8Bit(argv[++i]).toUInt();
} else if (a == QLatin1String("--exec") && i + 1 < argc) {
const QStringList parts = QString::fromLocal8Bit(argv[++i]).split(QLatin1Char(';'));
for (const QString& p : parts)
if (!p.trimmed().isEmpty())
execCmds << p.trimmed();
} else if (a == QLatin1String("--file") && i + 1 < argc) {
filePath = QString::fromLocal8Bit(argv[++i]);
} else if (a == QLatin1String("--replay") && i + 1 < argc) {
replayPath = QString::fromLocal8Bit(argv[++i]);
} else if (a == QLatin1String("--ai")) {
aiMode = true;
} else if (a == QLatin1String("--step")) {
stepMode = true;
} else if (a == QLatin1String("--connect") && i + 1 < argc) {
connectArg = QString::fromLocal8Bit(argv[++i]);
} else if (a == QLatin1String("--help") || a == QLatin1String("-h")) {
printUsage();
return 0;
}
}
// 联网对战模式
if (!connectArg.isEmpty()) {
QString host = QStringLiteral("127.0.0.1");
quint16 port = 0;
const int colon = connectArg.lastIndexOf(QLatin1Char(':'));
if (colon > 0) {
host = connectArg.left(colon);
port = connectArg.mid(colon + 1).toUShort();
} else {
port = connectArg.toUShort();
}
if (port == 0) {
std::cout << "用法:--connect 主机:端口\n";
return 1;
}
NetSession ns;
ns.connectTo(host, port);
ns.queue();
auto pump = [](int ms) {
QEventLoop loop;
QTimer::singleShot(ms, &loop, &QEventLoop::quit);
loop.exec();
};
QElapsedTimer timer;
timer.start();
while (!ns.matched() && timer.elapsed() < 8000)
pump(20);
if (!ns.matched()) {
std::cout << "等待匹配超时(需两名玩家)。\n";
return 1;
}
std::cout << ns.renderBoard().toStdString() << std::endl;
std::string line;
while (!ns.gameOver()) {
std::cout << "> " << std::flush;
if (!std::getline(std::cin, line))
break;
const QString cmd = QString::fromStdString(line).trimmed();
if (cmd.isEmpty())
continue;
if (cmd == QLatin1String("quit") || cmd == QLatin1String("q"))
break;
if (cmd == QLatin1String("surrender") || cmd == QLatin1String("gg"))
ns.surrender();
else
ns.sendAction(cmd);
pump(200);
std::cout << ns.renderBoard().toStdString() << std::endl;
}
if (ns.gameOver())
std::cout << "游戏结束:玩家" << (ns.winner() + 1) << " 获胜\n";
return 0;
}
GameModel model;
if (hasSeed)
model.setSeed(seed);
CliSession cli(model);
bool replayed = false;
if (!replayPath.isEmpty()) {
if (stepMode) {
QStringList actions;
if (!model.loadReplayFile(replayPath, actions)) {
std::cout << "回放加载失败:" << replayPath.toStdString() << std::endl;
return 1;
}
int n = 0;
for (const QString& c : actions) {
n++;
model.executeAction(c);
std::cout << "—— 第 " << n << " 步:" << c.toStdString() << " ——" << std::endl;
std::cout << cli.execute(QStringLiteral("board")).toStdString() << std::endl;
}
const int w = model.winner();
if (w != -1)
std::cout << QStringLiteral("玩家%1 获胜").arg(w + 1).toStdString() << std::endl;
else
std::cout << "未分胜负" << std::endl;
return 0;
}
if (!model.loadReplay(replayPath)) {
std::cout << "回放加载失败:" << replayPath.toStdString() << std::endl;
return 1;
}
replayed = true;
}
// 从脚本文件追加命令
if (!filePath.isEmpty()) {
QFile f(filePath);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
std::cout << "无法打开脚本文件:" << filePath.toStdString() << std::endl;
return 1;
}
QTextStream in(&f);
while (!in.atEnd()) {
const QString line = in.readLine().trimmed();
if (!line.isEmpty() && !line.startsWith(QLatin1Char('#')))
execCmds << line;
}
}
// AI 自动对局
if (aiMode) {
if (!replayed)
model.newGame();
int steps = 0;
while (model.winner() == -1 && steps++ < 400)
model.aiPlay();
int w = model.winner();
if (w == -1) {
// 僵局:按剩余总血量判胜负
int hp0 = 0, hp1 = 0;
for (int i = 0; i < model.shipCount(); i++) {
if (model.ship(i).hp <= 0) continue;
if (model.ship(i).owner == 0) hp0 += model.ship(i).hp;
else hp1 += model.ship(i).hp;
}
w = (hp0 >= hp1) ? 0 : 1;
std::cout << "AI 对局僵局(" << steps << " 步),按剩余血量判:玩家" << (w + 1) << " 胜"
<< std::endl;
} else {
std::cout << "AI 对局结束:共 " << steps << " 步,玩家" << (w + 1) << " 获胜"
<< std::endl;
}
std::cout << cli.execute(QStringLiteral("board")).toStdString() << std::endl;
return 0;
}
// 开局画面(回放时展示当前局面,否则开新局)
auto showStart = [&]() {
if (replayed)
std::cout << cli.execute(QStringLiteral("board")).toStdString() << std::endl;
else
std::cout << cli.start().toStdString() << std::endl;
};
// 非交互执行
if (!execCmds.isEmpty()) {
showStart();
for (const QString& cmd : execCmds) {
std::cout << "> " << cmd.toStdString() << std::endl;
const QString out = cli.execute(cmd);
if (!out.isEmpty())
std::cout << out.toStdString() << std::endl;
if (cli.quit())
break;
}
return 0;
}
// 交互模式
showStart();
std::string line;
while (!cli.quit()) {
std::cout << "> " << std::flush;
if (!std::getline(std::cin, line))
break;
const QString out = cli.execute(QString::fromStdString(line));
if (!out.isEmpty())
std::cout << out.toStdString() << std::endl;
}
return 0;
}