Chess---->简单命令框象棋(人VS人)
简单粗暴,直接先上代码:
ChessBoard.h:
1 #ifndef CHESBOARD_H
2 #include<iostream>
3 #include<string>
4 using namespace std;
5 class Chess
6 {
7 public:
8 virtual string getName() { return name; }
9 //virtual bool judge(int a, int b, int c, int d);
Chess(string name, int color) :name(name), color(color) {}
virtual int judge(int a, int b, int c, int d) { return ; } //1 移动非法 2 可移动到空位子 3 吃子
virtual int getColor() { return color; }
protected:
string name;
int color;
};
class Shuai :public Chess
{
public:
Shuai(string a, int b) :Chess(a, b) {} //a 名字 b 颜色
int judge(int a, int b, int c, int d);
};
class Shi :public Chess
{
public:
Shi(string a, int b) :Chess(a, b) {}
int judge(int a, int b, int c, int d);
};
class Bing :public Chess
{
public:
Bing(string name, int color) :Chess(name, color) {}
int judge(int a, int b, int c, int d);
};
class Xiang :public Chess
{
public:
Xiang(string name, int color) :Chess(name, color) {}
int judge(int a, int b, int c, int d);
};
class Ma :public Chess
{
public:
int judge(int a, int b, int c, int d);
Ma(string name, int color) :Chess(name, color) {}
};
class Pao :public Chess
{
public:
int judge(int a, int b, int c, int d);
Pao(string name, int color) :Chess(name, color) {}
};
class Che :public Chess
{
public:
int judge(int a, int b, int c, int d);
Che(string name, int color) :Chess(name, color) {}
};
class Player
{
public:
Player(int a) :color(a) {}
bool judge(int a, int b);
void inPut();
void moveChess(int a, int b, int c, int d);
void eatChess(int a, int b, int c, int d);
private:
int color;
};
78 #endif // CHESBOARD_H
Chess:
1 #include"ChesBoard.h"
2 #include<Windows.h>
3 #include<vector>
4
5 class ChessBoard {
6 public:
7 vector<vector<Chess *> > v_position;
8 bool judgePositionIsNULL(int a, int b) { //判断棋盘上是否是空位
9 if (v_position[a][b]) {
10 return false;
11 }
12 return true;
13 }
14
15 int judgePositionIsColor(int a, int b) { //有子则返回颜色(1为红色,2为黑色)
16 return v_position[a][b]->getColor();
17 }
18 ChessBoard() {
19 for (int i = ; i < ; i++) {
20 vector<Chess *> v_cell(, NULL);
21 v_position.push_back(v_cell);
22 }
23 }
24 void init_Red();
25 void init_Black();
26 void showBoard();
27 };
28
29 ChessBoard cb;
30
31 //静态数据,红方
32 Shuai shuai_red("帥", );
33 Shi shi_red1("士", );
34 Shi shi_red2("士", );
35 Xiang xiang_red1("相", );
36 Xiang xiang_red2("相", );
37 Ma ma_red1("馬", );
38 Ma ma_red2("馬", );
39 Che che_red1("車", );
40 Che che_red2("車", );
41 Pao pao_red1("炮", );
42 Pao pao_red2("炮", );
43 Bing bing_red1("兵", );
44 Bing bing_red2("兵", );
45 Bing bing_red3("兵", );
46 Bing bing_red4("兵", );
47 Bing bing_red5("兵", );
48
49 //静态数据,黑方
50 Shuai shuai_black("将", );
51 Shi shi_black1("仕", );
52 Shi shi_black2("仕", );
53 Xiang xiang_black1("相", );
54 Xiang xiang_black2("相", );
55 Ma ma_black1("馬", );
56 Ma ma_black2("馬", );
57 Che che_black1("車", );
58 Che che_black2("車", );
59 Pao pao_black1("炮", );
60 Pao pao_black2("炮", );
61 Bing bing_black1("卒", );
62 Bing bing_black2("卒", );
63 Bing bing_black3("卒", );
64 Bing bing_black4("卒", );
65 Bing bing_black5("卒", );
66
67
68 void ChessBoard::init_Red() {
69 v_position[][] = &shuai_red;
70 v_position[][] = &shi_red1;
71 v_position[][] = &shi_red2;
72 v_position[][] = &xiang_red1;
73 v_position[][] = &xiang_red2;
74 v_position[][] = &ma_red1;
75 v_position[][] = &ma_red2;
76 v_position[][] = &che_red1;
77 v_position[][] = &che_red2;
78 v_position[][] = &pao_red1;
79 v_position[][] = &pao_red2;
80 v_position[][] = &bing_red1;
81 v_position[][] = &bing_red2;
82 v_position[][] = &bing_red3;
83 v_position[][] = &bing_red4;
84 v_position[][] = &bing_red5;
85 }
86
87 void ChessBoard::init_Black() {
88 v_position[][] = &shuai_black;
89 v_position[][] = &shi_black1;
90 v_position[][] = &shi_black2;
91 v_position[][] = &xiang_black1;
92 v_position[][] = &xiang_black2;
93 v_position[][] = &ma_black1;
94 v_position[][] = &ma_black2;
95 v_position[][] = &che_black1;
96 v_position[][] = &che_black2;
97 v_position[][] = &pao_black1;
98 v_position[][] = &pao_black2;
99 v_position[][] = &bing_black1;
100 v_position[][] = &bing_black2;
101 v_position[][] = &bing_black3;
102 v_position[][] = &bing_black4;
103 v_position[][] = &bing_black5;
104 }
105
106 void ChessBoard::showBoard() {
107 //用于记录已打印的行数,以便在准确位置打印“楚河、汉界”
108 int flag = ;
109 cout << " ";
110 for (int i = ; i < ; i++) {
111 cout << ' ' << i << " ";
112 }
113 cout << endl;
114 cout << " ------------------------------------" << endl;
115 for (auto row : v_position) {
116 cout << flag << '|' << ' ';
117 flag++;
118 for (auto column : row) {
119 if (column == NULL)
120 cout << " 十 ";
121 else {
122 if (column->getColor() == ) {
123 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
124 }
125 else {
126 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
127 }
128 cout << ' ';
129 cout << (column->getName());
130 cout << ' ';
131 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
132 }
133 }
134 cout << endl << " |" << endl;
135 if (flag == ) {
136 cout << " ------------------------------------" << endl;
137 cout << " 楚河 汉界 " << endl;
138 cout << " ------------------------------------" << endl;
139 cout << endl;
140 }
141 }
142 }
143 int Shuai::judge(int a, int b, int c, int d)
144 {
145 if (getName() == "帥") //判断帅还是将
146 {
147 if ((c <= && c >= ) && (d >= && d <= )) // 帅只能在中心9格移动
148 {
149 if (cb.judgePositionIsNULL(c, d)) //判断在将要移动到的地方是否有子
150 {
151 if ((c == a&&d == b + ) || (c == a&&d == b - ) || (c == a + && d == b) || (c == a - && d == b)) //判断移动是否为一格
152 {
153 return ;
154 }
155 }
156 else
157 {
158 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
159 {
160 return ;
161 }
162 else //若是黑子,返回吃子判断
163 {
164 return ;
165 }
166 }
167 }
168 //此时可能对将(飞将)
169 else if((c >= && c <= ) && (d >= && d <= ) && b == d && cb.v_position[c][d]){
170 if (cb.v_position[c][d]->getName() == "将") {
171 for (int i = a + ; i < c; i++)
172 //中间有子,则不能移动,返回1
173 if (cb.v_position[i][b])
174 return ;
175 return ;
176 }
177 }
178 }
179 if (getName() == "将") //判断帅还是将
180 {
181 if ((c <= && c >= ) && (d >= && d <= )) // 将只能在中心9格移动
182 {
183 if (cb.judgePositionIsNULL(c, d)) //判断在将要移动到的地方是否有己方的子
184 {
185 if ((c == a&&d == b + ) || (c == a&&d == b - ) || (c == a + && d == b) || (c == a - && d == b)) //判断移动是否为一格
186 {
187 return ;
188 }
189 }
190 else
191 {
192 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑
193 {
194 return ;
195 }
196 else //若是红子,返回吃子判断
197 {
198 return ;
199 }
200 }
201 }
202 //此时可能对将(飞将)
203 else if ((c >= && c <= ) && (d >= && d <= ) && b == d && cb.v_position[c][d]) {
204 if (cb.v_position[c][d]->getName() == "帥") {
205 for (int i = a - ; i > c; i--)
206 //中间有子,则不能移动,返回1
207 if (cb.v_position[i][b])
208 return ;
209 return ;
210 }
211 }
212 }
213 return ;
214 }
215
216 int Shi::judge(int a, int b, int c, int d)
217 {
218 if (cb.judgePositionIsColor(a, b) == ) //判断是红子
219 {
220 if ((c <= && c >= ) && (d >= && d <= )) //判断目的地是否在中心9格
221 {
222 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
223 {
224 if ((c == a + && d == b + ) || (c == a + && d == b - ) || (c == a - && d == b + ) || (c == a - && d == b - )) //判断是否符合移动规则
225 {
226 return ;
227 }
228 }
229 else
230 {
231 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
232 {
233 return ;
234 }
235 else //若是黑子,返回吃子判断
236 {
237 return ;
238 }
239 }
240 }
241 }
242 if (cb.judgePositionIsColor(a, b) == ) //判断是黑子
243 {
244 if ((c <= && c >= ) && (d >= && d <= )) //判断目的地是否在中心9格
245 {
246 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
247 {
248 if ((c == a + && d == b + ) || (c == a + && d == b - ) || (c == a - && d == b + ) || (c == a - && d == b - )) //判断是否符合移动规则
249 {
250 return ;
251 }
252 }
253 else
254 {
255 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
256 {
257 return ;
258 }
259 else //若是红子,返回吃子判断
260 {
261 return ;
262 }
263 }
264 }
265 }
266 return ;
267 }
268 int Bing::judge(int a, int b, int c, int d)
269 {
270 if (cb.judgePositionIsColor(a, b) == ) //判断是红子
271 {
272 if (a >= ) //判断是否过河界
273 {
274 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
275 {
276 if ((c == a&&d == b + ) || (c == a&&d == b - ) || (c == a + && d == b))
277 {
278 return ;
279 }
280 }
281 else
282 {
283 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
284 {
285 return ;
286 }
287 else //若是黑子,返回吃子判断
288 {
289 return ;
290 }
291 }
292 }
293 else //未过河界
294 {
295 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
296 {
297 if (c == a + && d == b)
298 {
299 return ;
300 }
301 }
302 else
303 {
304 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
305 {
306 return ;
307 }
308 else //若是黑子,返回吃子判断
309 {
310 return ;
311 }
312 }
313 }
314 }
315 if (cb.judgePositionIsColor(a, b) == ) //判断是黑子
316 {
317 if (a <= ) //判断是否过河界
318 {
319 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
320 {
321 if ((c == a&&d == b + ) || (c == a&&d == b - ) || (c == a - && d == b))
322 {
323 return ;
324 }
325 }
326 else
327 {
328 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
329 {
330 return ;
331 }
332 else //若是红子,返回吃子判断
333 {
334 return ;
335 }
336 }
337 }
338 else //未过河界
339 {
340 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
341 {
342 if (c == a - && d == b)
343 {
344 return ;
345 }
346 }
347 else
348 {
349 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
350 {
351 return ;
352 }
353 else //若是红子,返回吃子判断
354 {
355 return ;
356 }
357 }
358 }
359 }
360 return ;
361 }
362 int Xiang::judge(int a, int b, int c, int d)
363 {
364 if (cb.judgePositionIsColor(a, b) == ) //判断是红子
365 {
366 if (b - >= && a - >= && !cb.judgePositionIsNULL(a - , b - )) //判断是否别象腿左上方 数组越界问题
367 {
368 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
369 {
370 if ((c == a - && d == b + ) || (c == a + && d == b + ) || (c == a + && d == b - )) //判断是否符合移动规则
371 return ;
372 }
373 else
374 {
375 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
376 {
377 return ;
378 }
379 else //若是黑子,返回吃子判断
380 {
381 return ;
382 }
383 }
384 }
385 else if (b + <= && a - >= && !cb.judgePositionIsNULL(a - , b + )) //判断是否别象腿右上方 数组越界问题
386 {
387 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
388 {
389 if ((c == a + && d == b + ) || (c == a + && d == b - ) || (c == a - && d == b - )) //判断是否符合移动规则
390 return ;
391 }
392 else
393 {
394 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
395 {
396 return ;
397 }
398 else //若是黑子,返回吃子判断
399 {
400 return ;
401 }
402 }
403 }
404 else if (a + <= && b - >= && !cb.judgePositionIsNULL(a + , b - )) //判断是否别象腿左下方 数组越界问题
405 {
406 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
407 {
408 if ((c == a - && d == b - ) || (c == a - && d == b + ) || (c == a + && d == b + )) //判断是否符合移动规则
409 return ;
410 }
411 else
412 {
413 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
414 {
415 return ;
416 }
417 else //若是黑子,返回吃子判断
418 {
419 return ;
420 }
421 }
422 }
423 else if (a + <= && b + <= && !cb.judgePositionIsNULL(a + , b + )) //判断是否别象腿右下方 数组越界问题
424 {
425 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
426 {
427 if ((c == a - && d == b + ) || (c == a - && d == b - ) || (c == a + && d - )) //判断是否符合移动规则
428 return ;
429 }
430 else
431 {
432 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
433 {
434 return ;
435 }
436 else //若是黑子,返回吃子判断
437 {
438 return ;
439 }
440 }
441 }
442 else
443 {
444 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
445 {
446 if ((c == a - && d == b + ) || (c == a - && d == b - ) || (c == a + && d == b - ) || (c == a + && d == b + )) //判断是否符合移动规则
447 return ;
448 }
449 else
450 {
451 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
452 {
453 return ;
454 }
455 else //若是黑子,返回吃子判断
456 {
457 return ;
458 }
459 }
460 }
461 }
462 if (cb.judgePositionIsColor(a, b) == ) //判断是黑子
463 {
464 if (b - >= && a - >= && !cb.judgePositionIsNULL(a - , b - )) //判断是否别象腿左上方 数组越界问题
465 {
466 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
467 {
468 if ((c == a - && d == b + ) || (c == a + && d == b + ) || (c == a + && d == b - )) //判断是否符合移动规则
469 return ;
470 }
471 else
472 {
473 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
474 {
475 return ;
476 }
477 else //若是红子,返回吃子判断
478 {
479 return ;
480 }
481 }
482 }
483 else if (b + <= && a - >= && !cb.judgePositionIsNULL(a - , b + )) //判断是否别象腿右上方 数组越界问题
484 {
485 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
486 {
487 if ((c == a + && d == b + ) || (c == a + && d == b - ) || (c == a - && d == b - )) //判断是否符合移动规则
488 return ;
489 }
490 else
491 {
492 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
493 {
494 return ;
495 }
496 else //若是红子,返回吃子判断
497 {
498 return ;
499 }
500 }
501 }
502 else if (a + <= && b - >= && !cb.judgePositionIsNULL(a + , b - )) //判断是否别象腿左下方 数组越界问题
503 {
504 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
505 {
506 if ((c == a - && d == b - ) || (c == a - && d == b + ) || (c == a + && d == b + )) //判断是否符合移动规则
507 return ;
508 }
509 else
510 {
511 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
512 {
513 return ;
514 }
515 else //若是红子,返回吃子判断
516 {
517 return ;
518 }
519 }
520 }
521 else if (a + <= && b + <= && !cb.judgePositionIsNULL(a + , b + )) //判断是否别象腿右下方 数组越界问题
522 {
523 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
524 {
525 if ((c == a - && d == b + ) || (c == a - && d == b - ) || (c == a + && d - )) //判断是否符合移动规则
526 return ;
527 }
528 else
529 {
530 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
531 {
532 return ;
533 }
534 else //若是红子,返回吃子判断
535 {
536 return ;
537 }
538 }
539 }
540 else
541 {
542 if (cb.judgePositionIsNULL(c, d)) //判断目的地是否有子
543 {
544 if ((c == a - && d == b + ) || (c == a - && d == b - ) || (c == a + && d == b - ) || (c == a + && d == b + )) //判断是否符合移动规则
545 return ;
546 }
547 else
548 {
549 if (cb.judgePositionIsColor(c, d) == ) //1红 2黑 判断是哪方子
550 {
551 return ;
552 }
553 else //若是红子,返回吃子判断
554 {
555 return ;
556 }
557 }
558 }
559 }
560 return ;
561 }
562
563 int Che::judge(int a, int b, int c, int d) {
564
565 //判断目标点是否在棋盘界内
566 if ((c >= && c <= ) && (d >= && d <= )) {
567
568 //判断車是否走的是直线,否则不能移动,返回1
569 if (a == c || b == d) {
570
571 if (a == c) { //横向移动
572 //目标点是自身,则不能移动,返回1
573 if (b == d) {
574 return ;
575 }
576 else if (b > d) {
577 for (int i = b - ; i > d; i--)
578 //中间有子,则不能移动,返回1
579 if (cb.v_position[c][i])
580 return ;
581 }
582 else {
583 for (int i = b + ; i < d; i++)
584 //中间有子,则不能移动,返回1
585 if (cb.v_position[c][i])
586 return ;
587 }
588 //目标点是空位置,则可直接移到目标点
589 if (!cb.v_position[c][d]) {
590 return ;
591 }
592 else {
593 //目标位置是己方棋子,则不能移动,返回1
594 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
595 return ;
596 }
597 else {
598 return ;
599 }
600 }
601 }
602 else { //纵向移动
603 //目标点是自身,则不能移动,返回1
604 if (a == c) {
605 return ;
606 }
607 else if (a > c) {
608 for (int i = a - ; i > c; i--)
609 //中间有子,则不能移动,返回1
610 if (cb.v_position[i][d])
611 return ;
612 }
613 else {
614 for (int i = a + ; i < c; i++)
615 //中间有子,则不能移动,返回1
616 if (cb.v_position[i][d])
617 return ;
618 }
619 //目标点是空位置,则可直接移到目标点
620 if (!cb.v_position[c][d]) {
621 return ;
622 }
623 else {
624 //目标位置是己方棋子,则不能移动,返回1
625 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
626 return ;
627 }
628 else {
629 return ;
630 }
631 }
632 }
633 }
634 else {
635 return ;
636 }
637 }
638 else {
639 return ;
640 }
641
642 }
643
644 int Ma::judge(int a, int b, int c, int d) {
645 //判断目标点是否在棋盘界内
646 if ((c >= && c <= ) && (d >= && d <= )) {
647 //判断走的是否是“日”
648 if (((a - c)*(a - c) + (b - d)*(b - d)) != ) {
649 return ;
650 }
651 else {
652 //偏左右
653 if (abs(a - c) == ) {
654 //上
655 if (a > c) {
656 if (b > d && !cb.v_position[a][b - ]/*判断是否别马脚*/) {
657 //目标点是空位置,则可直接移到目标点
658 if (!cb.v_position[c][d]) {
659 return ;
660 }
661 else {
662 //目标位置是己方棋子,则不能移动,返回1
663 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
664 return ;
665 }
666 else {
667 return ;
668 }
669 }
670 }
671 else if (b < d && !cb.v_position[a][b + ]/*判断是否别马脚*/) {
672 //目标点是空位置,则可直接移到目标点
673 if (!cb.v_position[c][d]) {
674 return ;
675 }
676 else {
677 //目标位置是己方棋子,则不能移动,返回1
678 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
679 return ;
680 }
681 else {
682 return ;
683 }
684 }
685 }
686 //别马脚了,不能移动
687 else {
688 return ;
689 }
690 }
691 //下
692 else {
693 if (b > d && !cb.v_position[a][b - ]/*判断是否别马脚*/) {
694 //目标点是空位置,则可直接移到目标点
695 if (!cb.v_position[c][d]) {
696 return ;
697 }
698 else {
699 //目标位置是己方棋子,则不能移动,返回1
700 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
701 return ;
702 }
703 else {
704 return ;
705 }
706 }
707 }
708 else if (b < d && !cb.v_position[a][b + ]/*判断是否别马脚*/) {
709 //目标点是空位置,则可直接移到目标点
710 if (!cb.v_position[c][d]) {
711 return ;
712 }
713 else {
714 //目标位置是己方棋子,则不能移动,返回1
715 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
716 return ;
717 }
718 else {
719 return ;
720 }
721 }
722 }
723 //别马脚了,不能移动
724 else {
725 return ;
726 }
727 }
728 }
729 //偏上下
730 else {
731 //左
732 if (b > d) {
733 if (a > c && !cb.v_position[a - ][b]/*判断是否别马脚*/) {
734 //目标点是空位置,则可直接移到目标点
735 if (!cb.v_position[c][d]) {
736 return ;
737 }
738 else {
739 //目标位置是己方棋子,则不能移动,返回1
740 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
741 return ;
742 }
743 else {
744 return ;
745 }
746 }
747 }
748 else if (a < c && !cb.v_position[a + ][b]/*判断是否别马脚*/) {
749 //目标点是空位置,则可直接移到目标点
750 if (!cb.v_position[c][d]) {
751 return ;
752 }
753 else {
754 //目标位置是己方棋子,则不能移动,返回1
755 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
756 return ;
757 }
758 else {
759 return ;
760 }
761 }
762 }
763 //别马脚了,不能移动
764 else {
765 return ;
766 }
767 }
768 //右
769 else {
770 if (a > c && !cb.v_position[a - ][b]/*判断是否别马脚*/) {
771 //目标点是空位置,则可直接移到目标点
772 if (!cb.v_position[c][d]) {
773 return ;
774 }
775 else {
776 //目标位置是己方棋子,则不能移动,返回1
777 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
778 return ;
779 }
780 else {
781 return ;
782 }
783 }
784 }
785 else if (a < c && !cb.v_position[a + ][b]/*判断是否别马脚*/) {
786 //目标点是空位置,则可直接移到目标点
787 if (!cb.v_position[c][d]) {
788 return ;
789 }
790 else {
791 //目标位置是己方棋子,则不能移动,返回1
792 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
793 return ;
794 }
795 else {
796 return ;
797 }
798 }
799 }
800 //别马脚了,不能移动
801 else {
802 return ;
803 }
804 }
805 }
806 }
807 }
808 else {
809 return ;
810 }
811 }
812
813 int Pao::judge(int a, int b, int c, int d) {
814 //用于标记炮中间是否隔子
815 int flag;
816 //判断目标点是否在棋盘界内
817 if ((c >= && c <= ) && (d >= && d <= )) {
818 //判断炮是否走的是直线,否则不能移动,返回1
819 if (a == c || b == d) {
820 if (a == c) { //横向移动
821 //目标点是自身,则不能移动,返回1
822 if (b == d) {
823 return ;
824 }
825 else if (b > d) {
826 flag = ;
827 for (int i = b - ; i > d; i--) {
828 //中间有一子,flag++
829 if (cb.v_position[c][i])
830 flag++;
831 }
832 switch (flag) {
833 case :
834 //目标点是空位置,则可直接移到目标点
835 if (!cb.v_position[c][d]) {
836 return ;
837 }
838 else {
839 //目标位置不为空,则不能移动
840 return ;
841 }
842 break;
843 case :
844 //目标点是空位置,则炮无法隔一子移动
845 if (!cb.v_position[c][d]) {
846 return ;
847 }
848 else {
849 //目标位置是己方棋子,则不能吃,返回1
850 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
851 return ;
852 }
853 else {
854 return ;
855 }
856 }
857 break;
858 default:
859 return ;
860
861 }
862
863 }
864 else {
865 flag = ;
866 for (int i = b + ; i < d; i++) {
867 //中间有一子,flag++
868 if (cb.v_position[c][i])
869 flag++;
870 }
871 switch (flag) {
872 case :
873 //目标点是空位置,则可直接移到目标点
874 if (!cb.v_position[c][d]) {
875 return ;
876 }
877 else {
878 //目标位置不为空,则不能移动
879 return ;
880 }
881 break;
882 case :
883 //目标点是空位置,则炮无法隔一子移动
884 if (!cb.v_position[c][d]) {
885 return ;
886 }
887 else {
888 //目标位置是己方棋子,则不能吃,返回1
889 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
890 return ;
891 }
892 else {
893 return ;
894 }
895 }
896 break;
897 default:
898 return ;
899
900 }
901 }
902 }
903 else { //纵向移动
904 //目标点是自身,则不能移动,返回1
905 if (a == c) {
906 return ;
907 }
908 else if (a > c) {
909 flag = ;
910 for (int i = a - ; i > c; i--) {
911 //中间有一子,flag++
912 if (cb.v_position[i][d])
913 flag++;
914 }
915 switch (flag) {
916 case :
917 //目标点是空位置,则可直接移到目标点
918 if (!cb.v_position[c][d]) {
919 return ;
920 }
921 else {
922 //目标位置不为空,则不能移动
923 return ;
924 }
925 break;
926 case :
927 //目标点是空位置,则炮无法隔一子移动
928 if (!cb.v_position[c][d]) {
929 return ;
930 }
931 else {
932 //目标位置是己方棋子,则不能吃,返回1
933 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
934 return ;
935 }
936 else {
937 return ;
938 }
939 }
940 break;
941 default:
942 return ;
943
944 }
945
946 }
947 else {
948 flag = ;
949 for (int i = a + ; i < c; i++) {
950 //中间有一子,flag++
951 if (cb.v_position[i][d])
952 flag++;
953 }
954 switch (flag) {
955 case :
956 //目标点是空位置,则可直接移到目标点
957 if (!cb.v_position[c][d]) {
958 return ;
959 }
960 else {
961 //目标位置不为空,则不能移动
962 return ;
963 }
964 break;
965 case :
966 //目标点是空位置,则炮无法隔一子移动
967 if (!cb.v_position[c][d]) {
968 return ;
969 }
970 else {
971 //目标位置是己方棋子,则不能吃,返回1
972 if (cb.v_position[c][d]->getColor() == cb.v_position[a][b]->getColor()) {
973 return ;
974 }
975 else {
976 return ;
977 }
978 }
979 break;
980 default:
981 return ;
982
983 }
984 }
985 }
986 }
987 //炮不走直线,不能移动,返回1
988 else {
989 return ;
990 }
991 }
992 else {
993 return ;
994 }
995 }
996
997 //judgePositionIsNULL
998 bool Player::judge(int a, int b)
999 {
if (!cb.judgePositionIsNULL(a, b) && cb.judgePositionIsColor(a, b) == color)
return true;
else
return false;
}
void Player::moveChess(int a, int b, int c, int d)
{
cb.v_position[c][d] = cb.v_position[a][b];
cb.v_position[a][b] = NULL;
}
void Player::eatChess(int a, int b, int c, int d)
{
if (cb.v_position[c][d]->getName() == "帥" || cb.v_position[c][d]->getName() == "将")
{
cb.v_position[c][d] = cb.v_position[a][b];
cb.v_position[a][b] = NULL;
system("cls");
cb.showBoard();
if (cb.v_position[c][d]->getColor() == )
cout << "红棋方即上方玩家胜利" << endl;
if (cb.v_position[c][d]->getColor() == )
cout << "黑棋方即下方玩家胜利" << endl;
system("pause");
exit();
}
else
{
cb.v_position[c][d] = cb.v_position[a][b];
cb.v_position[a][b] = NULL;
}
}
void Player::inPut()
{
int a, b, c, d;
cout << "输入要移动的子位置坐标 先行数再列数 :";
while (cin >> a >> b) {
//判断选择点是否在棋盘界内
if ((a >= && a <= ) && (b >= && b <= ) && judge(a, b)) {
break;
}
system("cls");
cb.showBoard();
cout << "选择位置不合法,请重新输入:";
}
cout << "输入目标位置:";
while (cin >> c >> d) {
//判断选择点是否在棋盘界内
if ((a >= && a <= ) && (b >= && b <= ) && cb.v_position[a][b]->judge(a, b, c, d) != ) {
break;
}
system("cls");
cb.showBoard();
cout << "目标位置不合法 无法移动,请重新输入:";
}
/*if (cb.v_position[a][b]->judge(a, b, c, d) == 1)
{
system("cls");
cb.showBoard();
cout << "目标位置不合法 无法移动" << endl;
inPut();
}*/
if (cb.v_position[a][b]->judge(a, b, c, d) == )
{
moveChess(a, b, c, d);
}
else //(cb.v_position[a][b]->judge(a, b, c, d) == 3)
{
eatChess(a, b, c, d);
}
}
int main()
{
Player player1();
Player player2();
cb.init_Red();
cb.init_Black();
cout << cb.v_position[][]->judge(, , , );
while ()
{
system("cls");
cb.showBoard();
cout << "上方玩家";
player1.inPut();
system("cls");
cb.showBoard();
cout << "下方玩家";
player2.inPut();
}
return ;
1091 }




