From 6501139f45e38b800f23bf70bbe1d3862813d7c8 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Mon, 3 Jun 2024 17:12:54 +0200 Subject: [PATCH 1/8] Fixed Persistence exception with unreadable EOF --- Qwirkle/QwirkleClassLibrary/Games/Game.cs | 6 ++++ .../Persistences/GamePersistenceXml.cs | 12 +++++-- Qwirkle/QwirkleConsoleApp/Program.cs | 35 +++++++++++++------ 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index ad12627..cab223f 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -384,6 +384,12 @@ namespace QwirkleClassLibrary.Games /// bool public bool SwapTiles(Player player, List tilesToSwap) { + if (cellUsed.Count != 0) + { + OnSwapTiles(new SwapTilesNotifiedEventArgs("You can't swap tiles after placing some !")); + return false; + } + if (tilesToSwap.Count == 0) { OnSwapTiles(new SwapTilesNotifiedEventArgs("You must select at least one tile to swap !")); diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs index 8325cc2..564134a 100644 --- a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs +++ b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs @@ -20,9 +20,17 @@ public class GamePersistenceXml : IGamePersistence { var serializer = new DataContractSerializer(typeof(Game)); - using (Stream reader = File.OpenRead("Game.xml")) + try { - return serializer.ReadObject(reader) as Game ?? throw new InvalidOperationException(); + using (Stream reader = File.OpenRead("Game.xml")) + { + var newGame = serializer.ReadObject(reader) as Game; + return newGame!; + } + } + catch + { + return new Game(); } } } \ No newline at end of file diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index a6224bb..9961774 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -1,17 +1,9 @@ using QwirkleClassLibrary.Boards; -using QwirkleClassLibrary.Events; using QwirkleClassLibrary.Games; using QwirkleClassLibrary.Players; using QwirkleClassLibrary.Tiles; using QwirkleConsoleApp; -using System.Collections.Immutable; -using System.Collections.ObjectModel; -using System.Diagnostics.Metrics; -using System.Net.Quic; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Json; using System.Text; -using System.Transactions; using QwirkleClassLibrary.Persistences; using static System.Console; @@ -423,12 +415,33 @@ static void MainGame() Game loadedGame; IGamePersistence gameLoad = new GamePersistenceXml(); - loadedGame = gameLoad.LoadGame(); + + try + { + loadedGame = gameLoad.LoadGame(); + } + catch + { + ForegroundColor = ConsoleColor.Red; + WriteLine("ERROR : No game saved ! Creating a new game !"); + ResetColor(); + loadedGame = new Game(); + } ILeaderboardPersistence leaderboardLoad = new LeaderboardPersistenceJson(); leaderboard = leaderboardLoad.LoadLeaderboard(); - - MainMenuContinue(loadedGame); + + if (loadedGame.GameRunning) + { + MainMenuContinue(loadedGame); + } + else + { + AddPlayers(loadedGame); + loadedGame.StartGame(); + MainMenu(loadedGame); + } + leaderboard.AddScoreInLead(loadedGame.ScoreBoard); break; From b91feee7a21e4e699c66cb7676d91a50fbbc2d0a Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Mon, 3 Jun 2024 18:37:00 +0200 Subject: [PATCH 2/8] Slight persistence fix + fixed Leaderboard behavior --- Qwirkle/Files/Game.xml | 1551 +---------------- Qwirkle/Files/LeaderBoard.json | 17 +- .../Players/Leaderboard.cs | 7 +- Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs | 36 +- Qwirkle/QwirkleConsoleApp/Program.cs | 26 +- 5 files changed, 65 insertions(+), 1572 deletions(-) diff --git a/Qwirkle/Files/Game.xml b/Qwirkle/Files/Game.xml index 3909ecb..b191d2f 100644 --- a/Qwirkle/Files/Game.xml +++ b/Qwirkle/Files/Game.xml @@ -1,1550 +1 @@ - - true - - - - Red - Square - - - Blue - Square - - - Green - Square - - - Yellow - Square - - - Orange - Square - - - Purple - Square - - - Red - Round - - - Blue - Round - - - Green - Round - - - Orange - Round - - - Purple - Round - - - Green - Rhombus - - - Yellow - Rhombus - - - Purple - Rhombus - - - Red - Club - - - Blue - Club - - - Green - Club - - - Yellow - Club - - - Purple - Club - - - Red - Shuriken - - - Blue - Shuriken - - - Green - Shuriken - - - Purple - Shuriken - - - Red - Star - - - Blue - Star - - - Green - Star - - - Yellow - Star - - - Orange - Star - - - Purple - Star - - - Red - Square - - - Blue - Square - - - Green - Square - - - Yellow - Square - - - Blue - Round - - - Purple - Round - - - Red - Rhombus - - - Green - Rhombus - - - Yellow - Rhombus - - - Purple - Rhombus - - - Blue - Club - - - Green - Club - - - Yellow - Club - - - Purple - Club - - - Red - Shuriken - - - Green - Shuriken - - - Yellow - Shuriken - - - Orange - Shuriken - - - Purple - Shuriken - - - Red - Star - - - Blue - Star - - - Orange - Star - - - Purple - Star - - - Red - Square - - - Blue - Square - - - Yellow - Square - - - Orange - Square - - - Purple - Square - - - Blue - Round - - - Green - Round - - - Yellow - Round - - - Purple - Round - - - Red - Rhombus - - - Blue - Rhombus - - - Green - Rhombus - - - Yellow - Rhombus - - - Orange - Rhombus - - - Purple - Rhombus - - - Red - Club - - - Blue - Club - - - Green - Club - - - Yellow - Club - - - Orange - Club - - - Purple - Club - - - Red - Shuriken - - - Blue - Shuriken - - - Green - Shuriken - - - Yellow - Shuriken - - - Orange - Shuriken - - - Purple - Shuriken - - - Red - Star - - - Green - Star - - - Yellow - Star - - - Orange - Star - - - Purple - Star - - - Orange - Round - - - Orange - Club - - - Red - Round - - - - - 15 - 12 - - - - Blue - Rhombus - - 0 - 0 - - - - 0 - 1 - - - - Orange - Round - - 0 - 2 - - - - Yellow - Round - - 0 - 3 - - - - Red - Round - - 0 - 4 - - - - 0 - 5 - - - - 0 - 6 - - - - 0 - 7 - - - - 0 - 8 - - - - 0 - 9 - - - - 0 - 10 - - - - 0 - 11 - - - - 0 - 12 - - - - 0 - 13 - - - - 0 - 14 - - - - Red - Rhombus - - 1 - 0 - - - - Blue - Rhombus - - 1 - 1 - - - - Orange - Rhombus - - 1 - 2 - - - - 1 - 3 - - - - 1 - 4 - - - - 1 - 5 - - - - 1 - 6 - - - - 1 - 7 - - - - 1 - 8 - - - - 1 - 9 - - - - 1 - 10 - - - - 1 - 11 - - - - 1 - 12 - - - - 1 - 13 - - - - 1 - 14 - - - - 2 - 0 - - - - Blue - Star - - 2 - 1 - - - - 2 - 2 - - - - 2 - 3 - - - - 2 - 4 - - - - 2 - 5 - - - - 2 - 6 - - - - 2 - 7 - - - - 2 - 8 - - - - 2 - 9 - - - - 2 - 10 - - - - 2 - 11 - - - - 2 - 12 - - - - 2 - 13 - - - - 2 - 14 - - - - 3 - 0 - - - - Blue - Shuriken - - 3 - 1 - - - - 3 - 2 - - - - 3 - 3 - - - - 3 - 4 - - - - 3 - 5 - - - - 3 - 6 - - - - 3 - 7 - - - - 3 - 8 - - - - 3 - 9 - - - - 3 - 10 - - - - 3 - 11 - - - - 3 - 12 - - - - 3 - 13 - - - - 3 - 14 - - - - 4 - 0 - - - - 4 - 1 - - - - 4 - 2 - - - - 4 - 3 - - - - 4 - 4 - - - - 4 - 5 - - - - 4 - 6 - - - - 4 - 7 - - - - 4 - 8 - - - - 4 - 9 - - - - 4 - 10 - - - - 4 - 11 - - - - 4 - 12 - - - - 4 - 13 - - - - 4 - 14 - - - - 5 - 0 - - - - 5 - 1 - - - - 5 - 2 - - - - 5 - 3 - - - - 5 - 4 - - - - 5 - 5 - - - - 5 - 6 - - - - 5 - 7 - - - - 5 - 8 - - - - 5 - 9 - - - - 5 - 10 - - - - 5 - 11 - - - - 5 - 12 - - - - 5 - 13 - - - - 5 - 14 - - - - 6 - 0 - - - - 6 - 1 - - - - 6 - 2 - - - - 6 - 3 - - - - 6 - 4 - - - - 6 - 5 - - - - 6 - 6 - - - - 6 - 7 - - - - 6 - 8 - - - - 6 - 9 - - - - 6 - 10 - - - - 6 - 11 - - - - 6 - 12 - - - - 6 - 13 - - - - 6 - 14 - - - - 7 - 0 - - - - 7 - 1 - - - - 7 - 2 - - - - 7 - 3 - - - - 7 - 4 - - - - 7 - 5 - - - - 7 - 6 - - - - 7 - 7 - - - - 7 - 8 - - - - 7 - 9 - - - - 7 - 10 - - - - 7 - 11 - - - - 7 - 12 - - - - 7 - 13 - - - - 7 - 14 - - - - 8 - 0 - - - - 8 - 1 - - - - 8 - 2 - - - - 8 - 3 - - - - 8 - 4 - - - - 8 - 5 - - - - 8 - 6 - - - - 8 - 7 - - - - 8 - 8 - - - - 8 - 9 - - - - 8 - 10 - - - - 8 - 11 - - - - 8 - 12 - - - - 8 - 13 - - - - 8 - 14 - - - - 9 - 0 - - - - 9 - 1 - - - - 9 - 2 - - - - 9 - 3 - - - - 9 - 4 - - - - 9 - 5 - - - - 9 - 6 - - - - 9 - 7 - - - - 9 - 8 - - - - 9 - 9 - - - - 9 - 10 - - - - 9 - 11 - - - - 9 - 12 - - - - 9 - 13 - - - - 9 - 14 - - - - 10 - 0 - - - - 10 - 1 - - - - 10 - 2 - - - - 10 - 3 - - - - 10 - 4 - - - - 10 - 5 - - - - 10 - 6 - - - - 10 - 7 - - - - 10 - 8 - - - - 10 - 9 - - - - 10 - 10 - - - - 10 - 11 - - - - 10 - 12 - - - - 10 - 13 - - - - 10 - 14 - - - - 11 - 0 - - - - 11 - 1 - - - - 11 - 2 - - - - 11 - 3 - - - - 11 - 4 - - - - 11 - 5 - - - - 11 - 6 - - - - 11 - 7 - - - - 11 - 8 - - - - 11 - 9 - - - - 11 - 10 - - - - 11 - 11 - - - - 11 - 12 - - - - 11 - 13 - - - - 11 - 14 - - - - - - - true - Jules - - - Yellow - Star - - - Orange - Club - - - Orange - Square - - - Yellow - Round - - - Orange - Rhombus - - - Green - Star - - - - - false - Jérémy - - - Green - Square - - - Purple - Square - - - Red - Club - - - Green - Round - - - Yellow - Shuriken - - - Orange - Shuriken - - - - - - - - 8 - - - - 9 - - - \ No newline at end of file +false15120001020304050607080901001101201301410RedRound11RedSquare121314151617181911011111211311420OrangeRound21OrangeSquare222324252627282921021121221321430PurpleRound31PurpleSquare323334353637383931031131231331440BlueRound41BlueSquare424344454647484941041141241341450YellowRound51525354555657585951051151251351460GreenRound6162636465666768696106116126136147071727374757677787971071171271371480818283848586878889810811812813814909192939495969798999109119129139141001011021031041051061071081091010101110121013101411011111211311411511611711811911101111111211131114falseJulesGreenSquareYellowSquaretrueJérémy430 \ No newline at end of file diff --git a/Qwirkle/Files/LeaderBoard.json b/Qwirkle/Files/LeaderBoard.json index fbec232..d109a1c 100644 --- a/Qwirkle/Files/LeaderBoard.json +++ b/Qwirkle/Files/LeaderBoard.json @@ -1 +1,16 @@ -{"leaderboard":[]} \ No newline at end of file +{ + "leaderboard": [ + { + "Date": "\/Date(1717432093871+0200)\/", + "PlayerName": "Jérémy", + "Points": 30, + "Victories": 2 + }, + { + "Date": "\/Date(1717432093871+0200)\/", + "PlayerName": "Jules", + "Points": 4, + "Victories": 0 + } + ] +} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs b/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs index 3a06518..db9a0b6 100644 --- a/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs +++ b/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs @@ -41,7 +41,7 @@ namespace QwirkleClassLibrary.Players /// public void AddScoreInLead(ReadOnlyDictionary scoreBoard) { - DateTime now = DateTime.Today; + DateTime now = DateTime.Now; bool first = true; var sb = scoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key.NameTag); @@ -59,7 +59,10 @@ namespace QwirkleClassLibrary.Players leaderboard[i].Victories++; } - leaderboard[i].Points = pair.Value; + if (pair.Value > leaderboard[i].Points) + { + leaderboard[i].Points = pair.Value; + } } else diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs index fc6381a..e616b4e 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs @@ -29,17 +29,31 @@ namespace QwirkleClassLibrary.Tiles throw new ArgumentException(nbSet.ToString()); } - for (int i = 0; i < nbSet; i++) - { - foreach (Shape s in Enum.GetValues(typeof(Shape))) - { - foreach (Color c in Enum.GetValues(typeof(Color))) - { - Tile t = new Tile(s, c); - tiles.Add(t); - } - } - } + // for (int i = 0; i < nbSet; i++) + // { + // foreach (Shape s in Enum.GetValues(typeof(Shape))) + // { + // foreach (Color c in Enum.GetValues(typeof(Color))) + // { + // Tile t = new Tile(s, c); + // tiles.Add(t); + // } + // } + // } + + tiles.Add(new Tile(Shape.Round, Color.Red)); + tiles.Add(new Tile(Shape.Round, Color.Orange)); + tiles.Add(new Tile(Shape.Round, Color.Yellow)); + tiles.Add(new Tile(Shape.Round, Color.Green)); + tiles.Add(new Tile(Shape.Round, Color.Blue)); + tiles.Add(new Tile(Shape.Round, Color.Purple)); + + tiles.Add(new Tile(Shape.Square, Color.Red)); + tiles.Add(new Tile(Shape.Square, Color.Orange)); + tiles.Add(new Tile(Shape.Square, Color.Yellow)); + tiles.Add(new Tile(Shape.Square, Color.Green)); + tiles.Add(new Tile(Shape.Square, Color.Blue)); + tiles.Add(new Tile(Shape.Square, Color.Purple)); Init(); } diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index 9961774..bd5f370 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -398,12 +398,10 @@ static void MainGame() game.StartGame(); MainMenu(game); - foreach (var player in game.PlayerList) + if (!game.GameRunning) { - if (game.CheckGameOver(player)) - { - leaderboard.AddScoreInLead(game.ScoreBoard); - } + leaderboard.AddScoreInLead(game.ScoreBoard); + break; } ILeaderboardPersistence leaderboardSave = new LeaderboardPersistenceJson(); @@ -422,8 +420,8 @@ static void MainGame() } catch { - ForegroundColor = ConsoleColor.Red; - WriteLine("ERROR : No game saved ! Creating a new game !"); + ForegroundColor = ConsoleColor.Yellow; + WriteLine("WARNING : No game saved ! Creating a new game !"); ResetColor(); loadedGame = new Game(); } @@ -437,16 +435,28 @@ static void MainGame() } else { + loadedGame = new Game(); + ForegroundColor = ConsoleColor.Yellow; + WriteLine("WARNING : The previous saved game was over ! Creating a new game !"); + ResetColor(); AddPlayers(loadedGame); loadedGame.StartGame(); MainMenu(loadedGame); } - leaderboard.AddScoreInLead(loadedGame.ScoreBoard); + if (!loadedGame.GameRunning) + { + leaderboard.AddScoreInLead(loadedGame.ScoreBoard); + } + + ILeaderboardPersistence leaderboardSave2 = new LeaderboardPersistenceJson(); + leaderboardSave2.SaveLeaderboard(leaderboard); break; case 3: + ILeaderboardPersistence leaderboardLoad2 = new LeaderboardPersistenceJson(); + leaderboard = leaderboardLoad2.LoadLeaderboard(); ShowLeaderboard(leaderboard); break; From 4dd8210b0f9463222f16741e6e71deef4266208b Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Mon, 3 Jun 2024 18:38:42 +0200 Subject: [PATCH 3/8] My bad ! --- Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs | 36 ++++++-------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs index e616b4e..fc6381a 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs @@ -29,31 +29,17 @@ namespace QwirkleClassLibrary.Tiles throw new ArgumentException(nbSet.ToString()); } - // for (int i = 0; i < nbSet; i++) - // { - // foreach (Shape s in Enum.GetValues(typeof(Shape))) - // { - // foreach (Color c in Enum.GetValues(typeof(Color))) - // { - // Tile t = new Tile(s, c); - // tiles.Add(t); - // } - // } - // } - - tiles.Add(new Tile(Shape.Round, Color.Red)); - tiles.Add(new Tile(Shape.Round, Color.Orange)); - tiles.Add(new Tile(Shape.Round, Color.Yellow)); - tiles.Add(new Tile(Shape.Round, Color.Green)); - tiles.Add(new Tile(Shape.Round, Color.Blue)); - tiles.Add(new Tile(Shape.Round, Color.Purple)); - - tiles.Add(new Tile(Shape.Square, Color.Red)); - tiles.Add(new Tile(Shape.Square, Color.Orange)); - tiles.Add(new Tile(Shape.Square, Color.Yellow)); - tiles.Add(new Tile(Shape.Square, Color.Green)); - tiles.Add(new Tile(Shape.Square, Color.Blue)); - tiles.Add(new Tile(Shape.Square, Color.Purple)); + for (int i = 0; i < nbSet; i++) + { + foreach (Shape s in Enum.GetValues(typeof(Shape))) + { + foreach (Color c in Enum.GetValues(typeof(Color))) + { + Tile t = new Tile(s, c); + tiles.Add(t); + } + } + } Init(); } From 004a7c190cbf5e3f8bfbf490fe8676e9de1ec192 Mon Sep 17 00:00:00 2001 From: rportet Date: Mon, 3 Jun 2024 19:03:23 +0200 Subject: [PATCH 4/8] code smell bye bye --- Qwirkle/QwirkleClassLibrary/Games/Game.cs | 8 ++++---- Qwirkle/QwirkleClassLibrary/Players/Player.cs | 2 +- Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index cab223f..f4729b8 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -27,7 +27,7 @@ namespace QwirkleClassLibrary.Games public bool GameRunning { get; set; } [DataMember] - private Board board = new Board(15, 12); + private Board board = new(15, 12); public bool PlayerSwapping { get; set; } public Board Board => board; @@ -35,15 +35,15 @@ namespace QwirkleClassLibrary.Games public ReadOnlyCollection PlayerList => players.AsReadOnly(); [DataMember] - private readonly List players = new(); + private readonly List players = []; [DataMember] - private readonly Dictionary scoreBoard = new(); + private readonly Dictionary scoreBoard = []; public ReadOnlyDictionary ScoreBoard => scoreBoard.AsReadOnly(); [DataMember] - private readonly List cellUsed = new(); + private readonly List cellUsed = []; public ReadOnlyCollection CellsUsed => cellUsed.AsReadOnly(); diff --git a/Qwirkle/QwirkleClassLibrary/Players/Player.cs b/Qwirkle/QwirkleClassLibrary/Players/Player.cs index 0274620..5d8cb7a 100644 --- a/Qwirkle/QwirkleClassLibrary/Players/Player.cs +++ b/Qwirkle/QwirkleClassLibrary/Players/Player.cs @@ -37,7 +37,7 @@ namespace QwirkleClassLibrary.Players } NameTag = name; - Tiles = new ObservableCollection(); + Tiles = []; } [DataMember] diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs index fc6381a..44c7129 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs @@ -12,7 +12,7 @@ namespace QwirkleClassLibrary.Tiles public class TileBag { [DataMember] - private readonly List tiles = new List(); + private readonly List tiles = []; public ReadOnlyCollection TilesBag { get; private set; } @@ -35,7 +35,7 @@ namespace QwirkleClassLibrary.Tiles { foreach (Color c in Enum.GetValues(typeof(Color))) { - Tile t = new Tile(s, c); + Tile t = new(s, c); tiles.Add(t); } } From 4b18224ca1ec06648c541a7f55a728a36c5726b2 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Mon, 3 Jun 2024 19:11:40 +0200 Subject: [PATCH 5/8] fix TileBag code smell --- Qwirkle/QwirkleClassLibrary/Games/Game.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index cab223f..d993b91 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -21,7 +21,7 @@ namespace QwirkleClassLibrary.Games public class Game : IPlayer, IRules { [DataMember] - public TileBag? bag = null; + private TileBag? bag = null; [DataMember] public bool GameRunning { get; set; } From 1f61ed27e1fe42c231359914389d7b2361dd3fd5 Mon Sep 17 00:00:00 2001 From: rportet Date: Mon, 3 Jun 2024 19:21:34 +0200 Subject: [PATCH 6/8] code smell bye bye 2 --- Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs | 2 +- Qwirkle/TestBase/TestPlayer.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs index 44c7129..291a4d0 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs @@ -14,7 +14,7 @@ namespace QwirkleClassLibrary.Tiles [DataMember] private readonly List tiles = []; - public ReadOnlyCollection TilesBag { get; private set; } + public ReadOnlyCollection? TilesBag { get; private set; } /// diff --git a/Qwirkle/TestBase/TestPlayer.cs b/Qwirkle/TestBase/TestPlayer.cs index 5c2b92a..fbf2147 100644 --- a/Qwirkle/TestBase/TestPlayer.cs +++ b/Qwirkle/TestBase/TestPlayer.cs @@ -7,7 +7,7 @@ public class TestPlayers [Theory] [InlineData(true, "Mathis")] [InlineData(false, null)] - public void Test_CreatePlayer(bool isValid, string playertag) + public void Test_CreatePlayer(bool isValid, string? playertag) { if (!isValid) { From 9ec219a98817cc147235f3d3a1a166eb2e09478b Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Mon, 3 Jun 2024 19:21:57 +0200 Subject: [PATCH 7/8] Made tilesBag private in Game => Code smell fix --- Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs index 44c7129..291a4d0 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/TileBag.cs @@ -14,7 +14,7 @@ namespace QwirkleClassLibrary.Tiles [DataMember] private readonly List tiles = []; - public ReadOnlyCollection TilesBag { get; private set; } + public ReadOnlyCollection? TilesBag { get; private set; } /// From b35512d8b19eed0f3e5055d4cf6037f40187f132 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Mon, 3 Jun 2024 19:28:21 +0200 Subject: [PATCH 8/8] Dereference of a possibly null reference x10 --- Qwirkle/QwirkleClassLibrary/Games/Game.cs | 20 ++++++-------------- Qwirkle/TestBase/TestPlayer.cs | 4 ++-- Qwirkle/TestBase/TestTileBag.cs | 6 +++--- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index 07d0050..9c9c078 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -1,13 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Xml.Linq; +using System.Collections.ObjectModel; using System.Security.Cryptography; -using System.Collections; -using System.Collections.Immutable; using System.Runtime.Serialization; using QwirkleClassLibrary.Tiles; using QwirkleClassLibrary.Boards; @@ -270,7 +262,7 @@ namespace QwirkleClassLibrary.Games { if (bag != null && p.Tiles.Count < 6) { - int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag.Count); + int val = RandomNumberGenerator.GetInt32(0, bag.TilesBag!.Count); p.AddTileToPlayer(bag.TilesBag[val]); bag.RemoveTileInBag(bag.TilesBag[val]); @@ -362,7 +354,7 @@ namespace QwirkleClassLibrary.Games { while (player.Tiles.Count < 6) { - if (bag!.TilesBag.Count == 0) + if (bag!.TilesBag!.Count == 0) { return false; } @@ -396,7 +388,7 @@ namespace QwirkleClassLibrary.Games return false; } - if (bag!.TilesBag.Count < tilesToSwap.Count) + if (bag!.TilesBag!.Count < tilesToSwap.Count) { OnSwapTiles(new SwapTilesNotifiedEventArgs("Not enough tiles in the bag to swap !")); return false; @@ -733,7 +725,7 @@ namespace QwirkleClassLibrary.Games { List playerTilesBagPos = []; - if (bag!.TilesBag.Count == 0) + if (bag!.TilesBag!.Count == 0) { for (int i = 0; i < players.Count; i++) { @@ -785,7 +777,7 @@ namespace QwirkleClassLibrary.Games { List playerTilesBagPos = CheckTilesBag(); - if (playerTilesBagPos.Count != 0 && !CheckPlacementPossibilities(playerTilesBagPos) || bag!.TilesBag.Count == 0 && players[GetPlayingPlayerPosition()].Tiles.Count==0) + if (playerTilesBagPos.Count != 0 && !CheckPlacementPossibilities(playerTilesBagPos) || bag!.TilesBag!.Count == 0 && players[GetPlayingPlayerPosition()].Tiles.Count==0) { OnEndOfGame(new EndOfGameNotifiedEventArgs(player)); GameRunning = false; diff --git a/Qwirkle/TestBase/TestPlayer.cs b/Qwirkle/TestBase/TestPlayer.cs index fbf2147..1727423 100644 --- a/Qwirkle/TestBase/TestPlayer.cs +++ b/Qwirkle/TestBase/TestPlayer.cs @@ -11,10 +11,10 @@ public class TestPlayers { if (!isValid) { - Assert.Throws(() => new Player(playertag)); + Assert.Throws(() => new Player(playertag!)); return; } - Player player = new Player(playertag); + Player player = new Player(playertag!); Assert.Equal(playertag, player.NameTag); } diff --git a/Qwirkle/TestBase/TestTileBag.cs b/Qwirkle/TestBase/TestTileBag.cs index cbfc07b..e202809 100644 --- a/Qwirkle/TestBase/TestTileBag.cs +++ b/Qwirkle/TestBase/TestTileBag.cs @@ -16,7 +16,7 @@ public class TestTileBag return; } TileBag bag = new TileBag(nbset); - Assert.Equal(bag.TilesBag.Count, nbset * 36); + Assert.Equal(bag.TilesBag!.Count, nbset * 36); } @@ -30,11 +30,11 @@ public class TestTileBag if (except) { - Assert.True(bag.RemoveTileInBag(bag.TilesBag[1])); + Assert.True(bag.RemoveTileInBag(bag.TilesBag![1])); return; } - Tile tile = bag.TilesBag[0]; + Tile tile = bag.TilesBag![0]; bag.RemoveTileInBag(tile); Assert.False(bag.RemoveTileInBag(tile));