// 负极大值搜索引擎
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;

解决方案 »

  1.   

    来自:thx1180, 时间:2005-4-30 14:03:47, ID:3061766 | 编辑
    问题在你的current 变量吧,每一次进入这个过程current 都被设置为 -20000了,而不是可能的上次找出来的结果,知道了原因解决方法就简单了:既然NegaMax是一个对象的方法,那就把current 变量作为一个对象成员变量即可[8D]
      

  2.   

    看看这一段有没有问题
            if nDepth = m_nMaxDepth then
            begin
              // 靠近根部时保存最佳走法
              m_cmBestMove := m_pMG.m_MoveList[nDepth][i];
            end;
    因为看起来似乎想作一些判断,但并没有改变程序流程和Result结果