本文实例为大家分享了C语言版三子棋游戏的具体代码,供大家参考,具体内容如下
一、游戏说明
三子棋相信大家都不陌生,小时候经常在纸上画出九宫格就可以直接和小伙伴一起玩,规则就是谁先在九宫格中下出同一行三个一样,或者同一列三个一样,又或者对角线的三个一样的棋子,谁就获胜;其实在编程中,代码也能实现人机版的三子棋小游戏,让我们一起来看看如何实现的吧!
二、游戏设计步骤
将三子棋游戏的实现逻辑分为以下步骤:
1.创建菜单(选择玩游戏或者退出游戏)
2.创建和初始化游戏棋盘(本质上为一个二维数组)
3.打印出棋盘
4.玩家下棋(用‘ * '表示棋子)
5.判断输赢或者是否平局
6.电脑下棋(用‘ # '表示棋子)
7.判断输赢(同第5步)
8.回到第3步直至确定输赢
三、代码实现
1.创建菜单
规定输入 1 为玩游戏,输入 0 为退出游戏
1
2
3
4
5
6
7
|
void menu()
{
printf ( "******************************\n" );
printf ( "********** 1.play **********\n" );
printf ( "********** 0.exit **********\n" );
printf ( "******************************\n" );
}
|
2.创建和初始化游戏棋盘
本质为创建一个二维数组并初始化
1
2
3
4
5
6
7
8
9
10
11
12
|
void InitBoard( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ' ;
}
}
}
|
3.打印出棋盘
利用循环的方式将字符排版成棋盘的样式(n x 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
26
27
28
29
|
void DisplayBoard( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
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" );
}
}
|
4.玩家下棋
规定玩家的棋子为字符‘ * ',同时需判断棋子坐标的合理性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
void PlayerMove( char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf ( "玩家走\n" );
printf ( "请输入坐标>:" );
while (1)
{
scanf ( "%d %d" , &x, &y);
if (x >= 1 && x <= 3 && y >= 1 && y <= 3) //判断游戏合法性
{
if (board[x - 1][y - 1] == ' ' )
{
board[x - 1][y - 1] = '*' ;
break ;
}
else
printf ( "坐标已被占用,请重新输入\n" );
}
else
printf ( "坐标非法,请重新输入\n" );
}
}
|
5.电脑下棋
规定电脑的棋子为字符‘ # ',同样需判断棋子坐标的合理性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
void ComputerMove( char board[ROW][COL], int row, int col)
{
printf ( "电脑走\n" );
while (1)
{
int x = rand () % 3;
int y = rand () % 3;
if (board[x][y] == ' ' )
{
board[x][y] = '#' ;
break ;
}
}
}
|
6.判断输赢或者是否平局
规定电脑赢返回字符‘ # ',玩家赢返回字符‘ * ',游戏继续返回字符‘C',平局(即棋盘满但还未出现胜负)则返回字符‘Q';而这里我们又需要注意,判断输赢的函数需返回一个字符,所以判断棋盘是否已满的函数需在判断输赢的函数中并且能返回一个字符,这样才能保证游戏输赢判断的完整性
(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
|
char IsWin( char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ' ) //判断行是否相等
return board[i][0];
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ' ) //判断列是否相等
return board[0][i];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ' ) //判断对角线
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[2][0] != ' ' ) //判断对角线
{
return board[0][2];
}
int ret = IsFull(board, row , col );
if (ret == 0)
{
return 'Q' ;
}
else
return 'C' ;
}
|
(2)判断棋盘是否已满的函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
int IsFull( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ' )
return 1;
}
}
return 0;
}
|
以上是各各步骤函数的具体实现,而为了方便,我们将代码分为三个文件:
1.game.h------关于游戏相关的函数声明,符号声明及头文件的包含
2.game.c------游戏相关函数的实现
3.test.c------测试游戏的逻辑
具体如下:
game.h文件:
1
2
3
4
5
6
7
8
9
|
#include<stdio.h>
#include<time.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 PlayerMove( char board[ROW][COL], int row, int col);
void ComputerMove( char board[ROW][COL], int row, int col);
char IsWin( char board[ROW][COL], int row, int col);
|
game.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
|
#include"game.h"
void InitBoard( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ' ' ;
}
}
}
void DisplayBoard( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
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 PlayerMove( char board[ROW][COL], int row, int col)
{
int x = 0;
int y = 0;
printf ( "玩家走\n" );
printf ( "请输入坐标>:" );
while (1)
{
scanf ( "%d %d" , &x, &y);
if (x >= 1 && x <= 3 && y >= 1 && y <= 3)
{
if (board[x - 1][y - 1] == ' ' )
{
board[x - 1][y - 1] = '*' ;
break ;
}
else
printf ( "坐标已被占用,请重新输入\n" );
}
else
printf ( "坐标非法,请重新输入\n" );
}
}
void ComputerMove( char board[ROW][COL], int row, int col)
{
printf ( "电脑走\n" );
while (1)
{
int x = rand () % 3;
int y = rand () % 3;
if (board[x][y] == ' ' )
{
board[x][y] = '#' ;
break ;
}
}
}
int IsFull( char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (board[i][j] == ' ' )
return 1;
}
}
return 0;
}
char IsWin( char board[ROW][COL], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ' )
return board[i][0];
}
for (i = 0; i < col; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ' )
return board[0][i];
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ' )
{
return board[0][0];
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][2] && board[2][0] != ' ' )
{
return board[0][2];
}
int ret = IsFull(board, row , col );
if (ret == 0)
{
return 'Q' ;
}
else
return 'C' ;
}
|
test.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
|
#include"game.h"
void menu()
{
printf ( "******************************\n" );
printf ( "********** 1.play **********\n" );
printf ( "********** 0.exit **********\n" );
printf ( "******************************\n" );
}
void game()
{
char board[ROW][COL]; //定义棋盘
InitBoard(board, ROW, COL); //初始化棋盘
DisplayBoard(board, ROW, COL); //打印棋盘
char ret = 0;
//
while (1)
{
PlayerMove(board, ROW, COL);
DisplayBoard(board, ROW, COL);
ret = IsWin(board, ROW, COL);
if (ret != 'C' )
break ;
ComputerMove(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" );
}
if (ret== 'Q' )
{
printf ( "平局\n" );
}
DisplayBoard(board, ROW, COL);
}
int main()
{
srand ((unsigned int ) time (NULL));
int input = 0;
do
{
menu();
printf ( "请选择:>" );
scanf ( "%d" , &input);
switch (input)
{
case 1:
game();
break ;
case 0:
printf ( "游戏结束\n" );
break ;
default :
printf ( ",文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下输入错误,请重新输入\n" );
}
} while (input);
return 0;
}
|
四、运行效果
玩游戏的三种情况分别如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_56808122/article/details/119304516