C++实现的简易五子棋游戏,供大家参考,具体内容如下
三个函数:
void menu(); //菜单
int fun1(char a[21][43]); //白旗
int fun2(char a[21][43]); //黑棋
通过二维数组来实现棋盘与棋子。
构建棋盘:
直接通过一个二维字符数组来实现棋盘,只需cout即可。比较直观,但判断条件时过于繁杂。也可以使用二维整型数组,通过不同的整数来表示不同的字符,简化判断。cout时只需根据数组元素的类型打印对应的字符即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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();
1
2
3
4
5
6
7
8
9
10
11
|
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;
左斜右斜检测原理与纵向横向类似;
以黑棋为例:
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
|
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则游戏结束,此时该棋子函数代表的一方获胜,否则游戏继续进行。
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
|
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; } |
实际效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/PlanetRT/article/details/107239291