当前位置:网站首页>C语言,实现三子棋小游戏
C语言,实现三子棋小游戏
2022-07-02 21:34:00 【51CTO】
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define a 3
#define b 3
void qi(char san[a][b])
{
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
san[i][j]=' ';
}
}
}
void bu(char san[a][b])
{
printf("+---+---+---+\n");
for(int i=0;i<a;i++)
{
printf("| %c | %c | %c |\n",san[i][0],san[i][1],san[i][2]);
printf("+---+---+---+\n");
}
}
void play(char san[a][b])
{
while(1)
{
int x,y;
printf("请输入所要下的坐标(中间用空格隔开)\n");
scanf("%d%d",&x,&y);
if(x<0||x>=a||y>=b||y<0)
{
printf("输入错误,请在(1,1)到(3,3)的范围内重新输入\n");
continue;
}
else if(san[x][y]!=' ')
{
printf("该位子已有棋子\n");
continue;
}
else
{
san[x][y]='x';
break;
}
}
}
void dian(char san[a][b])
{
while(1)
{
int x=rand()%a;
int y=rand()%b;
if(san[x][y]!=' '||san[x][y]=='x')
{
continue;
}
else
{
san[x][y]='o';
break;
}
}
}
int ping(char san[a][b])
{
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
if(san[i][j]==' ')
{
return 0;
}
}
}
return 1;
}
char pan(char san[a][b])
{
for(int i=0;i<a;i++)
{
if(san[i][0]!=' '&&san[i][0]==san[i][1]&&san[i][1]==san[i][2])
{
return san[i][0];
}
}
for(int j=0;j<b;j++)
{
if(san[0][j]!=' '&&san[0][j]==san[1][j]&&san[1][j]==san[2][j])
{
return san[1][j];
}
}
if(san[0][0]==san[1][1]&&san[1][1]==san[2][2]&&san[0][0]!=0)
{
return san[0][0];
}
if(san[2][0]==san[1][1]&&san[1][1]==san[0][2]&&san[2][0])
{
return san[2][0];
}
if(ping(san)==1)
{
return 'h';
}
return 'j';
}
void game()
{
char ret='0';
char san[a][b]={0};//定义一个3*3的数组
qi(san);//对3*3数组进行初始化
bu(san);//布置棋盘架构
while(1)
{
play(san);//玩家进行下棋
bu(san);//布置棋盘架构
ret=pan(san);
if(ret!='j')
{
break;
}
dian(san);//电脑进行下棋
bu(san);//布置棋盘架构
ret=pan(san);
if(ret!='j')
{
break;
}
}
if(ret=='x')
{
printf("恭喜您获胜了\n");
}
else if(ret=='o')
{
printf("电脑获胜了\n");
}
else
{
printf("平局\n");
}
}
int menu()
{
int en;
printf("##########################\n");
printf("##1.进入游戏 0.退出游戏##\n");
printf("##########################\n");
scanf("%d",&en);
return en;
}
int main()
{
srand((unsigned int)time(0));
while(1)
{
system("cls");
int en=menu();
if(en==1)
{
game();
}
else if(en==0)
{
printf("退出游戏\n");
break;
}
else
{
printf("输入错误!请重新输入!\n");
continue;
}
}
system("pause");
return 0;
}
边栏推荐
- 如何访问kubernetes API?
- pyqt圖片解碼 編碼後加載圖片
- How to prevent your jar from being decompiled?
- 【剑指 Offer】57. 和为s的两个数字
- [C language] [sword finger offer article] - replace spaces
- PIP audit: a powerful security vulnerability scanning tool
- Summary of the first week of summer vacation
- How to test the process of restoring backup files?
- 一周生活
- [shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
猜你喜欢
*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
From "bronze" to "King", there are three secrets of enterprise digitalization
MySQL learning record (7)
D4: unpaired image defogging, self enhancement method based on density and depth decomposition (CVPR 2022)
"New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
Oriental Aesthetics and software design
Off chip ADC commissioning record
【零基础一】Navicat下载链接
[use of pointer and pointer and array]
随机推荐
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
Interpretation of CVPR paper | generation of high fidelity fashion models with weak supervision
Accounting regulations and professional ethics [19]
Basic IO interface technology - microcomputer Chapter 7 Notes
MySQL inserts Chinese data and reports an error. Set the default collation
[shutter] statefulwidget component (pageview component)
Chargement de l'image pyqt après décodage et codage de l'image
地理探测器原理介绍
Construction and maintenance of business websites [10]
暑期第一周总结
[zero foundation I] Navicat download link
Redis distributed lock failure, I can't help but want to burst
[Jianzhi offer] 57 And are two numbers of S
Record the functions of sharing web pages on wechat, QQ and Weibo
How to test the process of restoring backup files?
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
MySQL learning record (5)
100 important knowledge points that SQL must master: management transaction processing
Etcd Raft 协议
Unity3D学习笔记4——创建Mesh高级接口