当前位置:网站首页>三子棋的代码实现
三子棋的代码实现
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");
}
运行结果如下:
边栏推荐
- $attrs/$listeners
- TCP和UDP详解
- PMP WeChat group daily exercises
- BUG definition of SonarQube
- [Swift]自定义点击APP图标弹出的快捷方式
- Redis uses LIST to cache the latest comments
- LeetCode每日一练 —— OR36 链表的回文结构
- (线段树) 基础线段树常见问题总结
- type_traits元编程库学习
- Select the smoke test case, and make the first pass for the product package entering QA
猜你喜欢
![[C language] Preprocessing operation](/img/69/0aef065ae4061edaf0d96b89846bf2.png)
[C language] Preprocessing operation

已解决(最新版selenium框架元素定位报错)NameError: name ‘By‘ is not defined

Database implements distributed locks

BP神经网络

SIP协议标准和实现机制

IDEA common shortcut keys and plug-ins

A brief introduction to the CheckboxListTile component of the basic components of Flutter

Mysql 45 study notes (twenty-four) MYSQL master-slave consistency

(5) final, abstract class, interface, inner class

Mysql 45 study notes (twenty-five) MYSQL guarantees high availability
随机推荐
Knowledge Distillation 7: Detailed Explanation of Knowledge Distillation Code
PMP WeChat group daily exercises
立足本土,链接全球 | 施耐德电气“工业SI同盟”携手伙伴共赴未来工业
C# 实现PLC的定时器
Redis 使用 sorted set 做最新评论缓存
端口排查步骤-7680端口分析-Dosvc服务
【SemiDrive源码分析】【MailBox核间通信】44 - 基于Mailbox IPCC RPC 实现核间通信(RTOS侧 IPCC_RPC Server 消息接收及回复 原理分析篇)
安全20220718
【AUTOSAR-RTE】-5-Explicit(显式)和Implicit(隐式) Sender-Receiver communication
Pytest电商项目实战(上)
Understanding and Using Unity2D Custom Scriptable Tiles (4) - Start to build a custom tile based on the Tile class (below)
volatile内存语义以及实现 -volatile写和读对普通变量的影响
BP神经网络
[C language] Three-pointed chess (classic solution + list diagram)
Safety 20220712
MySQL 8.0.30 GA
Redis 统计用户新增和留存
高等数学---第九章二重积分
SIP协议标准和实现机制
qlib架构