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

Change PieceUpdated, add new events #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
65 changes: 64 additions & 1 deletion src/Rudzoft.ChessLib.Test/PositionTests/PositionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using Rudzoft.ChessLib.Events;
using Rudzoft.ChessLib.Factories;
using Rudzoft.ChessLib.Types;
using Xunit.Abstractions;

namespace Rudzoft.ChessLib.Test.PositionTests;

public sealed class PositionTests
{
private readonly ITestOutputHelper _testOutputHelper;
public PositionTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public void AddPieceTest()
{
Expand Down Expand Up @@ -88,4 +96,59 @@ public void KingBlocker()

Assert.Equal(expectedSquare, actual);
}
}

[Fact]
public void PieceMovedGeneratesEventWithPieceInfo()
{
int moveEventsReceived = 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer var instead of int for these

int removeEventsReceived = 0;
int addEventsReceived = 0;
var moves = new[]
{
Move.Create(Square.F2, Square.F3),
Move.Create(Square.E7, Square.E5),
Move.Create(Square.G2, Square.G4),
Move.Create(Square.D7, Square.D6),
Move.Create(Square.H2, Square.H4),
Move.Create(Square.D8, Square.H4)
};

// construct game and start a new game
var game = GameFactory.Create(Fen.Fen.StartPositionFen);
game.Pos.IsProbing = false;
game.Pos.PieceMoved += PieceUpdated;
game.Pos.PieceAdded += PieceAdded;
game.Pos.PieceRemoved += PieceRemoved;

var position = game.Pos;

// make the moves necessary to create a mate
foreach (var move in moves)
position.MakeMove(move, game.Pos.State);

//We should have received 6 move events and one remove.
Assert.Equal(6, moveEventsReceived);
Assert.Equal(1, removeEventsReceived);
Assert.Equal(0, addEventsReceived);

void PieceUpdated(object sender, PieceMovedEventArgs args)
{
moveEventsReceived++;
Assert.NotEqual(Pieces.NoPiece, args.MovedPiece.Value);
_testOutputHelper.WriteLine($"{args.MovedPiece.Value} moved from {args.From} to {args.To}.");
}
void PieceRemoved(object sender, PieceRemovedEventArgs args)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spacing between these local methods for readability

{
removeEventsReceived++;
Assert.NotEqual(Pieces.NoPiece, args.RemovedPiece.Value);
_testOutputHelper.WriteLine($"{args.RemovedPiece.Value} removed from {args.EmptiedSquare}.");
}

void PieceAdded(object sender, PieceAddedEventArgs args)
{
addEventsReceived++;
}
}


}
3 changes: 3 additions & 0 deletions src/Rudzoft.ChessLib/Events/PieceAddedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Rudzoft.ChessLib.Events;

public delegate void PieceAddedEvent(object sender, PieceAddedEventArgs args);
15 changes: 15 additions & 0 deletions src/Rudzoft.ChessLib/Events/PieceAddedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Rudzoft.ChessLib.Types;

namespace Rudzoft.ChessLib.Events;

public struct PieceAddedEventArgs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, use 'record struct', else 'readonly struct' since the properties are not changed after object creation.

{
public Square Square { get; }
public Piece NewPiece { get; }

public PieceAddedEventArgs(Square square, Piece newPiece)
{
Square = square;
NewPiece = newPiece;
}
}
3 changes: 3 additions & 0 deletions src/Rudzoft.ChessLib/Events/PieceMovedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Rudzoft.ChessLib.Events;

public delegate void PieceMovedEvent(object sender, PieceMovedEventArgs args);
17 changes: 17 additions & 0 deletions src/Rudzoft.ChessLib/Events/PieceMovedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Rudzoft.ChessLib.Types;

namespace Rudzoft.ChessLib.Events;

public struct PieceMovedEventArgs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, use 'record struct', else 'readonly struct' since the properties are not changed after object creation.

{
public Square From { get; }
public Square To { get; }
public Piece MovedPiece { get; }

public PieceMovedEventArgs(Square from, Square to, Piece movedPiece)
{
From = from;
To = to;
MovedPiece = movedPiece;
}
}
3 changes: 3 additions & 0 deletions src/Rudzoft.ChessLib/Events/PieceRemovedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Rudzoft.ChessLib.Events;

public delegate void PieceRemovedEvent(object sender, PieceRemovedEventArgs args);
15 changes: 15 additions & 0 deletions src/Rudzoft.ChessLib/Events/PieceRemovedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Rudzoft.ChessLib.Types;

namespace Rudzoft.ChessLib.Events;

public struct PieceRemovedEventArgs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, use 'record struct', else 'readonly struct' since the properties are not changed after object creation.

{
public Square EmptiedSquare { get; }
public Piece RemovedPiece { get; }

public PieceRemovedEventArgs(Square emptiedSquare, Piece removedPiece)
{
EmptiedSquare = emptiedSquare;
RemovedPiece = removedPiece;
}
}
4 changes: 1 addition & 3 deletions src/Rudzoft.ChessLib/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ static Game()
Uci = new Uci();
}

public Action<IPieceSquare> PieceUpdated => _pos.PieceUpdated;

public int MoveNumber => 0; //(PositionIndex - 1) / 2 + 1;

public BitBoard Occupied => Pos.Pieces();
Expand Down Expand Up @@ -156,4 +154,4 @@ public ulong Perft(int depth, bool root = true)

return tot;
}
}
}
4 changes: 1 addition & 3 deletions src/Rudzoft.ChessLib/IGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ namespace Rudzoft.ChessLib;

public interface IGame : IEnumerable<Piece>
{
Action<IPieceSquare> PieceUpdated { get; }

BitBoard Occupied { get; }

IPosition Pos { get; }
Expand All @@ -60,4 +58,4 @@ public interface IGame : IEnumerable<Piece>
Player CurrentPlayer();

ulong Perft(int depth, bool root = true);
}
}
5 changes: 4 additions & 1 deletion src/Rudzoft.ChessLib/IPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Collections.Generic;
using System.Text;
using Rudzoft.ChessLib.Enums;
using Rudzoft.ChessLib.Events;
using Rudzoft.ChessLib.Fen;
using Rudzoft.ChessLib.Types;
using Rudzoft.ChessLib.Validation;
Expand All @@ -38,7 +39,9 @@ public interface IPosition : IEnumerable<Piece>
{
bool IsProbing { get; set; }

Action<IPieceSquare> PieceUpdated { get; set; }
public event PieceAddedEvent PieceAdded;
public event PieceMovedEvent PieceMoved;
public event PieceRemovedEvent PieceRemoved;

ChessMode ChessMode { get; set; }

Expand Down
31 changes: 19 additions & 12 deletions src/Rudzoft.ChessLib/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Text;
using Microsoft.Extensions.ObjectPool;
using Rudzoft.ChessLib.Enums;
using Rudzoft.ChessLib.Events;
using Rudzoft.ChessLib.Extensions;
using Rudzoft.ChessLib.Fen;
using Rudzoft.ChessLib.Hash;
Expand Down Expand Up @@ -77,6 +78,14 @@ public Position(IBoard board, IPieceValue pieceValues)
public BitBoard Checkers
=> State.Checkers;

/// <summary>
/// To let something outside the library be aware of changes (like a UI etc)
/// </summary>
public event PieceRemovedEvent PieceRemoved;

public event PieceAddedEvent PieceAdded;
public event PieceMovedEvent PieceMoved;

public ChessMode ChessMode { get; set; }

public Square EnPassantSquare
Expand All @@ -96,11 +105,6 @@ public bool IsMate
public bool IsRepetition
=> State.Repetition >= 3;

/// <summary>
/// To let something outside the library be aware of changes (like a UI etc)
/// </summary>
public Action<IPieceSquare> PieceUpdated { get; set; }

public IPieceValue PieceValue { get; }

public int Ply { get; private set; }
Expand All @@ -120,7 +124,7 @@ public void AddPiece(Piece pc, Square sq)
Board.AddPiece(pc, sq);

if (!IsProbing)
PieceUpdated?.Invoke(new PieceSquareEventArgs(pc, sq));
PieceAdded?.Invoke(this, new PieceAddedEventArgs(sq, pc));
}

public bool AttackedByKing(Square sq, Player p)
Expand Down Expand Up @@ -973,9 +977,14 @@ public BitBoard PinnedPieces(Player p)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void RemovePiece(Square sq)
{
Piece removedPiece = Board.PieceAt(sq);

Board.RemovePiece(sq);

if (!IsProbing)
PieceUpdated?.Invoke( new PieceSquareEventArgs(Piece.EmptyPiece, sq));
{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant block brackets :)

PieceRemoved?.Invoke(this, new PieceRemovedEventArgs(sq, removedPiece));
}
}

public bool SeeGe(Move m, Value threshold)
Expand Down Expand Up @@ -1353,7 +1362,7 @@ private void MovePiece(Square from, Square to)
if (IsProbing)
return;

PieceUpdated?.Invoke(new PieceSquareEventArgs(Board.PieceAt(from), to));
PieceMoved?.Invoke(this, new PieceMovedEventArgs(from, to, Board.PieceAt(to)));
}

///<summary>
Expand Down Expand Up @@ -1446,9 +1455,7 @@ private void SetupCastleling(ReadOnlySpan<char> castleling)
Square RookSquare(Square startSq, Piece rook)
{
Square targetSq;
for (targetSq = startSq; Board.PieceAt(targetSq) != rook; --targetSq)
{
}
for (targetSq = startSq; Board.PieceAt(targetSq) != rook; --targetSq) { }

return targetSq;
}
Expand Down Expand Up @@ -1500,4 +1507,4 @@ Square RookSquare(Square startSq, Piece rook)

return result;
}
}
}