#include<iostream>
#include<cstdlib>
using namespace std;
const int M=7;//规定棋盘大小
const int N=5;//玩五子棋
void initchess(char*);
void humanmove(char*);
void computermove(char*);
char wincheck(char*);
void showchess(char*);
int main(void)
{
char chess[M*M];
char done;
cout<<"------game of chess-----"<<endl;
cout<<"human against the stupid computer"<<endl;
done='*';
initchess(chess);
do{
showchess(chess);
humanmove(chess);
done=wincheck(chess);
if(done!='*')
break;
computermove(chess);
done=wincheck(chess);
if(done!='*')break;
}while(done=='*');
showchess(chess);
if(done=='h')
cout<<"human won!"<<endl;
else
cout<<"computer won!!!"<<endl;
return 0;
}
//初始化棋盘
void initchess(char chess[])
{
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
chess[i+M*j]='*';
}
//下棋人的落子
void humanmove(char chess[])
{
int x,y;
cout<<"enter x,y coordinates for your move";
cin>>x>>y;
cout<<endl;
if(chess[x+M*y]!='*')
{
cout<<"invalid move,try again!"<<endl;
humanmove(chess);
}
else
chess[x+M*y]='h';
}
//计算机落子
void computermove(char chess[])
{
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
if(chess[i+M*j]=='*')break;
if(chess[i+M*j]=='*')break;
}
if(i*j==M*M)
{
cout<<"diamond cut diamond"<<endl;
exit(0);
}
else
chess[i+M*j]='C';
}
//在屏幕上显示棋局
void showchess(char chess[])
{
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
cout<<chess[i+M*j]<<"\t";
cout<<endl<<endl;
}
}
//检查是否有赢家
char wincheck(char chess[])
{
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
char t=chess[i+M*j];
int count=1;
for(int a=i+1,b=j+1;chess[a+M*b]==t&&a<M&&b<M;a++,b++)
count++;
if(count>=N)
return t;
count=1;
for(a=i,b=j+1;chess[a+M*b]==t&&b<M;b++)
count ++;
if(count>=N)
return t;
count=1;
for(a=i+1,b=j;chess[a+M*b]==t&&a<M;a++)
count ++;
if(count>=N)
return t;
for(a=i+1,b=j-1;chess[a+M*b]==t&&a<M&&b>=0;a++,b--)
count ++;
if(count>=N)
return t;
}
}
return '*';
}
wuziqi.rar_五指棋人机C++
版权申诉
70 浏览量
2022-09-24
03:49:41
上传
评论
收藏 1.6MB RAR 举报
我虽横行却不霸道
- 粉丝: 75
- 资源: 1万+
最新资源
- Pytorch-pytorch深度学习教程之Tensorboard.zip
- 基于C++和Python开发yolov8-face作为人脸检测器dlib作为人脸识别器的人脸考勤系统源码+项目说明.zip
- Pytorch-pytorch深度学习教程之变分自动编码器.zip
- Pytorch-pytorch深度学习教程之神经风格迁移.zip
- Pytorch-pytorch深度学习教程之深度残差网络.zip
- Pytorch-pytorch深度学习教程之循环神经网络.zip
- Pytorch-pytorch深度学习教程之逻辑回归.zip
- Pytorch-pytorch深度学习教程之双向循环网络.zip
- Pytorch-pytorch深度学习教程之卷积神经网络.zip
- Pytorch-pytorch深度学习教程之前馈神经网络.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