Skip to content

Commit

Permalink
optimizations (fixed bugs, improved speed and general code cleanup)
Browse files Browse the repository at this point in the history
* implemented parallel perft()
* code cleanup
* implemented stuck piece detection (except for pawns)
* trapped pieces score less
* fixed broken "go perft n"
* renamed to greek letters
* fixed alpha/beta pruning
* hashtable is now concurrent collection
* mended some tests + updated opening book
  • Loading branch information
sictransit committed Dec 31, 2022
1 parent 85018ce commit 2ce9c6c
Show file tree
Hide file tree
Showing 28 changed files with 1,795 additions and 413 deletions.
37 changes: 19 additions & 18 deletions Common/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ public int Score
{
var evaluation = internals.Scoring.EvaluatePiece(piece, phase);

switch (piece.GetPieceType())
if (piece.GetPieceType() == Piece.Pawn && IsPassedPawn(piece))
{
case Piece.Pawn:
if (IsPassedPawn(piece))
{
evaluation *= 2;
}
break;
default:
break;
evaluation *= 2;
}

if (IsBlocked(piece))
{
evaluation = evaluation * phase / 24;
}

score += piece.Is(Piece.White) ? evaluation : -evaluation;
Expand Down Expand Up @@ -316,9 +314,16 @@ public IEnumerable<Piece> GetAttackers(Piece piece)
}
}

private bool IsBlocked(Piece piece)
{
var blockedMask = internals.Moves.GetBlockedMask(piece);

return (blockedMask & GetBitboard(ActiveColor).All) == blockedMask;
}

public IEnumerable<Move> GetLegalMoves()
{
foreach (var piece in GetPieces(ActiveColor))
foreach (var piece in GetPieces(ActiveColor).Where(p => !IsBlocked(p)))
{
foreach (var move in GetLegalMoves(piece))
{
Expand Down Expand Up @@ -349,7 +354,10 @@ public IEnumerable<Move> GetLegalMoves(Piece piece)
break;
}

if (IsMovingIntoCheck(move))
var testBoard = Play(move);

// Moving into check?
if (testBoard.IsAttacked(testBoard.FindKing(ActiveColor)))
{
if (hostileTarget != Piece.None)
{
Expand Down Expand Up @@ -421,13 +429,6 @@ private bool ValidateMove(Move move, Piece hostileTarget)
return true;
}

private bool IsMovingIntoCheck(Move move)
{
var testBoard = Play(move);

return testBoard.IsAttacked(testBoard.FindKing(ActiveColor));
}

public bool IsChecked => IsAttacked(FindKing(ActiveColor));

public bool IsAttacked(Piece piece) => GetAttackers(piece).Any();
Expand Down
2 changes: 1 addition & 1 deletion Common/Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<ItemGroup>
<None Update="Resources\eco.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
14 changes: 13 additions & 1 deletion Common/Extensions/BoardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public static string PrettyPrint(this IBoard b)
}

public static ulong Perft(this IBoard board, int depth)
{
ulong count = 0;

Parallel.ForEach(board.GetLegalMoves(), move =>
{
Interlocked.Add(ref count, board.Play(move).ParallelPerft(depth - 1));
});

return count;
}

private static ulong ParallelPerft(this IBoard board, int depth)
{
if (depth <= 1)
{
Expand All @@ -58,7 +70,7 @@ public static ulong Perft(this IBoard board, int depth)

foreach (var move in board.GetLegalMoves())
{
count += Perft(board.Play(move), depth - 1);
count += board.Play(move).ParallelPerft(depth - 1);
}

return count;
Expand Down
28 changes: 24 additions & 4 deletions Common/Lookup/Moves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,39 @@ namespace SicTransit.Woodpusher.Common.Lookup
public class Moves
{
private readonly Dictionary<Piece, Move[][]> vectors = new();
private readonly Dictionary<Piece, ulong> blockedMasks = new();
private readonly Dictionary<ulong, ulong> travelMasks = new();
private readonly Dictionary<Piece, ulong> passedPawnMasks = new();

public Moves()
{
InitializeVectors();

InitializeBlockedMasks();

InitializeTravelMasks();

InitializePassedPawnMasks();
}

private void InitializeBlockedMasks()
{
foreach (var vector in vectors)
{
if (vector.Key.Is(Piece.Pawn))
{
// TODO: implement for pawns
blockedMasks.Add(vector.Key, ulong.MaxValue);

continue;
}

var mask = vector.Value.Select(series => series.First()).Aggregate(0ul, (current, move) => current | move.Target);

blockedMasks.Add(vector.Key, mask);
}
}

private void InitializeTravelMasks()
{
var squares = Enumerable.Range(0, 64).Select(shift => 1ul << shift);
Expand Down Expand Up @@ -65,6 +86,8 @@ private void InitializePassedPawnMasks()
}
}

public ulong GetBlockedMask(Piece piece) => blockedMasks[piece];

public ulong GetTravelMask(ulong current, ulong target) => travelMasks[current | target];

public ulong GetPassedPawnMask(Piece piece) => passedPawnMasks[piece];
Expand All @@ -82,10 +105,7 @@ private void InitializeVectors()
});
}

public Move[][] GetVectors(Piece piece)
{
return vectors[piece];
}
public Move[][] GetVectors(Piece piece) => vectors[piece];

private static IEnumerable<IEnumerable<Move>> CreateVectors(Piece piece)
{
Expand Down
4 changes: 0 additions & 4 deletions Common/Movement/RookMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public static IEnumerable<IEnumerable<Move>> GetTargetVectors(Piece piece)
{
yield return Enumerable.Range(0, square.File).Reverse().Select(f => new Move(piece, square.NewFile(f)));
}

}
}



}

0 comments on commit 2ce9c6c

Please sign in to comment.