?? negamaxengine.cpp
字號:
// NegaMaxEngine.cpp: implementation of the CNegaMaxEngine class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Chess.h"
#include "NegaMaxEngine.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNegaMaxEngine::CNegaMaxEngine()
{
}
CNegaMaxEngine::~CNegaMaxEngine()
{
}
CNegaMaxEngine::SearchAGoodMove(BYTE position[][9])
{
memcpy(CurPosition,position,90);//將傳入的棋盤復制到成員變量中
m_nMaxDepth=m_nSearchDepth; //設定搜索層數
NegaMax(m_nMaxDepth); //調用負極大值搜索函數找最佳走法
m_umUndoMove.cmChessMove=m_cmBestMove;
m_umUndoMove.nChessID=MakeMove(&m_cmBestMove);
//將棋盤修改為已走過的
memcpy(position,CurPosition,90);//將修改后的棋盤復制到傳入的棋盤中,傳去
}
int CNegaMaxEngine::NegaMax(int nDepth)
{
int current=-20000;
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=-NegaMax(nDepth-1); //遞歸調用負極大值搜索下一層節點
UnMakeMove(&m_pMG->m_MoveList[nDepth][i],type); //恢復當前局面
if(score>current) //如果score大于已知的最大值
{
current=score; //修改當前最大值為score
if(nDepth==m_nMaxDepth)
m_cmBestMove=m_pMG->m_MoveList[nDepth][i];//靠近根部時保存最佳走法
}
}
return current;//返回極大值
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -