Skip to content

Commit

Permalink
[WIP] Copy-make with readonly Position struct
Browse files Browse the repository at this point in the history
  • Loading branch information
eduherminio committed Jan 3, 2025
1 parent 7dc51c7 commit 282c9f2
Show file tree
Hide file tree
Showing 37 changed files with 308 additions and 450 deletions.
4 changes: 2 additions & 2 deletions src/Lynx.Benchmark/LocalVariableIn_vs_NoIn_Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public int LocalVariableAndIn(string fen)
var moves = new List<Move>(50_000);

for (int i = 0; i < 1000; ++i)
moves.AddRange(Sort_LocalVariableAndIn(MoveGenerator.GenerateAllMoves(position), position));
moves.AddRange(Sort_LocalVariableAndIn(position.GenerateAllMoves(), position));

return Sort_LocalVariableAndIn(moves, position)[0];
}
Expand All @@ -37,7 +37,7 @@ public int NoIn(string fen)
var position = new Position(fen);

for (int i = 0; i < 1000; ++i)
moves.AddRange(Sort_NoIn(MoveGenerator.GenerateAllMoves(position), position));
moves.AddRange(Sort_NoIn(position.GenerateAllMoves(), position));

return Sort_NoIn(moves, position)[0];
}
Expand Down
14 changes: 7 additions & 7 deletions src/Lynx.Benchmark/ParseGame_Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ internal OriginalGame(string fen, string[] movesUCIString) : this(fen)
{
foreach (var moveString in movesUCIString)
{
var moveList = MoveGenerator.GenerateAllMoves(CurrentPosition);
var moveList = CurrentPosition.GenerateAllMoves();

if (!MoveExtensions.TryParseFromUCIString(moveString, moveList, out var parsedMove))
{
Expand Down Expand Up @@ -574,7 +574,7 @@ public ImprovedGame(ReadOnlySpan<char> fen)

PositionHashHistory = new(1024) { CurrentPosition.UniqueIdentifier };
HalfMovesWithoutCaptureOrPawnMove = parsedFen.HalfMoveClock;
_gameInitialPosition = new Position(CurrentPosition);
_gameInitialPosition = new Position(in CurrentPosition);
#if DEBUG
MoveHistory = new(1024);
#endif
Expand All @@ -590,7 +590,7 @@ internal ImprovedGame(string fen, ReadOnlySpan<char> rawMoves, Span<Range> range
break;
}
var moveString = rawMoves[range];
var moveList = MoveGenerator.GenerateAllMoves(CurrentPosition);
var moveList = CurrentPosition.GenerateAllMoves();

if (!MoveExtensions.TryParseFromUCIString(moveString, moveList, out var parsedMove))
{
Expand All @@ -601,7 +601,7 @@ internal ImprovedGame(string fen, ReadOnlySpan<char> rawMoves, Span<Range> range
MakeMove(parsedMove.Value);
}

_gameInitialPosition = new Position(CurrentPosition);
_gameInitialPosition = new Position(in CurrentPosition);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -655,7 +655,7 @@ public ImprovedGame2(ReadOnlySpan<char> fen)

PositionHashHistory = new(1024) { CurrentPosition.UniqueIdentifier };
HalfMovesWithoutCaptureOrPawnMove = parsedFen.HalfMoveClock;
_gameInitialPosition = new Position(CurrentPosition);
_gameInitialPosition = new Position(in CurrentPosition);

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (ubuntu-latest, Configuration)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (ubuntu-latest, LongRunning)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / perft-tests (ubuntu-latest, Perft)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, ubuntu-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / winning-at-chess (ubuntu-latest, WinningAtChess_FixedTime)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, ubuntu-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-13)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-latest, LongRunning)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (windows-latest, Configuration)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / perft-tests (windows-latest, Perft)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (windows-latest, LongRunning)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, windows-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-13, LongRunning)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, windows-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-13)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-latest, Configuration)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / perft-tests (macOS-latest, Perft)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 658 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-13, Configuration)

An expression cannot be used in this context because it may not be passed or returned by reference
#if DEBUG
MoveHistory = new(1024);
#endif
Expand All @@ -670,7 +670,7 @@ public ImprovedGame2(ReadOnlySpan<char> fen, ReadOnlySpan<char> rawMoves, Span<R
break;
}
var moveString = rawMoves[rangeSpan[i]];
var moveList = MoveGenerator.GenerateAllMoves(CurrentPosition, movePool);
var moveList = CurrentPosition.GenerateAllMoves(movePool);

