当前位置:网站首页>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;
}

边栏推荐
- pyqt圖片解碼 編碼後加載圖片
- 技术人创业:失败不是成功,但反思是
- MySQL learning record (8)
- 攻防世界pwn题:Recho
- 将 EMQX Cloud 数据通过公网桥接到 AWS IoT
- MySQL learning record (9)
- MySQL inserts Chinese data and reports an error. Set the default collation
- Introduction to the principle of geographical detector
- [shutter] shutter layout component (physicalmodel component)
- ServiceMesh主要解决的三大痛点
猜你喜欢

kubernetes资源对象介绍及常用命令(四)

pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform

Browser - clean up the cache of JS in the page

Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
![[shutter] shutter layout component (wrap component | expanded component)](/img/a7/824a990235fc5ce67841ebdcf001fb.jpg)
[shutter] shutter layout component (wrap component | expanded component)

《Just because》阅读感受

《ActBERT》百度&悉尼科技大学提出ActBERT,学习全局局部视频文本表示,在五个视频-文本任务中有效!

地理探测器原理介绍

C语言,实现三子棋小游戏

Find objects you can't see! Nankai & Wuhan University & eth proposed sinet for camouflage target detection, and the code has been open source
随机推荐
[leetcode] sword finger offer 11 Rotate the minimum number of the array
Physical layer cables and equipment
MySQL learning record (6)
Daily book -- analyze the pain points of software automation from simple to deep
A specially designed loss is used to deal with data sets with unbalanced categories
读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
MySQL installation failed -gpg verification failed
pyqt图片解码 编码后加载图片
Basic IO interface technology - microcomputer Chapter 7 Notes
Pyqt picture decodes and encodes and loads pictures
Structure array, pointer and function and application cases
What is it that makes you tremble? Those without fans can learn
LightGBM原理及天文数据中的应用
如何防止你的 jar 被反编译?
Basic knowledge of tree and binary tree (detailed illustration)
Research Report on market supply and demand and strategy of China's plastic pump industry
Introduction to victoriametrics
PIP version update timeout - download using domestic image
pip安裝whl文件報錯:ERROR: ... is not a supported wheel on this platform
关于测试用例