当前位置:网站首页>三子棋的代码实现
三子棋的代码实现
2022-07-31 04:00:00 【GD_small_bit】
在C语言的学习过程中,自主实现小游戏能提高我们对C语言兴趣,我今天将为大家带来三子棋小游戏的实现。
三子棋代码的文档
test.c--------检验三子棋
game.c--------游戏代码的实现
game.h---------游戏代码的声明
游戏菜单的实现
1.选择是否开始游戏
2.选择是否结束游戏
3,判断非法输入
void menu ()
{
printf("###############################################\n");
printf("######## 1.play ############# 0.exit ##########\n");
printf("###############################################\n");
}
int main ()
{
int arr = 0;
printf("请选择。\n");
do
{
srand((unsigned int)time(NULL));
menu();
scanf("%d",&arr);
switch(arr)
{
case 1:
printf("开始游戏。\n");
game();
break;
case 0:
printf("退出游戏。\n");
break;
default :
printf("输入错误,请重新输入。\n");
break;
}
}while(arr);
return 0;
}
游戏代码的实现
1.棋盘的初始化
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]=' ';
}
}
}
2.棋盘的打印
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");
}
}
}
3.玩家下棋
void Playermove (char Board[ROW][COL],int row,int col)
{
int x = 0;
int y = 0;
while(1)
{
scanf("%d %d",&x,&y);
//判断合法性
if(x-1<row && y-1<col )
{
if(Board[x-1][y-1]==' ')
{
Board[x-1][y-1]='*';
break;
}
else
{
printf("你输入的坐标已被占用。\n");
}
}
else
{
printf("你输入的坐标错误,请重新输入。\n");
}
}
}
4.电脑自动下棋
void Computermove(char Board[ROW][COL],int row,int col)
{
int x = 0;
int y = 0;
printf("电脑下棋。\n");
while(1)
{
Sleep(1000);
x = rand()%3;
y = rand()%3;
if(Board[x][y]==' ')
{
Board[x][y]='#';
break;
}
}
}
5.游戏胜负的判断
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][0] && Board[0][2]!=' ')
{
return Board[0][2];
}
if(Isfull(Board,ROW,COL)==1)
{
return 'c';
}
else
{
return 0;
}
}
游戏代码的整体调用
void game ()
{
char Board [ROW][COL] = {
0};
//初始化棋盘为空格
InitBoard(Board,ROW,COL);
DisplayBoard(Board,ROW,COL);
while(1)
{
Playermove(Board,ROW,COL);
DisplayBoard(Board,ROW,COL);
if(Iswin(Board,ROW,COL)!='c')
{
break;
}
Computermove(Board,ROW,COL);
DisplayBoard(Board,ROW,COL);
if(Iswin(Board,ROW,COL)!='c')
{
break;
}
}
if(Iswin(Board,ROW,COL)=='*')
{
printf("玩家胜利。\n");
}
else if(Iswin(Board,ROW,COL)=='#')
{
printf("电脑胜利。\n");
}
else
printf("平局。\n");
}
运行结果如下:
边栏推荐
- BP神经网络
- IDEA常用快捷键与插件
- [CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem
- PMP WeChat group daily exercises
- $parent/$children 与 ref
- (五)final、抽象类、接口、内部类
- Difference between unallocated blocks and unused blocks in database files
- SocialFi 何以成就 Web3 去中心化社交未来
- Detailed explanation of TCP (2)
- Distributed locks and three implementation methods
猜你喜欢
A brief introduction to the CheckBox component of the basic components of Flutter
endian mode
浅识Flutter 基本组件之showDatePicker方法
exsl文件预览,word文件预览网页方法
Detailed explanation of TCP (2)
SIP协议标准和实现机制
Component pass value provide/inject
从滴滴罚款后数据治理思考
A brief introduction to the CheckboxListTile component of the basic components of Flutter
[C language] Three-pointed chess (classic solution + list diagram)
随机推荐
els block to the right
beforeDestroy与destroyed的使用
Day32 LeetCode
binom二项分布,
PMP WeChat group daily exercises
安全20220715
Distributed locks and three implementation methods
[C language] Preprocessing operation
The application and practice of mid-to-platform brand advertising platform
SQL Interview Questions (Key Points)
VS QT - ui does not display newly added members (controls) || code is silent
Safety 20220715
postgresql 15源码浅析(5)—— pg_control
With 7 years of experience, how can functional test engineers improve their abilities step by step?
No qualifying bean of type 问题
Safety 20220718
A brief introduction to the CheckboxListTile component of the basic components of Flutter
【AUTOSAR-RTE】-4-Port和Interface以及Data Type
C language from entry to such as soil, the data store
Notes on the establishment of the company's official website (6): The public security record of the domain name is carried out and the record number is displayed at the bottom of the web page