Code: Select all
int searchNonPV(int alpha, int depth)/*beta=alpha+1*/
{
if (insufficentMaterial) return 0;
if (depth == 0) return qsearch<NonPV>(alpha, alpha + 1);
int bestValue = -Value_Infinite, value;
if (NMoves == 0)
{
TTStore(Move(), depth, BoundType::Exact, bestValue);
bestValue = InCheck ? -Value_Mate + ply : Value_Draw;
}
Move mBest = Move();
BoundType bound = BoundType::Upper;
for (size_t i = 0; i < NMoves; i++)
{
Position::MakeMove(Available[i]);
value = -searchNonPV(-(alpha + 1), depth - 1, true);
Position::UnMakeMove(Available[i]);
if (value > bestValue)
{
bestValue = value;
if (value > alpha)
{
mBest = Available[i];
bound = BoundType::Lower;
if ((TypeBoard[Available[i].To] == 0)) UpdateKillersAndHistory(Available[i], depth);
break;// Fail high:>=beta=alpha+1
}
}
}
TTStore(mBest, depth, bound, bestValue);
return bestValue;
}
int searchPV(int alpha, int beta, int depth)
{
if (insufficentMaterial) return 0;
if (depth == 0) return qsearch<PV>(alpha, beta);
int bestValue = -Value_Infinite, value;
if (NMoves == 0)
{
TTStore(Move(), depth, BoundType::Exact, bestValue);
bestValue = InCheck ? -Value_Mate + ply : Value_Draw;
}
Move mBest = Move();
BoundType bound = BoundType::Upper;
for (size_t i = 0; i < NMoves; i++)
{
Position::MakeMove(Available[i]);
if (i > 0) value = -searchNonPV(-(alpha + 1), depth - 1, true);
if (i == 0 || (value > alpha && (value < beta))) value = -searchPV(-beta, -alpha, depth - 1);
Position::UnMakeMove(Available[i]);
if (value > bestValue)
{
bestValue = value;
if (value > alpha)
{
if (value < beta) alpha = value;
else
{
mBest = Available[i];
bound = BoundType::Lower;
if ((TypeBoard[Available[i].To] == 0)) UpdateKillersAndHistory(Available[i], depth);
break; // Fail high
}
}
}
}
TTStore(mBest, depth, bound, bestValue);
return bestValue;
}