Now my positionhash returns complete nonsense when implementing it in
my AlphaBeta search. I call AlphaBeta with a certain depth and when it gets to zero it's ready.
Below I attached a stripped version of my AlphaBeta and my Hash.Probe.
Could anyone please check the code if the probing and storing is allright?
I have my doubts about the Hash.Probe (if Hash.Depth < aDepth).
Without the hash the engine plays allright, with the hash it just produces nonsense so there must be some severe error.
Code: Select all
function AlphaBeta(Depth, Alpha, Beta: Integer): Integer;
begin
HashFlag := hash_alpha;
// Probe hash
if Personality.UseHash then
begin
Value := Hash.Probe(EngineBoard, Depth, Alpha, Beta);
if Value <> PROBE_UNKNOWN then
begin
Result := Value;
Exit;
end;
end;
// Depth <= 0: Call Quiet and return the result
if Depth <= 0 then
begin
Result := Quiet(Alpha, Beta);
if Personality.UseHash then
Hash.Store(EngineBoard, Depth, Result, hash_exact);
Exit;
end;
Inc(CurrentPly);
with MoveStack[CurrentPly] do
begin
// Generate moves and order them
GenerateMoves;
// Loop through the moves
for i := 0 to MoveCount - 1 do
begin
Undo := MakeMove(EngineBoard, CurrentMove);
// Skip illegal moves
if RunnedIntoCheck(EngineBoard) then
begin
UnmakeMove(EngineBoard, CurrentMove, Undo);
Continue;
end;
UnmakeMove(EngineBoard, CurrentMove, Undo);
// check beta cutoff
if Value >= Beta then
begin
Result := Beta;
if Personality.UseHash then
Hash.Store(EngineBoard, Depth, Beta, hash_beta);
goto ExitPoint;
end;
if Value > Alpha then
begin
Alpha := Value;
HashFlag := hash_exact;
end;
end; // loop through moves
// There are no valid moves: it's mate or stalemate
if ValidMoves = 0 then
begin
if PrevLevel^.GivingCheck then
Result := -MATE + CurrentPly - 1
else
Result := 0;
end
// We have valid moves, return alpha
else begin
Result := Alpha;
if Personality.UseHash then
Hash.Store(EngineBoard, Depth, Alpha, HashFlag);
end;
end; // with MoveStack
ExitPoint:
Dec(CurrentPly);
end;
function THash.Probe(const B: TBoard; aDepth, Alpha, Beta: Integer): Integer;
var
Hash: THashElement;
begin
Result := PROBE_UNKNOWN;
// get slot
Hash := fArray^[B.HashKey mod Size];
// check hashkey
if Hash.hKey <> B.HashKey then
Exit;
// check if depth good enough
if Hash.Depth < aDepth then
Exit;
// return appropriate result
case Rec.HashFlag of
hash_exact:
Result := Hash.Eval;
hash_alpha:
if Hash.Eval <= Alpha then
Result := Alpha;
hash_beta:
if Hash.Eval >= Beta then
Result := Beta;
end; // case
end;