C++实现简易五子棋游戏
作者:PlanetRT
这篇文章主要为大家详细介绍了C++实现简易五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
C++实现的简易五子棋游戏,供大家参考,具体内容如下
三个函数:
void menu(); //菜单
int fun1(char a[21][43]); //白旗
int fun2(char a[21][43]); //黑棋
通过二维数组来实现棋盘与棋子。
构建棋盘:
直接通过一个二维字符数组来实现棋盘,只需cout即可。比较直观,但判断条件时过于繁杂。也可以使用二维整型数组,通过不同的整数来表示不同的字符,简化判断。cout时只需根据数组元素的类型打印对应的字符即可。
char a[21][43] = { ' ' };//数组初始化; int m1 = 0, m2 = 0; int z = 1; for (int j = 1; j < 21; j++) { for (int i = 1; i < 43; i++) { if ((i == 5 || i == 9 || i == 13 || i == 17 || i == 21 || i == 25 || i == 29 || i == 33 || i == 37 || i == 41) && (j == 3 || j == 5 || j == 7 || j == 9 || j == 11 || j == 13 || j == 15 || j == 17 || j == 19)) { a[j][i] = '|'; } if (!(i==0||i==1||i==2||i==3||i==4||i == 5 || i == 9 || i == 13 || i == 17 || i == 21 || i == 25 || i == 29 || i == 33 || i == 37 || i == 41||i==42) && (j == 2 || j == 4 || j == 6 || j == 8 || j == 10 || j == 12 || j == 14 || j == 16 || j == 18 || j == 20)) { a[j][i] = '-'; } } }
菜单:
void menu();
void menu() { cout << "*********************************************************************************************************" << endl;//美化; cout << "* # 五 子 棋 # *" << endl; cout << "* 游戏规则: *" << endl; cout << "* 1、白方先走第一步; *" << endl; cout << "* 2、输入棋子的位置,i 行j 列(i = 0,1,2,3,4,5,6,7,8,9;j = 0,1,2,3,4,5,6,7,8,9); *" << endl; cout << "* 同一位置只能有一种棋子; *" << endl; cout << "* 3、当行、列、斜线上有五个连续的相同棋子,则该方获胜; *" << endl; cout << "* *" << endl; cout << "*********************************************************************************************************" << endl; }
棋子:
通过输入(cin)两个int整数,表示棋子的坐标,同时二维字符数组中对应位置的元素变为表示相应棋子的字符。其中,“W”表示白棋,“B”表示黑棋。
然后进行检测判断:以每一个棋子为中心分别进行横向、纵向、左斜和右斜检测;
分别定义两个整形变量,num和sum2,初始化为0;
棋子函数返回sum2;
纵向检测:每当横向为连续两个相等的棋子时,num+1;当num等于4时,sum2+1;
横向检测:每当纵向为连续两个相等的棋子时,num+1;当num等于4时,sum2+1;
左斜右斜检测原理与纵向横向类似;
以黑棋为例:
int fun2(char a[21][43]) { int sum2 = 0; cout << endl; cout << " 黑方 " << endl; cout << " 行:"; cin >> p2; cout << " 列:"; cin >> q2; cout << "------------" << endl; a[p2*2+2][q2*4+5] = 'B'; for (int j = 2; j <= 20; j += 2) { for (int i = 5; i <= 42; i += 4) { int num1 = 0; for (int x = 1, y = 1; x <= 4; x++, y++) { if ((a[j][i] == a[j + 2 * x][i + 4 * y] && i + 2 * x <= 42 && j + 4 * y <= 20 && a[j][i] == 'B')) { num1++; } } if (num1 == 4) { sum2++; } int num2 = 0; for (int x = 1, y = 1; x <= 4; x++, y++) { if ((a[j][i] == a[j + 2 * x][i + 4 * y] && i + 2 * x <= 42 && j + 4 * y <= 20 && a[j][i] == 'B')) { num2++; } } if (num2 == 4) { sum2++; } int num3 = 0; for (int x = 1; x <= 4; x++) { if ((a[j][i] == a[j][i + 4 * x] && i + 4 * x <= 42 && a[j][i] == 'B')) { num3++; } } if (num3 == 4) { sum2++; } int num4 = 0; for (int x = 1; x <= 4; x++) { if ((a[j][i] == a[j + 2 * x][i] && j + 2 * x <= 20 && a[j][i] == 'B')) { num4++; } } if (num4 == 4) { sum2++; } } } return sum2; }
主函数:
白棋与黑棋依次走棋:
定义一个整型变量,初始化为0,每当走棋一次,+1;
根据该整型能否被2整除,判断哪一方走棋。
游戏结束的判断:
调用棋子函数,如果返回的值是否等于0则游戏结束,此时该棋子函数代表的一方获胜,否则游戏继续进行。
int main() { char a[21][43] = { ' ' };//数组初始化; int m1 = 0, m2 = 0; int z = 1; for (int j = 1; j < 21; j++) { for (int i = 1; i < 43; i++) { if ((i == 5 || i == 9 || i == 13 || i == 17 || i == 21 || i == 25 || i == 29 || i == 33 || i == 37 || i == 41) && (j == 3 || j == 5 || j == 7 || j == 9 || j == 11 || j == 13 || j == 15 || j == 17 || j == 19)) { a[j][i] = '|'; } if (!(i==0||i==1||i==2||i==3||i==4||i == 5 || i == 9 || i == 13 || i == 17 || i == 21 || i == 25 || i == 29 || i == 33 || i == 37 || i == 41||i==42) && (j == 2 || j == 4 || j == 6 || j == 8 || j == 10 || j == 12 || j == 14 || j == 16 || j == 18 || j == 20)) { a[j][i] = '-'; } } } char num0 = '0';//设置基准数字; for (int k = 5; k < 43; k += 4, num0++) { a[1][k] = num0; } char num00 = '0'; for (int k = 2; k < 21; k += 2, num00++) { a[k][4] = num00; } setlocale(LC_ALL, "chs");//转中文 menu(); cout << endl; cout << " *游戏开始!* " << endl; cout << endl; while (m1 == 0 || m2 == 0) { if (z % 2 == 1) {//白棋检测; for (int b = 0; b < 21; b++) { for (int c = 0; c < 43; c++) { cout << a[b][c]; } cout << endl; } m1 = fun1(a); if (m1 != 0) { cout << "***********************" << endl; cout << "* 游戏结束!白方胜! *" << endl; cout << "***********************" << endl; break; } } else if (z % 2 == 0) {//黑棋检测; for (int b = 0; b < 21; b++) { for (int c = 0; c < 43; c++) { cout << a[b][c]; } cout << endl; } m2 = fun2(a); if (m2 != 0) { cout << "***********************" << endl; cout << "* 游戏结束!黑方胜! *" << endl; cout << "***********************" << endl; break; } } z++; cout << endl; cout << "******************************************" << endl; cout << endl; cout << "请输入任意数字进行下一步"; cin >> A; system("cls"); //清屏 menu(); } return 0; }
实际效果
更多有趣的经典小游戏实现专题,分享给大家:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。