-
Notifications
You must be signed in to change notification settings - Fork 0
/
90-b2-base.cpp
394 lines (389 loc) · 9.71 KB
/
90-b2-base.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//2151753 彭坤宇 计科
#include<iostream>
#include<iomanip>
#include<queue>
#include<string>
#include <conio.h>
#include"90-b2.h"
#include"cmd_console_tools.h"
using namespace std;
void print(int s[11][11], int line, int row,int choose) {
for (int i = 0; i <= row; i++) {
if (i == 0)
cout << setw(3) << "|";
else
cout << setw(3) << i;
}
cout<<endl << "--+" << setfill('-') << setw(3 * row + 1) << "-" << setfill(' ') << endl;
for (int i = 1; i <= line; i++) {
for (int j = 0; j <= row; j++) {
if (j == 0)
cout << char('A' + i - 1) << " |";
else {
if (s[i][j] != 0) {
if (choose == 1) {
cout << " ";
cct_setcolor(14, s[i][j]);
cout << s[i][j];
cct_setcolor(0);
}
if (choose == 2) {
cout << setw(3) << "*";
}
if (choose == 3) {
if (s[i][j] <0) {
if (s[i][j] == -10)
s[i][j] = 0;
else
s[i][j] = -s[i][j];
cout << " ";
cct_setcolor(14, 3);
cout << s[i][j];
cct_setcolor(0);
}
else
cout << setw(3) << s[i][j];
}
}
else
cout << setw(3) << s[i][j];
}
}
cout << endl;
}
}
//输出图像
void nextthree(int *next) {
srand((unsigned)time(NULL));
for (int i = 0; i < 3; i++) {
next[i] = rand() % 7 + 1;
}
}
//预测剩余三个数字
void start_input(int s[11][11],int line,int row, char t[3]) {
cout << "请以数字+字母的形式[例:c2]输入要移动球的矩阵坐标:";
int x, y;
cct_getxy(x, y);
while (1) {
cin.getline(t, 3, '\n');
atoA(t);
if (t[0] >= 'A' && t[0] < 'A' + line && t[1]>'0' && t[1] <= row+'0') {
if (s[t[0] - 'A' + 1][t[1]-'0'] != 0)
break;
else {
cout << "起始位置为空,请重新输入" << endl;
cct_gotoxy(x, y);
cout << " ";
cct_gotoxy(x, y);
}
}
else {
cout << "输入错误,请重新输入" << endl;
cct_gotoxy(x, y);
cout << " ";
cct_gotoxy(x, y);
}
}
cout << "输入为" << t[0] << "行" << t[1] << "列 " << endl;
}
//起点输入
void end_input(int s[11][11], int line, int row,char t[3])
{
cout << "请以数字+字母的形式[例:c2]输入要移动球的目的坐标:";
int x, y;
cct_getxy(x, y);
while (1) {
cin.getline(t, 3,'\n');
atoA(t);
if (t[0] >= 'A' && t[0] < 'A' + line && t[1]>'0' && t[1] <= row + '0') {
if (s[t[0] - 'A' + 1][t[1]-'0'] == 0)
break;
else {
cout << "目标位置非空,请重新输入" << endl;
cct_gotoxy(x, y);
cout << " ";
cct_gotoxy(x, y);
}
}
else {
cout << "输入错误,请重新输入" << endl;
cct_gotoxy(x, y);
cout << " ";
cct_gotoxy(x, y);
}
}
cout << "输入为" << t[0] << "行" << t[1] << "列 " << endl;
}
//终点输入
int DFS(int start_x,int start_y,int end_x,int end_y,int s1[11][11],int s2[11][11],int line,int row) {
if (start_x - 1 > 0 && s1[start_x - 1][start_y ] == 0) {
s1[start_x - 1][start_y] = -10;
s2[start_x - 1][start_y ] = 1;
if (start_x-1 == end_x && start_y == end_y)
return 1;
else {
int check=DFS(start_x - 1, start_y, end_x, end_y, s1, s2,line,row);
if (!check) {
s2[start_x - 1][start_y] = 0;
s1[start_x - 1][start_y] = 0;
}
else
return 1;
}
}
if (start_x +1<=line && s1[start_x + 1][start_y ] == 0) {
s1[start_x + 1][start_y] = -10;
s2[start_x + 1][start_y ] = 1;
if (start_x+1 == end_x && start_y == end_y)
return 1;
else {
int check = DFS(start_x + 1, start_y, end_x, end_y, s1, s2, line, row);
if (!check) {
s1[start_x + 1][start_y] = 0;
s2[start_x + 1][start_y] = 0;
}
else
return 1;
}
}
if (start_y+1<=row&&s1[start_x ][start_y +1 ] == 0) {
s1[start_x][start_y + 1] = -10;
s2[start_x ][start_y +1] = 1;
if (start_x == end_x && start_y+1 == end_y)
return 1;
else {
int check= DFS(start_x, start_y + 1, end_x, end_y, s1, s2, line, row);
if (!check) {
s2[start_x][start_y + 1] = 0;
s1[start_x][start_y + 1] = 0;
}
else
return 1;
}
}
if (start_y-1>0&&s1[start_x ][start_y - 1] == 0) {
s1[start_x][start_y - 1] = -10;
s2[start_x ][start_y - 1] = 1;
if (start_x == end_x && start_y-1 == end_y)
return 1;
else {
int check=DFS(start_x, start_y - 1, end_x, end_y, s1, s2, line, row);
if (!check) {
s1[start_x][start_y - 1] = 0;
s2[start_x][start_y - 1] = 0;
}
else
return 1;
}
}
return 0;
}
//探究是否有路径
void goback(int s[11][11]) {
for (int i = 0; i < 11; i++) {
for (int j = 1; j < 11; j++)
if (s[i][j] == -10)
s[i][j] = 0;
}
}
//在路径被破坏时进行恢复
int findway(int s1[11][11],char start[3], char end[3],int line,int row,int choose) {
int s2[11][11];
reset(s2, line, row);
s2[start[0] - 'A' + 1][start[1] - '0'] = 1;
int flag = DFS(start[0] - 'A' + 1, start[1] - '0', end[0] - 'A' + 1, end[1] - '0', s1, s2,line,row);
if (flag == 1&&choose==2) {
cout << "查找结果数组" << endl;
print(s2, line, row, 2);
cout << endl;
cout << "移动路线<不同色标记>" << endl;
s1[start[0] - 'A' + 1][start[1] - '0'] = -s1[start[0] - 'A' + 1][start[1] - '0'];
print(s1, line, row, 3);
return 1;
}
if (flag == 1 && (choose == 3||choose==4))
return 1;
else {
if(flag!=4)
cout << "无法找到移动路径" << endl;
return 0;
}
}
//路径相关的输出
int judge(int s[11][11],int line,int row,int move_x,int move_y,int *p) {
int score = 0;
int color = s[move_x][move_y];
int judge11 = 0, judge12 = 0, judge21 = 0, judge22 = 0;
int judge31 = 0, judge32=0,judge41 = 0,judge42=0;
for (int i = 1;; i++) {
if (move_x - i > 0 && move_y - i > 0 && s[move_x - i][move_y - i] == color)
judge11++;
else
break;
}
for (int i = 1;; i++) {
if (move_x + i <=line && move_y + i <= row && s[move_x + i][move_y + i] == color)
judge12++;
else
break;
}
if (judge11 + judge12 > 3) {
score += (judge11 + judge12 ) * (judge11 + judge12 - 1);
for (int i = 1; i <= judge11; i++) {
s[move_x - i][move_y - i] = 0;
}
for (int i = 1; i <= judge12; i++) {
s[move_x + i][move_y + i] = 0;
}
*p -= judge11 + judge12;
}
for (int i = 1;; i++) {
if (move_x - i > 0 && move_y + i <= row && s[move_x - i][move_y + i] == color)
judge21++;
else
break;
}
for (int i = 1;; i++) {
if (move_x + i <= line &&move_y-i>0 && s[move_x + i][move_y - i] == color)
judge22++;
else
break;
}
if (judge21 + judge22 > 3) {
score += (judge21 + judge22) * (judge21 + judge22 - 1);
for (int i = 1; i <= judge21; i++) {
s[move_x - i][move_y + i] = 0;
}
for (int i = 1; i <= judge22; i++) {
s[move_x + i][move_y - i] = 0;
}
*p -= judge21 + judge22;
}
for (int i = 1;; i++) {
if ( move_y + i <= row && s[move_x ][move_y + i] == color)
judge31++;
else
break;
}
for (int i = 1;; i++) {
if (move_y - i > 0 && s[move_x][move_y - i] == color)
judge32++;
else
break;
}
if (judge31 + judge32 > 3) {
score += (judge31 + judge32 - 1) * (judge31 + judge32);
for (int i = 1; i <= judge31; i++) {
s[move_x][move_y + i] = 0;
}
for (int i = 1; i <= judge32; i++) {
s[move_x][move_y - i] = 0;
}
*p -= judge31 + judge32;
}
for (int i = 1;; i++) {
if (move_x - i > 0&& s[move_x - i][move_y] == color)
judge41++;
else
break;
}
for (int i = 1;; i++) {
if (move_x + i <= line &&s[move_x + i][move_y] == color)
judge42++;
else
break;
}
if (judge41 + judge42 > 3) {
score += (judge41 + judge42 - 1) * (judge41 + judge42);
for (int i = 1; i <= judge41; i++) {
s[move_x-i][move_y] = 0;
}
for (int i = 1; i <= judge42; i++) {
s[move_x+i][move_y ] = 0;
}
*p -= judge41 + judge42;
}
if (score > 0) {
s[move_x][move_y] = 0;
(* p)--;
}
else
*p += 3;
return score;
}
//判断是否可以消除,包括直线和斜线。
void addthree(int s[11][11], int next[3],int line,int row) {
srand((unsigned)time(NULL));
for (int i = 0; i <= 2; i++) {
int x = rand() % line + 1;
int y = rand() % row + 1;
if (s[x][y] != 0)
i--;
else
s[x][y] = next[i];
}
}
//三个点加进去
void choice1(int s[11][11], int line, int row) {
cout << "初始数组" << endl;
print(s, line, row,1);
cout << endl;
}
//打印五个点
void choice2(int s[11][11], int line, int row) {
cout << "当前数组" << endl;
print(s, line, row,1);
cout << endl;
int next[3];
//新增的三个点
nextthree(next);
char start[3], end[3];
start_input(s, line, row,start);
end_input(s, line, row,end);
cout << endl;
findway(s,start, end,line,row,2);
cout << endl;
}
//打印60%的点
void choice3(int s[11][11], int line, int row) {
int next[3];
//新增的三个点
int do_nextthree = 1;
int score = 0;
int all = 5;
while (1) {
cout << "当前数组" << endl;
print(s, line, row, 1);
cout << endl;
if(do_nextthree)
nextthree(next);
cout << "下三个彩球的颜色分别是:" << " " << next[0] << " " << next[1] << " " << next[2] << endl;
char start[3], end[3];
start_input(s, line, row, start);
end_input(s, line, row, end);
cout << endl;
int flag = findway(s, start, end, line, row, 3);
goback(s);
if (flag == 1) {
s[end[0] - 'A' + 1][end[1] - '0'] = s[start[0] - 'A' + 1][start[1] - '0'];
s[start[0] - 'A' + 1][start[1] - '0'] = 0;
int add=judge(s,line,row, end[0] - 'A' + 1, end[1] - '0',&all);
score += add;
if (add == 0) {
addthree(s, next, line, row);
do_nextthree = 1;
}
else {
do_nextthree = 0;
}
cout << "移动后的数组<不同色标识>" << endl;
print(s, line, row, 1);
cout << "本次得分:" << add << " " << "总得分:" << score << endl << endl;
}
if (all == line * row) {
cout << "游戏结束,你的得分为:" << score << endl;
break;
}
}
}
//完整版内部数组小游戏