C++实现九宫格游戏人机对战

九宫格游戏是大家熟悉的“草稿纸”游戏。记得曾经经常和同学在草稿纸上画着玩。规则很简单,就是在3*3的格子里双方交替落子,先连成3个的(横竖斜都行)一方获胜。

实现人机对战我主要分成两大类,一是人先下,二是电脑先下。期间主要运用九宫格的对称性以及等价位置来画博弈树来实现决策。

大致四个决策顺序:

1.看自己是否连成两个。2.看对方是否连成两个。3.特殊情况讨论。4.已是绝对平局情况,找到第一个空的位置落子。

//main.cpp//

#include<cstring>
#include<cstdlib>

#include"gongju.h"
#include"huagezi.h"
//#include"shuchu.h"
#include"renren.h"
#include"renji.h"
#include"jiren.h"
char s[10][10];
int main()
{   
    char F='1',xuanze;
    char (*p)[10];
    p=s;
    system("color f1"); 
    
    while(F!='0')
    {///
    
xuanze='0';
    system("cls");
cout<<"*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*九 宫 格*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n\n\n";

huagezi(p);
//s[2][2] = s[4][4] = s[2][6] = 'O';
//s[4][2] = s[6][2] = s[6][6] = 'X';
shuchu(p);

while(xuanze!='1' && xuanze!='2'){

cout<<"请选择模式(输入1表示人人对战,输入2表示人机对战):";
cin>>xuanze;
if(xuanze=='1'){//人人模式 


while(F!='0'){ //hehe

huagezi(p);
system("cls");
   cout<<"*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*九 宫 格*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n\n\n";
   cout<<"                                   人----人"<<endl;
   shuchu(p);

   renren(p);
   
   while(F!='0'){//
cout<<"输入0结束游戏 输入1重玩 输入2重选模式:";
   cin>>F; 
   if(F!='0' && F!='1' && F!='2') {cout<<"输入错误!\n";F='1';}
   else if(F=='1' || F=='2') break;
}/

   if(F=='2') break;   //重选模式 
   
   
}         //hehe

}///人人if的括号 

else if(xuanze=='2')  //人机模式
{
xuanze='0';
system("cls");
        cout<<"*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*九 宫 格*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n\n\n";
   //cout<<"                                   人----机"<<endl;
   shuchu(p);



while(xuanze!='1' && xuanze!='2'){
cout<<"输入1人先落子 输入2电脑先落子:";
cin>>xuanze;
if(xuanze == '1'){//f1
   
   while(F!='0'){//f2
   
    huagezi(p);
   system("cls");
   cout<<"*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*九 宫 格*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n\n\n";
   
   cout<<"                                   人----机"<<endl;
   shuchu(p);
   
    renji(p); 
   
    while(F!='0'){//
cout<<"输入0结束游戏 输入1重玩 输入2重选模式:";
   cin>>F; 
   if(F!='0' && F!='1' && F!='2') {cout<<"输入错误!\n";F='1';}
   else if(F=='1' || F=='2') break;
}/

   if(F=='2') break;   //重选模式 
   
   
    }//f2
   
} //人先下的if括号 f1

else if(xuanze == '2'){//电脑先下的if括号


   while(F!='0'){//f2
   
    huagezi(p);
   system("cls");
   cout<<"*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*九 宫 格*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*^*\n\n\n";
   
   cout<<"                                   人----机"<<endl;
   shuchu(p);
   
    jiren(p); 
   
    while(F!='0'){//
cout<<"输入0结束游戏 输入1重玩 输入2重选模式:";
   cin>>F; 
   if(F!='0' && F!='1' && F!='2') {cout<<"输入错误!\n";F='1';}
   else if(F=='1' || F=='2') break;
}/

   if(F=='2') break;   //重选模式 
   
   
    }//f2
   

} //电脑先下的if括号

else{
cout<<"输入错误!\n";///


}//小 xuanze != 1 || 2

}  //人机的if的括号 

else cout<<"输入错误!\n"; 

}//大xuanze != 1 || 2

} //大while 
   
return 0;


//gongju.h//

#include<iostream>
#include<cstdlib>
using namespace std;


void print(char (*p)[10],int f)
{
if(!f)
cout<<"                                   人----人"<<endl;
else if(f==1)
cout<<"                                   人----机"<<endl;
else 
cout<<"                                   机----人"<<endl;

for(int i=1;i<=7;i++){
for(int I=0;I<15;I++)
cout<<"  ";
cout<<"     ";
for(int j=1;j<=7;j++)
cout<<(*(*(p+i)+j));
cout<<endl;
}
cout<<"\n\n\n"; 
//return 1;
}


int suiji()
{
    srand(time(NULL));
    int i;
     
    i=rand()%3;
    return i;
}


int panduan(char (*p)[10],int x,int y,int f ,char Sa[])

    int c1,c2,c3,c4;
    c1=c2=c3=c4=0;
//横方向 
if((y*2+2)<=6) {if( ( *(*( p+(x*2) )+ (y*2+2) ) ) == Sa[f]) ++c1;}
if((y*2-2)>=2) {if( ( *(*( p+(x*2) )+ (y*2-2) ) ) == Sa[f]) ++c1; if(c1>=2) return 1;}
if((y*2-4)>=2) {if( ( *(*( p+(x*2) )+ (y*2-4) ) ) == Sa[f]) ++c1; if(c1>=2) return 1;}
if((y*2+4)<=6) {if( ( *(*( p+(x*2) )+ (y*2+4) ) ) == Sa[f]) ++c1; if(c1>=2) return 1;}
//竖方向 
if((x*2+2)<=6) {if( ( *(*( p+(x*2+2) )+ (y*2) ) ) == Sa[f]) ++c2;}
if((x*2-2)>=2) {if( ( *(*( p+(x*2-2) )+ (y*2) ) ) == Sa[f]) ++c2; if(c2>=2) return 1;}
if((x*2-4)>=2) {if( ( *(*( p+(x*2-4) )+ (y*2) ) ) == Sa[f]) ++c2; if(c2>=2) return 1;}
if((x*2+4)<=6) {if( ( *(*( p+(x*2+4) )+ (y*2) ) ) == Sa[f]) ++c2; if(c2>=2) return 1;}
//右斜方向
if((x*2+2)<=6 && (y*2+2)<=6) {if( ( *(*( p+(x*2+2) )+ (y*2+2) ) ) == Sa[f]) ++c3;}
if((x*2+4)<

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值