-1

Solved ,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; 

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

I am Sum GNU Anti Reversi Author. I would like add Hash table for my Delphi program.I am using Delphi 2010. I add hash table in minmax function.I don't know that computer give wrong score.Please help me Thanks. Original program

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

Hash table version:

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

Modified part :

  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; 
AstroCB
  • 12,337
  • 20
  • 57
  • 73
  • 2
    Don't let it put you down but honestly, that code is in need of some serious refactoring before you can expect anyone to look into it. I was curious enough to diff your original and modified version but there's a lot more changed than what you post here. I'm not curious enough to try to determine if all those changes are relevant to your problem or not. – Lieven Keersmaekers Jun 08 '12 at 15:09
  • Maybe I only ask the correct way to use hash table in minmax is enough... – user1444684 Jun 08 '12 at 16:05
  • +1 on finding and posting the solution. – Lieven Keersmaekers Jun 10 '12 at 08:08

1 Answers1

1

I won't have time to download/inspect your code, but if I'm understanding you correctly you are attempting to speed up a minimax search by storing already evaluated positions in a hash table and then by first looking to see if you already have an evaluation for that position.

If that's the case, the way to debug such issues is to have a version that does both, i.e. does the hash table store/lookup as well as the full evaluation and then compares the results at every stage. That way you should be able to see immediately when the two results differ and that should give you some idea what the problem is.