// Player.cpp: implementation of the CPlayer class.
//
//////////////////////////////////////////////////////////////////////
#include "Player.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CPlayer::CPlayer()
{
for(int i=0;i<ROWS;i++) // 玩家有禁手时的辅助数据结构 初始化
for(int j=0;j<COLUMNS;j++)
flag[i][j]=ABLE;
}
CPlayer::~CPlayer()
{
}
int CPlayer::SetPlayerName(char *pname)
{
strcpy(m_name,pname);
return 1;
}
int CPlayer:: SetPlayerChess(CChess pchess)
{
m_chess=pchess;
return 1;
}
int CPlayer:: PutonChess() //落棋
{
CDisplay Disp;
PARRAY ChessArray=situation.GetChessArray();
POINT point;
char c;
if(strcmp(m_name,"Computer"))//人下棋
{
int count1=0;
cout<<"请按以下格式输入落棋位置:B6 或者 b6 将在棋盘标号为6的行和标号为B的列下棋"<<endl;
do{ int count2=0;
if(count2>0)
cout<<"已有棋子,请重新落子!!"<<"请按以下格式输入落棋位置:B6 或者 b6 将在棋盘标号为6的行和标号为B的列下棋"<<endl;;
do{
if(count1>0)
cout<<"输入错误!!请重新输入:"<<endl;
cin>>c>>point.x;
count1++;
}while(!(c>='A'&&c<='A'+COLUMNS||c>='a'&&c<='o'+COLUMNS)||point.x<=0||point.x>ROWS);//保证输入不越界
if(c>='a')
point.y=c-'a';
else
point.y=c-'A';
point.x--;
count2++;
}while(ChessArray[point.x][point.y]!=BLANK);//防止在已经落子的地方再次落子
}
else
point= Optimization( ); //计算机下棋
c=point.y+'A';
situation.SetChessArray(point,m_chess);
system("cls");
Disp.DisplaySituationOnBoard( m_chess);
cout<<c<<point.x+1 <<endl;
return OK;
}
POINT CPlayer:: Optimization( )
{
int situat[2][15][15][8][2]={0}; /* 棋型数组*/
int k,i,j,q,b=0,a=1,d,y1=0,y2=0,x1=0,x2=0;
int a1[15][15]={0},a2[15][15]={0};
PARRAY ChessArray=situation.GetChessArray();
POINT optpoint;
int t,h;
static int cccc=0;
if(cccc==0)
{
optpoint.x=7;
optpoint.y=7;
cccc++;
return optpoint;
}
/****************为双方填写棋型表************/
for(k=0;k<2;k++)
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
if(ChessArray[i][j]==0)
{
for(q=0;q<8;q++)
{
if(k==0) d=1;
else d=2;
if(q==0&&j>=0)
{
for(;j-a>=0;)
{
if(ChessArray[i][j-a]==d){b++;a++;continue;}
else break;
}
situat[k][i][j][q][0]=b;b=0;
if(ChessArray[i][j-a]==0&&j-a>=0){situat[k][i][j][q][1]=1;a=1;}
else {situat[k][i][j][q][1]=0;a=1;}
}
if(q==1&&i>=0&&j>=0)
{
for(;i-a>=0&&j-a>=0;)
{
if(ChessArray[i-a][j-a]==d){b++;a++;continue;}
else break;
}
situat[k][i][j][q][0]=b;b=0;
if(ChessArray[i-a][j-a]==0&&j-a>=0&&i-a>=0){situat[k][i][j][q][1]=1;a=1;}
else {situat[k][i][j][q][1]=0;a=1;}
}
if(q==2&&i>=0)
{
for(;i-a>=0;)
{
if(ChessArray[i-a][j]==d){b++;a++;continue;}
else break;
}
situat[k][i][j][q][0]=b;b=0;
if(ChessArray[i-a][j]==0&&i-a>=0){situat[k][i][j][q][1]=1;a=1;}
else {situat[k][i][j][q][1]=0;a=1;}
}
if(q==3&&i>=0&&j<15)
{
for(;i-a>=0&&j+a<15;)
{
if(ChessArray[i-a][j+a]==d){b++;a++;continue;}
else break;
}
situat[k][i][j][q][0]=b;b=0;
if(ChessArray[i-a][j+a]==0&&i-a>=0&&j+a<15){situat[k][i][j][q][1]=1;a=1;}
else {situat[k][i][j][q][1]=0;a=1;}
}
if(q==4&&j<15)
{
for(;j+a<15;)
{
if(ChessArray[i][j+a]==d){b++;a++;continue;}
else break;
}
situat[k][i][j][q][0]=b;b=0;
if(ChessArray[i][j+a]==0&&j+a<15){situat[k][i][j][q][1]=1;a=1;}
else {situat[k][i][j][q][1]=0;a=1;}
}
if(q==5&&i<15&&j<15)
{
for(;i+a<15&&j+a<15;)
{
if(ChessArray[i+a][j+a]==d){b++;a++;continue;}
else break;
}
situat[k][i][j][q][0]=b;b=0;
if(ChessArray[i+a][j+a]==0&&i+a<15&&j+a<15){situat[k][i][j][q][1]=1;a=1;}
else {situat[k][i][j][q][1]=0;a=1;}
}
if(q==6&&i<15)
{
for(;i+a<15;)
{
if(ChessArray[i+a][j]==d){b++;a++;continue;}
else break;
}
situat[k][i][j][q][0]=b;b=0;
if(ChessArray[i+a][j]==0&&i+a<15){situat[k][i][j][q][1]=1;a=1;}
else {situat[k][i][j][q][1]=0;a=1;}
}
if(q==7&&j>=0&&i<15)
{
for(;i+a<15&&j-a>=0;)
{
if(ChessArray[i+a][j-a]==d){b++;a++;continue;}
else break;
}
situat[k][i][j][q][0]=b;b=0;
if(ChessArray[i+a][j-a]==0&&i+a<15&&j-a>=0){situat[k][i][j][q][1]=1;a=1;}
else {situat[k][i][j][q][1]=0;a=1;}
}
}
}
}
/******************根据评分规则对每一个空格评分***************/
for(k=0;k<2;k++)
for(i=0;i<15;i++)
for(j=0;j<15;j++)
{
if(k==0) /*为白棋评分*/
{
for(q=0;q<4;q++)
{
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==4
&&situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==1)
b+=7000;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==3
&&situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==1)
b+=301;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==2
&&situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==1)
b+=43;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==1
&&situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==1)
b+=11;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==4
&&((situat[k][i][j][q+4][1]==0)
||(situat[k][i][j][q][1]==0)))
b+=7000;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==3
&&((situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==0)
||(situat[k][i][j][q][1]==0&&situat[k][i][j][q+4][1]==1)))
b+=63;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==2
&&((situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==0)
||(situat[k][i][j][q][1]==0&&situat[k][i][j][q+4][1]==1)))
b+=6;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==1
&&((situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==0)
||(situat[k][i][j][q][1]==0&&situat[k][i][j][q+4][1]==1)))
b+=1;
}
if(b==126||b==189||b==252) b=1500;
if(b==106) b=1000;
a1[i][j]=b;b=0;
}
if(k==1) /*为黑棋评分*/
{
for(q=0;q<4;q++)
{
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==4
&&situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==1)
b+=30000;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==3
&&situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==1)
b+=1500;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==2
&&situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==1)
b+=51;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==1
&&situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==1)
b+=16;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==4
&&((situat[k][i][j][q+4][1]==0)
||(situat[k][i][j][q][1]==0)))
b+=30000;
if((situat[k][i][j][q][0]+situat[k][i][j][q+4][0])==3
&&((situat[k][i][j][q][1]==1&&situat[k][i][j][q+4][1]==0)
||(situat[k][i][j][q][1]==0&&situat[k][i][j][q+4][1]==1)))