解决了,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 函数中添加了哈希表。我不知道那台计算机给出了错误的分数。请帮助我谢谢。原创节目
哈希表版本:
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;