Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumuzy committed Jan 16, 2023
1 parent a0fb5f6 commit 857529e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/ChessSharp/ChessGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public class ChessGame : IDeepCloneable<ChessGame>
/// <summary>Gets <see cref="Piece"/> in a specific square.</summary>
/// <param name="file">The <see cref="Linie"/> of the square.</param>
/// <param name="rank">The <see cref="Rank"/> of the square.</param>
public Piece? this[Linie file, Rank rank] => Board[(int)rank][(int)file];
public Piece this[Linie file, Rank rank] => Board[(int)rank][(int)file];

public Piece? this[Square sq] => Board[(int)sq.Rank][(int)sq.File];
public Piece this[Square sq] => Board[(int)sq.Rank][(int)sq.File];

public void SetPiece(Square sq, Piece? p) => Board[(int)sq.Rank][(int)sq.File] = p;
public void SetPiece(Square sq, Piece p) => Board[(int)sq.Rank][(int)sq.File] = p;

/// <summary>Gets a list of the game moves.</summary>
public Li<Move> Moves { get; private set; } // TODO: BAD! Investigate why the class consumer would even need this. Make it a private field if appropriate. And make it some kind of interface (`IEnumerable` for example).

/// <summary>Gets a 2D array of <see cref="Piece"/>s in the board.</summary>
public Piece?[][] Board { get; private set; } // TODO: It's bad idea to expose this to public.
public Piece[][] Board { get; private set; } // TODO: It's bad idea to expose this to public.

/// <summary>Gets the <see cref="Player"/> who has turn.</summary>
public Player WhoseTurn { get; private set; } = Player.White;
Expand Down
6 changes: 2 additions & 4 deletions src/ChessSharp/ChessUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static Li<Move> GetValidMoves(ChessGame board)
/// <param name="source">The source <see cref="Square"/> that you're looking for its valid moves.</param>
/// <param name="board">The <see cref="ChessGame"/> that you want to get its valid moves from the specified square.</param>
/// <returns>Returns a list of the valid moves that has the given source square.</returns>
///
public static Li<Move> GetValidMovesOfSourceSquare(ChessGame board, Square source)
{
if (board == null || source == null)
Expand All @@ -76,8 +75,8 @@ public static Li<Move> GetValidMovesOfTargetSquare(ChessGame board, Square targe
throw new ArgumentNullException(nameof(board) + " or " + nameof(target));

var validMoves = GetValidMoves(board);
validMoves = validMoves.Where(m => m.Destination == target && board[m.Source]?.Owner == board.WhoseTurn
&& board[m.Source]?.GetType() == pieceType).ToLi();
validMoves = validMoves.Where(m => m.Destination == target && board[m.Source].Owner == board.WhoseTurn
&& board[m.Source].GetType() == pieceType).ToLi();
return validMoves;
}

Expand Down Expand Up @@ -110,6 +109,5 @@ internal static bool IsPlayerInCheck(Player player, ChessGame board)
where piece.IsValidGameMove(move, board)
select piece).Any();
}

}
}
2 changes: 1 addition & 1 deletion src/ChessSharp/LichessPuzzle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static Move CreateMoveFromSMove(ChessGame game, string sMove, Player fir
throw new NotImplementedException();
// var possMoves = ChessUtilities.GetValidMovesOfTargetSquare(game, Square.Create(toString), typeof(Pawn));
}
else if (sMove[sMove.Length - 1].IsContainedIn(piecesChars))
else if (sMove[^1].IsContainedIn(piecesChars)) // 1 from end == last char.
{
// Bauernumwandlungszug
possMoves = possMoves.Where(m => m.Source.File == Parser.ParseFile(sMove[0])).ToLi();
Expand Down
5 changes: 1 addition & 4 deletions src/ChessSharp/SquareData/Square.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ public static Square Parse(ReadOnlySpan<char> square)
/// <summary>Represents a chess square.</summary>
public struct Square : IEquatable<Square>
{

/// <summary>
/// Initializes a new instance of the <c>Square</c>class.
/// </summary>
/// <summary> Initializes a new instance of the <c>Square</c>class. </summary>
/// <param name="file">A <see cref="SquareData.Linie"/> enum representing the file of the square.</param>
/// <param name="rank">A <see cref="SquareData.Rank"/> enum representing the rank of the square.</param>
public Square(Linie file, Rank rank)
Expand Down
18 changes: 9 additions & 9 deletions src/ChessUI/PuzzleSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public PuzzleGame NextPuzzle()
RebasePuzzles();
if (Puzzles[currentPuzzleNum + 1].NumCorrect != CurrentRound - 1)
RebasePuzzles();
game = new PuzzleGame(Puzzles[++currentPuzzleNum].SLichessPuzzle);
game = new PuzzleGame(Puzzles[++currentPuzzleNum].SRawPuzzle);
}
return game;
}
Expand Down Expand Up @@ -144,7 +144,7 @@ void SearchPuzzles()
// for debugging
foreach (var f in Filters)
{
var num = puzzles.Values.Where(p => f.IsMatching(p.SLichessPuzzle.Split(',').ToLiro())).Count();
var num = puzzles.Values.Where(p => f.IsMatching(p.SRawPuzzle.Split(',').ToLiro())).Count();
Debug.WriteLine($"Filter={f} Num={num}");
}
if (puzzles.Count < NumPuzzlesWanted)
Expand Down Expand Up @@ -283,12 +283,12 @@ internal class Puzzle
{
public Puzzle(string sLichessPuzzle)
{
this.SLichessPuzzle = sLichessPuzzle;
this.SRawPuzzle = sLichessPuzzle;
}

public Puzzle(string dbString, int dummy) => Read(dbString);

public string SLichessPuzzle { get; private set; }
public string SRawPuzzle { get; private set; }

public int NumCorrect { get; set; }

Expand All @@ -304,11 +304,11 @@ public Puzzle(string sLichessPuzzle)

public string Rating => SPuzzle.Split(',')[3];

public override string ToString() => $"TT={NumTriedInRound} C={NumCorrect} E= P={SLichessPuzzle}";
public override string ToString() => $"TT={NumTriedInRound} C={NumCorrect} E= P={SRawPuzzle}";

public string ToDbString() => $"TR={NumTriedInRound};C={NumCorrect};TT={NumTriedTotal};P={SLichessPuzzle}";
public string ToDbString() => $"TR={NumTriedInRound};C={NumCorrect};TT={NumTriedTotal};P={SRawPuzzle}";

private string SPuzzle => SLichessPuzzle.Split(',').Length >= 4 ? SLichessPuzzle : ",,,,,";
private string SPuzzle => SRawPuzzle.Split(',').Length >= 4 ? SRawPuzzle : ",,,,,";

public void Read(string dbString)
{
Expand All @@ -321,11 +321,11 @@ public void Read(string dbString)
case "C": NumCorrect = Helper.ToInt(parts2[1]); break;
case "TT": NumTriedTotal = Helper.ToInt(parts2[1]); break;
case "TR": NumTriedInRound = Helper.ToInt(parts2[1]); break;
case "P": SLichessPuzzle = parts2[1]; break;
case "P": SRawPuzzle = parts2[1]; break;
}
}
}

public override int GetHashCode() => (LichessId + Rating).GetHashCode();
public override int GetHashCode() => SRawPuzzle.GetHashCode();
}

0 comments on commit 857529e

Please sign in to comment.