From 619cdaf3cc60d3b8805fb11f3dc5adb09022020f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Wed, 15 May 2024 17:23:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Utilisation=20de=20LINQ.=20Am=C3=A9?= =?UTF-8?q?lioration=20de=20la=20s=C3=A9curit=C3=A9=20(visibilit=C3=A9=20e?= =?UTF-8?q?t=20static).=20Factorisation=20et=20r=C3=A9duction=20d'imbricat?= =?UTF-8?q?ion=20du=20code.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Rules/Rules.cs | 88 ++++++++++------------------ 1 file changed, 32 insertions(+), 56 deletions(-) diff --git a/source/Trek-12/Models/Rules/Rules.cs b/source/Trek-12/Models/Rules/Rules.cs index 5a375da..a2d99bb 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -8,107 +8,83 @@ namespace Models.Rules { public class Rules { - public bool NearCell(Cell playerChoice, List cells) + public static bool NearCell(Cell playerChoice, List cells) { - foreach (var item in cells) - { - if(playerChoice.Pos.X == item.Pos.X +1 || playerChoice.Pos.X == item.Pos.X - 1 - || playerChoice.Pos.Y == item.Pos.Y +1 || playerChoice.Pos.Y == item.Pos.Y -1 ) - { - return true; - } - } - return false; + return cells.Any(item => playerChoice.Pos.X == item.Pos.X + 1 || playerChoice.Pos.X == item.Pos.X - 1 || playerChoice.Pos.Y == item.Pos.Y + 1 || playerChoice.Pos.Y == item.Pos.Y - 1); } - public bool IsCellEmpty(Cell playerChoice) + public static bool IsCellEmpty(Cell playerChoice) { - if (playerChoice.Value == null) - { - return true; - } - - return false; + return playerChoice.Value == null; } - public bool IsCellValid(Cell playerChoice, List cells) + public static bool IsCellValid(Cell playerChoice, List cells) { - if (NearCell(playerChoice, cells) && IsCellEmpty(playerChoice)) - { - return true; - } - - return false; + return NearCell(playerChoice, cells) && IsCellEmpty(playerChoice); } - public bool IsZone(Cell playerChoice, List cells) + public static bool IsZone(Cell playerChoice, List cells) { foreach (var item in cells) { - if (playerChoice.Pos.X == item.Pos.X + 1 || playerChoice.Pos.X == item.Pos.X - 1 - || playerChoice.Pos.Y == item.Pos.Y + 1 || playerChoice.Pos.Y == item.Pos.Y - 1) + if (playerChoice.Pos.X != item.Pos.X + 1 && playerChoice.Pos.X != item.Pos.X - 1 + && playerChoice.Pos.Y != item.Pos.Y + 1 && + playerChoice.Pos.Y != item.Pos.Y - 1) continue; + if (playerChoice.Value == item.Value) { - if (playerChoice.Value == item.Value) - { - return true; - } + return true; } } return false; } - public bool IsCheminDeCorde(Cell playerChoice, List cells, List cheminsDeCorde) + public static bool IsRopePath(Cell playerChoice, List cells, List ropePaths) { foreach (var item in cells) { - if (playerChoice.Pos.X == item.Pos.X + 1 || playerChoice.Pos.X == item.Pos.X - 1 - || playerChoice.Pos.Y == item.Pos.Y + 1 || playerChoice.Pos.Y == item.Pos.Y - 1) + if (playerChoice.Pos.X != item.Pos.X + 1 && playerChoice.Pos.X != item.Pos.X - 1 + && playerChoice.Pos.Y != item.Pos.Y + 1 && + playerChoice.Pos.Y != item.Pos.Y - 1) continue; + + if (playerChoice.Value != item.Value + 1 && playerChoice.Value != item.Value - 1) continue; + + foreach (var path in ropePaths) { - if (playerChoice.Value == item.Value+1 || playerChoice.Value == item.Value-1) + if (path.Equals(playerChoice)) { - foreach (var chemin in cheminsDeCorde) - { - if (chemin.Equals(playerChoice)) - { - return true; - } - } - return IsCheminDeCorde(item, cells, cheminsDeCorde); + return true; } } + return IsRopePath(item, cells, ropePaths); } return false; } - public int HowMany(Cell playerChoice, List cells) + public static int HowMany(Cell playerChoice, List cells) { foreach(var pos in cells) { if (pos.Equals(playerChoice)) { - if (pos.GetCellType()) - { - return 6; - } - return 12; + return pos.GetCellType() ? 6 : 12; } } return 0; } - public void SetValueAndPenalty(int valueChoice, Cell playerChoice, List cells) + public static void SetValueAndPenalty(int valueChoice, Cell playerChoice, List cells) { int val = HowMany(playerChoice, cells); + foreach (var pos in cells) { - if (pos.Equals(playerChoice)) + if (!pos.Equals(playerChoice)) continue; + + if (valueChoice > val) { - if (valueChoice > val) - { - playerChoice.Background = "notcontent"; - } - playerChoice.Value = valueChoice; + playerChoice.Background = "penalty"; } + playerChoice.Value = valueChoice; } } }