当前位置:网站首页>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;
}
边栏推荐
- *C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
- Blue Bridge Cup Winter vacation homework (DFS backtracking + pruning)
- D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
- Leetcode theme [array] -169- most elements
- Research Report on market supply and demand and strategy of China's Plastic Geogrid industry
- Summary of the first week of summer vacation
- [leetcode] sword finger offer 04 Search in two-dimensional array
- Introduction to the principle of geographical detector
- The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
- "New programmer 003" was officially launched, and the cloud native and digital practical experience of 30+ companies such as Huawei and Alibaba
猜你喜欢
Analysis of neural network
Read a doctor, the kind that studies cows! Dr. enrollment of livestock technology group of Leuven University, milk quality monitoring
图像基础概念与YUV/RGB深入理解
Off chip ADC commissioning record
基本IO接口技术——微机第七章笔记
Cardinality sorting (detailed illustration)
Official announcement! The golden decade of new programmers and developers was officially released
[shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
Daily book CSO advanced road first exposed
[shutter] shutter page Jump (route | navigator | page close)
随机推荐
MySQL learning record (7)
*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)
PIP version update timeout - download using domestic image
Centos7 installation and configuration of redis database
[leetcode] sword finger offer 04 Search in two-dimensional array
暑期第一周总结
关于测试用例
使用 EMQX Cloud 实现物联网设备一机一密验证
【零基础一】Navicat下载链接
[Yu Yue education] reference materials of analog electronic technology of Nanjing Institute of information technology
SQL必需掌握的100个重要知识点:使用游标
D4:非成对图像去雾,基于密度与深度分解的自增强方法(CVPR 2022)
Ransack组合条件搜索实现
技术人创业:失败不是成功,但反思是
发现你看不到的物体!南开&武大&ETH提出用于伪装目标检测SINet,代码已开源!...
Etcd raft protocol
【leetcode】1380. Lucky number in matrix
记录一下微信、QQ、微博分享web网页功能
How to prevent your jar from being decompiled?
*C language final course design * -- address book management system (complete project + source code + detailed notes)