五子棋
作者:互联网
判断的方法和设计思路都挺难的,借鉴了网上一个五子棋的思路,改了他的一些小框架和小错误,总体思路还是很值得学习的。利于对数组的掌握和全局的掌控。
还存在的一些小问题:边缘五个数判断时的数组越界问题,虽然正常玩的话应该不会遇到,但这个也是个小Bug,有兴趣的同学自己改,源码附上。
#include<iostream> #include<conio.h> using namespace std; int qiPan[19][19] = { 0 }; //声明一个19行19列的数组来存储棋盘 int qiPanTwo[19][19] = { 0 }; //声明一个19行19列的数组来存储棋盘 int X = 9; //2.声明并初始化光标的横纵坐标 int Y = 9; int player = 1;//设定玩家并进行初始化赋值 void Show() { for (int h = 0; h < 19; h++) //打印 { for (int l = 0; l < 19; l++) { if (qiPan[h][l] == 8) //由于光标所在图层应该在棋子图层之上,所以优先考虑。 { cout << "╋" << " "; } else if (qiPanTwo[h][l] == 0) { cout << "·" << " "; } else if (qiPanTwo[h][l] == 1) { cout << "●" << " "; } else if (qiPanTwo[h][l] == 2) { cout << "○" << " "; } } cout << endl; cout << endl; } } void CSH() { qiPan[9][9] = 8; //更改棋盘中光标所在元素的值为8 } void Judge(int X, int Y) { int shu = 1, heng = 1, pie = 1, na = 1;//横竖撇捺计数器,累计到5则表示某方向出现五个相同的旗子 for (int i = 1; i <= 4; i++) //上 { if (qiPanTwo[Y + i][X] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) shu++; else break; } for (int i = 1; i <= 4; i++) //下 { if (qiPanTwo[Y - i][X] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) shu++; else break; } for (int i = 1; i <= 4; i++) //左 { if (qiPanTwo[Y][X - i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) heng++; else break; } for (int i = 1; i <= 4; i++) //右 { if (qiPanTwo[Y][X + i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) heng++; else break; } for (int i = 1; i <= 4; i++) //左下 { if (qiPanTwo[Y + i][X - i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) pie++; else break; } for (int i = 1; i <= 4; i++) //右上 { if (qiPanTwo[Y - i][X + i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) pie++; else break; } for (int i = 1; i <= 4; i++) //右下 { if (qiPanTwo[Y + i][X + i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) na++; else break; } for (int i = 1; i <= 4; i++) //左上 { if (qiPanTwo[Y - i][X - i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) na++; else break; } if (shu == 5 || heng == 5 || pie == 5 || na == 5) { if (qiPanTwo[Y][X] == 1) { cout << "白子获胜!"; system("pause"); } else { cout << "黑子获胜!"; system("pause"); } } shu = 1, heng = 1, pie = 1, na = 1; } int main() { for (int h = 0; h < 19; h++) //对数组进行遍历,所有元素进行初始化(赋值),默认为0值 { for (int l = 0; l < 19; l++) { qiPan[h][l] = 0; //对当前元素进行初始化(赋值),默认为0值 } } for (int h = 0; h < 19; h++) //对数组进行遍历,所有元素进行初始化(赋值),默认为0值 { for (int l = 0; l < 19; l++) { qiPanTwo[h][l] = 0; //对当前元素进行初始化(赋值),默认为0值 } } CSH(); Show(); char xx ; //控制台从键盘获得一个字符的函数(方法) while (xx = _getch()) { switch (xx) //控制 { case 'w': //上 Y = Y - 1; //使光标纵坐标-1 qiPan[Y][X] = 8; //将光标写入棋盘 qiPan[Y + 1][X] = 0; //使原本位置的值还原 system("cls"); Show(); break; case 's': //下 Y = Y + 1; qiPan[Y][X] = 8; qiPan[Y - 1][X] = 0; system("cls"); Show(); break; case 'a': //左 X = X - 1; qiPan[Y][X] = 8; qiPan[Y][X + 1] = 0; system("cls"); Show(); break; case 'd': //右 X = X + 1; qiPan[Y][X] = 8; qiPan[Y][X - 1] = 0; system("cls"); Show(); break; case 'j': //落子 if (player == 1) //如果当前玩家为白色方时 { qiPanTwo[Y][X] = 1; //通过棋盘1中的光标位置更改棋盘2 //中对应元素的值为1,也就是白棋落子 player = 2; //白棋落子后更换玩家 system("cls"); Show(); for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { Judge(i, j); } } } else if (player == 2) { qiPanTwo[Y][X] = 2; player = 1; system("cls"); Show(); for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { Judge(i, j); } } } break; } } }
还存在的一些小问题:边缘五个数判断时的数组越界问题,虽然正常玩的话应该不会遇到,但这个也是个小Bug,有兴趣的同学自己改,源码附上。
#include<iostream> #include<conio.h> using namespace std; int qiPan[19][19] = { 0 }; //声明一个19行19列的数组来存储棋盘 int qiPanTwo[19][19] = { 0 }; //声明一个19行19列的数组来存储棋盘 int X = 9; //2.声明并初始化光标的横纵坐标 int Y = 9; int player = 1;//设定玩家并进行初始化赋值 void Show() { for (int h = 0; h < 19; h++) //打印 { for (int l = 0; l < 19; l++) { if (qiPan[h][l] == 8) //由于光标所在图层应该在棋子图层之上,所以优先考虑。 { cout << "╋" << " "; } else if (qiPanTwo[h][l] == 0) { cout << "·" << " "; } else if (qiPanTwo[h][l] == 1) { cout << "●" << " "; } else if (qiPanTwo[h][l] == 2) { cout << "○" << " "; } } cout << endl; cout << endl; } } void CSH() { qiPan[9][9] = 8; //更改棋盘中光标所在元素的值为8 } void Judge(int X, int Y) { int shu = 1, heng = 1, pie = 1, na = 1;//横竖撇捺计数器,累计到5则表示某方向出现五个相同的旗子 for (int i = 1; i <= 4; i++) //上 { if (qiPanTwo[Y + i][X] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) shu++; else break; } for (int i = 1; i <= 4; i++) //下 { if (qiPanTwo[Y - i][X] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) shu++; else break; } for (int i = 1; i <= 4; i++) //左 { if (qiPanTwo[Y][X - i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) heng++; else break; } for (int i = 1; i <= 4; i++) //右 { if (qiPanTwo[Y][X + i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) heng++; else break; } for (int i = 1; i <= 4; i++) //左下 { if (qiPanTwo[Y + i][X - i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) pie++; else break; } for (int i = 1; i <= 4; i++) //右上 { if (qiPanTwo[Y - i][X + i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) pie++; else break; } for (int i = 1; i <= 4; i++) //右下 { if (qiPanTwo[Y + i][X + i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) na++; else break; } for (int i = 1; i <= 4; i++) //左上 { if (qiPanTwo[Y - i][X - i] == qiPanTwo[Y][X] && qiPanTwo[Y][X] != 0) na++; else break; } if (shu == 5 || heng == 5 || pie == 5 || na == 5) { if (qiPanTwo[Y][X] == 1) { cout << "白子获胜!"; system("pause"); } else { cout << "黑子获胜!"; system("pause"); } } shu = 1, heng = 1, pie = 1, na = 1; } int main() { for (int h = 0; h < 19; h++) //对数组进行遍历,所有元素进行初始化(赋值),默认为0值 { for (int l = 0; l < 19; l++) { qiPan[h][l] = 0; //对当前元素进行初始化(赋值),默认为0值 } } for (int h = 0; h < 19; h++) //对数组进行遍历,所有元素进行初始化(赋值),默认为0值 { for (int l = 0; l < 19; l++) { qiPanTwo[h][l] = 0; //对当前元素进行初始化(赋值),默认为0值 } } CSH(); Show(); char xx ; //控制台从键盘获得一个字符的函数(方法) while (xx = _getch()) { switch (xx) //控制 { case 'w': //上 Y = Y - 1; //使光标纵坐标-1 qiPan[Y][X] = 8; //将光标写入棋盘 qiPan[Y + 1][X] = 0; //使原本位置的值还原 system("cls"); Show(); break; case 's': //下 Y = Y + 1; qiPan[Y][X] = 8; qiPan[Y - 1][X] = 0; system("cls"); Show(); break; case 'a': //左 X = X - 1; qiPan[Y][X] = 8; qiPan[Y][X + 1] = 0; system("cls"); Show(); break; case 'd': //右 X = X + 1; qiPan[Y][X] = 8; qiPan[Y][X - 1] = 0; system("cls"); Show(); break; case 'j': //落子 if (player == 1) //如果当前玩家为白色方时 { qiPanTwo[Y][X] = 1; //通过棋盘1中的光标位置更改棋盘2 //中对应元素的值为1,也就是白棋落子 player = 2; //白棋落子后更换玩家 system("cls"); Show(); for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { Judge(i, j); } } } else if (player == 2) { qiPanTwo[Y][X] = 2; player = 1; system("cls"); Show(); for (int i = 0; i < 19; i++) { for (int j = 0; j < 19; j++) { Judge(i, j); } } } break; } } }
标签:qiPanTwo,19,int,五子棋,else,break,++ 来源: https://www.cnblogs.com/sheep-mr/p/16387827.html