From 857529e4845c155e587a122f4f05da1e4f293f0d Mon Sep 17 00:00:00 2001 From: Andreas Date: Mon, 16 Jan 2023 22:37:22 +0100 Subject: [PATCH] Refactoring --- src/ChessSharp/ChessGame.cs | 8 ++++---- src/ChessSharp/ChessUtilities.cs | 6 ++---- src/ChessSharp/LichessPuzzle.cs | 2 +- src/ChessSharp/SquareData/Square.cs | 5 +---- src/ChessUI/PuzzleSet.cs | 18 +++++++++--------- 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/ChessSharp/ChessGame.cs b/src/ChessSharp/ChessGame.cs index 8c581c2..dd65176 100644 --- a/src/ChessSharp/ChessGame.cs +++ b/src/ChessSharp/ChessGame.cs @@ -14,17 +14,17 @@ public class ChessGame : IDeepCloneable /// Gets in a specific square. /// The of the square. /// The of the square. - 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; /// Gets a list of the game moves. public Li 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). /// Gets a 2D array of s in the board. - 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. /// Gets the who has turn. public Player WhoseTurn { get; private set; } = Player.White; diff --git a/src/ChessSharp/ChessUtilities.cs b/src/ChessSharp/ChessUtilities.cs index 8c580f9..a896640 100644 --- a/src/ChessSharp/ChessUtilities.cs +++ b/src/ChessSharp/ChessUtilities.cs @@ -50,7 +50,6 @@ public static Li GetValidMoves(ChessGame board) /// The source that you're looking for its valid moves. /// The that you want to get its valid moves from the specified square. /// Returns a list of the valid moves that has the given source square. - /// public static Li GetValidMovesOfSourceSquare(ChessGame board, Square source) { if (board == null || source == null) @@ -76,8 +75,8 @@ public static Li 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; } @@ -110,6 +109,5 @@ internal static bool IsPlayerInCheck(Player player, ChessGame board) where piece.IsValidGameMove(move, board) select piece).Any(); } - } } diff --git a/src/ChessSharp/LichessPuzzle.cs b/src/ChessSharp/LichessPuzzle.cs index 8d00f57..644b598 100644 --- a/src/ChessSharp/LichessPuzzle.cs +++ b/src/ChessSharp/LichessPuzzle.cs @@ -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(); diff --git a/src/ChessSharp/SquareData/Square.cs b/src/ChessSharp/SquareData/Square.cs index 13cb3d0..4beeb3e 100644 --- a/src/ChessSharp/SquareData/Square.cs +++ b/src/ChessSharp/SquareData/Square.cs @@ -54,10 +54,7 @@ public static Square Parse(ReadOnlySpan square) /// Represents a chess square. public struct Square : IEquatable { - - /// - /// Initializes a new instance of the Squareclass. - /// + /// Initializes a new instance of the Squareclass. /// A enum representing the file of the square. /// A enum representing the rank of the square. public Square(Linie file, Rank rank) diff --git a/src/ChessUI/PuzzleSet.cs b/src/ChessUI/PuzzleSet.cs index 939816c..a654e6c 100644 --- a/src/ChessUI/PuzzleSet.cs +++ b/src/ChessUI/PuzzleSet.cs @@ -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; } @@ -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) @@ -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; } @@ -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) { @@ -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(); }