Skip to content

Commit

Permalink
negascout (#43)
Browse files Browse the repository at this point in the history
* NegaScout
* added test: Issue38_DoNotPlayF3E5
* removed obsolete evaluatedBestMove
* uci interface rewrite + fixed opening book (illegal move issue) (#40)
* pruned logs
* reverted to working Quiesce
* remove ply from best line
* implemented history heuristics
* implemented ponder
* inverted depth (#41)
* updated README
  • Loading branch information
sictransit authored Dec 9, 2024
1 parent 3320b76 commit 8efe828
Show file tree
Hide file tree
Showing 21 changed files with 397 additions and 253 deletions.
36 changes: 26 additions & 10 deletions Common/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,26 @@ public int Score

score = 0;

foreach (var piece in GetPieces())
foreach (var (bitboard, sign) in new[] { (white, 1), (black, -1) })
{
var evaluation = internals.Scoring.EvaluatePiece(piece, phase);
foreach (var piece in bitboard.GetPieces())
{
var evaluation = internals.Scoring.EvaluatePiece(piece, phase);

score += evaluation * sign;
}

score += evaluation * (piece.Is(Piece.White) ? 1 : -1);
//// Connected rooks bonus
//var rooks = bitboard.GetMasks(Piece.Rook, ulong.MaxValue).ToArray();
//if (rooks.Length == 2 && rooks[0].SameFileOrRank(rooks[1]))
//{
// var travelMask = internals.Moves.GetTravelMask(rooks[0], rooks[1]);

// if (!IsOccupied(travelMask))
// {
// score += Scoring.ConnectedRooksBonus * sign;
// }
//}
}
}

Expand Down Expand Up @@ -307,21 +322,21 @@ public IEnumerable<Move> GetLegalMoves(Piece piece)
return PlayLegalMoves().Where(b => b.Counters.LastMove.Piece == piece).Select(b => b.Counters.LastMove);
}

public List<Board> PlayLegalMoves(bool onlyCaptures = false)
public IEnumerable<Board> PlayLegalMoves(bool quiescence = false)
{
var boards = new List<Board>();

foreach (var piece in activeBoard.GetPieces())
{
foreach (var board in PlayLegalMoves(piece, onlyCaptures))
foreach (var board in PlayLegalMoves(piece, quiescence))
{
boards.Add(board);
yield return board;
}
}

return boards;

}

private IEnumerable<Board> PlayLegalMoves(Piece piece, bool onlyCaptures)
private IEnumerable<Board> PlayLegalMoves(Piece piece, bool quiescence)
{
foreach (var vector in internals.Moves.GetVectors(piece))
{
Expand All @@ -334,7 +349,8 @@ private IEnumerable<Board> PlayLegalMoves(Piece piece, bool onlyCaptures)

var taking = opponentBoard.IsOccupied(move.Target);

if (!taking && onlyCaptures)
//if (quiescence && (!taking || !move.Flags.HasFlag(SpecialMove.PawnPromotes)))
if (quiescence && !taking)
{
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions Common/Extensions/BoardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public static ulong Perft(this Board board, int depth)
{
ulong count = 0;

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

return count;
Expand Down
2 changes: 2 additions & 0 deletions Common/Interfaces/IEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public interface IEngine

AlgebraicMove? FindBestMove(int timeLimit = 1000);

AlgebraicMove? GetPonderMove();

void Perft(int depth);
}
}
2 changes: 2 additions & 0 deletions Common/Lookup/Scoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class Scoring
public const int MateScore = 1000000;
public const int MoveMaximumScore = MateScore * 2;

public const int ConnectedRooksBonus = 25;

private static readonly int[] PawnMiddleGameModifiers =
{
0, 0, 0, 0, 0, 0, 0, 0,
Expand Down
19 changes: 6 additions & 13 deletions Common/Movement/BishopMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,28 @@ public static IEnumerable<IEnumerable<Move>> GetTargetVectors(Piece piece)
var square = piece.GetSquare();

var topRight = Math.Max(square.File, square.Rank);

if (topRight < 7)
{
yield return Enumerable.Range(1, 7 - topRight).Select(d => new Move(piece, square.AddFileAndRank(d, d)));
}

var bottomRight = Math.Max(square.File, 7 - square.Rank);
var topLeft = Math.Min(square.File, 7 - square.Rank);
if (topLeft > 0)
{
yield return Enumerable.Range(1, topLeft).Select(d => new Move(piece, square.AddFileAndRank(-d, d)));
}

var bottomRight = Math.Max(square.File, 7 - square.Rank);
if (bottomRight < 7)
{
yield return Enumerable.Range(1, 7 - bottomRight).Select(d => new Move(piece, square.AddFileAndRank(d, -d)));
}

var bottomLeft = Math.Min(square.File, square.Rank);

if (bottomLeft > 0)
{
yield return Enumerable.Range(1, bottomLeft).Select(d => new Move(piece, square.AddFileAndRank(-d, -d)));
}

var topLeft = Math.Min(square.File, 7 - square.Rank);

if (topLeft > 0)
{
yield return Enumerable.Range(1, topLeft).Select(d => new Move(piece, square.AddFileAndRank(-d, d)));
}
}

}


}
2 changes: 1 addition & 1 deletion Common/Parsing/Moves/CastlingMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override Move CreateMove(IEngine engine)
}


throw new PgnParsingException(Raw, "unable to find a legal move to match");
throw new PgnParsingException(Raw, "Unable to find a legal move to match.");
}
}
}
2 changes: 1 addition & 1 deletion Common/Parsing/Moves/PieceOnFileMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected override Move CreateMove(IEngine engine)
}
}

throw new PgnParsingException(Raw, "unable to match a legal move");
throw new PgnParsingException(Raw, "Unable to find a legal move to match.");
}
}
}
2 changes: 1 addition & 1 deletion Common/Parsing/Moves/PieceOnRankMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected override Move CreateMove(IEngine engine)
}
}

throw new PgnParsingException(Raw, "unable to a legal move to match");
throw new PgnParsingException(Raw, "Unable to find a legal move to match.");
}
}
}
2 changes: 1 addition & 1 deletion Common/Parsing/Moves/SimplePieceMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override Move CreateMove(IEngine engine)
}
}

throw new PgnParsingException(Raw, "unable to a legal move to match");
throw new PgnParsingException(Raw, "Unable to find a legal move to match.");
}

public override string ToString()
Expand Down
2 changes: 1 addition & 1 deletion Common/Parsing/Moves/SpecificPieceMove.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected override Move CreateMove(IEngine engine)
}
}

throw new PgnParsingException(Raw, "unable to find a legal move to match");
throw new PgnParsingException(Raw, "Unable to find a legal move to match.");
}

public override string ToString()
Expand Down
Loading

0 comments on commit 8efe828

Please sign in to comment.