// 负极大值搜索引擎
function CNegamaxEngine.NegaMax(nDepth: Integer): Integer;
var
current, score: Integer;
i, Count: Integer;
MoveType: Byte;
begin
current := -20000; i := IsGameOver(CurPosition, nDepth); // 检查棋局是否结束
if i <> 0 then
begin
// 棋局结束,返回极大/极小值
Result := i;
exit;
end; if nDepth <= 0 then
begin
// 返回估值
Result := m_pEval.Evaluate(CurPosition, ((m_nMaxDepth - nDepth) mod 2)=1);
end
else
begin
// 列举出当前局面下一步所有可能的走法
Count := m_pMG.CreatePossibleMove(CurPosition, nDepth, (m_nMaxDepth - nDepth) mod 2);
for i:=0 to Count-1 do
begin
// 根据走法产生新局面
MoveType := MakeMove(m_pMG.m_MoveList[nDepth][i]); // 递归调用负极大值搜索下一层的节点
score := -NegaMax(nDepth-1); // 恢复当前局面
UnMakeMove(m_pMG.m_MoveList[nDepth][i], MoveType); if score > current then // 如果score大于已知的最大值
begin
current := score; // 修改当前最大值为score
if nDepth = m_nMaxDepth then
begin
// 靠近根部时保存最佳走法
m_cmBestMove := m_pMG.m_MoveList[nDepth][i];
end;
end;
end;
Result := current; // 返回极大值
end;
end;
function CNegamaxEngine.NegaMax(nDepth: Integer): Integer;
var
current, score: Integer;
i, Count: Integer;
MoveType: Byte;
begin
current := -20000; i := IsGameOver(CurPosition, nDepth); // 检查棋局是否结束
if i <> 0 then
begin
// 棋局结束,返回极大/极小值
Result := i;
exit;
end; if nDepth <= 0 then
begin
// 返回估值
Result := m_pEval.Evaluate(CurPosition, ((m_nMaxDepth - nDepth) mod 2)=1);
end
else
begin
// 列举出当前局面下一步所有可能的走法
Count := m_pMG.CreatePossibleMove(CurPosition, nDepth, (m_nMaxDepth - nDepth) mod 2);
for i:=0 to Count-1 do
begin
// 根据走法产生新局面
MoveType := MakeMove(m_pMG.m_MoveList[nDepth][i]); // 递归调用负极大值搜索下一层的节点
score := -NegaMax(nDepth-1); // 恢复当前局面
UnMakeMove(m_pMG.m_MoveList[nDepth][i], MoveType); if score > current then // 如果score大于已知的最大值
begin
current := score; // 修改当前最大值为score
if nDepth = m_nMaxDepth then
begin
// 靠近根部时保存最佳走法
m_cmBestMove := m_pMG.m_MoveList[nDepth][i];
end;
end;
end;
Result := current; // 返回极大值
end;
end;
解决方案 »
- 搜索局域网上的sql server服务器?
- CRC16校验的问题,急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!急!
- 如何得到cxGrid 一列 footer summary显示的内容,并更改它?
- 帮忙!!!菜单问题?
- 如何保存控件属性为文件并能载入动态重建生成控件?(像dfm文件一样)
- 指针的问题,大家给看看
- 怎么单独引用LISTBOX得到的一组数据中的某一项?
- 如何在delphi中用DBCheckbox和SQL Server实现相关联?
- 帮个忙,请PING一下好吗?谢谢!
- 如何获得表中任意一行的值?
- IDHttp上传总出错,提示"http/1.1 404 object not found"错误,请问是怎么回事?如何解决
- 如何在Richedit中贴图片阿?就像QQ的聊天窗口可以发送图片,显示图片阿?
问题在你的current 变量吧,每一次进入这个过程current 都被设置为 -20000了,而不是可能的上次找出来的结果,知道了原因解决方法就简单了:既然NegaMax是一个对象的方法,那就把current 变量作为一个对象成员变量即可[8D]
if nDepth = m_nMaxDepth then
begin
// 靠近根部时保存最佳走法
m_cmBestMove := m_pMG.m_MoveList[nDepth][i];
end;
因为看起来似乎想作一些判断,但并没有改变程序流程和Result结果