diff --git a/source/Trek-12/Models/Game/Cell.cs b/source/Trek-12/Models/Game/Cell.cs index 3628445..6f2d0c2 100644 --- a/source/Trek-12/Models/Game/Cell.cs +++ b/source/Trek-12/Models/Game/Cell.cs @@ -1,6 +1,6 @@ namespace Models; -public class Cell : Position +public class Cell : Position, IEquatable { private int? _value; public int? Value { @@ -26,4 +26,11 @@ public class Cell : Position } public bool GetCellType() => IsDangerous; + + public bool Equals(Cell? other) + { + if (other == null) return false; + if (this.X == other.X && this.Y == other.Y) return true; + return false; + } } \ 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 591c362..82d1b0e 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -88,38 +88,48 @@ namespace Models.Game } } - public void AddToRopePath(Cell playerChoice, List> ropePaths,Cell adjacente) + public void AddToRopePath(Cell playerChoice,List adjacentes) { + int index =0; - List> possiblePaths; - - // {adjacentes} ne doit pas etre egale a {playerChoice} - if (adjacente.Value == playerChoice.Value) return; - - // {adjacentes} doit etre croissant ou decroissant - if ((adjacente.Value - playerChoice.Value > 1) || (adjacente.Value - playerChoice.Value < -1)) return; - - // Un nombre ne peut appartenir qu'a un seul chemin de corde - foreach (List paths in ropePaths) - { - foreach (var cells in paths) + foreach (var cells in adjacentes) + { + // La cellule choisi peut creer/s'ajouter a un chemin de corde + if (cells.Value - playerChoice.Value == 1 || cells.Value - playerChoice.Value == -1) { - if (adjacente.X == cells.X && adjacente.Y == adjacente.Y) + // Le cas si il n'existe aucun chemin de corde + if (UsedMap.RopePaths.Count == 0) { - possiblePaths.Add(paths); + // Creer un nouveau chemin de corde avec la cellule choisi par le joueur et celle adjacente + UsedMap.RopePaths.Add(new List {playerChoice, cells}); } - } + // A modifier dans le cas ou il est possible de fusionner deux chemins de corde + if (GameRules.IsInRopePaths(playerChoice, UsedMap.RopePaths, index)) break; - } + // Le cas si il existe des chemins de corde + // Est-ce que la cellule adjacentes fait parti d'un chemin de corde + if (!GameRules.IsInRopePaths(cells,UsedMap.RopePaths,index)) + { + UsedMap.RopePaths.Add(new List { playerChoice, cells }); + continue; + } - // Si {ropePaths} est vide, il faut alors creer un chemin de corde entre {playerChoice} - if (ropePaths.Count == 0) - { + if (!GameRules.AsValue(playerChoice,UsedMap.RopePaths,index)) + { + UsedMap.RopePaths[index].Add(playerChoice); + } + + // Si oui, est-ce que le chemin possede deja la valeur correspondante a la valeur de la cellule du joueur choisi + // {playerChoice.Value} n'est pas dans le chemin de corde + // Ajouter au chemin + // {playerChoice.Value} existe dans le chemin de corde pas possible + } } + } /// diff --git a/source/Trek-12/Models/Interfaces/IRules.cs b/source/Trek-12/Models/Interfaces/IRules.cs index b44e43d..fac4d42 100644 --- a/source/Trek-12/Models/Interfaces/IRules.cs +++ b/source/Trek-12/Models/Interfaces/IRules.cs @@ -36,4 +36,8 @@ public interface IRules public int? FinalCalculusOfZones(List> zones); + public bool IsInRopePaths(Cell adjacente, List> ropePaths, int index); + + public bool AsValue(Cell choosenCell, List> ropePaths, int index); + } \ 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 37a505a..d6645e6 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -18,13 +18,29 @@ namespace Models.Rules return false; return true; + } + + public bool IsInRopePaths (Cell adjacente,List> ropePaths,int index) + { + foreach (List path in ropePaths) + { + if (path.Contains(adjacente)) + { + index=ropePaths.IndexOf(path); + return true; + } + } + return false; } - - public bool ropePathCellIsValid (Cell choosenCell, Cell targetCell) + + public bool AsValue (Cell choosenCell, List> ropePaths,int index) { - if (Math.Abs(choosenCell.Value - targetCell.Value ) + foreach (var item in ropePaths[index]) + { + if (choosenCell.Value == item.Value) return true; + } + return false; } - public bool NearCellIsValid(Cell choosenCell, List cells) { if (choosenCell == null || cells == null) return false;