Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getlegalmoves nonalloc #3

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions Chess-Challenge/src/My Bot/MyBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,14 @@ public Move Think(Board board, Timer timer)
//if (!isQuiescence && _position.IsInCheck()) // TODO investigate, this makes the bot suggest null moves either other move
// ++_targetDepth;

// TODO: GetLegalMovesNonAlloc
//Span<Move> spanLegalMoves = stackalloc Move[256];
//_position.GetLegalMovesNonAlloc(ref spanLegalMoves);
//spanLegalMoves.Sort((a, b) => Score(a, ply > Score(b, ply) ? 1 : 0));

if (!isQuiescence && ply > _targetDepth)
return _position.GetLegalMoves().Any()//.Length > 0
? NegaMax(ply, alpha, beta, true) // Quiescence
: EvaluateFinalPosition(ply);
{
Span<Move> legalMoves = stackalloc Move[256];
_position.GetLegalMovesNonAlloc(ref legalMoves);
return legalMoves.IsEmpty
? EvaluateFinalPosition(ply)
: NegaMax(ply, alpha, beta, true); // Quiescence
}

int pvIndex = _indexes[ply],
nextPvIndex = _indexes[ply + 1],
Expand All @@ -162,8 +161,12 @@ public Move Think(Board board, Timer timer)
#region Move sorting

if (!isQuiescence && _isFollowingPV)
_isFollowingPV = _position.GetLegalMoves().Any(m => m == _pVTable[ply])
{
Span<Move> legalMoves = stackalloc Move[256];
_position.GetLegalMovesNonAlloc(ref legalMoves);
_isFollowingPV = legalMoves.Contains(_pVTable[ply])
&& (_isScoringPV = true);
}

#endregion

Expand Down Expand Up @@ -227,12 +230,14 @@ void Eval(bool localIsWhiteToMove)
++_nodes;
#endif

var moves = _position.GetLegalMoves(isQuiescence);

Span<Move> moves = stackalloc Move[256];
_position.GetLegalMovesNonAlloc(ref moves, isQuiescence);
if (isQuiescence && moves.Length == 0)
return staticEvaluation;

foreach (var move in moves.OrderByDescending(move => Score(move, ply/*, _killerMoves*/)))
moves.Sort((a, b) => Score(a, ply/*, _killerMoves*/) > Score(b, ply/*, _killerMoves*/) ? 1 : 0);

foreach (var move in moves)
{
_position.MakeMove(move);
var evaluation = -NegaMax(ply + 1, -beta, -alpha, isQuiescence); // Invokes itself, either Negamax or Quiescence
Expand Down Expand Up @@ -261,8 +266,17 @@ void Eval(bool localIsWhiteToMove)
}
}

if (bestMove.IsNull && _position.GetLegalMoves().Length == 0)
return EvaluateFinalPosition(ply);
if (bestMove.IsNull)
{
if (isQuiescence)
{
moves = stackalloc Move[256];
_position.GetLegalMovesNonAlloc(ref moves);
}

if (moves.IsEmpty)
return EvaluateFinalPosition(ply);
}

// Node fails low
return alpha;
Expand Down