前言
检验学习成果最好的方式是实践,在学习完C语言的函数和数组以后,就可以简易的实现一些小游戏,本文将介绍如何实现三字棋小游戏。欢迎探讨
思路分析
三子棋的规则是在3X3的棋盘里,率先将自己的三个棋子连成一条直线为赢家,程序实现的核心是玩家的落子以及输赢的判断。
核心流图:当前棋盘->玩家1移动->当前棋盘->输赢判断->玩家2移动->当前棋盘->判断输赢->玩家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
|
void PlayerMove( char board[ROW][COL], int row, int col)
{
printf ( "玩家走:>\n" );
int x = 0;
int y = 0;
while (1)
{
printf ( "请输入坐标:>" );
scanf ( "%d%d" , &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ' )
{
board[x - 1][y - 1] = '*' ;
break ;
}
else
{
printf ( "坐标被占用,请重新输入\n" );
}
}
else
{
printf ( "坐标非法,超出范围\n" );
}
}
}
|
当前棋盘
实质是输出二维数组
输出二维数组,但是为了美化棋盘,我们需要一些符号来分割棋盘,让它结构清晰。
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
|
void DisplayBoard( char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf ( " %c " , board[i][j]);
if (j < col - 1)
printf ( "|" );
}
printf ( "\n" );
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf ( "---" );
if (j < col - 1)
printf ( "|" );
}
}
printf ( "\n" );
}
}
|
判断输赢
只要我们找到三个连成一条直线的棋子即可,这里选三个棋子中中间那个即可。然后判断是不是满足条件。
说明:
返回*,表示输入*的玩家赢;
返回#,表示输入#的玩家赢;
返回 Q,表示平局;
返回C,表示游戏继续。
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
|
char IsWin( char board[ROW][COL], int row, int col){
int i, j, count = 0;
for (i=0;i<row;i++)
for (j = 0; j < col; j++) {
if (board[i][j] == '*' ) {
if (i-1>=0&&i+1<row&&board[i-1][j] == '*' && board[i + 1][j] == '*' )
return '*' ;
if (j-1>=0&&j+1<col&&board[i][j-1] == '*' && board[i][j + 1] == '*' )
return '*' ;
if (i-1 >=0 && i + 1 < row && j - 1 >= 0 && j + 1 < col&& board[i-1][j-1] == '*' && board[i + 1][j + 1] == '*' )
return '*' ;
if (i-1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j + 1] == '*' && board[i + 1][j - 1] == '*' )
return '*' ;
}
if (board[i][j] == '#' ) {
if (i - 1 >= 0 && i + 1 < row && board[i - 1][j] == '#' && board[i + 1][j] == '#' )
return '#' ;
if (j - 1 >= 0 && j + 1 < col && board[i][j - 1] == '#' && board[i][j + 1] == '#' )
return '#' ;
if (i-1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j - 1] == '#' && board[i + 1][j + 1] == '#' )
return '*' ;
if (i-1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j + 1] == '#' && board[i + 1][j - 1] == '#' )
return '#' ;
}
if (board[i][j] == ' ' )
count++;
}
if (count == 0)
return 'Q' ;
else
return 'C' ;
}
|
源码实现
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
|
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define ROW 3
#define COL 3
void InitBoard( char board[ROW][COL], int row, int col);
void DisplayBoard( char board[ROW][COL], int row, int col);
void Player1Move( char board[ROW][COL], int row, int col);
void Player2Move( char board[ROW][COL], int row, int col);
char IsWin( char board[ROW][COL], int row, int col);
void InitBoard( char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
board[i][j] = ' ' ;
}
}
} //棋盘初始化,二维数组全部初始化空格
void DisplayBoard( char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf ( " %c " , board[i][j]);
if (j < col - 1)
printf ( "|" );
}
printf ( "\n" );
if (i < row - 1)
{
for (j = 0; j < col; j++)
{
printf ( "---" );
if (j < col - 1)
printf ( "|" );
}
}
printf ( "\n" );
}
} //棋盘打印函数,打印出当前棋盘
void Player1Move( char board[ROW][COL], int row, int col)
{
printf ( "玩家1走:>\n" );
int x = 0;
int y = 0;
while (1)
{
printf ( "请输入坐标:>" );
scanf ( "%d%d" , &x, &y); //2 1 -- > 1 0
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ' )
{
board[x - 1][y - 1] = '*' ;
break ;
}
else
{
printf ( "坐标被占用,请重新输入\n" );
}
}
else
{
printf ( "坐标非法,超出范围\n" );
}
}
}
void Player2Move( char board[ROW][COL], int row, int col)
{
printf ( "玩家1走:>\n" );
int x = 0;
int y = 0;
while (1)
{
printf ( "请输入坐标:>" );
scanf ( "%d%d" , &x, &y); //2 1 -- > 1 0
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (board[x - 1][y - 1] == ' ' )
{
board[x - 1][y - 1] = '#' ;
break ;
}
else
{
printf ( "坐标被占用,请重新输入\n" );
}
}
else
{
printf ( "坐标非法,超出范围\n" );
}
}
}
char IsWin( char board[ROW][COL], int row, int col) {
int i, j, count = 0;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++) {
if (board[i][j] == '*' ) {
if (i - 1 >= 0 && i + 1 < row && board[i - 1][j] == '*' && board[i + 1][j] == '*' )
return '*' ;
if (j - 1 >= 0 && j + 1 < col && board[i][j - 1] == '*' && board[i][j + 1] == '*' )
return '*' ;
if (i - 1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j - 1] == '*' && board[i + 1][j + 1] == '*' )
return '*' ;
if (i - 1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j + 1] == '*' && board[i + 1][j - 1] == '*' )
return '*' ;
}
if (board[i][j] == '#' ) {
if (i - 1 >= 0 && i + 1 < row && board[i - 1][j] == '#' && board[i + 1][j] == '#' )
return '#' ;
if (j - 1 >= 0 && j + 1 < col && board[i][j - 1] == '#' && board[i][j + 1] == '#' )
return '#' ;
if (i - 1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j - 1] == '#' && board[i + 1][j + 1] == '#' )
return '*' ;
if (i - 1 >= 0 && i + 1 < row && j - 1 >= 0 && j + 1 < col && board[i - 1][j + 1] == '#' && board[i + 1][j - 1] == '#' )
return '#' ;
}
if (board[i][j] == ' ' )
count++;
}
if (count == 0)
return 'Q' ;
else
return 'C' ;
}
int main()
{
char board[ROW][COL];
InitBoard(board, ROW, COL);
DisplayBoard(board, ROW, COL);
char ret = 0;
while (1)
{
Player1Move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C' )
{
break ;
}
Player2Move(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C' )
{
break ;
}
}
if (ret == '*' )
{
printf ( "玩家赢\n" );
}
else if (ret == '#' )
{
printf ( "电脑赢\n" );
}
else
{
printf ( "平局\n" );
}
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wang_fm/article/details/119143711