Skip to content

Commit

Permalink
28/02/2022 Refactorings - Move Parameter and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Geras1mleo committed Feb 28, 2022
1 parent a357e3d commit e1d05cc
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 97 deletions.
24 changes: 12 additions & 12 deletions ChessBenchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public void MoveUsingMoveObject()
{
var board = new ChessBoard();

board.Move(new Move(new Position("e2"), new Position("e4")));
board.Move(new Move(new Position("e7"), new Position("e5")));
board.Move(new Move(new Position("g1"), new Position("e2")));
board.Move(new Move(new Position("f7"), new Position("f6")));
board.Move(new Move(new Position("b1"), new Position("c3")));
board.Move(new Move("e2", "e4"));
board.Move(new Move("e7", "e5"));
board.Move(new Move("g1", "e2"));
board.Move(new Move("f7", "f6"));
board.Move(new Move("b1", "c3"));
}

[Benchmark]
Expand Down Expand Up @@ -127,13 +127,13 @@ public class ChessIsValidMoveBenchmark
public void IsValidMove()
{
var board = new ChessBoard();
board.IsValidMove(new Move(new("b1"), new("c3")));
board.IsValidMove(new Move(new("c1"), new("g5")));
board.IsValidMove(new Move(new("d1"), new("d6")));
board.IsValidMove(new Move(new("e1"), new("f2")));
board.IsValidMove(new Move(new("e2"), new("e4")));
board.IsValidMove(new Move(new("g2"), new("g4")));
board.IsValidMove(new Move(new("b2"), new("b4")));
board.IsValidMove(new Move("b1", "c3"));
board.IsValidMove(new Move("c1", "g5"));
board.IsValidMove(new Move("d1", "d6"));
board.IsValidMove(new Move("e1", "f2"));
board.IsValidMove(new Move("e2", "e4"));
board.IsValidMove(new Move("g2", "g4"));
board.IsValidMove(new Move("b2", "b4"));
}

// Tests:
Expand Down
37 changes: 20 additions & 17 deletions ChessLibrary/ChessBoard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public partial class ChessBoard
/// <summary>
/// Whether board has been loaded from Forsyth-Edwards Notation
/// </summary>
public bool LoadedFromFEN => FenObj is not null;
public bool LoadedFromFen => FenObj is not null;

