C++五子棋人机对战(已修复已知问题)
2024-01-08 20:50:48
这两篇博客中有个bug,就是没加srand(time(0));这个语句(放在主函数的最开头)
改正代码
#include<bits/stdc++.h>//万能头
#define ll long long
using namespace std;
ll n,a[1000][1000],x,y,b[1000][1000];
int xd[10]={1,-1,0};
int yd[10]={1,-1,0};
void init(){//初始化
for(int i=0;i<1000;i++) for(int j=0;j<1000;j++) a[i][j]=2;
}
bool find(){//判断黑子连珠
//竖着
ll sum=1,i,j;
i=x,j=y;
b[i][j]=3;
while(1){
if(a[i+1][j]==1){
i++;
sum++;
b[i][j]=3;
}
else break;
}
i=x,j=y;
while(1){
if(a[i-1][j]==1){
i--;
sum++;
b[i][j]=3;
}
else break;
}
if(sum>=5) return true;
//横着
sum=1;
i=x,j=y;
while(1){
if(a[i][j+1]==1){
j++;
sum++;
b[i][j]=3;
}
else break;
}
i=x,j=y;
while(1){
if(a[i][j-1]==1){
j--;
sum++;
b[i][j]=3;
}
else break;
}
if(sum>=5) return true;
//斜着 1
sum=1;
i=x,j=y;
while(1){
if(a[i-1][j-1]==1){
i--;
j--;
sum++;
b[i][j]=3;
}
else break;
}
i=x,j=y;
while(1){
if(a[i+1][j+1]==1){
i++;
j++;
sum++;
b[i][j]=3;
}
else break;
}
if(sum>=5) return true;
//斜着 2
sum=1;
i=x,j=y;
while(1){
if(a[i-1][j+1]==1){
i--;
j++;
sum++;
b[i][j]=3;
}
else break;
}
i=x,j=y;
while(1){
if(a[i+1][j-1]==1){
i++;
j--;
sum++;
b[i][j]=3;
}
else break;
}
if(sum>=5) return true;
return false;
}
bool find2(){//判断白子连珠
//竖着
ll sum=1,i,j;
i=x,j=y;
b[i][j]=3;
while(1){
if(a[i+1][j]==0){
i++;
sum++;
b[i][j]=3;
}
else break;
}
i=x,j=y;
while(1){
if(a[i-1][j]==0){
i--;
sum++;
b[i][j]=3;
}
else break;
}
if(sum>=5) return true;
//横着
sum=1;
i=x,j=y;
while(1){
if(a[i][j+1]==0){
j++;
sum++;
b[i][j]=3;
}
else break;
}
i=x,j=y;
while(1){
if(a[i][j-1]==0){
j--;
sum++;
b[i][j]=3;
}
else break;
}
if(sum>=5) return true;
//斜着 1
sum=1;
i=x,j=y;
while(1){
if(a[i-1][j-1]==0){
i--;
j--;
sum++;
b[i][j]=3;
}
else break;
}
i=x,j=y;
while(1){
if(a[i+1][j+1]==0){
i++;
j++;
sum++;
b[i][j]=3;
}
else break;
}
if(sum>=5) return true;
//斜着 2
sum=1;
i=x,j=y;
while(1){
if(a[i-1][j+1]==0){
i--;
j++;
sum++;
b[i][j]=3;
}
else break;
}
i=x,j=y;
while(1){
if(a[i+1][j-1]==0){
i++;
j--;
sum++;
b[i][j]=3;
}
else break;
}
if(sum>=5) return true;
return false;
}
bool find4(int t,int tt){//AI判断对方是否有连珠
//竖着
ll sum=1,i,j,ii,jj;
i=x,j=y;
while(1){
if(a[i+1][j]==tt){
i++;
sum++;
}
else break;
}
ii=x,jj=y;
while(1){
if(a[ii-1][jj]==2&&sum==t){
x=ii-1;
y=jj;
return true;
}
if(a[ii-1][jj]==tt){
ii--;
sum++;
}
else break;
}
if(sum==t){
if(a[i+1][j]==2&&sum==t){
x=i+1;
y=j;
return true;
}
}
//横着
sum=1;
i=x,j=y;
while(1){
if(a[i][j+1]==tt){
j++;
sum++;
}
else break;
}
ii=x,jj=y;
while(1){
if(a[ii][jj-1]==2&&sum==t){
x=ii;
y=jj-1;
return true;
}
if(a[ii][jj-1]==tt){
jj--;
sum++;
}
else break;
}
if(sum==t){
if(a[i][j+1]==2&&sum==t){
x=i;
y=j+1;
return true;
}
}
//斜着 1
sum=1;
i=x,j=y;
while(1){
if(a[i-1][j-1]==tt){
i--;
j--;
sum++;
}
else break;
}
ii=x,jj=y;
while(1){
if(a[ii+1][jj+1]==2&&sum==t){
x=ii+1;
y=jj+1;
return true;
}
if(a[ii+1][jj+1]==tt){
ii++;
jj++;
sum++;
}
else break;
}
if(sum==t){
if(a[i-1][j-1]==2&&sum==t){
x=i-1;
y=j-1;
return true;
}
}
//斜着 2
sum=1;
i=x,j=y;
while(1){
if(a[i-1][j+1]==tt){
i--;
j++;
sum++;
}
else break;
}
ii=x,jj=y;
while(1){
if(a[ii+1][jj-1]==2&&sum==t){
x=ii+1;
y=jj-1;
return true;
}
if(a[ii+1][jj-1]==tt){
ii++;
jj--;
sum++;
}
else break;
}
if(sum==t){
if(a[i-1][j+1]==2&&sum==t){
x=i-1;
y=j+1;
return true;
}
}
return false;
}
bool find3(){//平局
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]==2) return false;
return true;
}
int getRand(int min,int max){//随机种子函数
return (rand()%(max-min+1))+min;
}
int main(){
srand(time(0));
init();
cout<<"AI再聪明也有失误的时候\n";
cout<<"棋盘大小:";
cin>>n;
cout<<"1.黑子\n";
cout<<"2.白子\n";
int d;
cin>>d;
if(d==1){
cout<<"白子为0,黑子为1,2为空棋位\n下棋时请输入棋的坐标(格式:x y 坐标范围是从1,1到n,n)\n如果一方赢了那他赢得那一行棋都会变成3\n";
while(1){
cout<<" ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
cout<<"\n";
}
if(find3()){
cout<<"平局,游戏结束";
break;
}
//黑子
cout<<"该你了\n";
cin>>x>>y;
while((a[x][y]!=2)||(x>n||y>n||x<1||y<1)){
if(a[x][y]!=2)cout<<"此坐标已有棋子\n";
else if(x>n||y>n||x<1||y<1) cout<<"此坐标不正确\n";
cout<<"该你了\n";
cin>>x>>y;
}
a[x][y]=1;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=a[i][j];
if(find()){
cout<<" ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
cout<<"\n";
}
cout<<"你赢了,游戏结束";
break;
}
cout<<" ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
cout<<"\n";
}
if(find3()){
cout<<"平局,游戏结束";
break;
}
//白子
cout<<"AI思考中...\n";
if(find4(4,1));
else if(find4(3,1));
else if(find4(2,1));
/*else if(find4(4,0));
else if(find4(3,0));
else if(find4(2,0));*/
else{
while((a[x][y]!=2)||(x>n||y>n||x<1||y<1)){
x=x+xd[getRand(0,3)];
y=y+yd[getRand(0,3)];
}
}
cout<<"x:"<<x<<" y:"<<y<<"\n";
a[x][y]=0;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=a[i][j];
if(find2()){
cout<<" ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
cout<<"\n";
}
cout<<"AI赢了,游戏结束";
break;
}
}
cout<<"\n ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",b[i][j]);
cout<<"\n";
}
}
else if(d==2){
int ff=1;
cout<<"白子为0,黑子为1,2为空棋位\n下棋时请输入棋的坐标(格式:x y 坐标范围是从1,1到n,n)\n如果一方赢了那他赢得那一行棋都会变成3\n";
while(1){
cout<<" ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
cout<<"\n";
}
if(find3()){
cout<<"平局,游戏结束";
break;
}
//黑子
cout<<"AI思考中...\n";
if(ff){
ff=0;
x=n/2;
y=n/2;
}
else{
if(find4(4,0));
else if(find4(3,0));
else if(find4(2,0));
/*else if(find4(4,1));
else if(find4(3,1));
else if(find4(2,1));*/
else{
while((a[x][y]!=2)||(x>n||y>n||x<1||y<1)){
x=x+xd[getRand(0,3)];
y=y+yd[getRand(0,3)];
}
}
}
cout<<"x:"<<x<<" y:"<<y<<"\n";
a[x][y]=1;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=a[i][j];
if(find()){
cout<<" ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
cout<<"\n";
}
cout<<"AI赢了,游戏结束";
break;
}
cout<<" ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
cout<<"\n";
}
if(find3()){
cout<<"平局,游戏结束";
break;
}
//白子
cout<<"该你了\n";
cin>>x>>y;
while((a[x][y]!=2)||(x>n||y>n||x<1||y<1)){
if(a[x][y]!=2)cout<<"此坐标已有棋子\n";
else if(x>n||y>n||x<1||y<1) cout<<"此坐标不正确\n";
cout<<"该你了\n";
cin>>x>>y;
}
a[x][y]=0;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=a[i][j];
if(find2()){
cout<<" ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
cout<<"\n";
}
cout<<"你赢了,游戏结束";
break;
}
}
cout<<"\n ";
for(int i=1;i<=n;i++) printf("%3d",i);
cout<<"\n\n";
for(int i=1;i<=n;i++){
printf("%3d",i);
for(int j=1;j<=n;j++) printf("%3d",b[i][j]);
cout<<"\n";
}
}
else cout<<"无效输入";
return 0;
}
文章来源:https://blog.csdn.net/coduck_S12004zbj/article/details/135393584
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!