// Bwfind.cpp: implementation of the CBwfind class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "bw.h"
#include "Bwfind.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBwfind::CBwfind()
{
int i,j;
stepx=-1;
stepy=-1;
depth=6;
searchend=51;
preview=FALSE;
outcome=0;
for(i=0;i<8;i++) for(j=0;j<8;j++)
board[i][j]=EMPTY;
board[3][3]=WHITE;
board[4][4]=WHITE;
board[3][4]=BLACK;
board[4][3]=BLACK;
}
CBwfind::~CBwfind()
{
}
//决策
BOOL CBwfind::decide(int chessx, int chessy, int flag)
{
//判断有效性
if(chessx>7||chessx<0||chessy>7||chessy<0)
return FALSE;
if(board[chessx][chessy]==0)
return judge(board,chessx,chessy,flag);
else
return FALSE;
}
//判断棋子能否落在当前位置
BOOL CBwfind::judge(int tb[][8],int chessx, int chessy, int flag)
{
int x=chessx,y=chessy;
if(tb[x][y]!=0) return FALSE;
//左上
if(x>=2&&y>=2)
if(tb[x-1][y-1]+flag==0){
x=x-2;
y=y-2;
while(x>=0&&y>=0){
if(tb[x][y]==flag) return TRUE;
else if(tb[x][y]==-flag){x--;y--;}
else {x=-1;y=-1;}
}
}
//上
x=chessx;
y=chessy;
if(x>=2)
if(tb[x-1][y]+flag==0){
x=x-2;
while(x>=0){
if(tb[x][y]==flag) return TRUE;
else if(tb[x][y]==-flag) x--;
else {x=-1;}
}
}
//右上
x=chessx;
y=chessy;
if(x>=2&&y<=5)
if(tb[x-1][y+1]+flag==0){
x=x-2;y=y+2;
while(x>=0&&y<=7){
if(tb[x][y]==flag) return TRUE;
else if(tb[x][y]==-flag){x--;y++;}
else {x=-1;}
//x--;y++;
}
}
//右
x=chessx;
y=chessy;
if(y<=5)
if(tb[x][y+1]+flag==0){
y=y+2;
while(y<=7){
if(tb[x][y]==flag) return TRUE;
else if(tb[x][y]==-flag){y++;}
else {y=8;}
//y++;
}
}
//右下
x=chessx;
y=chessy;
if(x<=5&&y<=5)
if(tb[x+1][y+1]+flag==0){
x=x+2;y=y+2;
while(x<=7&&y<=7){
if(tb[x][y]==flag) return TRUE;
else if(tb[x][y]==-flag){x++;y++;}
else {x=8;y=8;}
//x++;y++;
}
}
//下
x=chessx;
y=chessy;
if(x<=5)
if(tb[x+1][y]+flag==0){
x=x+2;
while(x<=7){
if(tb[x][y]==flag) return TRUE;
else if(tb[x][y]==-flag){x++;}
else {x=8;}
//x++;
}
}
//左下
x=chessx;
y=chessy;
if(x<=5&&y>=2)
if(tb[x+1][y-1]+flag==0){
x=x+2;y=y-2;
while(x<=7&&y>=0){
if(tb[x][y]==flag) return TRUE;
else if(tb[x][y]==-flag){x++;y--;}
else {y=-1;}
}
}
//左
x=chessx;
y=chessy;
if(y>=2)
if(tb[x][y-1]+flag==0){
y=y-2;
while(y>=0){
if(tb[x][y]==flag) return TRUE;
else if(tb[x][y]==-flag){y--;}
else {y=-1;}
}
}
return FALSE;
}
void CBwfind::select(int flag)
{
int step,i,j,cut=1000000,tf=flag;
int tb[8][8];
for(i=0;i<8;i++)for(j=0;j<8;j++) tb[i][j]=board[i][j];
step=CountNum();
//开局
if(step<searchend){
int ss=0;
findfirst(tb,ss,tf,cut);
}
//终局
else if(step>=searchend){
preview=TRUE;
outcome=findlast(tb,step,tf,cut);
}
}
int CBwfind::CountBlack()
{
int CB=0;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
if(board[i][j]==1) CB++;
return CB;
}
int CBwfind::CountWhite()
{
int WB=0;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
if(board[i][j]==-1) WB++;
return WB;
}
int CBwfind::CountNum()
{
int Number=0,i,j;
for(i=0;i<8;i++)for(j=0;j<8;j++)
if(board[i][j]!=0) Number++;
return Number;
}
int CBwfind::Total(int tb[][8])
{
int Num=0,i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(tb[i][j]==0) Num++;
return Num;
}
int CBwfind::black(int tb[][8] )
{
int Bn=0,i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(tb[i][j]-1==0) Bn++;
return Bn;
}
int CBwfind::white(int tb[][8] )
{
int Wn=0,i,j;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(tb[i][j]==-1) Wn++;
return Wn;
}
BOOL CBwfind::search(int tb[][8] , int flag)
{
int i,j;
for(i=0;i<8;i++)for(j=0;j<8;j++)
if(judge(tb,i,j,flag)) return TRUE;
return FALSE;
}
int CBwfind::findfirst(int tb[][8] , int &step, int &flag, int cut)
{
int value=0,max=-100000,min=100000,k=0,i,j;
int temp[8][8];
if(step==depth){
return valuefirst(tb);
}
for(i=0;i<8;i++)for(j=0;j<8;j++) temp[i][j]=tb[i][j];
for(i=0;i<8;i++)for(j=0;j<8;j++){
if(judge(tb,i,j,flag)){
int ii,jj;
value=0;
k=1;//存在合理的步子
newboard(tb,i,j,flag);
flag=-flag;
step++;
if(-flag==ComChoice){
LineSense(tb,i,j,value);
Corner(tb,i,j,value);
value+=findfirst(tb,step,flag,max);
}
if(-flag==ManChoice){
Corner(tb,i,j,value);
LineSense(tb,i,j,value);
value+=findfirst(tb,step,flag,min);
}
step--;
flag=-flag;
//恢复棋盘
for(ii=0;ii<8;ii++)
for(jj=0;jj<8;jj++)
tb[ii][jj]=temp[ii][jj];
if(flag-ManChoice==0&&value<cut)
return value;
if(flag-ComChoice==0&&value>cut)
return value;
if(flag-ManChoice==0){
if(value<min){
min=value;
}
}
if(flag-ComChoice==0){
if(value>max){
max=value;
if(step==0){
stepx=i;
stepy=j;
}
}
}
}//if
}//for
//当前没有步可走
if(k==0){
flag=-flag;
if(search(tb,flag)){
value=findfirst(tb,step,flag,cut);
flag=-flag;
return value;
}
else{
flag=-flag;
return valuefirst(tb);
}
}
//存在合理步
else{
if(flag-ManChoice==0) return min;
else if(flag-ComChoice==0){
//if
return max;
}//if
}//else
}
int CBwfind::valuefirst(int tb[][8] )
{
int flag,minus,corner=0,v,ComMobil,ManMobil,DiffMobil,Num,squre;
int danger;
flag=ComChoice;
ComMobil=Mobility(tb,ComChoice);
ManMobil=Mobility(tb,ManChoice);
//DiffMobil=ComMobil*ComMobil-ManMobil*ManMobil;
//a
DiffMobil=ComMobil-ManMobil;
//b
Num=Total(tb);
if(flag==1){
minus=white(tb)-black(tb);
if(minus>=0) squre=minus*minus;
else squre=-minus*minus;
danger=black(tb);
}
if(flag==-1){
minus=black(tb)-white(tb);
if(minus>=0) squre=minus*minus;
else squre=-minus*minus;
danger=white(tb);
}
if(Num<25){
v=minus+2*DiffMobil;
}
else{
v=minus+3*DiffMobil;
}
if(danger==0) v=-10000;
return v;
}
int CBwfind::findlast(int tb[][8], int &step, int &flag, int cut)
{
int value=0,max=-100000,min=100000,k=0,i,j;
int temp[8][8];
if(step==64){
if(ComChoice==1) return(black(tb)-white(tb));
else return(white(tb)-black(tb));
}
for(i=0;i<8;i++)for(j=0;j<8;j++) temp[i][j]=tb[i][j];
for(i=0;i<8;i++)for(j=0;j<8;j++){
if(judge(tb,i,j,flag)){
int ii,jj;
k=1;//存在合理的步子
newboard(tb,i,j,flag);
flag=-flag;
step++;
if(-flag==ComChoice){
value=findlast(tb,step,flag,max);
}
if(-flag==ManChoice){
value=findlast(tb,step,flag,min);
}
step--;
flag=-flag;
//恢复棋盘
for(ii=0;ii<8;ii++)for(jj=0;jj<8;jj++) tb[ii][jj]=temp[ii][jj];
if(flag-ManChoice==0&&value<cut) return value;
if(flag-ComChoice==0&&value>cut) return value;
if(flag-ManChoice==0){
if(value<min){
min=value;
}
}
if(flag-ComChoice==0){
if(value>max){
max=value;
if(step==CountNum()){
stepx=i;
stepy=j;
}
}
}
}//if
}//for
//当前没有步可走
if(k==0){
flag=-flag;
if(search(tb,flag)){
value=findlast(tb,step,flag,cut);
flag=-flag;
return value;
}
else{
flag=-flag;
if(ComChoice==1) return(black(tb)-white(tb));
else return(white(tb)-black(tb));
}
}
//存在合理步
else{
if(flag-ManChoice==0) return min;
if(flag-ComChoice==0){
return max;
}
}
}
void CBwfind::newboard(int tb[][8],int chessx, int chessy, int flag)
{
int x=chessx,y=chessy;
bw.rar_bw源码_othello
版权申诉
135 浏览量
2022-09-21
07:29:06
上传
评论
收藏 30KB RAR 举报
刘良运
- 粉丝: 68
- 资源: 1万+
最新资源
- 写入三菱plcD位寄存器的值
- 确保你的操作系统符合Docker的要求 Docker支持的操作系统包括Ubuntu、Debian、CentOS、Fedora和m
- 确保你的操作系统符合Docker的要求 Docker支持的操作系统包括Ubuntu、Debian、CentOS、Fedora和m
- HDMI 虚拟软件欺骗器
- 确保你的操作系统符合Docker的要求 Docker支持的操作系统包括Ubuntu、Debian、CentOS、Fedora和m
- 读取三菱PLC D位寄存器
- HDMI edid 编辑工具
- 要在你的计算机上安装Docker,你可以按照以下步骤进行:
- 要在你的计算机上安装Docker,你可以按照以下步骤进行:
- html加JavaScript进行表单验证
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