#include "ChessAI.h"
DirResult operator+(DirResult u, DirResult v)
{
return DirResult(u.total + v.total, u.numOfEmpty + v.numOfEmpty);
}
bool operator==(DirResult u, DirResult v)
{
return (u.total == v.total&& u.numOfEmpty == v.numOfEmpty);
}
ChessAI::ChessAI(ChessState *p)
{
m_pState=p;
}
ChessAI::~ChessAI(void)
{
}
void ChessAI::AddResult(vector<DirResult*> &old,vector<DirResult*> &add )
{
int n=add.size();
for(int i=0;i<n;i++)
old.push_back(add[i]);
}
int ChessAI::GetPower(int who)
{
int rt=0;
int i,j,n=GRID_LINES-1;
vector<DirResult*> data[4];
for(i=0;i<GRID_LINES;i++)
{
AddResult(data[0],Analyse(0,i,1,0,who));//横向检查 -
AddResult(data[1],Analyse(i,0,0,1,who));//纵向检查 |
}
for(i=0;i<n;i++)
{
AddResult(data[2],Analyse(0,i,1,1,who));// 左斜
if(i>0)
{
AddResult(data[2],Analyse(i,0,1,1,who));
}
AddResult(data[3],Analyse(i,n,1,-1,who));
if(i>0)
{
AddResult(data[3],Analyse(0,i,1,-1,who));//右斜
}
}
int f5=0,f4e2=0,f4e1=0,f4e0=0,f3e2=0,f3e1=0,f3e0=0,f2e2=0,f2e1=0,f2e0=0;;
for(i=0;i<4;i++)
{
vector<DirResult*> list=data[i];
n=list.size();
for(j=0;j<n;j++)
{
DirResult* r=list[j];
if(r->total==5)
f5++;
else if(r->total==4)
{
if(r->numOfEmpty==2)
f4e2++;
else if(r->numOfEmpty==1)
f4e1++;
else f4e0++;
}
else if(r->total==3)
{
if(r->numOfEmpty==2)
f3e2++;
else if(r->numOfEmpty==1)
f3e1++;
else f3e0++;
}
if(r->total==2)
{
if(r->numOfEmpty==2)
f2e2++;
else if(r->numOfEmpty==1)
f2e1++;
else f2e0++;
}
}
}
if(f5>0)
{
rt=10000;
}
else if(f4e2>0)
{
rt=9000;
}
else if(f3e2>1)
{
rt=8000;
}
else
{
rt=f4e1*600+f4e0*10;
rt+=f3e2*1000+f3e1*50+f3e0*10;
rt+=f2e2*100+f2e1*50+f2e0*10;
if(rt>7999) rt=7999;
}
for(i=0;i<4;i++)
{
n=(int)(data[i].size());
for(j=0;j<n;j++)
{
delete data[i][j];
}
data[i].clear();
}
return rt;
}
vector<DirResult*> ChessAI::Analyse(int col,int raw,int dx,int dy,int who)
{
vector<DirResult*> rt;
DirResult* r=NULL;
int n=GRID_LINES-1;
int w1,w2;
int x=col,y=raw,oldx,oldy;
while(1)
{
oldx=x;oldy=y;
w1=GetFlag(oldy,oldx);
x+=dx;y+=dy;
if(0<=x&&x<=n&&0<=y&&y<=n)
{
w2=GetFlag(y,x);
if(w1==who)
{
if(r==NULL) r=new DirResult;
r->total++;
if(w2!=who)
{
if(w2==0) r->numOfEmpty++;
if(r->total>1)
rt.push_back(r);
r=NULL;
}
}
else if(w1==0&&w2==who)
{
if(r==NULL)
r=new DirResult;
r->numOfEmpty++;
}
}
else
{
if(r!=NULL&&w1==who&&r->total>1)
rt.push_back(r);
break;
}
}
return rt;
}
Chessman* ChessAI::Exploer()
{
int maxPower1=0,maxPower2=0,index,t1,t2;
Chessman *temp=new Chessman;
Chessman rt1,rt2;
temp->who=2;rt1.who=rt2.who=2;
for (int i=m_pState->top;i<=m_pState->bottom;i++)
{
for (int j=m_pState->left;j<=m_pState->right;j++)
{
index=i*GRID_LINES+j;
if(m_pState->flags[index]!=0) continue;
temp->row=i;temp->col=j;
m_pState->flags[index]=2;
t1=GetPower(2);
if(maxPower1<t1)
{
maxPower1=t1;
rt1.row=temp->row;rt1.col=temp->col;
}
m_pState->flags[index]=1;
t2=GetPower(1);
if(maxPower2<t2)
{
maxPower2=t2;
rt2.row=temp->row;rt2.col=temp->col;
}
m_pState->flags[index]=0;
}
}
if(maxPower1>9999)
{
temp->row= rt1.row;
temp->col= rt1.col;
}
else if (maxPower2>9999)
{
temp->row= rt2.row;
temp->col= rt2.col;
}
else if(maxPower2>maxPower1*1.5f)
{
temp->row= rt2.row;
temp->col= rt2.col;
}
else
{
temp->row= rt1.row;
temp->col= rt1.col;
}
return temp;
}