当前位置:网站首页>C language, to achieve three chess games
C language, to achieve three chess games
2022-07-02 21:55: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(" Please enter the coordinates you want ( Space separates the middle )\n");
scanf("%d%d",&x,&y);
if(x<0||x>=a||y>=b||y<0)
{
printf(" Input error , Please be there. (1,1) To (3,3) Re input within the range of \n");
continue;
}
else if(san[x][y]!=' ')
{
printf(" This seat already has chess pieces \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};// Define a 3*3 Array of
qi(san);// Yes 3*3 Array to initialize
bu(san);// Layout chessboard structure
while(1)
{
play(san);// Players play chess
bu(san);// Layout chessboard structure
ret=pan(san);
if(ret!='j')
{
break;
}
dian(san);// The computer plays chess
bu(san);// Layout chessboard structure
ret=pan(san);
if(ret!='j')
{
break;
}
}
if(ret=='x')
{
printf(" Congratulations on your victory \n");
}
else if(ret=='o')
{
printf(" The computer won \n");
}
else
{
printf(" It ends in a draw \n");
}
}
int menu()
{
int en;
printf("##########################\n");
printf("##1. Enter the game 0. Quit the game ##\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(" Quit the game \n");
break;
}
else
{
printf(" Input error ! Please re-enter !\n");
continue;
}
}
system("pause");
return 0;
}

边栏推荐
- A week's life
- [shutter] shutter layout component (physicalmodel component)
- A specially designed loss is used to deal with data sets with unbalanced categories
- pyqt图片解码 编码后加载图片
- 发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
- 图像基础概念与YUV/RGB深入理解
- Official announcement! The golden decade of new programmers and developers was officially released
- Structure array, pointer and function and application cases
- Unity3D学习笔记4——创建Mesh高级接口
- Daily book - low code you must understand in the era of digital transformation
猜你喜欢

MySQL learning record (4)

treevalue——Master Nested Data Like Tensor
![[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)](/img/fa/5c1b6c16d9aabd13e9a4f7c7b9c7da.jpg)
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)

Basic knowledge of tree and binary tree (detailed illustration)

*C language final course design * -- address book management system (complete project + source code + detailed notes)

Evolution of messaging and streaming systems under the native tide of open source cloud

Official announcement! The golden decade of new programmers and developers was officially released

发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...

Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
![The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article](/img/8f/6759b4685a129f9d10d6ea1dc8e61e.jpg)
The web version of xshell supports FTP connection and SFTP connection [detailed tutorial] continued from the previous article
随机推荐
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
It is said that this year gold three silver four has become gold one silver two..
: last child does not take effect
如何访问kubernetes API?
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
#include<>和#include“”的区别
Redis distributed lock failure, I can't help but want to burst
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
~91 rotation
Market trend report, technical innovation and market forecast of China's Micro pliers
Riding the wind of "cloud native" and stepping on the wave of "digitalization", new programmer 003 starts pre-sale
[shutter] statefulwidget component (pageview component)
Three chess games
*C language final course design * -- address book management system (complete project + source code + detailed notes)
在beforeDestroy中销毁localStorage中的值无效
Technical solution of vision and manipulator calibration system
Official announcement! The golden decade of new programmers and developers was officially released
beginning
Plastic granule Industry Research Report - market status analysis and development prospect forecast
pip安装whl文件报错:ERROR: ... is not a supported wheel on this platform