/// <summary>
/// Determinize whose player turn is it now
Expand All @@ -65,7 +65,7 @@ public PieceColor Turn
{
get
{
if (LoadedFromFEN)
if (LoadedFromFen)
return DisplayedMoves.Count % 2 == 0 ? FenObj.Turn : FenObj.Turn.OppositeColor();
else
return DisplayedMoves.Count % 2 == 0 ? PieceColor.White : PieceColor.Black;
Expand Down Expand Up @@ -112,7 +112,7 @@ private set
/// </summary>
public Position WhiteKing => GetKingPosition(PieceColor.White, this);
/// <summary>
/// Returns White king position on chess board
/// Returns Black king position on chess board
/// </summary>
public Position BlackKing => GetKingPosition(PieceColor.Black, this);

Expand All @@ -125,7 +125,7 @@ public Piece[] CapturedWhite
{
var cap = new List<Piece>();

if (LoadedFromFEN) cap.AddRange(FenObj.WhiteCaptured);
if (LoadedFromFen) cap.AddRange(FenObj.WhiteCaptured);

cap.AddRange(DisplayedMoves.Where(m => m.CapturedPiece?.Color == PieceColor.White).Select(m => new Piece(m.CapturedPiece.Color, m.CapturedPiece.Type)));

Expand All @@ -142,7 +142,7 @@ public Piece[] CapturedBlack
{
var cap = new List<Piece>();

if (LoadedFromFEN) cap.AddRange(FenObj.BlackCaptured);
if (LoadedFromFen) cap.AddRange(FenObj.BlackCaptured);

cap.AddRange(DisplayedMoves.Where(m => m.CapturedPiece?.Color == PieceColor.Black).Select(m => new Piece(m.CapturedPiece.Color, m.CapturedPiece.Type)));

Expand All @@ -152,7 +152,7 @@ public Piece[] CapturedBlack

private EndGameInfo? endGame;
/// <summary>
/// Represents End of game state(or null), won side(or draw) and type of end game
/// Represents End of game state(or null), won side(or null if draw) and type of end game
/// </summary>
public EndGameInfo? EndGame
{
Expand All @@ -177,11 +177,11 @@ private set
/// <summary>
/// Executed moves in SAN
/// </summary>
public List<string> MovesInSan => new List<Move>(executedMoves).Select(m => m.San).ToList();
public List<string> MovesToSan => new List<Move>(executedMoves).Select(m => m.San).ToList();

private int moveIndex = -1;
/// <summary>
/// Displayed move index in this chess board
/// Index of displayed move on this chess board
/// </summary>
public int MoveIndex
{
Expand All @@ -195,7 +195,8 @@ public int MoveIndex
}
}
/// <summary>
/// Is last move displayed on this chess board, false after DisplayPrevious()
/// Is last move displayed on this chess board<br/>
/// False after Previous() / First() / MoveIndex = ...
/// </summary>
public bool IsLastMoveDisplayed => moveIndex == executedMoves.Count - 1;

Expand All @@ -221,7 +222,7 @@ private ChessBoard(Piece?[,] pieces, List<Move> moves)
}

/// <summary>
/// Converts san move into Move object and performs it on chess board
/// Converts SAN move into Move object and performs it on chess board
/// </summary>
/// <param name="sanMove">Chess move in SAN</param>
public bool Move(string sanMove)
Expand Down Expand Up @@ -270,7 +271,7 @@ public bool Move(Move move)
/// <summary>
/// Adding header to this chess game<br/>
/// ex.:<br/>
/// name => Black; value => Geras1mleo<br/>
/// name => Black, value => Geras1mleo<br/>
/// Pgn Output: [Black "Geras1mleo"]
/// </summary>
/// <param name="name">Header name</param>
Expand Down Expand Up @@ -300,11 +301,13 @@ public void RemoveHeader(string name)
headers.Remove(name);
}

// Temporary disabled

/// <summary>
/// Puts given piece on given position<br/>
/// Warning! Checked state and end game state is not being updated
/// </summary>
public void Put(Piece piece, Position position)
private void Put(Piece piece, Position position)
{
pieces[position.Y, position.X] = piece;
}
Expand All @@ -313,13 +316,13 @@ public void Put(Piece piece, Position position)
/// Removes a piece on given position from board<br/>
/// Warning! Checked state and end game state is not being updated
/// </summary>
public void Remove(Position position)
private void Remove(Position position)
{
pieces[position.Y, position.X] = null;
}

/// <summary>
/// Clears board and restores begin positions
/// Clears board and sets begin positions
/// </summary>
public void Clear()
{
Expand Down Expand Up @@ -444,7 +447,7 @@ internal static void RestorePiece(Move move, ChessBoard board)

private void DisplayMoves(List<Move> moves)
{
if (LoadedFromFEN)
if (LoadedFromFen)
pieces = FenObj.Pieces;
else
SetChessBeginSituation();
Expand Down Expand Up @@ -479,7 +482,7 @@ private void HandleKingChecked()
}
else
{
if (LoadedFromFEN)
if (LoadedFromFen)
{
WhiteKingChecked = IsKingChecked(PieceColor.White, this);
BlackKingChecked = IsKingChecked(PieceColor.Black, this);
Expand All @@ -503,7 +506,7 @@ private void HandleEndGame()
}
else
{
if (LoadedFromFEN)
if (LoadedFromFen)
{
var mw = !PlayerHasMoves(PieceColor.White, this);
var mb = !PlayerHasMoves(PieceColor.Black, this);
Expand Down
26 changes: 10 additions & 16 deletions ChessLibrary/ChessConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public partial class ChessBoard
/// Converts San move into Move object<br/>
/// Long algebraic notation is also acceptable
/// </summary>
/// <param name="move">San move that will be converted</param>
/// <param name="move">San move to be converted</param>
/// <returns>Move object according to given san</returns>
/// <exception cref="ArgumentNullException">move was null</exception>
/// <exception cref="ArgumentException">Given move didn't match the Regex pattern</exception>
Expand Down Expand Up @@ -168,9 +168,9 @@ public string San(Move move)

StringBuilder builder = new();

if (move.Parameter is MoveCastle castle)
if (move.Parameter is MoveCastle)
{
builder.Append(castle.ShortStr);
builder.Append(move.Parameter.ShortStr);
goto CheckOrMateValidation;
}

Expand All @@ -193,8 +193,8 @@ public string San(Move move)

builder.Append(move.NewPosition);

if (move.Parameter is MovePromotion prom)
builder.Append(prom.ShortStr);
if (move.Parameter is MovePromotion)
builder.Append(move.Parameter.ShortStr);

// Not required
//else if (move.Parameter == MoveParameter.EnPassant)
Expand Down Expand Up @@ -238,20 +238,14 @@ public void LoadFen(string fen)
/// <summary>
/// Loads Chess game from Portable Game Notation<br/>
/// ex.:<br/>
/// [Event "Live Chess"]<br/>
/// [Site "Chess.com"]<br/>
/// [White "Milan1905"]<br/>
/// [Black "Geras1mleo"]<br/>
/// [Result "1-0"]<br/>
/// [WhiteElo "1006"]<br/>
/// [BlackElo "626"]<br/>
/// [EndTime "11:58:56 PST"]<br/>
/// [Termination "Milan1905 won by resignation"]<br/>
/// <br/>
/// 1. e4 e5 2. Nf3 Nf6 3. Nc3 Nc6 4. Bb5 Bc5 5. Bxc6 bxc6 6. Nxe5 Bxf2+ 7. Kxf2 O-O
/// 8. d4 d5 (8... c5 9. b4 (9. a3 c4 10. b4 cxb3) 9... c4) 9. exd5 cxd5 10. Nc6
/// Ng4+ 11. Kg1 Qf6 12. Qf1 Qxc6 13. h3 Nf6 14. Bg5 Qb6 15. Bxf6 Qxf6 16. Qxf6 gxf6
/// 17. Nxd5 Rb8 18. Nxf6+ Kh8 19. b3 Rb4 20. c3 Bb7 21. cxb4 1-0
/// 1. e4 e5 2. Nf3 Nf6 3...
/// </summary>
/// <param name="pgn">PGN string</param>
public void LoadPgn(string pgn)
Expand Down Expand Up @@ -371,7 +365,7 @@ public string ToPgn()
if (moveIndex == -1)
{
// From position?
if (LoadedFromFEN && FenObj.Turn == PieceColor.Black)
if (LoadedFromFen && FenObj.Turn == PieceColor.Black)
builder.Append("..");
}

Expand Down Expand Up @@ -405,7 +399,7 @@ public string ToAscii(bool displayFull = false)

for (int i = 8 - 1; i >= 0; i--)
{
builder.Append(" " + (i + 1) + "│");
builder.Append(" " + (i + 1) + " │");
for (int j = 0; j < 8; j++)
{
builder.Append(' ');
Expand Down Expand Up @@ -498,7 +492,7 @@ internal static int GetHalfMovesCount(ChessBoard board)
{
int index = board.DisplayedMoves.FindLastIndex(m => m.CapturedPiece is not null || m.Piece.Type == PieceType.Pawn);

if (board.LoadedFromFEN && index < 0)
if (board.LoadedFromFen && index < 0)
return board.FenObj.HalfMoves + board.moveIndex + 1;

if (index >= 0)
Expand All @@ -511,7 +505,7 @@ internal static int GetFullMovesCount(ChessBoard board)
{
var count = 0;

if (board.LoadedFromFEN)
if (board.LoadedFromFen)
count += (board.FenObj.FullMoves * 2) + (board.FenObj.Turn == PieceColor.Black ? 1 : 0) - 2;

return (board.moveIndex + count + 3) / 2;
Expand Down
2 changes: 1 addition & 1 deletion ChessLibrary/ChessEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public partial class ChessBoard
/// </summary>
public event ChessCheckedChangedEventHandler OnBlackKingCheckedChanged = delegate { };
/// <summary>
/// Raises when user has to choose promote action
/// Raises when user has to choose promotion action
/// </summary>
public event ChessPromotionResultEventHandler OnPromotePawn = delegate { };
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions ChessLibrary/ChessValidations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ internal static bool HasRightToCastle(PieceColor side, CastleType castleType, Ch
{
var valid = false;

if (board.LoadedFromFEN)
if (board.LoadedFromFen)
{
if (side == PieceColor.White)
{
Expand Down Expand Up @@ -572,7 +572,7 @@ private static bool IsValidEnPassant(Move move, ChessBoard board, short v, short
if (board.moveIndex >= 0)
valid = LastMoveEnPassantPosition(board) == move.NewPosition;

else if (board.LoadedFromFEN)
else if (board.LoadedFromFen)
valid = board.FenObj.EnPassant == move.NewPosition;

return valid;
Expand Down
3 changes: 2 additions & 1 deletion ChessLibrary/Types/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Made by Geras1mleo

#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

namespace Chess;

public enum EndgameType : byte
Expand All @@ -22,7 +23,7 @@ public enum EndgameType : byte
Repetition,
}

internal enum CastleType : byte
public enum CastleType : byte
{
King,
Queen,
Expand Down
11 changes: 10 additions & 1 deletion ChessLibrary/Types/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class Move
public string? San { get; internal set; }

/// <summary>
/// Initializes new Move that has to be validated
/// Initializes new Move object by given positions
/// </summary>
public Move(Position originalPosition, Position newPosition)
{
Expand Down Expand Up @@ -124,6 +124,15 @@ public Move(string move)
}
}

/// <summary>
/// Initializes new Move object by given positions
/// </summary>
public Move(string originalPos, string newPos)
{
OriginalPosition = new(originalPos);
NewPosition = new(newPos);
}

/// <summary>
/// Needed to Generate move from SAN in ChessConversions
/// </summary>
Expand Down
Loading

0 comments on commit e1d05cc

Please sign in to comment.