if (!MoveExtensions.TryParseFromUCIString(moveString, moveList, out var parsedMove))
{
Expand All @@ -681,7 +681,7 @@ public ImprovedGame2(ReadOnlySpan<char> fen, ReadOnlySpan<char> rawMoves, Span<R
MakeMove(parsedMove.Value);
}

_gameInitialPosition = new Position(CurrentPosition);
_gameInitialPosition = new Position(in CurrentPosition);

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (ubuntu-latest, Configuration)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (ubuntu-latest, LongRunning)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / perft-tests (ubuntu-latest, Perft)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, ubuntu-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / winning-at-chess (ubuntu-latest, WinningAtChess_FixedTime)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, ubuntu-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-13)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-latest, LongRunning)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (windows-latest, Configuration)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / perft-tests (windows-latest, Perft)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (windows-latest, LongRunning)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, windows-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-13, LongRunning)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, windows-latest)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-13)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-latest, Configuration)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / perft-tests (macOS-latest, Perft)

An expression cannot be used in this context because it may not be passed or returned by reference

Check failure on line 684 in src/Lynx.Benchmark/ParseGame_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-13, Configuration)

An expression cannot be used in this context because it may not be passed or returned by reference
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
10 changes: 5 additions & 5 deletions src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ public string Id(Position newPosition)
public static IEnumerable<Position> Data =>
[
//Constants.EmptyBoardFEN,
Positions[0].ClonePositionAndMakeMove(MoveGenerator.GenerateAllMoves(Positions[0])[0]),
Positions[1].ClonePositionAndMakeMove(MoveGenerator.GenerateAllMoves(Positions[1])[0]),
Positions[2].ClonePositionAndMakeMove(MoveGenerator.GenerateAllMoves(Positions[2])[0]),
Positions[3].ClonePositionAndMakeMove(MoveGenerator.GenerateAllMoves(Positions[3])[0])
Positions[0].ClonePositionAndMakeMove(Positions[0].GenerateAllMoves()[0]),
Positions[1].ClonePositionAndMakeMove(Positions[1].GenerateAllMoves()[0]),
Positions[2].ClonePositionAndMakeMove(Positions[2].GenerateAllMoves()[0]),
Positions[3].ClonePositionAndMakeMove(Positions[3].GenerateAllMoves()[0])
];
}

internal static class PositionExtensions
{
public static Position ClonePositionAndMakeMove(this Position position, Move move)
{
var newPosition = new Position(position);
var newPosition = new Position(in position);
newPosition.MakeMove(move);

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (ubuntu-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (ubuntu-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-13)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (windows-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / perft-tests (windows-latest, Perft)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (windows-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, windows-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-13, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, windows-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-13)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 77 in src/Lynx.Benchmark/PositionIdGeneration_Benchmark.cs

View workflow job for this annotation

GitHub Actions / perft-tests (macOS-latest, Perft)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

return newPosition;
Expand Down
2 changes: 1 addition & 1 deletion src/Lynx.Benchmark/TryParseFromUCIString_Benchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public TryParseFromUCIString_Benchmark_Game(ReadOnlySpan<char> fen, ReadOnlySpan
break;
}
var moveString = rawMoves[rangeSpan[i]];
var moveList = MoveGenerator.GenerateAllMoves(CurrentPosition, movePool);
var moveList = CurrentPosition.GenerateAllMoves(movePool);

if (!MoveExtensions.TryParseFromUCIString(moveString, moveList, out var parsedMove))
{
Expand Down
56 changes: 28 additions & 28 deletions src/Lynx.Dev/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ static void _22_Generate_Moves()
position.Print();

//position.OccupancyBitBoards[2] |= 0b11100111UL << 8 * 4;
var moves = MoveGenerator.GenerateAllMoves(position);
var moves = position.GenerateAllMoves();

foreach (var move in moves)
{
Expand All @@ -394,7 +394,7 @@ static void _22_Generate_Moves()
position.PieceBitBoards[0].Print();
position.PieceBitBoards[6].Print();
position.Print();
moves = MoveGenerator.GenerateAllMoves(position);
moves = position.GenerateAllMoves();

foreach (var move in moves)
{
Expand Down Expand Up @@ -423,10 +423,10 @@ static void _26_Piece_Moves()
var position = new Position(TrickyPosition);
position.Print();

var moves = MoveGenerator.GenerateAllMoves(position).Where(m => m.Piece() == (int)Piece.N || m.Piece() == (int)Piece.n).ToList();
var moves = position.GenerateAllMoves().Where(m => m.Piece() == (int)Piece.N || m.Piece() == (int)Piece.n).ToList();
moves.ForEach(m => Console.WriteLine(m));

moves = [.. MoveGenerator.GenerateAllMoves(position)];
moves = [.. position.GenerateAllMoves()];
Console.WriteLine($"Expected 48, found: {moves.Count}");
foreach (var move in moves)
{
Expand Down Expand Up @@ -482,16 +482,16 @@ static void PrintMoveList(IEnumerable<Move> moves)
static void _29_Move_List()
{
var position = new Position(TrickyPosition);
var moves = MoveGenerator.GenerateAllMoves(position);
var moves = position.GenerateAllMoves();
PrintMoveList(moves);

position = new Position(TrickyPositionReversed);
moves = MoveGenerator.GenerateAllMoves(position);
moves = position.GenerateAllMoves();
PrintMoveList(moves);

position = new Position(KillerPosition);
position.Print();
moves = MoveGenerator.GenerateAllMoves(position);
moves = position.GenerateAllMoves();
PrintMoveList(moves);
}

Expand All @@ -511,13 +511,13 @@ static void _32_Make_Move()
//CastlingRightsTest(game);
//CastlingRightsTest(reversedGame);

PrintMoveList(MoveGenerator.GenerateAllMoves(gameWithPromotion.CurrentPosition));
PrintMoveList(gameWithPromotion.CurrentPosition.GenerateAllMoves());

GeneralMoveTest(gameWithPromotion);

static void GeneralMoveTest(Game game)
{
foreach (var move in MoveGenerator.GenerateAllMoves(game.CurrentPosition))
foreach (var move in game.CurrentPosition.GenerateAllMoves())
{
game.CurrentPosition.Print();

Expand All @@ -537,7 +537,7 @@ static void GeneralMoveTest(Game game)

static void CastlingRightsTest(Game game)
{
foreach (var move in MoveGenerator.GenerateAllMoves(game.CurrentPosition))
foreach (var move in game.CurrentPosition.GenerateAllMoves())
{
if (move.Piece() == (int)Piece.R || (move.Piece() == (int)Piece.r)
|| move.Piece() == (int)Piece.K || (move.Piece() == (int)Piece.k))
Expand All @@ -561,7 +561,7 @@ static void _42_Perft()

for (int depth = 0; depth < 7; ++depth)
{
Perft.RunPerft(pos, depth, Console.WriteLine);
pos.RunPerft(depth, Console.WriteLine);
}
}

Expand All @@ -583,7 +583,7 @@ static void _49_Rudimetary_Evaluation()

//foreach (var move in MoveGenerator.GenerateAllMoves(position))
//{
// var newBlackPosition = new Position(position, move);
// var newBlackPosition = new Position(in position, move);
// if (newBlackPosition.IsValid())
// {
// var newWhitePosition = new Position(newBlackPosition, newBlackPosition.AllPossibleMoves()[0]);
Expand Down Expand Up @@ -698,7 +698,7 @@ static void _54_ScoreMove()

var engine = new Engine(Channel.CreateBounded<object>(new BoundedChannelOptions(100) { SingleReader = true, SingleWriter = false }));
engine.SetGame(new(position.FEN()));
foreach (var move in MoveGenerator.GenerateAllMoves(position, capturesOnly: true))
foreach (var move in position.GenerateAllMoves(capturesOnly: true))
{
Console.WriteLine($"{move} {engine.ScoreMove(move, default, default)}");
}
Expand All @@ -707,7 +707,7 @@ static void _54_ScoreMove()
position.Print();

engine.SetGame(new(position.FEN()));
foreach (var move in MoveGenerator.GenerateAllMoves(position, capturesOnly: true))
foreach (var move in position.GenerateAllMoves(capturesOnly: true))
{
Console.WriteLine($"{move} {engine.ScoreMove(move, default, default)}");
}
Expand All @@ -718,7 +718,7 @@ static void ZobristTable()
var pos = new Position(KillerPosition);
var zobristTable = InitializeZobristTable();
var hash = CalculatePositionHash(zobristTable, pos);
var updatedHash = UpdatePositionHash(zobristTable, hash, MoveGenerator.GenerateAllMoves(pos)[0]);
var updatedHash = UpdatePositionHash(zobristTable, hash, pos.GenerateAllMoves()[0]);

Console.WriteLine(updatedHash);
}
Expand Down Expand Up @@ -1079,30 +1079,30 @@ static void TesSize(int size)

transpositionTable.Clear();

//transpositionTable.RecordHash(position, depth: 3, maxDepth: 5, move: 1234, eval: +5, nodeType: NodeType.Alpha);
//var entry = transpositionTable.ProbeHash(position, maxDepth: 5, depth: 3, alpha: 1, beta: 2);
//transpositionTable.RecordHash(in position, depth: 3, maxDepth: 5, move: 1234, eval: +5, nodeType: NodeType.Alpha);
//var entry = transpositionTable.ProbeHash(in position, maxDepth: 5, depth: 3, alpha: 1, beta: 2);

transpositionTable.RecordHash(position, position.StaticEvaluation().Score, depth: 5, ply: 3, score: +19, nodeType: NodeType.Alpha, move: 1234);
var entry = transpositionTable.ProbeHash(position, ply: 3);
transpositionTable.RecordHash(in position, position.StaticEvaluation().Score, depth: 5, ply: 3, score: +19, nodeType: NodeType.Alpha, move: 1234);
var entry = transpositionTable.ProbeHash(in position, ply: 3);
Console.WriteLine(entry); // Expected 20

transpositionTable.RecordHash(position, position.StaticEvaluation().Score, depth: 5, ply: 3, score: +21, nodeType: NodeType.Alpha, move: 1234);
entry = transpositionTable.ProbeHash(position, ply: 3);
transpositionTable.RecordHash(in position, position.StaticEvaluation().Score, depth: 5, ply: 3, score: +21, nodeType: NodeType.Alpha, move: 1234);
entry = transpositionTable.ProbeHash(in position, ply: 3);
Console.WriteLine(entry); // Expected 12_345_678

transpositionTable.RecordHash(position, position.StaticEvaluation().Score, depth: 5, ply: 3, score: +29, nodeType: NodeType.Beta, move: 1234);
entry = transpositionTable.ProbeHash(position, ply: 3);
transpositionTable.RecordHash(in position, position.StaticEvaluation().Score, depth: 5, ply: 3, score: +29, nodeType: NodeType.Beta, move: 1234);
entry = transpositionTable.ProbeHash(in position, ply: 3);
Console.WriteLine(entry); // Expected 12_345_678

transpositionTable.RecordHash(position, position.StaticEvaluation().Score, depth: 5, ply: 3, score: +31, nodeType: NodeType.Beta, move: 1234);
entry = transpositionTable.ProbeHash(position, ply: 3);
transpositionTable.RecordHash(in position, position.StaticEvaluation().Score, depth: 5, ply: 3, score: +31, nodeType: NodeType.Beta, move: 1234);
entry = transpositionTable.ProbeHash(in position, ply: 3);
Console.WriteLine(entry); // Expected 30
}

static void UnmakeMove()
{
var pos = new Position("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq");
var a = Perft.PerftRecursiveImpl(pos, 1, default);
var a = pos.PerftRecursiveImpl(1, default);

TestMoveGen(Constants.InitialPositionFEN);
TestMoveGen(Constants.TTPositionFEN);
Expand All @@ -1115,15 +1115,15 @@ static void TestMoveGen(string fen)
var position = new Position(fen);
Console.WriteLine($"**Position\t{position.FEN()}, Zobrist key {position.UniqueIdentifier}**");

var allMoves = MoveGenerator.GenerateAllMoves(position);
var allMoves = position.GenerateAllMoves();

var oldZobristKey = position.UniqueIdentifier;
foreach (var move in allMoves)
{
var epdMoveString = move.ToEPDString(position);
Console.WriteLine($"Trying {epdMoveString} in\t{position.FEN()}");

var newPosition = new Position(position);
var newPosition = new Position(in position);
newPosition.MakeMove(move);

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / other-tests (ubuntu-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (ubuntu-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / perft-tests (ubuntu-latest, Perft)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, ubuntu-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / winning-at-chess (ubuntu-latest, WinningAtChess_FixedTime)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, ubuntu-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-13)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / other-tests (windows-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / perft-tests (windows-latest, Perft)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (windows-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, windows-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-13, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, windows-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-13)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / perft-tests (macOS-latest, Perft)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1127 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-13, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)
var savedState = position.MakeMove(move);

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / other-tests (ubuntu-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (ubuntu-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / perft-tests (ubuntu-latest, Perft)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, ubuntu-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / winning-at-chess (ubuntu-latest, WinningAtChess_FixedTime)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, ubuntu-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, macOS-13)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / other-tests (windows-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / perft-tests (windows-latest, Perft)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (windows-latest, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, windows-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / long-running-tests (macOS-13, LongRunning)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Debug, windows-latest)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / fast-tests (Release, macOS-13)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-latest, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / perft-tests (macOS-latest, Perft)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 1128 in src/Lynx.Dev/Program.cs

View workflow job for this annotation

GitHub Actions / other-tests (macOS-13, Configuration)

'Position' does not contain a definition for 'MakeMove' and no accessible extension method 'MakeMove' accepting a first argument of type 'Position' could be found (are you missing a using directive or an assembly reference?)

Expand Down
15 changes: 8 additions & 7 deletions src/Lynx/Model/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class Game : IDisposable

public int HalfMovesWithoutCaptureOrPawnMove { get; set; }

public Position CurrentPosition { get; private set; }
public Position CurrentPosition { get; set; }

public Position PositionBeforeLastSearch { get; private set; }

Expand Down Expand Up @@ -66,7 +66,7 @@ public Game(ReadOnlySpan<char> fen, ReadOnlySpan<char> rawMoves, Span<Range> ran
break;
}
var moveString = rawMoves[rangeSpan[i]];
var moveList = MoveGenerator.GenerateAllMoves(CurrentPosition, movePool);
var moveList = CurrentPosition.GenerateAllMoves(movePool);

// TODO: consider creating moves on the fly
if (!MoveExtensions.TryParseFromUCIString(moveString, moveList, out var parsedMove))
Expand Down Expand Up @@ -159,7 +159,7 @@ public bool Is50MovesRepetition()
return false;
}

return !CurrentPosition.IsInCheck() || MoveGenerator.CanGenerateAtLeastAValidMove(CurrentPosition);
return !CurrentPosition.IsInCheck() || CurrentPosition.CanGenerateAtLeastAValidMove();
}

/// <summary>
Expand Down Expand Up @@ -190,9 +190,10 @@ public static bool IsThreefoldRepetition(ReadOnlySpan<ulong> positionHashHistory
public static bool Is50MovesRepetition(int halfMovesWithoutCaptureOrPawnMove) => halfMovesWithoutCaptureOrPawnMove >= 100;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GameState MakeMove(Move moveToPlay)
public Position MakeMove(Move moveToPlay)
{
var gameState = CurrentPosition.MakeMove(moveToPlay);
var oldPosition = CurrentPosition;
CurrentPosition = new Position(CurrentPosition, moveToPlay);

if (CurrentPosition.WasProduceByAValidMove())
{
Expand All @@ -205,10 +206,10 @@ public GameState MakeMove(Move moveToPlay)
else
{
_logger.Warn("Error trying to play {0}", moveToPlay.UCIString());
CurrentPosition.UnmakeMove(moveToPlay, gameState);
CurrentPosition = oldPosition;
}

return gameState;
return oldPosition;
}

/// <summary>
Expand Down
7 changes: 4 additions & 3 deletions src/Lynx/Model/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,16 +393,17 @@ private static string DisambiguateMove(Move move, Position position)
var targetSquare = move.TargetSquare();

Span<Move> moves = stackalloc Move[Constants.MaxNumberOfPossibleMovesInAPosition];
var pseudoLegalMoves = MoveGenerator.GenerateAllMoves(position, moves).ToArray();
var pseudoLegalMoves = position.GenerateAllMoves(moves).ToArray();

var movesWithSameSimpleRepresentation = pseudoLegalMoves
.Where(m => m != move && m.Piece() == piece && m.TargetSquare() == targetSquare)
.Where(m =>
{
// If any illegal moves exist with the same simple representation there's no need to disambiguate
var gameState = position.MakeMove(m);
var oldPosition = position;
position = new Position(in position, m);
var isLegal = position.WasProduceByAValidMove();
position.UnmakeMove(m, gameState);
position = oldPosition;

return isLegal;
})
Expand Down
Loading

0 comments on commit 282c9f2

Please sign in to comment.