-
Notifications
You must be signed in to change notification settings - Fork 22
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
{ | ||
|
@@ -88,4 +96,59 @@ public void KingBlocker() | |
|
||
Assert.Equal(expectedSquare, actual); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void PieceMovedGeneratesEventWithPieceInfo() | ||
{ | ||
int moveEventsReceived = 0; | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++; | ||
} | ||
} | ||
|
||
|
||
} |
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); |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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); |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
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); |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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; } | ||
|
@@ -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) | ||
|
@@ -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)); | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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> | ||
|
@@ -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; | ||
} | ||
|
@@ -1500,4 +1507,4 @@ Square RookSquare(Square startSq, Piece rook) | |
|
||
return result; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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