-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
182 lines (160 loc) · 7.1 KB
/
main.cpp
File metadata and controls
182 lines (160 loc) · 7.1 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
#include <iostream>
#include <memory>
#include <exception>
#include <string>
#include <cstring>
#include <csignal>
#include <atomic>
#include <unistd.h>
#include <libgen.h>
#include <mach-o/dyld.h>
#include <climits>
#include "include/Model.h"
#include "include/Controller.h"
#include "include/View.h"
#include "include/GameTypes.h"
// 全局标志:用于处理Ctrl+C中断
std::atomic<bool> g_interrupted(false);
std::shared_ptr<GameController> g_controller = nullptr;
// 信号处理函数
void signalHandler(int signum) {
if (signum == SIGINT) {
std::cout << "\n\n️ Received interrupt signal (Ctrl+C)" << std::endl;
std::cout << "Saving training data gracefully..." << std::endl;
g_interrupted = true;
// 停止游戏控制器,触发保存
if (g_controller) {
g_controller->stop();
}
}
}
// 解析命令行参数
struct GameConfig {
GameMode mode = GameMode::HUMAN_VS_AI;
PlayerType team0 = PlayerType::HUMAN;
PlayerType team1 = PlayerType::AI_RULE_BASED;
};
GameConfig parseArgs(int argc, char* argv[]) {
GameConfig config;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--mode" && i + 1 < argc) {
std::string mode = argv[++i];
if (mode == "training") config.mode = GameMode::TRAINING;
else if (mode == "ai_vs_ai") config.mode = GameMode::AI_VS_AI;
else if (mode == "human_vs_ai") config.mode = GameMode::HUMAN_VS_AI;
}
else if (arg == "--team0" && i + 1 < argc) {
std::string type = argv[++i];
if (type == "human") config.team0 = PlayerType::HUMAN;
else if (type == "ai_python") config.team0 = PlayerType::AI_PYTHON;
else if (type == "ai_rule") config.team0 = PlayerType::AI_RULE_BASED;
}
else if (arg == "--team1" && i + 1 < argc) {
std::string type = argv[++i];
if (type == "human") config.team1 = PlayerType::HUMAN;
else if (type == "ai_python") config.team1 = PlayerType::AI_PYTHON;
else if (type == "ai_rule") config.team1 = PlayerType::AI_RULE_BASED;
}
else if (arg == "--help" || arg == "-h") {
std::cout << "Usage: " << argv[0] << " [OPTIONS]\n\n";
std::cout << "Options:\n";
std::cout << " --mode <mode> Game mode: training, ai_vs_ai, human_vs_ai (default)\n";
std::cout << " --team0 <type> Team 0 type: human, ai_python, ai_rule (default: human)\n";
std::cout << " --team1 <type> Team 1 type: human, ai_python, ai_rule (default: ai_rule)\n";
std::cout << " --help, -h Show this help message\n\n";
std::cout << "Examples:\n";
std::cout << " " << argv[0] << " --mode ai_vs_ai --team0 ai_python --team1 ai_rule\n";
std::cout << " " << argv[0] << " --mode training --team0 ai_python --team1 ai_rule\n";
exit(0);
}
}
return config;
}
int main(int argc, char* argv[]) {
try {
// 切换到项目根目录(可执行文件的上一级目录)
// 这样相对路径 python/infer.py 就能正确找到
char exePath[PATH_MAX];
uint32_t size = sizeof(exePath);
if (_NSGetExecutablePath(exePath, &size) == 0) {
char* dirPath = dirname(exePath); // 获取目录部分
char* parentPath = dirname(dirPath); // 再上一级
if (chdir(parentPath) == 0) {
std::cout << "Working directory: " << parentPath << std::endl;
}
}
// 注册信号处理函数
std::signal(SIGINT, signalHandler);
// 解析命令行参数
GameConfig config = parseArgs(argc, argv);
std::cout << "Strategy Game Starting..." << std::endl;
std::cout << "Mode: " << gameModeToString(config.mode) << std::endl;
std::cout << "Team 0: " << playerTypeToString(config.team0) << std::endl;
std::cout << "Team 1: " << playerTypeToString(config.team1) << std::endl;
// 创建MVC组件
std::cout << "Creating Model..." << std::endl;
auto model = std::make_shared<GameModel>();
std::cout << "Creating Controller..." << std::endl;
auto controller = std::make_shared<GameController>(model, config.mode, config.team0, config.team1);
g_controller = controller; // 保存到全局变量,供信号处理使用
// 只在非训练模式下创建View
std::shared_ptr<GameView> view = nullptr;
if (config.mode != GameMode::TRAINING) {
std::cout << "Creating View..." << std::endl;
view = std::make_shared<GameView>(model, controller);
}
// 启动游戏控制器
std::cout << "Starting Game Controller..." << std::endl;
controller->start();
std::cout << "Game Controller Started!" << std::endl;
// 主循环
std::cout << "Entering main loop..." << std::endl;
if (view) {
// 有渲染的模式
while (view->isOpen() && !model->isGameOver()) {
view->handleEvents();
view->render();
}
// 游戏结束后继续显示结果
std::cout << "Game ended, showing results..." << std::endl;
while (view->isOpen() && model->isGameOver()) {
view->handleEvents();
view->render();
}
} else {
// 训练模式:无渲染,等待游戏结束(不sleep,让Controller全速运行)
int lastReportedTurn = 0;
while (!model->isGameOver() && !g_interrupted) {
int currentTurn = controller->getCurrentTurn();
// 每1000回合报告一次进度
if (currentTurn > 0 && currentTurn % 1000 == 0 && currentTurn != lastReportedTurn) {
std::cout << "Turn " << currentTurn << " - Game still running..." << std::endl;
lastReportedTurn = currentTurn;
}
// 短暂让出CPU,避免100%占用
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
// 如果是被中断,显示提示
if (g_interrupted) {
std::cout << "Training interrupted by user. Data has been saved." << std::endl;
}
}
// 停止游戏控制器
std::cout << "Stopping Controller..." << std::endl;
controller->stop();
g_controller = nullptr; // 清除全局引用
std::cout << "Game Over!" << std::endl;
if (model->isGameOver()) {
std::string winner = (model->getWinner() == Team::TEAM_A) ? "Team A" : "Team B";
std::cout << winner << " Wins!" << std::endl;
}
return 0;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
} catch (...) {
std::cerr << "Unknown error occurred!" << std::endl;
return 1;
}
}