From c377a107c06507fc1280044accfc0bc3cf401a37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Fri, 17 May 2024 19:26:48 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20des=20r=C3=A8gles=20des=20zones?= =?UTF-8?q?=F0=9F=91=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Interfaces/IRules.cs | 43 +++++++--- source/Trek-12/Models/Rules/Rules.cs | 92 ++++++++++++++++++++-- 2 files changed, 117 insertions(+), 18 deletions(-) diff --git a/source/Trek-12/Models/Interfaces/IRules.cs b/source/Trek-12/Models/Interfaces/IRules.cs index 5c0fa5c..249bb51 100644 --- a/source/Trek-12/Models/Interfaces/IRules.cs +++ b/source/Trek-12/Models/Interfaces/IRules.cs @@ -1,20 +1,37 @@ -namespace Models.Interfaces; +using Models.Game; + +namespace Models.Interfaces; public interface IRules -{ - public bool NearCellIsValid(Position playerChoicePosition, SortedDictionary cells); +{ + //public bool NearCellIsValid(Position playerChoicePosition, SortedDictionary cells); + + //public bool IsCellEmpty(Cell playerChoice); + + //public bool IsCellValid(Position playerChoicePosition, List cells); + + //public bool IsRopePath(Cell playerChoice, HashSet ropePaths); + + //public bool IsAdjacent(Cell cell1, Cell cell2, List cells); + + //public int HowMany(Cell playerChoice, List cells); + + //public void SetValueAndPenalty(int valueChoice, Cell playerChoice, List cells); + + public bool IsCellAdjacent(Cell choosenCell, Cell targetCell); + + public bool NearCellIsValid(Cell choosenCell, List cells); + + public bool IsZoneValidAndAddToZones(Cell chosenCell, Map map); + + public bool IsValueInZones(Cell chosenCell, List> zones); + + public void AddToZone(Cell chosenCell, List> zones); - public bool IsCellEmpty(Cell playerChoice); + public void NewZoneIsCreated(Cell firstCell, Cell secondCell, Map map); - public bool IsCellValid(Position playerChoicePosition, List cells); - - public bool IsZone(Cell playerChoice, List cells); + public List EveryAdjacentCells(Cell choosenCell, List cells); - public bool IsRopePath(Cell playerChoice, HashSet ropePaths); - - public bool IsAdjacent(Cell cell1, Cell cell2, List cells); - - public int HowMany(Cell playerChoice, List cells); + public int FinalCalculusOfZones(List> zones); - public void SetValueAndPenalty(int valueChoice, Cell playerChoice, List cells); } \ 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 0daa484..b5ce805 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -35,12 +35,94 @@ namespace Models.Rules } } - public bool IsZone(Cell choosen, List cells,List> zones) + public bool IsZoneValidAndAddToZones(Cell chosenCell, Map map) { - IEnumerable PlayedCellsQuery = - from cell in cells - where cell.Value != null - select cell; + if (chosenCell == null) return false; + + List adjacentCells = new List(); + + adjacentCells = EveryAdjacentCells(chosenCell, map.Boards); + + foreach(var cells in adjacentCells) + { + if (cells.Value == chosenCell.Value) + { + if(IsValueInZones(cells, map.Zones)) + { + AddToZone(chosenCell, map.Zones); + } + else + { + NewZoneIsCreated(chosenCell, cells, map); + } + return true; // Il y a une cellule adjacente avec la même valeur donc une zone est créée si elle n'est pas déjà existante + // Si il return true, tout c'est bien passer + } + } + + return false; + } + + public bool IsValueInZones(Cell chosenCell, List> zones) + { + if (chosenCell == null) return false; + + for (int i = 0; i < zones.Count; i++) + { + if (zones[i][0].Value == chosenCell.Value) + { + return true; + } + } + return false; + } + + public void AddToZone(Cell chosenCell, List> zones) + { + if (chosenCell == null) return; + + for (int i = 0; i < zones.Count; i++) + { + if (zones[i][0].Value == chosenCell.Value) + { + zones[i].Add(chosenCell); + return; + } + } + return; + } + + public void NewZoneIsCreated(Cell firstCell, Cell secondCell, Map map) + { + List newZone = new List(); + newZone.Add(firstCell); + newZone.Add(secondCell); + map.Zones.Add(newZone); + } + + public List EveryAdjacentCells(Cell choosenCell, List cells) + { + List adjacentCells = new List(); + + foreach (var cell in cells) + { + if (IsCellAdjacent(choosenCell, cell)) + { + adjacentCells.Add(cell); + } + } + + return adjacentCells; + } + + public int FinalCalculusOfZones(List> zones) + { + int calculus = 0; + for(int i = 0; i < zones.Count; i++) + { + calculus += zones[i].Count * zones[i][0].Value; + } + return calculus; } }