?? 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()
{
}
//此函數針對傳入的position找出一步最佳走法
//并修改棋盤數據為走過的狀態
void CNegaMaxEngine::SearchAGoodMove(BYTE position[10][9],int IsRed)
{
memcpy(CurPosition, position, 90);//將傳入的棋盤復制到成員變量中
m_nMaxDepth = m_nSearchDepth;//設定搜索層數為m_nSearchDepth
NegaMax(m_nMaxDepth,IsRed);//調用負極大值搜索函數找最佳走法
MakeMove(&m_cmBestMove);//將棋盤修改為走過的
memcpy(position, CurPosition, 90);//將修改過的棋盤復制到傳入的棋盤中,傳出
}
//負極大值搜索函數
//depth表示節點離葉子節點的層數
int CNegaMaxEngine::NegaMax(int depth,int IsRed)
{
int current = -20000 ;
int score;
int Count,i;
BYTE type;
i = IsGameOver(CurPosition, depth + IsRed);//檢查棋局是否結束
if (i != 0)
return i; //棋局結束,返回極大/極小值
if (depth <= 0) //葉子節點取估值 到底是1還是0?????
return m_pEval->Eveluate(CurPosition, (m_nMaxDepth-depth+IsRed)%2);
//列舉出當前局面下一步所有可能的走法
Count = m_pMG->CreatePossibleMove(CurPosition, depth, (m_nMaxDepth-depth+IsRed)%2);
for (i=0;i<Count;i++)
{
type = MakeMove(&m_pMG->m_MoveList[depth][i]);
score = -NegaMax(depth - 1,IsRed);
UnMakeMove(&m_pMG->m_MoveList[depth][i],type);
if (score > current)
{
current = score;
if(depth == m_nMaxDepth)
{
m_cmBestMove = m_pMG->m_MoveList[depth][i];
}
}
}
return current;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -