机器博奕程序是否可以用多核
理论上觉得可以,就是不好改造
如下面的代码,实递归的,怎样处理?
// NegaScout_TT_HH.cpp: implementation of the CNegaScout_TT_HH class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NegaScout_TT_HH.h"
#include "GClog.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNegaScout_TT_HH::CNegaScout_TT_HH()
{
}
CNegaScout_TT_HH::~CNegaScout_TT_HH()
{
}
int CNegaScout_TT_HH::SearchAGoodMove(char position[][GRID_NUM],int type)
{
if(m_pMG->ModeFlag==2)
{
EveFlag=1;
}
else
{
EveFlag=2;
}
int iRet=0,i,j;
bool RandTrue=false,NoStone=false;
m_cmBestMove.stonePos.x=NOSTONE;
m_cmBestMove.stonePos.y=NOSTONE;
m_BackMove.stonePos.x=NOSTONE;
m_BackMove.stonePos.y=NOSTONE;
//如果局面均衡,且没有脱先,随机采用一个走法,返回
if(RandPtr==0)
{
RandPtr=GetTickCount();
}
RandPtr++;
int Count;
m_nMaxDepth=m_nSearchDepth;
CalculateInitHashKey(m_pMG->CurPosition);
m_pMG->ResetHistoryTable();
if(PreMove[MovePoint].stonePos.x==m_pMG->iPosX
&& PreMove[MovePoint].stonePos.y==m_pMG->iPosY && MovePoint<m_nMaxDepth-1 && m_nMaxDepth>2)
{
MovePoint++;
m_cmBestMove=PreMove[MovePoint];
MovePoint++;
iRet=m_pMG->IsValidStone(m_pMG->CurPosition,m_cmBestMove.stonePos.x,
m_cmBestMove.stonePos.y,type);
}
if(iRet!=1)
{
if(type==WHITE)
{
iUserColor=BLACK;
}
else
{
iUserColor=WHITE;
}
RandTrue=false;
//如果局面有利
NoStone=false;
for(i=0;i<GRID_NUM;i++)
{
for(j=0;j<GRID_NUM;j++)
{
if(m_pMG->CurPosition[i][j]!=NOSTONE)
{
NoStone=false;
break;
}
}
if(!NoStone)
{
break;
}
}
if(NoStone)
{//棋盘上没有棋子
RandTrue=true;
Count=1;
m_pMG->m_MoveList[m_nMaxDepth][0].stonePos.x=3;
m_pMG->m_MoveList[m_nMaxDepth][0].stonePos.y=3;
}
if(RandTrue)
{
RegCount=Count;
}
else
{
RegCount=0;
}
if(m_pMG->ModeFlag!=2 && MovePoint<m_nMaxDepth && m_pMG->Step>1)
{
int TmpX,TmpY;
for(i=MovePoint;i<m_nMaxDepth;i++)
{
TmpX=PreMove[i].stonePos.x;
TmpY=PreMove[i].stonePos.y;
int iRet=m_pMG->IsValidStone(m_pMG->CurPosition,TmpX,
TmpY,type);
if(iRet==1)
{
m_pMG->EnterHistoryScore(&PreMove[i],m_nMaxDepth-i,type);
}
}
}
MovePoint=1;
timecount=GetTickCount();//取当前时间
for(i=0;i<m_nMaxDepth;i++)
{
PreMove[i].stonePos.x=NOSTONE;
PreMove[i].stonePos.y=NOSTONE;
}
m_pMG->SaveAll();
NegaScout(m_nMaxDepth,-MAXSCORE,MAXSCORE,type);
if(MovePoint==MAXDEPTH)
{
//time out
m_cmBestMove=m_BackMove;
}
m_pMG->RestoreAll();
}
iRet=m_pMG->IsValidStone(m_pMG->CurPosition,m_cmBestMove.stonePos.x,
m_cmBestMove.stonePos.y,type);
if(iRet==1)
{
m_pMG->MakeMove(&m_cmBestMove,type);
}
else
{
m_cmBestMove=m_BackMove;
iRet=m_pMG->IsValidStone(m_pMG->CurPosition,m_cmBestMove.stonePos.x,
m_cmBestMove.stonePos.y,type);
if(iRet==1)
{//保证合法
m_pMG->MakeMove(&m_cmBestMove,type);
}
}
memcpy(position,m_pMG->CurPosition,GRID_COUNT);
return iRet;
}
int CNegaScout_TT_HH::NegaScout(int depth, int alpha, int beta,BYTE Color)
{
int Count,i;
BYTE bType;
if(Color==WHITE)
{
bType=BLACK;
}
else
{
bType=WHITE;
}
int a,b,t;
int score;
if(GetTickCount()-timecount>(unsigned)TimeOut)
{
if(MovePoint==1)
{
m_BackMove=PreMove[0];
if(m_BackMove.stonePos.x==NOSTONE)
{
m_BackMove=m_pMG->m_MoveList[m_nMaxDepth][0];
}
MovePoint=MAXDEPTH;
}
return MAXSCORE;
}
i=IsGameStatic(depth,m_nMaxDepth,Color);
if(i!=MAXSCORE*2)
{
//产生截断,后面的预测都不准确,放弃
return i;
}
//计算当前节点的类型,极大 0/极小 1
score=LookUpHashTable(alpha,beta,depth,(m_nMaxDepth-depth)&1,&m_BackMove.stonePos);
if(score!=MAXSCORE+1)
return score;
if(depth<=0)//叶子节点取估值
{
score=m_pEval->Eveluate(m_pMG,(m_nMaxDepth-depth)&1,iUserColor,EveFlag);
EnterHashTable(exact,score,depth,(m_nMaxDepth-depth)&1,m_pMG->PosLog[m_pMG->Step-1]);//将估值存入置换表
return score;
}
if(RegCount!=0)
{
Count=RegCount;
RegCount=0;
}
else
{
Count=m_pMG->CreatePossibleMove(depth,Color);
}
if(depth==m_nMaxDepth)
{
//在根节点设定进度条
StatusCount=1;
AllCount=Count;
(*gCallBack)(StatusCount,AllCount);
if(Count==1)
{//只有1手,不搜索了
m_BackMove=m_pMG->m_MoveList[depth][0];
MovePoint=MAXDEPTH;
return MAXSCORE;
}
}
int bestmove=-1;
a=alpha;
b=beta;
int eval_is_exact=0;
for(i=0;i<Count;i++)
{
if(depth==m_nMaxDepth)
{
StatusCount++;//走进度条
(*gCallBack)(StatusCount,0);
}
Hash_MakeMove(&m_pMG->m_MoveList[depth][i],Color);//产生该走法所对应子节点的哈希值
m_pMG->MakeMove(&m_pMG->m_MoveList[depth][i],Color); //产生子节点
t=-NegaScout(depth-1,-b,-a,bType);
if(t>a && t<beta && i>0)
{
//对于第一个后的节点,如果上面的搜索 failhigh
a=-NegaScout(depth-1,-beta,-t,bType); /* re-search */
eval_is_exact=1;//设数据类型为精确值
if(depth==m_nMaxDepth)
{
m_cmBestMove=m_pMG->m_MoveList[depth][i];
PreMove[0]=m_cmBestMove;
}
bestmove=i;
PreMove[m_nMaxDepth-depth]=m_pMG->m_MoveList[depth][i];
}
Hash_UnMakeMove(&m_pMG->m_MoveList[depth][i],Color);//恢复当前节点的哈希值
m_pMG->UnMakeMove(); //撤销子节点
if(a<t)
{
eval_is_exact=1;
a=t;
if(depth==m_nMaxDepth)
{
m_cmBestMove=m_pMG->m_MoveList[depth][i];
PreMove[0]=m_cmBestMove;
}
PreMove[m_nMaxDepth-depth]=m_pMG->m_MoveList[depth][i];
}
if(a>= beta)
{
EnterHashTable(lower_bound,a,depth,(m_nMaxDepth-depth)&1,m_pMG->m_MoveList[depth][i].stonePos);
m_pMG->EnterHistoryScore(&m_pMG->m_MoveList[depth][i],depth,Color);
return a;
}
b=a+1; /* set new null window */
}
if(bestmove!=-1)
{
m_pMG->EnterHistoryScore(&m_pMG->m_MoveList[depth][bestmove],depth,Color);
}
if (eval_is_exact)
{
if(bestmove!=-1)
{
EnterHashTable(exact,a,depth,(m_nMaxDepth-depth)&1,m_pMG->m_MoveList[depth][bestmove].stonePos);
}
else
{
EnterHashTable(exact,a,depth,(m_nMaxDepth-depth)&1,m_pMG->m_MoveList[depth][i-1].stonePos);
}
}
else
{//如果不命中,bestmove=-1,所以选择i做输入数据
EnterHashTable(upper_bound,a,depth,(m_nMaxDepth-depth)&1,m_pMG->m_MoveList[depth][i-1].stonePos);
}
return a;
}