From ecfcc1cb3723d068df34eaecfa89fe5f6c99f9d7 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Fri, 7 Jun 2024 15:21:28 +0200 Subject: [PATCH] Fix 1 minor code smell (normally) --- Qwirkle/QwirkleClassLibrary/Games/Game.cs | 65 +++++++++++++---------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index c374d43..9543a70 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -551,6 +551,35 @@ namespace QwirkleClassLibrary.Games return false; } + + public static bool CheckTileInCompletedLines(Tile? t1, Tile? t2, ref int nbTiles, ref List checkdoubles) + { + if (t1 != null) + { + nbTiles++; + + if (checkdoubles.Any(t => t.CompareTo(t1) == 0)) + { + return false; + } + + checkdoubles.Add(t1); + } + + if (t2 == null) return true; + { + nbTiles++; + + if (checkdoubles.Any(t => t.CompareTo(t2) == 0)) + { + return false; + } + + checkdoubles.Add(t2); + } + + return true; + } public bool CheckWrongCompletedLines(Tile tile, int x, int y, int dx, int dy, Board b, ref List checkdoubles) { @@ -566,34 +595,14 @@ namespace QwirkleClassLibrary.Games break; } - if (extendedCell?.Tile != null) - { - nbTiles++; - - if(checkdoubles.Any(t => t.CompareTo(extendedCell.Tile) == 0)) - { - return false; - } - - checkdoubles.Add(extendedCell.Tile); - } - - if (extendedCell2?.Tile == null) continue; - { - nbTiles++; - - if (checkdoubles.Any(t => t.CompareTo(extendedCell2.Tile) == 0)) - { - return false; - } - - checkdoubles.Add(extendedCell2.Tile); - } + return CheckTileInCompletedLines(extendedCell?.Tile, extendedCell2?.Tile, ref nbTiles, ref checkdoubles); } return nbTiles <= 6; } + + /// /// Main method to check if the move the player is trying to make is correct /// @@ -654,12 +663,10 @@ namespace QwirkleClassLibrary.Games { return false; } - - if (!CheckWrongCompletedLines(t, x, y, dx, dy, b, ref checkDoubles)) - { - OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't complete this line ! (More than 6 tiles / same tiles on the line)")); - return false; - } + + if (CheckWrongCompletedLines(t, x, y, dx, dy, b, ref checkDoubles)) continue; + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't complete this line ! (More than 6 tiles / same tiles on the line)")); + return false; }