From f73549b0d5e17f22519ed880f3ef05baee66165a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Fri, 17 May 2024 13:37:00 +0200 Subject: [PATCH] =?UTF-8?q?M=C3=A0J=20r=C3=A9vision=20du=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Cell.cs | 36 ++-------- .../Models/Events/GameStartedEventArgs.cs | 14 ++++ source/Trek-12/Models/Game/Dice.cs | 8 +-- source/Trek-12/Models/Game/Game.cs | 9 ++- source/Trek-12/Models/Game/Map.cs | 9 ++- source/Trek-12/Models/Game/RopePath.cs | 6 ++ source/Trek-12/Models/Models.csproj | 3 +- source/Trek-12/Models/Position.cs | 38 ++++------ source/Trek-12/Models/Rules/Rules.cs | 70 ++++++++++++------- 9 files changed, 100 insertions(+), 93 deletions(-) create mode 100644 source/Trek-12/Models/Events/GameStartedEventArgs.cs create mode 100644 source/Trek-12/Models/Game/RopePath.cs diff --git a/source/Trek-12/Models/Cell.cs b/source/Trek-12/Models/Cell.cs index b9a8b33..e8f4bf4 100644 --- a/source/Trek-12/Models/Cell.cs +++ b/source/Trek-12/Models/Cell.cs @@ -2,51 +2,27 @@ public class Cell { - public Position Pos { get; private set; } - - private int? value; + private int? _value; public int? Value { - get - { - return value; - } + get => _value; set { if (value < 0) { throw new Exception("La valeur doit être supérieure à 0"); } - this.value = value; + this._value = value; } } - public bool IsDangerous { get; set; } + private bool IsDangerous { get; set; } public string? Background { get; set; } - public Cell(int x, int y) + public Cell(bool isDangerous = false) { - Pos = new Position(x, y); + IsDangerous = isDangerous; } public bool GetCellType() => IsDangerous; - - - // Redefinition de l'opérateur == pour comparer deux cellules - public override bool Equals(object? obj) - { - if (obj == null || GetType() != obj.GetType()) - { - return false; - } - Cell c = (Cell)obj; - return (Pos.X == c.Pos.X) && (Pos.Y == c.Pos.Y); - } - - // Redefinition de la méthode de Hash pour l'opérateur == - public override int GetHashCode() - { - return Pos.X * Pos.Y; - } - } \ No newline at end of file diff --git a/source/Trek-12/Models/Events/GameStartedEventArgs.cs b/source/Trek-12/Models/Events/GameStartedEventArgs.cs new file mode 100644 index 0000000..45cf88b --- /dev/null +++ b/source/Trek-12/Models/Events/GameStartedEventArgs.cs @@ -0,0 +1,14 @@ +using Models.Game; +namespace Models.Events; + +public class GameStartedEventArgs : EventArgs +{ + public Player Player { get; } + public Map Map { get; } + + public GameStartedEventArgs(Player player, Map map) + { + Player = player; + Map = map; + } +} \ No newline at end of file diff --git a/source/Trek-12/Models/Game/Dice.cs b/source/Trek-12/Models/Game/Dice.cs index 64f0819..ec61dcc 100644 --- a/source/Trek-12/Models/Game/Dice.cs +++ b/source/Trek-12/Models/Game/Dice.cs @@ -12,7 +12,7 @@ namespace Models.Game public int NbMax { get; private set; } - public int Nb { get; private set; } + public int Value { get; private set; } public Dice(int nbmin) { @@ -30,17 +30,17 @@ namespace Models.Game public override string ToString() { - return $"Ce dé a pour valeur {Nb} et est entre {NbMin} et {NbMax}"; + return $"Ce dé a pour valeur {Value} et est entre {NbMin} et {NbMax}"; } public void Lancer() { - Nb = new Random().Next(NbMin, NbMax + 1); + Value = new Random().Next(NbMin, NbMax + 1); } public bool IsLower(Dice dice2) { - return dice2.Nb > this.Nb; + return dice2.Value > this.Value; } } } \ No newline at end of file diff --git a/source/Trek-12/Models/Game/Game.cs b/source/Trek-12/Models/Game/Game.cs index 7fc24c2..c19bfaa 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -19,8 +19,10 @@ namespace Models.Game private Rules.Rules GameRules { get; } + private HashSet ropePaths { get; } - public Game(Player player,Map map) + + public Game(Player player, Map map) { UsedMap = map; CurrentPlayer = player; @@ -28,6 +30,7 @@ namespace Models.Game Dice2 = new Dice(1); Turn = 0; GameRules = new Rules.Rules(); + ropePaths = new HashSet(); } public void RollAllDice() @@ -68,8 +71,10 @@ namespace Models.Game } } - public void AddRopeZone(Cell playerChoice,RopesZones list) + public void AddToRopePath(Cell cell) { + if (GameRules.IsRopePath(cell, ropePaths)) return; + } } diff --git a/source/Trek-12/Models/Game/Map.cs b/source/Trek-12/Models/Game/Map.cs index 7370f01..6d634cd 100644 --- a/source/Trek-12/Models/Game/Map.cs +++ b/source/Trek-12/Models/Game/Map.cs @@ -2,13 +2,12 @@ public class Map { - public List Cells { get; private set; } - + public SortedDictionary Cells { get; private set; } public string Background { get; private set; } - - public Map(List cells, string background) + + public Map(string background) { - Cells = cells; + Cells = new SortedDictionary(); Background = background; } } \ No newline at end of file diff --git a/source/Trek-12/Models/Game/RopePath.cs b/source/Trek-12/Models/Game/RopePath.cs new file mode 100644 index 0000000..15fd51c --- /dev/null +++ b/source/Trek-12/Models/Game/RopePath.cs @@ -0,0 +1,6 @@ +namespace Models.Game; + +public class RopePath +{ + public HashSet Cells { get; } = new HashSet(); +} \ No newline at end of file diff --git a/source/Trek-12/Models/Models.csproj b/source/Trek-12/Models/Models.csproj index 0572af5..04d698b 100644 --- a/source/Trek-12/Models/Models.csproj +++ b/source/Trek-12/Models/Models.csproj @@ -7,9 +7,8 @@ - + - diff --git a/source/Trek-12/Models/Position.cs b/source/Trek-12/Models/Position.cs index 44f47b8..f05bdbc 100644 --- a/source/Trek-12/Models/Position.cs +++ b/source/Trek-12/Models/Position.cs @@ -1,25 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Models -{ - public class Position - { - public Position(int x, int y) - { - X = x; - Y = y; - } - - public int X { get; set; } - public int Y { get; set; } - - public override string ToString() - { - return $"{X} {Y}"; - } - } -} +namespace Models; + +public struct Position +{ + public int X { get; set; } + public int Y { get; set; } + + public Position(int x, int y) + { + X = x; + Y = y; + } +} \ No newline at end of file diff --git a/source/Trek-12/Models/Rules/Rules.cs b/source/Trek-12/Models/Rules/Rules.cs index a2d99bb..da33ee7 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -3,31 +3,48 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Models.Game; namespace Models.Rules { public class Rules { - public static bool NearCell(Cell playerChoice, List cells) + public bool IsCellExist(Position playerChoicePosition, SortedDictionary cells) { - 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); + return cells.ContainsKey(playerChoicePosition); + } + + public bool NearCellIsValid(Position playerChoicePosition, SortedDictionary cells) + { + if (!IsCellExist(playerChoicePosition, cells)) return false; + + foreach (var cell in cells) + { + if (Math.Abs(playerChoicePosition.X - cell.Key.X) > 1 || Math.Abs(playerChoicePosition.Y - cell.Key.Y) > 1) continue; + + if (!IsCellEmpty(cell.Value)) + { + return true; + } + } + return false; } - public static bool IsCellEmpty(Cell playerChoice) + public bool IsCellEmpty(Cell playerChoice) { - return playerChoice.Value == null; + return !playerChoice.Value.HasValue; } - public static bool IsCellValid(Cell playerChoice, List cells) + public bool IsCellValid(Position playerChoicePosition, SortedDictionary cells) { - return NearCell(playerChoice, cells) && IsCellEmpty(playerChoice); + return NearCellIsValid(playerChoicePosition, cells) && IsCellEmpty(cells. } - public static bool IsZone(Cell playerChoice, List cells) + public bool IsZone(Cell playerChoice, SortedDictionary cells) { - foreach (var item in cells) + foreach (var k,v in cells) { - if (playerChoice.Pos.X != item.Pos.X + 1 && playerChoice.Pos.X != item.Pos.X - 1 + if (playerChoice.X != item.X + 1 && playerChoice.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) @@ -38,29 +55,32 @@ namespace Models.Rules return false; } - public static bool IsRopePath(Cell playerChoice, List cells, List ropePaths) + /// + /// Determines if a given cell is part of any rope path. + /// + /// The cell to check. + /// A collection of rope paths to check against. + /// True if the cell is part of any rope path, false otherwise. + public bool IsRopePath(Cell playerChoice, HashSet ropePaths) { - foreach (var item in cells) + foreach (var path in ropePaths) { - 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 (path.Cells.Contains(playerChoice)) { - if (path.Equals(playerChoice)) - { - return true; - } + return true; } - return IsRopePath(item, cells, ropePaths); } return false; } - public static int HowMany(Cell playerChoice, List cells) + public bool IsAdjacent(Cell cell1, Cell cell2, SortedDictionary cells) + { + bool isSequence = cell1.Value.HasValue && cell2.Value.HasValue && Math.Abs(cell1.Value.Value - cell2.Value.Value) == 1; + + return IsCellValid(cell1, cells) && isSequence; + } + + public int HowMany(Cell playerChoice, SortedDictionary cells) { foreach(var pos in cells) { @@ -72,7 +92,7 @@ namespace Models.Rules return 0; } - public static void SetValueAndPenalty(int valueChoice, Cell playerChoice, List cells) + public void SetValueAndPenalty(int valueChoice, Cell playerChoice, SortedDictionary cells) { int val = HowMany(playerChoice, cells);