?? falphabetaengine.cpp
字號:
// FAlphaBetaEngine.cpp: implementation of the CFAlphaBetaEngine class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "chess.h"
#include "FAlphaBetaEngine.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFAlphaBetaEngine::CFAlphaBetaEngine()
{
}
CFAlphaBetaEngine::~CFAlphaBetaEngine()
{
}
CFAlphaBetaEngine::SearchAGoodMove(BYTE position[][9])
{
memcpy(CurPosition,position,90); //復制傳入的棋盤
m_nMaxDepth=m_nSearchDepth; //設定搜索深度
FAlphaBeta(m_nMaxDepth,-20000,20000);//調用fail-softalpha-beta
m_umUndoMove.cmChessMove=m_cmBestMove;
m_umUndoMove.nChessID=MakeMove(&m_cmBestMove);
memcpy(position,CurPosition,90); //將走過的棋盤傳出
}
int CFAlphaBetaEngine::FAlphaBeta(int nDepth,int alpha,int beta)
{
int current=-20000;//current初始為極小值
int score;
int Count,i;
BYTE type;
i=IsGameOver(CurPosition,nDepth);//檢查此節點游戲是否結束
if(i!=0)
return i;//此節點游戲結束,返回極大/極小值
//如果是葉子節點取估值
if(nDepth<=0)
return m_pEval->Eveluate(CurPosition,(m_nMaxDepth-nDepth)%2,m_nUserChessColor);
Count=m_pMG->CreatePossibleMove(CurPosition,nDepth,(m_nMaxDepth-nDepth)%2,m_nUserChessColor);
if(nDepth==m_nMaxDepth)
{
//在根節點設定進度條
m_pThinkProgress->SetRange(0,Count);
m_pThinkProgress->SetStep(1);
}
for(i=0;i<Count;i++)
{
if(nDepth==m_nMaxDepth)
m_pThinkProgress->StepIt();//走進度條
type=MakeMove(&m_pMG->m_MoveList[nDepth][i]);
score=-FAlphaBeta(nDepth-1,-beta,-alpha);
UnMakeMove(&m_pMG->m_MoveList[nDepth][i],type);
if(score>current)
{
current=score;//保留最大值
//當靠近根節點時記錄最佳走法
if(nDepth==m_nMaxDepth)
m_cmBestMove=m_pMG->m_MoveList[nDepth][i];
if(score>=alpha)
alpha=score;//修改alpha邊界
if(score>=beta)
break;//beta剪枝
}
}
return current;//返回極大值或邊界值
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -