-1

解决了,stuid bug

function Tform1.Boardtostr(const aboard:Tboard):string; 
var a,b:integer; 
begin 
  result:=''; 
  for a:= 1 to 8 do 
  begin 
    for b:=1 to 8 do 
    begin 
      if board[b][a] = -1 then //<--should be aboard instead of board
        result:=result+'0' 
      else if board[b][a] = 0 then//<--should be aboard instead of board
        result:=result+'1' 
      else if board[b][a] = 1 then //<--should be aboard instead of board
        result:=result+'2'; 
    end; 
  end; 
 end; 

====================================

我是 Sum GNU Anti Reversi 作者。我想为我的 Delphi 程序添加哈希表。我使用的是 Delphi 2010。我在 minmax 函数中添加了哈希表。我不知道那台计算机给出了错误的分数。请帮助我谢谢。原创节目

http://sourceforge.net/projects/antireversi8x8/files/Delphi_v0.6c3_Eng/SumAntiReversi8x8v0.6c3src.zip/download

哈希表版本:

https://sourceforge.net/projects/antireversi8x8/files/Debug-Will%20delete/debuging.zip/download

修改部分:

  aHashTable :TStringHash;
  aHashTable :=TStringHash.Create;

 function TForm1.AI(Aboard:Tboard;ComputerIsRed:Boolean):string; 
 ....
 //   if (a + b < 46) and  (c > 4) and (Realdepth > 5) then
 //   a:=minMaxStart(Aboard,ComputerIsRed,Realdepth,thinkstep)
 // else
 //    a:=minMaxRandom(Aboard,ComputerIsRed,Realdepth,thinkstep);
 a:=minMaxRandom(Aboard,ComputerIsRed,Realdepth,thinkstep);//<-To test hash table
 ...
 function TForm1.MinMaxRandom(Aboard:Tboard;SideIsRed:Boolean;depth:integer;var aithinkstep:string):integer; 

var a,b,c,d,bestvalue,   value:integer;templist:tstringlist;tempboard:Tboard;oldaithinkstep:string;aithinksteplist:Tstringlist; 
begin 
aHashTable.clear; 
Application.ProcessMessages; 
Score(Aboard,a,b);
if a = 0 then
begin
if SideIsRed then
  result:= 2000
else
  result:= -2000; 
exit; 
end; 
if b = 0 then 
begin 
if SideIsRed then
  result:= -2000
else
  result:= 2000;
exit;
end; 
if (depth<=0) or (a+b>63) then 
begin 
    result:= EvaluateScore(Aboard,SideIsRed);  
exit; 
end;
templist := Tstringlist.Create; 
bestvalue:=-INF
if SideIsRed Then
MakeRedMove(Aboard,templist)
else
MakeBlackMove(Aboard,templist);
  if templist.Count = 0 then 
begin 
if SideIsRed Then
  MakeBlackMove(Aboard,templist)
else
  MakeRedMove(Aboard,templist);

if templist.Count = 0 then // both red and black no move
begin
  templist.Free;
    result:= EvaluateScore(Aboard,SideIsRed);
  exit;
end;
result := -MinMax(Aboard,Not SideIsRed,depth,aithinkstep);//);

templist.Free; 
exit; 
end;
...
end;

function TForm1.MinMax(Aboard:Tboard;SideIsRed:Boolean;depth:integer;var aithinkstep:string):integer; 
 var a,b,c,d,bestvalue, value:integer;templist:tstringlist;tempboard:Tboard;oldaithinkstep,bestaithinkstep:string; 
 Application.ProcessMessages; 
 bestaithinkstep:=aithinkstep; 
  Score(Aboard,a,b); 
  if a = 0 then 
  begin 
  if SideIsRed then
    result:= 2000 
  else
   result:= -2000;
exit;
end;
if b = 0 then
begin
  if SideIsRed then
    result:= -2000
 else
   result:= 2000;
 exit;
end;
if (depth<=0) or (a+b>63) or StopThink then
begin
  result:= EvaluateScore(Aboard,SideIsRed);
exit;
end; 
templist := Tstringlist.Create; 
bestvalue:=-INF;
if SideIsRed Then 
  MakeRedMove(Aboard,templist) 
else
  MakeBlackMove(Aboard,templist); 
if templist.Count = 0 then
begin
  if SideIsRed Then
   MakeBlackMove(Aboard,templist)
  else
    MakeRedMove(Aboard,templist);
  if templist.Count = 0 then // both red and black no move
  begin
    templist.Free;
    result:= EvaluateScore(Aboard,SideIsRed);
   exit;
 end;
 aithinkstep := aithinkstep +'->PASS'; 
 result := -MinMax(Aboard,Not SideIsRed,depth,aithinkstep); 
 templist.Free; 
 exit; 
end;
tempboard:=Aboard;
oldaithinkstep :=aithinkstep;
if aHashTable.ValueOf(boardtostr(aboard)+Booltostr(SideIsRed)+inttostr(depth)) = -1 then //Hash test 
begin
For a:= 0 to templist.Count-1 do 
begin 
  Application.ProcessMessages; 
aithinkstep := oldaithinkstep; 
  d:= strtoint(templist[a]); 
  b:= d div 8 +1 ; 
  c:= d mod 8; 
  if c = 0 then 
   begin 
  b:=b-1; 
  c:=8; 
   end; 
 aithinkstep := aithinkstep+'->'+intTostr(c)+','+intTostr(b); 
 Aboard:=tempboard; 
 if SideIsRed Then
 RedboardUpdate(Aboard,strToint(templist[a]))
  else
  BlackboardUpdate(aboard,strToint(templist[a]));
  value:= -MinMax(Aboard,Not SideIsRed,depth-1,aithinkstep);

  if value > bestvalue then
  begin
    bestvalue:=value;
    bestaithinkstep := aithinkstep;
  end;
end;
  aHashTable.Add(boardtostr(tempboard)+Booltostr(SideIsRed)+inttostr(depth),bestvalue);
end
else
  BestValue:= aHashTable.ValueOf(boardtostr(aboard)+Booltostr(SideIsRed)+inttostr(depth)); 
templist.Free; 
aithinkstep :=bestaithinkstep; 
Result:= bestvalue; 
end; 
4

1 回答 1

1

我没有时间下载/检查您的代码,但是如果我对您的理解正确,您正试图通过将已评估的位置存储在哈希表中来加速最小极大搜索,然后首先查看您是否已经拥有对该职位的评估。

如果是这种情况,调试此类问题的方法是拥有一个兼具两者的版本,即进行哈希表存​​储/查找以及完整评估,然后在每个阶段比较结果。这样,当两个结果不同时,您应该能够立即看到,这应该让您了解问题所在。

于 2012-06-08T16:38:16.797 回答