当前位置:网站首页>The third is the code to achieve
The third is the code to achieve
2022-07-31 04:01:00 【GD_small_bit】
在C语言的学习过程中,Implementing mini-games independently can improve our accuracyClanguage interest,Today I will bring you the realization of the tri-bang game.
Documentation for the backgammon code
test.c--------Check out three pieces
game.c--------游戏代码的实现
game.h---------游戏代码的声明
游戏菜单的实现
1.选择是否开始游戏
2.Choose whether to end the game or not
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("The coordinates you entered are already taken.\n");
}
}
else
{
printf("The coordinates you entered are wrong,请重新输入.\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;
}
}
The overall call to the game code
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");
}
运行结果如下:
边栏推荐
- Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
- Safety 20220715
- C语言表白代码?
- [C language] General method of expression evaluation
- log level and print log note
- 安全20220722
- Web container and IIS --- Middleware penetration method 1
- Why don't you programmers make a living off your own projects?And have to work for someone else?
- errno error code and meaning (Chinese)
- Detailed explanation of TCP (2)
猜你喜欢
LocalDate加减操作及比较大小
A brief introduction to the CheckboxListTile component of the basic components of Flutter
端口排查步骤-7680端口分析-Dosvc服务
postgresql 15源码浅析(5)—— pg_control
Thinking about data governance after Didi fines
type_traits元编程库学习
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
exsl文件预览,word文件预览网页方法
IDEA common shortcut keys and plug-ins
高等数学---第九章二重积分
随机推荐
[Swift] Customize the shortcut that pops up by clicking the APP icon
Good place to download jar packages
【C语言进阶】文件操作(一)
Redis uses sorted set to cache latest comments
RESTful api interface design specification
[shell basics] determine whether the directory is empty
已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined
exsl文件预览,word文件预览网页方法
[CV project debugging] CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT problem
[C language] Preprocessing operation
(4) Recursion, variable parameters, access modifiers, understanding main method, code block
Safety 20220718
浅识Flutter 基本组件之CheckboxListTile组件
PMP WeChat group daily exercises
(6) Enumeration and annotation
[C language] General method of expression evaluation
LeetCode每日一练 —— OR36 链表的回文结构
【AUTOSAR-RTE】-4-Port and Interface and Data Type
Pytest电商项目实战(上)
Day32 LeetCode