-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartgame.cpp
More file actions
371 lines (355 loc) · 10.3 KB
/
Copy pathstartgame.cpp
File metadata and controls
371 lines (355 loc) · 10.3 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <iostream>
#include <stdexcept>
#include <limits>
#include "startgame.h"
#include "utils.h"
#include "chessboard.h"
using namespace std;
Chess_for_deploy::Chess_for_deploy()
: beequeen(1), spider(2), grasshopper(3), beetle(2), ant(3), mosquito(1), ladybug(1), id('A' - 1), queenid(0), player(0),is_ai(false) {}
bool Chess_for_deploy::queendeployed() const
{
return beequeen == 0;
}
char Chess_for_deploy::getid() const
{
return id+1;//返回当前最大的字母+1,便于取消操作。
}
bool Chess_for_deploy::not_lose(const Chessboard &chessboard) const
{
if (beequeen != 0)
{
return true;
}
Point p = chessboard.ID2Pnt({player, queenid});
auto check = enum_nearby(p);
auto allchess = chessboard.get_chess(0);
for (auto it = check.begin(); it != check.end(); ++it)
{
if (allchess.count(*it) == 0)
{
return true;
}
}
SetInfoColor();
chessboard.print();
cout << "PLAYER " << player << " 的蜂王被包围了,输掉了游戏。" << endl;
system("pause");
system("cls");
return false;
}
std::shared_ptr<Chess> Chess_for_deploy::deploy_chess(int step)
{
int x;
SetInfoColor(player);
cout << "Player " << player << "'s Turn" << endl;
cout << "1.beequeen:" << beequeen;
cout << " 2.spider:" << spider;
cout << " 3.grasshopper:" << grasshopper;
cout << " 4.beetle:" << beetle;
cout << " 5.ant:" << ant;
//扩展棋子在此加入
cout << " 6.mosquito:" <<mosquito;
cout << " 7.ladybug:" << ladybug;
//
cout << endl;
cout << "请选择棋子: ";
if(is_ai)
{
x = AIinput(1, 7);
}
else{
x = input(1,7);
}
SetInfoColor();
if (step >= 7 && beequeen != 0)
{
SetInfoColor(player);
if (!is_ai)
{
cout << "你现在必须放置蜂王..." << endl;
system("pause");
}
SetInfoColor();
beequeen--;
id++;
queenid = id;
return move(make_shared<Beequeen>(player, id));
}
switch (x)
{
case 1:
if (beequeen == 0)
return nullptr;
beequeen--;
id++;
queenid = id;
return move(make_shared<Beequeen>(player, id));
case 2:
if (spider == 0)
return nullptr;
spider--;
id++;
return move(make_shared<Spider>(player, id));
case 3:
if (grasshopper == 0)
return nullptr;
grasshopper--;
id++;
return move(make_shared<Grasshopper>(player, id));
case 4:
if (beetle == 0)
return nullptr;
beetle--;
id++;
return move(make_shared<Beetle>(player, id));
case 5:
if (ant == 0)
return nullptr;
ant--;
id++;
return move(make_shared<Ant>(player, id));
case 6:
if (mosquito == 0)
return nullptr;
mosquito--;
id++;
return move(make_shared<Mosquito>(player, id));
case 7:
if (ladybug == 0)
return nullptr;
ladybug--;
id++;
return move(make_shared<Ladybug>(player, id));
default:
return nullptr;
}
}
StartGame* StartGame::self = nullptr;
StartGame& StartGame::GetInstance()
{
if(self==nullptr)
self = new StartGame;
return *self;
}
int StartGame::startgame()
{
cout << "请选择游戏模式: 1. 双人对战 2. 玩家 vs 电脑 (AI)" << endl;
use_ai = input(1, 2);
if (use_ai == 1)
{
use_ai=0; // 双人对战模式
}
else
{
use_ai=1; // AI对战模式
}
unordered_map<char,Point> map4newchess;
c[1].SetAttr(1,false);
c[2].SetAttr(2, use_ai);
SetInfoColor(1);
cout << "第一轮" << endl;
int step = 3;
int current_player = 1;
auto p = c[1].deploy_chess(step);
while (p==nullptr)
{
p = c[1].deploy_chess(step);
}
SetInfoColor();
main_chessboard.add({0, 0, 0}, move(p));
auto temp_chessboard=new Chessboard ;
*temp_chessboard=main_chessboard;
auto s = enum_nearby({0, 0, 0});
char char_id='A';
for (auto it = s.begin(); it != s.end(); it++)
{
temp_chessboard->add(*it, make_shared<Chess>(-1, char_id));
map4newchess.insert({char_id, *it});
char_id++;
}
temp_chessboard->print();
SetInfoColor(2);
cout << "第二轮 玩家2 请选择摆放棋子的位置: ";
delete temp_chessboard;
char enter_char;
if(!use_ai)
{
enter_char = input('A', --char_id);
}
else{
enter_char = AIinput('A', --char_id);
}
p = c[2].deploy_chess(step);
while (p == nullptr)
{
p = c[2].deploy_chess(step);
}
SetInfoColor();
main_chessboard.add(map4newchess.at(enter_char), p);
map4newchess.clear();
main_chessboard.print();
int otherplayer;
Point temp_point;
cid temp_cid;
set<Point> s1, s2, s3;
while (c[1].not_lose(main_chessboard) && c[2].not_lose(main_chessboard)) // 2回合之后开始的循环逻辑
{
//FLAG://用户取消操作
map4newchess.clear();
system("cls");
main_chessboard.print();
cout << "Step: " << step << endl;
SetInfoColor(current_player);
cout << "现在是玩家" << current_player << "的回合" << endl;
cout << "选项:0=弃权:1=摆放:2=移动" << endl;
int x;
if (!use_ai || step % 2 == 1)
{
x = input(0,2);
}
else{
x = AIinput(1, 2);
}
SetInfoColor();
switch (x)
{
case 1:
s3.clear();
s1 = main_chessboard.get_chess(current_player);
s1 = enum_nearby(s1);
if(current_player==1)
{
otherplayer = 2;
}
else{
otherplayer = 1;
}
s2 = main_chessboard.get_chess(otherplayer);
s1 = s1 - s2;
s2 = enum_nearby(s2);
s3 = s1 - s2;
temp_chessboard = new Chessboard;
*temp_chessboard = main_chessboard;
char_id = 'A';
for (auto it = s3.begin(); it != s3.end();++it)
{
temp_chessboard->add(*it, move(make_shared<Chess>(-1, char_id)));
map4newchess.insert({char_id, *it});
char_id++;
}
s3.clear();
system("cls");
temp_chessboard->print();
delete temp_chessboard;
SetInfoColor(current_player);
if (!use_ai || step % 2 == 1)
{
enter_char = input('A', char_id);
}
else
{
enter_char = AIinput('A', static_cast<char>(char_id-1));
}
SetInfoColor();
if (enter_char==char_id)
{
cout << "无效的操作,返回上一步..." << endl;
if(!use_ai&&step%2==0)
system("pause");
continue;
}
p = c[current_player].deploy_chess(step);
if (p == nullptr)
{
cout << "无效的操作,返回上一步..." << endl;
if (!use_ai || step % 2 == 1)
system("pause");
continue;
}
main_chessboard.add(map4newchess.at(enter_char), move(p));
break;
case 2:
SetInfoColor(current_player);
if (!c[current_player].queendeployed())
{
cout << "没有放置蜂王,不允许移动棋子..." << endl;
if (!use_ai || step % 2 == 1)
system("pause");
continue;
}
cout << "请输入移动的棋子的字母:";
if (!use_ai || step % 2 == 1)
{
enter_char = input('A', c[current_player].getid());
}
else
{
enter_char = AIinput('A', static_cast<char>(c[current_player].getid()-1));
}
temp_cid = {current_player, enter_char};
if (main_chessboard.getId2PntMap().count(temp_cid)==0)
{
cout << "无效的操作,返回上一步..." << endl;
if (!use_ai || step % 2 == 1)
system("pause");
continue;
}
temp_point = main_chessboard.ID2Pnt(temp_cid);
s3 = (main_chessboard.getBoard()).at(temp_point)->get_dest(temp_cid,main_chessboard);
if (s3.empty())
{
cout << "棋子没有可走的目标格子,返回上一步..." << endl;
if (!use_ai || step % 2 == 1)
system("pause");
continue;
}
SetInfoColor();
temp_chessboard = new Chessboard;
*temp_chessboard = main_chessboard;
char_id = 'A';
for (auto it = s3.begin(); it != s3.end(); ++it)
{
temp_chessboard->add(*it, move(make_shared<Chess>(-1, char_id)));
map4newchess.insert({char_id, *it});
char_id++;
}
temp_chessboard->print();
delete temp_chessboard;
SetInfoColor(current_player);
if (!use_ai || step % 2 == 1)
{
enter_char = input('A', char_id);
}
else
{
enter_char = AIinput('A', static_cast<char>(char_id-1));
}
SetInfoColor();
if (enter_char == char_id)
{
cout << "无效的操作,返回上一步..." << endl;
if (!use_ai || step % 2 == 1)
system("pause");
continue;
}
main_chessboard.move_chess(temp_cid, map4newchess.at(enter_char));
break;
default:
break;
}
switch_player(current_player);
step++;
}
c[1] = Chess_for_deploy();//结束后重置棋子
c[2] = Chess_for_deploy();
main_chessboard=Chessboard();
return 1;
}
void StartGame::switch_player(int &p)
{
if (p==1)
p = 2;
else
p = 1;
}