本文实例为大家分享了C语言编写五子棋游戏的具体代码,供大家参考,具体内容如下
一、构建棋盘
首先可以用一个二维数组来构建棋盘,下标表示位置,内容表示黑子白子或者空位。当数组内容为1时,该位置为白字,当数组为0时,该位置为白子,空位输出+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
int w[11][11], flag = 0; int a, b; for ( int k = 0; k < 11; k++) printf ( "第%d列\t" , k); printf ( "\n" ); for ( int i = 0; i < 11; i++) { for ( int j = 0; j < 11; j++) { if (w[i][j] == 0) printf ( "黑\t" ); else if (w[i][j] == 1) printf ( "白\t" ); else printf ( "+\t" ); } printf ( "第%d行\n\n" , i); } |
二、判断位置超范围与错误输入
当输入的棋子坐标超出范围或者该位置已经存在棋子,则再次输入棋子坐标。其中flag作为标志作为判别黑手白手的标志。
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
|
if (flag == 0) { printf ( "黑色下棋\n" ); scanf ( "%d %d" , &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf ( "此位置超出范围,请重新输入:" ); scanf ( "%d %d" , &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf ( "此位置已有棋子,请重新输入:" ); scanf ( "%d %d" , &a, &b); } flag = 1; w[a][b] = 0; } else { printf ( "白色下棋\n" ); scanf ( "%d %d" , &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf ( "此位置超出范围,请重新输入:" ); scanf ( "%d %d" , &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf ( "此位置已有棋子,请重新输入:" ); scanf ( "%d %d" , &a, &b); } |
三、判断胜负
在五子棋中,出现五个棋子排成一排或者一列或者斜排即可判为胜出。每当落下一个棋子,对该棋子前后上下斜排五个棋子进行判别,超过五个连续的一样颜色的棋子则胜出。
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
|
int A = 1, B = 1, jieguo = 0, C = 1, D = 1; int x = 1, y = 1, m = 1, n = 1; if (w[a][b] == 1) { for ( int i = 1; i < 5; i++) { if (w[a][b + i] == 1) A++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a][b - i] == 1) A++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a+i][b] == 1) C++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b] == 1) C++; else break ; } if (A > 4 || C > 4) jieguo = 1; //白棋胜利标志 else { for ( int i = 1; i < 5; i++) { if (w[a+i][b + i] == 1) x++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a-i][b - i] == 1) x++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a + i][b-i] == 1) y++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b+i] == 1) y++; else break ; } if (x > 4 || y > 4) jieguo = 1; } } if (w[a][b] == 0) { for ( int i = 1; i < 5; i++) { if (w[a][b + i] == 0) B++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a][b - i] == 0) B++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a + i][b] == 0) D++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b] == 0) D++; else break ; } if (B > 4 || D > 4) jieguo = 2; //黑棋胜利标志 else { for ( int i = 1; i < 5; i++) { if (w[a + i][b + i] == 0) m++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b - i] == 0) m++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a + i][b - i] == 0) n++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b + i] == 0) n++; else break ; } if (m > 4 || n > 4) jieguo = 2; } } |
程序完成
四、演示结果
五、源代码
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
|
int main() { int w[11][11], flag = 0; int a, b; while (1) { if (flag == 0) { printf ( "黑色下棋\n" ); scanf ( "%d %d" , &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf ( "此位置超出范围,请重新输入:" ); scanf ( "%d %d" , &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf ( "此位置已有棋子,请重新输入:" ); scanf ( "%d %d" , &a, &b); } flag = 1; w[a][b] = 0; } else { printf ( "白色下棋\n" ); scanf ( "%d %d" , &a, &b); while (a < 0 || a>10 || b < 0 || b>10) { printf ( "此位置超出范围,请重新输入:" ); scanf ( "%d %d" , &a, &b); } while (w[a][b] == 0 || w[a][b] == 1) { printf ( "此位置已有棋子,请重新输入:" ); scanf ( "%d %d" , &a, &b); } flag = 0; w[a][b] = 1; } int A = 1, B = 1, jieguo = 0, C = 1, D = 1; int x = 1, y = 1, m = 1, n = 1; if (w[a][b] == 1) { for ( int i = 1; i < 5; i++) { if (w[a][b + i] == 1) A++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a][b - i] == 1) A++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a+i][b] == 1) C++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b] == 1) C++; else break ; } if (A > 4 || C > 4) jieguo = 1; //白棋胜利标志 else { for ( int i = 1; i < 5; i++) { if (w[a+i][b + i] == 1) x++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a-i][b - i] == 1) x++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a + i][b-i] == 1) y++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b+i] == 1) y++; else break ; } if (x > 4 || y > 4) jieguo = 1; } } if (w[a][b] == 0) { for ( int i = 1; i < 5; i++) { if (w[a][b + i] == 0) B++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a][b - i] == 0) B++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a + i][b] == 0) D++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b] == 0) D++; else break ; } if (B > 4 || D > 4) jieguo = 2; //黑棋胜利标志 else { for ( int i = 1; i < 5; i++) { if (w[a + i][b + i] == 0) m++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b - i] == 0) m++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a + i][b - i] == 0) n++; else break ; } for ( int i = 1; i < 5; i++) { if (w[a - i][b + i] == 0) n++; else break ; } if (m > 4 || n > 4) jieguo = 2; } } for ( int k = 0; k < 11; k++) printf ( "第%d列\t" , k); printf ( "\n" ); for ( int i = 0; i < 11; i++) { for ( int j = 0; j < 11; j++) { if (w[i][j] == 0) printf ( "黑\t" ); else if (w[i][j] == 1) printf ( "白\t" ); else printf ( "+\t" ); } printf ( "第%d行\n\n" , i); } if (jieguo == 1) { printf ( "白棋胜利" ); break ; } if (jieguo == 2) { printf ( "黑棋胜利" ); break ; } } return 0; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_46265516/article/details/104171447