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

边栏推荐
- How to write a good program when a big book speaks every day?
- [use of pointer and pointer and array]
- Construction and maintenance of business websites [7]
- [zero foundation I] Navicat download link
- Research Report on market supply and demand and strategy of China's plastic pump industry
- [shutter] shutter gesture interaction (click event handling | click OnTap | double click | long press | click Cancel | press ontapdown | lift ontapup)
- China plastic box market trend report, technological innovation and market forecast
- About test cases
- Construction and maintenance of business websites [9]
- Daily book - low code you must understand in the era of digital transformation
猜你喜欢

*C语言期末课程设计*——通讯录管理系统(完整项目+源代码+详细注释)

Cardinality sorting (detailed illustration)

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

Browser - clean up the cache of JS in the page

What "real skills" should a million year old cloud native developer master? Alibaba, Tencent, meituan and byte decrypt together

Daily book -- analyze the pain points of software automation from simple to deep

Etcd raft protocol

Error in PIP installation WHL file: error: is not a supported wheel on this platform

Redis分布式锁故障,我忍不住想爆粗...

treevalue——Master Nested Data Like Tensor
随机推荐
Analysis of enterprise financial statements [4]
Chargement de l'image pyqt après décodage et codage de l'image
Free open source web version of xshell [congratulations on a happy new year]
SQL必需掌握的100个重要知识点:管理事务处理
Record the functions of sharing web pages on wechat, QQ and Weibo
Detailed explanation of OSI seven layer model
Construction and maintenance of business websites [7]
读博士吧,研究奶牛的那种!鲁汶大学 Livestock Technology 组博士招生,牛奶质量监测...
The source code of the daily book analyzes the design idea of Flink and solves the problems in Flink
TinyMCE visual editor adds Baidu map plug-in
Structure array, pointer and function and application cases
Technical solution of vision and manipulator calibration system
Cloud computing technology [1]
Unity3D学习笔记4——创建Mesh高级接口
PIP version update timeout - download using domestic image
一周生活
Analysis of enterprise financial statements [1]
[shutter] statefulwidget component (pageview component)
一周生活
Gbase 8s database basic syntax