本文实例为大家分享了C语言实现双人五子棋游戏的具体代码,供大家参考,具体内容如下
实现功能
生成棋盘玩家1与玩家2对战,哪个玩家率先有连续5子连线,哪个玩家赢。
如何实现
组成:
二维数组:board[ROW][COL],定义一个ROW*COL的棋盘。
主要逻辑:
显示棋盘,提示用户下子,下子后判断
1.显示棋盘很简单,慢慢凑棋盘就好
2. 用户下子,注意两个条件:棋子在棋盘里,下子位置未被占用。
3.判断是最难的,
方法:从下子位置的8个方向(上,下,左,右,右上,右下,左上,左下)计算相同棋子数目,然后将对角的棋子数相加,等于5说明有5子连线
主要函数中用到三个主要实现函数:
1
2
3
4
|
Showboard(board, ROW, COL); //展示棋盘 Playermove(board, ROW, COL, cur); //玩家下注,cur表示哪个玩家下子 Judge(board, ROW, COL); //判断5子连线 Getcount(board[][COL], row, col, dir) //计算方向dir相同棋子数 |
代码
头文件
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
|
#ifndef __FIVECHREE_H__ #define __FIVECHEE_H__ #include<stdio.h> #include<windows.h> #pragma warning(disable:4996) #define ROW 10//棋盘行数 #define COL 10//棋盘列数 #define INIT '*'//棋盘初始化 #define PLAYER1 1 #define PLAYER2 2 #define NEXT 3//继续往下下 #define DRAW 4//棋盘下满 平局 //8个方向 #define UP 10 #define RIGHT_UP 11 #define RIGHT 12 #define RIGHT_DOWN 13 #define DOWN 14 #define LEFT_DOWN 15 #define LEFT 16 #define LEFT_UP 17 extern void Menu(); extern void Game(); #endif |
main函数源文件
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
|
#include"fivechree.h" int main(){ int quit = 0; while (!quit){ Menu(); int select = 0; scanf ( "%d" , &select); switch (select){ case 1: Game(); break ; case 2: quit = 1; break ; default : printf ( "Enter Error!\n" ); break ; } } printf ( "Byebye\n" ); system ( "pause" ); return 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
#include"fivechree.h" static int x = 0; static int y = 0; void Menu(){ printf ( "+---------------------+\n" ); printf ( "+- 1.Play 2.Exit -+\n" ); printf ( "+---------------------+\n" ); printf ( "Please Enter Your Select#" ); } static void Showboard( int board[][COL], int row, int col){ //展示棋盘 o玩家1棋子,x玩家2棋子 system ( "cls" ); for ( int i = 0; i < row; i++){ for ( int j = 0; j < col; j++){ switch (board[i][j]){ case PLAYER1: board[i][j] = 'o' ; break ; case PLAYER2: board[i][j] = 'x' ; break ; case 0: board[i][j] = INIT; break ; default : break ; } } } printf ( " " ); for ( int i =1; i <= row; i++){ printf ( "%2d " , i); } printf ( "\n" ); for ( int i = 1; i <= row; i++){ printf ( "%-2d" , i); for ( int j = 1; j <= col; j++){ printf ( " %c " , board[i - 1][j - 1]); } printf ( "\n" ); } } static void Playermove( int board[][COL], int row, int col, int who){ //玩家下子,who 为哪个玩家下子 while (1){ printf ( "Please Enter PLAYER%d Postion<x,y>#" , who); scanf ( "%d %d" , &x, &y); if (x<1 || x>row || y<1 || y>col){ //超过棋盘范围 printf ( "Postion is error!\n" ); continue ; } if (board[x - 1][y - 1] == INIT){ //判断位置是否已被下子 board[x - 1][y - 1] = who; break ; } printf ( "Postion is not empty\n" ); } } static int Getcount( int board[][COL], int row, int col, int dir){ //判断8个方向相同棋子的数目 int _x = x; //_x,_y变化,后面与x,y棋子相比较 int _y = y; int count = 0; while (1){ switch (dir){ case UP: _x--; break ; case DOWN: _x++; break ; case LEFT: _y--; break ; case RIGHT: _y++; break ; case RIGHT_UP: _x--, _y++; break ; case RIGHT_DOWN: _x++, _y++; break ; case LEFT_DOWN: _x++, _y--; break ; case LEFT_UP: _x--, _y--; break ; default : break ; } if (_x>=1 || _x<=row || _y>=1 || _y<=col){ //棋子不能越界 if (board[x-1][y-1] == board[_x-1][_y-1]){ //printf("yes\n"); count++; } else { //printf("no\n"); break ; } } else { return count; } } return count; } //如何判断:从下子位置的8个方向(上,下,左,右,右上,右下,左上,左下) //计算相同棋子数目,然后将对角的棋子数相加,等于5说明有5子连线 static int Judge( int board[][COL], int row, int col){ int count1 = Getcount(board, row, col, UP)\ + Getcount(board, row, col, DOWN); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = Getcount(board, row, col, RIGHT_UP)\ + Getcount(board, row, col, LEFT_DOWN); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = Getcount(board, row, col, RIGHT)\ + Getcount(board, row, col, LEFT); //printf("%d\n", count1); if (count1 >= 4){ return board[x-1][y-1]; } count1 = Getcount(board, row, col, RIGHT_DOWN)\ + Getcount(board, row, col, LEFT_UP); if (count1 >= 4){ return board[x-1][y-1]; } for ( int i = 0; i < row; i++){ //判断棋盘是否下满 for ( int j = 0; j < col; j++){ if (board[i][j] == INIT){ return NEXT; } } } return DRAW; } void Game(){ int board[ROW][COL] = { 0 }; //memset(board, INIT, ROW*COL); int result = 0; int cur = PLAYER1; Showboard(board, ROW, COL); //先展示棋盘 while (1){ //Showboard(board, ROW, COL); Playermove(board, ROW, COL, cur); Showboard(board, ROW, COL); //棋盘将board数组变化,所以要在判断前将数组变化 result = Judge(board, ROW, COL); if (result != NEXT){ break ; } cur = (cur == PLAYER1 ? PLAYER2 : PLAYER1); //三目表达式,注意不是 PLAYER1 ? PLAYER2 : PLAYER1 } Showboard(board, ROW, COL); switch (result){ case 'o' : printf ( "Player1 Win!\n" ); break ; case 'x' : printf ( "Player2 Win!\n" ); break ; case DRAW: printf ( "Tie Game!\n" ); break ; default : //printf("%c\n", result); printf ( "BUG\n" ); break ; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_57023347/article/details/117463405