当前位置:网站首页>三子棋的代码实现
三子棋的代码实现
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");
}
运行结果如下:
边栏推荐
猜你喜欢

C language from entry to such as soil, the data store

What skills do I need to learn to move from manual testing to automated testing?
![[C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution](/img/60/fa75e06af4d143ee3fb493221fa3d9.jpg)
[C language] General method for finding the sum of the greatest common factor and the least common multiple of two integers m and n, the classical solution
![[C language] General method of base conversion](/img/28/954af5f47a79ff02d3cc0792ac8586.jpg)
[C language] General method of base conversion

Component pass value provide/inject

LocalDate加减操作及比较大小

扫雷游戏(c语言写)

web容器及IIS --- 中间件渗透方法1

The BP neural network

A brief introduction to the CheckboxListTile component of the basic components of Flutter
随机推荐
"DeepJIT: An End-To-End Deep Learning Framework for Just-In-Time Defect Prediction" paper notes
Key Technologies of Interface Testing
强化学习:从入门到入坑再到拉屎
C language from entry to such as soil, the data store
大小端模式
Safety 20220709
$parent/$children and ref
[Swift]自定义点击APP图标弹出的快捷方式
qlib自动化quant
MySQL 8.0.30 GA
Thinking about data governance after Didi fines
Know the showTimePicker method of the basic components of Flutter
组件传值 provide/inject
浅识Flutter 基本组件之CheckboxListTile组件
Component pass value provide/inject
Regarding the primary key id in the mysql8.0 database, when the id is inserted using replace to be 0, the actual id is automatically incremented after insertion, resulting in the solution to the repea
LeetCode每日一练 —— 138. 复制带随机指针的链表
IDEA common shortcut keys and plug-ins
PMP WeChat group daily exercises
日志级别 和 打印log注意