-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
299 lines (285 loc) · 11 KB
/
main.cpp
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
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include "Qiju.h"
#include "Qipan.h"
#include "Tic.h"
using namespace std;
int main()
{
srand((int)time(0));
Tic::TTTS array[3][3] = {Tic::TTTS_Default};
int index_x, index_y, indexx, indexy;
int counter = 0;
string choice;
Qipan::Print(array);
cout << "请选择模式:" << endl
<< "1.人机模式" << endl
<< "2.挑战自己模式" << endl;
cin >> choice;
LOOP:
if (choice == "2") //人人模式
{
cout << "请输入坐标(行 列):";
while (cin >> indexx >> indexy)
{
index_x = indexx - 1;
index_y = indexy - 1;
if (!((index_x >= 0 && index_x < 3) && (index_y >= 0 && index_y < 3)) || (array[index_x][index_y] != Tic::TTTS_Default))
{
cout << "下错了,再输入一次吧" << endl
<< "请输入坐标(行 列):";
continue;
}
array[index_x][index_y] = counter % 2 ? Tic::TTTS_O : Tic::TTTS_X;
Qipan::Print(array);
cout << "请输入坐标(行 列):";
if (Tic::TTTS_O == Qiju::CheckWin(array))
{
cout << "游戏结束,O获胜" << endl;
break;
}
else if (Tic::TTTS_X == Qiju::CheckWin(array))
{
cout << "游戏结束,X获胜" << endl;
break;
}
counter++;
if (counter >= 9)
{
cout << "游戏结束,平局" << endl;
break;
}
}
}
else if (choice == "1") //人机模式
{
cout << "请选择难度:" << endl
<< "1.简单模式" << endl
<< "2.难一点点模式" << endl;
a:
cin >> choice;
if (choice == "1") //简单模式
{
cout << "请输入坐标(行 列,玩家为O):";
HAHA:
while (cin >> indexx >> indexy)
{
index_x = indexx - 1;
index_y = indexy - 1;
if (!((index_x >= 0 && index_x < 3) && (index_y >= 0 && index_y < 3)) || (array[index_x][index_y] != Tic::TTTS_Default))
{
cout << "下错了,再输入一次吧";
Qipan::Print(array);
cout << endl
<< "请输入坐标(行 列):";
continue;
}
else
counter += 2;
array[index_x][index_y] = Tic::TTTS_O;
if (Tic::TTTS_O == Qiju::CheckWin(array))
{
cout << "游戏结束,O获胜" << endl;
Qipan::Print(array);
break;
}
else if (counter >= 9)
{
cout << "游戏结束,平局" << endl;
Qipan::Print(array);
break;
}
Qipan::Print(array);
cout << "电脑乱下ing.." << endl;
wuwu:
indexx = rand() % 3;
indexy = rand() % 3;
if (array[indexx][indexy] == Tic::TTTS_Default)
array[indexx][indexy] = Tic::TTTS_X;
else
goto wuwu;
Qipan::Print(array);
cout << "请输入坐标(行 列):";
if (Tic::TTTS_O == Qiju::CheckWin(array))
{
cout << "游戏结束,O获胜" << endl;
break;
}
else if (Tic::TTTS_X == Qiju::CheckWin(array))
{
cout << "游戏结束,X获胜" << endl;
break;
}
goto HAHA;
}
}
else if (choice == "2") //困难模式
{
Qipan::Print(array);
cout << "请输入坐标(行 列,玩家为O):";
LALA:
while (cin >> indexx >> indexy)
{
index_x = indexx - 1;
index_y = indexy - 1;
if (!((index_x >= 0 && index_x < 3) && (index_y >= 0 && index_y < 3)) || (array[index_x][index_y] != Tic::TTTS_Default))
{
cout << "下错了,再输入一次吧" << endl;
Qipan::Print(array);
cout << "请输入坐标(行 列):";
continue;
}
else
counter += 2;
array[index_x][index_y] = Tic::TTTS_O;
if (Tic::TTTS_O == Qiju::CheckWin(array)) //判断
{
Qipan::Print(array);
cout << "游戏结束,O获胜" << endl;
break;
}
else if (Tic::TTTS_X == Qiju::CheckWin(array)) //判断
{
Qipan::Print(array);
cout << "游戏结束,X获胜" << endl;
break;
}
else if (counter >= 9) //判断
{
Qipan::Print(array);
cout << "游戏结束,平局" << endl;
break;
}
Qipan::Print(array);
cout << "电脑乱下ing.." << endl;
if (!(counter == 2))
{
for (int i = 0; i < 3; i++) //前18种情况
{
if (array[i][0] == Tic::TTTS_O && array[i][1] == Tic::TTTS_O && array[i][2] == Tic::TTTS_Default)
{
array[i][2] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[i][1] == Tic::TTTS_O && array[i][2] == Tic::TTTS_O && array[i][2] == Tic::TTTS_Default)
{
array[i][0] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[i][0] == Tic::TTTS_O && array[i][2] == Tic::TTTS_O && array[i][2] == Tic::TTTS_Default)
{
array[i][2] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[0][i] == Tic::TTTS_O && array[1][i] == Tic::TTTS_O && array[2][i] == Tic::TTTS_Default)
{
array[2][i] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[1][i] == Tic::TTTS_O && array[2][i] == Tic::TTTS_O && array[0][i] == Tic::TTTS_Default)
{
array[0][i] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[0][i] == Tic::TTTS_O && array[2][i] == Tic::TTTS_O && array[1][i] == Tic::TTTS_Default)
{
array[1][i] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
if (array[0][0] == Tic::TTTS_O && array[1][1] == Tic::TTTS_O && array[2][2] == Tic::TTTS_Default)
{
array[2][2] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[0][0] == Tic::TTTS_O && array[2][2] == Tic::TTTS_O && array[1][1] == Tic::TTTS_Default)
{
array[2][0] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[1][1] == Tic::TTTS_O && array[2][2] == Tic::TTTS_O && array[0][0] == Tic::TTTS_Default)
{
array[0][0] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[0][2] == Tic::TTTS_O && array[1][1] == Tic::TTTS_O && array[2][0] == Tic::TTTS_Default)
{
array[2][0] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[0][2] == Tic::TTTS_O && array[2][0] == Tic::TTTS_O && array[1][1] == Tic::TTTS_Default)
{
array[2][0] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else if (array[1][1] == Tic::TTTS_O && array[2][0] == Tic::TTTS_O && array[0][2] == Tic::TTTS_Default)
{
array[0][2] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else
{
qwe:
indexx = rand() % 3;
indexy = rand() % 3;
if (array[indexx][indexy] == Tic::TTTS_Default)
{
array[indexx][indexy] = Tic::TTTS_X;
Qipan::Print(array);
break;
}
else
goto qwe;
}
}
if (Tic::TTTS_X == Qiju::CheckWin(array))
{
cout << "游戏结束,X获胜" << endl;
break;
}
cout << "请输入坐标(行 列):";
}
else
{
ahha:
indexx = rand() % 3;
indexy = rand() % 3;
if (array[indexx][indexy] == Tic::TTTS_Default)
array[indexx][indexy] = Tic::TTTS_X;
else
goto ahha;
Qipan::Print(array);
if (Tic::TTTS_X == Qiju::CheckWin(array))
{
cout << "游戏结束,X获胜" << endl;
break;
}
cout << "请输入坐标(行 列):";
}
}
}
else
{
cout << "输错了,再输一次八:";
goto a;
}
}
else
{
cout << "输错了,再输一次八:";
cin >> choice;
goto LOOP;
}
}