Chess---->简单命令框象棋(人VS人)的更多相关文章
- 从零单排Linux – 1 – 简单命令
从零单排Linux – 1 – 简单命令 Posted in: Linux 从零单排Linux – 1 一.Linux的简单命令: 1.忘记root密码: 读秒时按任意键进入 – e – ↓选择第二个 ...
- GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载
GO实现简单(命令行)工具:sftp,文檔压解,RDS备份,RDS备份下载 轉載請註明出處:https://www.cnblogs.com/funnyzpc/p/11721978.html 内容提要: ...
- 五大Linux简单命令解决系统性能问题
五大Linux简单命令解决系统性能问题 2010-12-17 10:07 James Turnbull TechTarget中国 字号:T | T 管理Linux主机的性能看起来经常象是在变魔术一样. ...
- 【DDD】业务建模实践 —— 人关注人
社区业务领域中,存在‘人关注人’的场景,在这个场景中,关系较为复杂,且均表现在‘人’同一个业务实体上,因此,这个case的建模过程值得思考.本文将就‘人关注人’这个业务case的领域建模进行探讨,欢迎 ...
- Apache 的搭建及vim的简单命令
一. vim 简单命令 pwd 当前路径 ls 当前路径所有目录 cd 目录地址 跳转到指定目录 /xxx 查找xxx x 删除当前字符 n 执行上一次查找 二.为什么使用apa ...
- Kafka学习(一)配置及简单命令使用
一. Kafka中的相关概念的介绍 Kafka是一个scala实现的分布式消息中间件,当中涉及到的相关概念例如以下: Kafka中传递的内容称为message(消息),message 是通过topic ...
- Kafka配置及简单命令使用
一. Kafka中的相关概念的介绍 Kafka是一个scala实现的分布式消息中间件,其中涉及到的相关概念如下: Kafka中传递的内容称为message(消息),message 是通过topic(话 ...
- Linux的简单命令
Linux的简单命令 1.更改linux服务器的登录密码 成功登录后输入命令: passwd 然后按照提示操作即可 2.在当前路径下新建文件夹:mkdir 新建文件夹名 3.解压和压缩文件tar.gz ...
- C++ 实现简单命令行学生管理系统
C++ 实现简单命令行学生管理系统 预览: 编译环境是macOS.system("clear") 在windows下请换成 system("cls") #inc ...
随机推荐
- 低压差稳压器AMS1585
(1)高效线性稳压. (2)输出高达4.6A,最高输入电压15V,推荐最低压差1.5V(最低1.35V),最大压差12V. (3)两种封装:TO220(直插式),TO230(贴片). 典型电路如下图所 ...
- 如何理解这段代码:void (*signal (int sinno,void(*func)(int)))(int)
void (*signal (int sinno,void(*func)(int)))(int) 先来看void(*func)(int) 这里的意思是声明一个函数指针func,它的参数类型为int ...
- poj 2425 A Chess Game(SG函数)
A Chess Game Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 3551 Accepted: 1440 Desc ...
- typedef用法小结
typedef用法小结- - 注意:本文转自网络,版权归原作者所有. typedef typedef用法小结- - 这两天在看程序的时候,发现很多地方都用到typedef,在结构体定义,还有一些数组等 ...
- 600字读懂 Git
译注:来自 Hacker School 的 Mary Rose Cook 实现了一个纯 JavaScript 写就的 Git:Gitlet,包含了最主要的一些命令.这个项目一是为了了解 Git 内部原 ...
- TCP连接建立的三次握手过程可以携带数据吗?
前几天实验室的群里扔出了这样一个问题:TCP连接建立的三次握手过程可以携带数据吗?突然发现自己还真不清楚这个问题,平日里用tcpdump或者Wireshark抓包时,从来没留意过第三次握手的ACK包有 ...
- C语言学习_C如何在一个文件里调用另一个源文件中的函数
问题 C如何在一个文件里调用另一个源文件中的函数,如题. 解决办法 当程序大了代码多了之后,想模块化开发,不同文件中存一点,是很好的解决办法,那我们如何做才能让各个文件中的代码协同工作呢?我们知道,m ...
- zy 送画
问题描述 话说在军训的倒数第二天,zy终于下定决心要将画了 10天之久的画像送给他心怡的法学院mm.但是,他不敢自己一个人去,倒霉的 kk 只能和他一起去了.不过,为了表现的有诚意,kk和zy不能走在 ...
- 【Android - MD】之RecyclerView的使用
RecyclerView是Android 5.0新特性--Material Design中的一个控件,它将ListView.GridView整合到一起,可以使用极少的代码在ListView.GridV ...
- Delphi Format中的换行符号是什么
Delphi Format中的换行符号是什么 #,s1]); s3#'%s',[s,s1]); ShowMessage(s2); ShowMessage(s3); end; #13#10两边 ...