diff --git a/source/Trek-12/ConsoleApp/Program.cs b/source/Trek-12/ConsoleApp/Program.cs index c9af02e..117e7cb 100644 --- a/source/Trek-12/ConsoleApp/Program.cs +++ b/source/Trek-12/ConsoleApp/Program.cs @@ -1,134 +1,246 @@ // See https://aka.ms/new-console-template for more information - - using Models; +using Models.Events; +using Models.Exceptions; using Models.Game; -using Models.Rules; - - -Cell pos01 = new Cell(0,0); -Cell pos02 = new Cell(1,0); -Cell pos03 = new Cell(2,0); -Cell pos04 = new Cell(3,0); -Cell pos05 = new Cell(4,0); //4 - -Cell pos11 = new Cell(0, 1); -Cell pos12 = new Cell(1, 1); -Cell pos13 = new Cell(2, 1); //8 -Cell pos14 = new Cell(3, 1); -Cell pos15 = new Cell(4, 1); //4 - -Cell pos21 = new Cell(0, 2); //8 -Cell pos22 = new Cell(1, 2); -Cell pos23 = new Cell(2, 2); -Cell pos24 = new Cell(3, 2); -Cell pos25 = new Cell(4, 2); //4 - -Cell pos31 = new Cell(0, 3); //8 -Cell pos32 = new Cell(1, 3); -Cell pos33 = new Cell(2, 3); -Cell pos34 = new Cell(3, 3); -Cell pos35 = new Cell(4, 3); //4 - - -List list = new List(); -list.Add(pos01); -list.Add(pos02); -list.Add(pos03); -list.Add(pos04); -list.Add(pos05); - -list.Add(pos11); -list.Add(pos12); -list.Add(pos13); -list.Add(pos14); -list.Add(pos15); - -list.Add(pos21); -list.Add(pos22); -list.Add(pos23); -list.Add(pos24); -list.Add(pos25); - -list.Add(pos31); -list.Add(pos32); -list.Add(pos33); -list.Add(pos34); -list.Add(pos35); - -Player p2 = new Player("Luka"); - -Game game = new Game(p2, new Map("background")); - -game.UsedMap.Boards = list; - -game.PlaceResult(pos02, 5); -game.PlaceResult(pos03, 5); -game.PlaceResult(pos32, 5); - -game.PlaceResult(pos31, 8); -game.PlaceResult(pos21, 8); -game.PlaceResult(pos21, 8); -game.PlaceResult(pos13, 8); - -game.PlaceResult(pos05, 4); -game.PlaceResult(pos15, 4); -game.PlaceResult(pos25, 4); -game.PlaceResult(pos35, 4); - -int cpt = 0; -/* -foreach (var item in game.UsedMap.Boards) -{ - Console.Write(" | "); - Console.Write(item.Value); - cpt++; - if (cpt % 4 == 0) Console.Write(" |\n"); -} -*/ - -for (int i = 0; i + /// Main function of the console app + /// + /// + static void Main(string[] args) + { + Console.WriteLine("Enter your pseudo:"); + string? pseudo = Console.ReadLine(); + if (pseudo != null) + { + Player player = new Player(pseudo); + + Map map = new Map("background"); + Game game = new Game(player, map); + + // Abonnement aux événements + game.GameStarted += OnGameStarted!; + game.GameEnded += OnGameEnded!; + game.BoardUpdated += OnBoardUpdated!; + game.DiceRolled += OnDiceRolled!; + game.OperationChosen += OnOperationChosen!; + game.CellChosen += OnCellChosen!; + + // Initialisation + game.InitializeGame(); + } + } -game.GameRules.IsZoneValidAndAddToZones(pos02, game.UsedMap); + /// + /// Handles the event when the game has started. + /// + /// + /// + static void OnGameStarted(object sender, GameStartedEventArgs e) + { + Console.WriteLine($"The game has started! Player: {e.CurrentPlayer.Pseudo}"); + } -game.GameRules.IsZoneValidAndAddToZones(pos13, game.UsedMap); -game.GameRules.IsZoneValidAndAddToZones(pos31, game.UsedMap); -game.GameRules.IsZoneValidAndAddToZones(pos13, game.UsedMap); + /// + /// Handles the event when the game has ended. + /// + /// + /// + static void OnGameEnded(object sender, GameEndedEventArgs e) + { + Console.WriteLine($"The game has ended! Player: {e.CurrentPlayer.Pseudo}"); + } -//foreach(var oui in game.GameRules.EveryAdjacentCells(pos13, game.UsedMap.Boards)) //du débug -//{ -// Console.WriteLine(oui.Value); -//} + /// + /// Handles the event when the board is updated. + /// + /// + /// + static void OnBoardUpdated(object sender, EventArgs e) + { + DisplayBoard(((Game)sender).UsedMap); + DisplayOperationTable(((Game)sender).UsedMap.OperationGrid); + } -game.GameRules.IsZoneValidAndAddToZones(pos32, game.UsedMap); -game.GameRules.IsZoneValidAndAddToZones(pos33, game.UsedMap); + /// + /// Handles the event when the dice are rolled. + /// + /// + /// + static void OnDiceRolled(object sender, DiceRolledEventArgs e) + { + Console.WriteLine($"Dice 1: {e.Dice1Value} | Dice 2: {e.Dice2Value}"); + Operation playerOperation = GetPlayerOperation(); + ((Game)sender).HandlePlayerOperation(playerOperation); + } -game.GameRules.IsZoneValidAndAddToZones(pos05, game.UsedMap); -game.GameRules.IsZoneValidAndAddToZones(pos15, game.UsedMap); -game.GameRules.IsZoneValidAndAddToZones(pos25, game.UsedMap); -game.GameRules.IsZoneValidAndAddToZones(pos35, game.UsedMap); + /// + /// Handles the event when an operation is chosen by the player. + /// + /// + /// + static void OnOperationChosen(object sender, OperationChosenEventArgs e) + { + Console.WriteLine($"Operation: {e.Operation}, Result: {e.Result}"); + DisplayOperationTable(((Game)sender).UsedMap.OperationGrid); + Cell playerChoice = GetPlayerChoice(); + try + { + ((Game)sender).HandlePlayerChoice(playerChoice, e.Result); + } + catch (InvalidCellCoordinatesException err) + { + Console.WriteLine(err.Message); + playerChoice = GetPlayerChoice(); + ((Game)sender).HandlePlayerChoice(playerChoice, e.Result); + } + catch (InvalidCellException err) + { + Console.WriteLine(err.Message); + playerChoice = GetPlayerChoice(); + ((Game)sender).HandlePlayerChoice(playerChoice, e.Result); + } + } + /// + /// Handles the event when a cell is chosen by the player. + /// + /// + /// + static void OnCellChosen(object sender, CellChosenEventArgs e) + { + Console.WriteLine($"Cell chosen: ({e.Cell.X}, {e.Cell.Y}) with result: {e.Result}"); + DisplayBoard(((Game)sender).UsedMap); + } -//affichage des zones -foreach (var item in game.UsedMap.Zones) -{ - foreach (var cell in item) + /// + /// Displays the game board. + /// + /// Used map to display + static void DisplayBoard(Map map) + { + int cpt = 0; + for (int i = 0; i < map.Boards.Count; i++) + { + if (cpt % 6 == 0) + { + Console.Write("| "); + } + + if (map.Boards[i].Value.HasValue) + { + Console.Write(map.Boards[i].Value?.ToString().PadLeft(2)); + } + else + { + Console.Write(" O"); + } + + cpt++; + if (cpt % 6 == 0) + { + Console.Write(" |"); + Console.WriteLine(); + } + } + } + + /// + /// Displays the operation table. + /// + /// The operation table to display. + static void DisplayOperationTable(List operationTable) { - Console.WriteLine(cell.Value); + Console.WriteLine("Operation Table:"); + string[] operations = { "Addition (+)", "Subtraction (-)", "Multiplication (*)", "Lower Dice (less)", "Higher Dice (high)" }; + + for (int i = 0; i < operations.Length; i++) + { + Console.Write(operations[i].PadRight(18)); + + for (int j = 0; j < 4; j++) + { + int index = i * 4 + j; + if (index < operationTable.Count) + { + string status = operationTable[index].IsChecked ? "X" : " "; + Console.Write($" | {status}"); + } + } + Console.WriteLine(); + } } - Console.Write(" |\n"); -} -Console.WriteLine(game.GameRules.FinalCalculusOfZones(game.UsedMap.Zones)); + /// + /// Gets the cell chosen by the player. + /// + /// The cell chosen by the player. + static Cell GetPlayerChoice() + { + int row, column; + while (true) + { + Console.WriteLine("Enter the position of the cell you want to play"); + Console.WriteLine("Enter the row number (0-5)"); + if (!int.TryParse(Console.ReadLine(), out row) || row < 0 || row >= 6) + { + Console.WriteLine("Invalid row number. Please enter a number between 0 and 5."); + continue; + } + + Console.WriteLine("Enter the column number (0-5)"); + if (!int.TryParse(Console.ReadLine(), out column) || column < 0 || column >= 6) + { + Console.WriteLine("Invalid column number. Please enter a number between 0 and 5."); + continue; + } + + return new Cell(row, column); + } + } -Map maaaap = new Map("background"); -Game Gaaame = new(new Player("Luka"), maaaap); + /// + /// Gets the operation chosen by the player. + /// + /// + /// + static Operation GetPlayerOperation() + { + DisplayOperationOptions(); + string? op = Console.ReadLine(); + while (op != "1" && op != "2" && op != "3" && op != "4" && op != "5") + { + Console.WriteLine("Invalid operation. Please choose again."); + op = Console.ReadLine(); + } + + return op switch + { + "1" => Operation.ADDITION, + "2" => Operation.SUBTRACTION, + "3" => Operation.MULTIPLICATION, + "4" => Operation.LOWER, + "5" => Operation.HIGHER, + _ => throw new ArgumentOutOfRangeException() + }; + } -Gaaame.InitializeGame(); \ No newline at end of file + /// + /// Displays the operation options for the player to choose from. + /// + static void DisplayOperationOptions() + { + Console.WriteLine("Choose an operation:"); + Console.WriteLine("1: Addition (+)"); + Console.WriteLine("2: Subtraction (-)"); + Console.WriteLine("3: Multiplication (*)"); + Console.WriteLine("4: Lower Dice (less)"); + Console.WriteLine("5: Higher Dice (high)"); + } +} \ No newline at end of file diff --git a/source/Trek-12/Models/Events/CellChosenEventArgs.cs b/source/Trek-12/Models/Events/CellChosenEventArgs.cs new file mode 100644 index 0000000..2367a26 --- /dev/null +++ b/source/Trek-12/Models/Events/CellChosenEventArgs.cs @@ -0,0 +1,13 @@ +namespace Models.Events; + +public class CellChosenEventArgs : EventArgs +{ + public Cell Cell { get; } + public int Result { get; } + + public CellChosenEventArgs(Cell cell, int result) + { + Cell = cell; + Result = result; + } +} \ No newline at end of file diff --git a/source/Trek-12/Models/Events/DiceRolledEventArgs.cs b/source/Trek-12/Models/Events/DiceRolledEventArgs.cs new file mode 100644 index 0000000..a800a95 --- /dev/null +++ b/source/Trek-12/Models/Events/DiceRolledEventArgs.cs @@ -0,0 +1,13 @@ +namespace Models.Events; + +public class DiceRolledEventArgs : EventArgs +{ + public int Dice1Value { get; } + public int Dice2Value { get; } + + public DiceRolledEventArgs(int dice1Value, int dice2Value) + { + Dice1Value = dice1Value; + Dice2Value = dice2Value; + } +} \ No newline at end of file diff --git a/source/Trek-12/Models/Events/OperationChosenEventArgs.cs b/source/Trek-12/Models/Events/OperationChosenEventArgs.cs new file mode 100644 index 0000000..04a1f14 --- /dev/null +++ b/source/Trek-12/Models/Events/OperationChosenEventArgs.cs @@ -0,0 +1,13 @@ +namespace Models.Events; + +public class OperationChosenEventArgs : EventArgs +{ + public Operation Operation { get; } + public int Result { get; } + + public OperationChosenEventArgs(Operation operation, int result) + { + Operation = operation; + Result = result; + } +} \ No newline at end of file diff --git a/source/Trek-12/Models/Exceptions/BadMapSelectException.cs b/source/Trek-12/Models/Exceptions/BadMapSelectException.cs deleted file mode 100644 index 1931c87..0000000 --- a/source/Trek-12/Models/Exceptions/BadMapSelectException.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Models.Exceptions; - -public class BadMapSelectException : Exception -{ - public BadMapSelectException() : base() {} - public BadMapSelectException(string message) : base(message) {} -} \ No newline at end of file diff --git a/source/Trek-12/Models/Exceptions/InvalidCellCoordinatesException.cs b/source/Trek-12/Models/Exceptions/InvalidCellCoordinatesException.cs new file mode 100644 index 0000000..d310326 --- /dev/null +++ b/source/Trek-12/Models/Exceptions/InvalidCellCoordinatesException.cs @@ -0,0 +1,6 @@ +namespace Models.Exceptions; + +public class InvalidCellCoordinatesException : Exception +{ + public InvalidCellCoordinatesException(string message) : base(message) { } +} \ No newline at end of file diff --git a/source/Trek-12/Models/Exceptions/InvalidCellException.cs b/source/Trek-12/Models/Exceptions/InvalidCellException.cs new file mode 100644 index 0000000..5a7e4fd --- /dev/null +++ b/source/Trek-12/Models/Exceptions/InvalidCellException.cs @@ -0,0 +1,6 @@ +namespace Models.Exceptions; + +public class InvalidCellException : Exception +{ + public InvalidCellException(string message) : base(message) { } +} \ No newline at end of file diff --git a/source/Trek-12/Models/Game/Cell.cs b/source/Trek-12/Models/Game/Cell.cs index 9692ac2..c0bb1c5 100644 --- a/source/Trek-12/Models/Game/Cell.cs +++ b/source/Trek-12/Models/Game/Cell.cs @@ -49,7 +49,7 @@ public class Cell : Position, IEquatable /// /// Redefine the equal operation between cells. /// - /// The object to compare with the current cell. + /// The object to compare with the current cell. /// true if the specified object is equal to the current cell; otherwise, false. public bool Equals(Cell? other) { diff --git a/source/Trek-12/Models/Game/Dice.cs b/source/Trek-12/Models/Game/Dice.cs index b38f5d9..62e2038 100644 --- a/source/Trek-12/Models/Game/Dice.cs +++ b/source/Trek-12/Models/Game/Dice.cs @@ -21,7 +21,18 @@ namespace Models.Game /// /// Value of the dice. /// - public int Value { get; private set; } + private int _value; + public int Value { + get => _value; + private set + { + if (value < NbMin || value > NbMax) + { + value = NbMin; + } + _value = value; + } + } /// /// Initializes a new instance of the class. @@ -66,5 +77,6 @@ namespace Models.Game { 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 a65f998..ca6ede7 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using System.Threading.Tasks.Dataflow; using Models.Events; +using Models.Exceptions; using Models.Rules; namespace Models.Game @@ -27,9 +28,14 @@ namespace Models.Game public Rules.Rules GameRules { get; } + // == Events == public event EventHandler GameStarted; public event EventHandler GameEnded; + public event EventHandler BoardUpdated; + public event EventHandler DiceRolled; + public event EventHandler OperationChosen; + public event EventHandler CellChosen; /// @@ -51,10 +57,30 @@ namespace Models.Game /// /// Rolls all the dice. /// - public void RollAllDice() + private void RollAllDice() { Dice1.Roll(); Dice2.Roll(); + DiceRolled?.Invoke(this, new DiceRolledEventArgs(Dice1.Value, Dice2.Value)); + } + + /// + /// Marks an operation as checked in the operation grid of the game. + /// + /// + private void MarkOperationAsChecked(Operation operation) + { + int operationIndex = (int)operation; + int operationsPerType = 4; // Chaque type d'opération peut être fait 4 fois + + for (int i = operationIndex * operationsPerType; i < (operationIndex + 1) * operationsPerType; i++) + { + if (!UsedMap.OperationGrid[i].IsChecked) + { + UsedMap.OperationGrid[i].Check(); + break; + } + } } /// @@ -68,28 +94,20 @@ namespace Models.Game /// If the operation is MULTIPLICATION, it returns the product of the values of the two dice. /// If the operation is not one of the operations, it throws an ArgumentOutOfRangeException. /// - public int ResultOperation(Operation o) + private int ResultOperation(Operation o) { - switch (o) + int result = o switch { - case Operation.LOWER: - return Dice1.IsLower(Dice2) ? Dice1.Value : Dice2.Value; - - case Operation.HIGHER: - return Dice1.IsLower(Dice2) ? Dice2.Value : Dice1.Value; - - case Operation.SUBTRACTION: - return Dice1.IsLower(Dice2) ? Dice2.Value - Dice1.Value : Dice1.Value - Dice2.Value; - - case Operation.ADDITION: - return Dice2.Value + Dice1.Value; - - case Operation.MULTIPLICATION: - return Dice2.Value * Dice1.Value; - - default: - throw new ArgumentOutOfRangeException(); - } + Operation.LOWER => Dice1.IsLower(Dice2) ? Dice1.Value : Dice2.Value, + Operation.HIGHER => Dice1.IsLower(Dice2) ? Dice2.Value : Dice1.Value, + Operation.SUBTRACTION => Dice1.IsLower(Dice2) ? Dice2.Value - Dice1.Value : Dice1.Value - Dice2.Value, + Operation.ADDITION => Dice2.Value + Dice1.Value, + Operation.MULTIPLICATION => Dice2.Value * Dice1.Value, + _ => throw new ArgumentOutOfRangeException() + }; + + MarkOperationAsChecked(o); + return result; } @@ -99,15 +117,21 @@ namespace Models.Game /// /// The cell chosen by the player to place the result. /// The result of the dice operation to be placed in the cell. - public void PlaceResult(Cell playerChoice, int result) + private void PlaceResult(Cell playerChoice, int result) { if (Turn == 1 || GameRules.NearCellIsValid(playerChoice, UsedMap.Boards)) { playerChoice.Value = result; + BoardUpdated?.Invoke(this, EventArgs.Empty); } } - public void AddToRopePath(Cell playerChoice,List adjacentes) + /// + /// Add the choosen cell to a rope path if it's possible. + /// + /// + /// + private void AddToRopePath(Cell playerChoice,List adjacentes) { int index =0; @@ -160,92 +184,69 @@ namespace Models.Game GameStarted?.Invoke(this, new GameStartedEventArgs(CurrentPlayer)); GameLoop(); } + + /// + /// Ends the game. + /// + private void EndGame() + { + _isRunning = false; + GameEnded?.Invoke(this, new GameEndedEventArgs(CurrentPlayer)); + } /// /// The main game loop that runs while the game is active. /// - private async void GameLoop() + private void GameLoop() { - Cell PlayerChoice = new Cell(0, 0); - string op = ""; - Operation convOp = new Operation(); - int valueCurrentCell = 0; while (_isRunning) { - if (Turn == 20) GameEnded?.Invoke(this, new GameEndedEventArgs(CurrentPlayer)); - /* - if (CheckGameEnd()) //TODO Règle pour check si fin de jeu + if (Turn == 20) { - _isRunning = false; - //TODO Code pour comptabiliser les points - int scoreZones = GameRules.FinalCalculusOfZones(UsedMap.Zones); - //GameEnded?.Invoke(this, new GameEndedEventArgs(player)); + EndGame(); + break; } - */ - - await Task.Delay(1000); // 1 seconde - UsedMap.DisplayBoards(); + RollAllDice(); - Console.WriteLine($"Dice 1: {Dice1.Value} | Dice 2: {Dice2.Value}"); - Console.WriteLine("What do you want : " + - "Addition ? ( + )" + - "Substraction ? ( - )" + - "Multiplication ? ( * )" + - "The lower dice ? ( less )" + - "The higher dice ? high )"); - - op = Console.ReadLine(); - while (op != "+" && op != "-" && op != "*" && op != "less" && op != "high") - { - op = Console.ReadLine(); - } - - if (op == "less") - { - convOp = Operation.LOWER; - //incrémentation du tableau des opérations - } - - else if (op == "high") - { - convOp = Operation.HIGHER; - //incrémentation du tableau des opérations - } - if (op == "+") - { - convOp = Operation.ADDITION; - //incrémentation du tableau des opérations - } - if (op == "-") - { - convOp = Operation.SUBTRACTION; - //incrémentation du tableau des opérations - } - if (op == "*") - { - convOp = Operation.MULTIPLICATION; - //incrémentation du tableau des opérations - } - - valueCurrentCell = ResultOperation(convOp); - Console.WriteLine($"The value of the dice is : {valueCurrentCell}"); - Console.WriteLine(); - PlayerChoice = UsedMap.PlayerChoiceOfPosition(); - - while (!GameRules.IsCellValid(PlayerChoice, UsedMap.Boards)) - { - PlayerChoice = UsedMap.PlayerChoiceOfPosition(); - } - PlaceResult(PlayerChoice, valueCurrentCell); - - GameRules.IsZoneValidAndAddToZones(PlayerChoice, UsedMap); - AddToRopePath(PlayerChoice, GameRules.EveryAdjacentCells(PlayerChoice, UsedMap.Boards)); Turn++; + } + } + + /// + /// Handles the player's choice of an operation based on the dice values.s + /// + /// The operation chosen by the player. + public void HandlePlayerOperation(Operation operation) + { + int result = ResultOperation(operation); + OperationChosen?.Invoke(this, new OperationChosenEventArgs(operation, result)); + } + /// + /// Handles the player's choice of a cell on the game board. + /// + /// + /// + /// + /// + public void HandlePlayerChoice(Cell cell, int result) + { + if (cell.X < 0 || cell.X >= UsedMap.Boards.Count / 6 || cell.Y < 0 || cell.Y >= 6) + { + throw new InvalidCellCoordinatesException("Invalid cell coordinates. Please choose again."); + } + if (!GameRules.IsCellValid(cell, UsedMap.Boards)) + { + throw new InvalidCellException("Cell is not valid. Please choose again."); } - } + PlaceResult(cell, result); + GameRules.IsZoneValidAndAddToZones(cell, UsedMap); + AddToRopePath(cell, GameRules.EveryAdjacentCells(cell, UsedMap.Boards)); + CellChosen?.Invoke(this, new CellChosenEventArgs(cell, result)); + BoardUpdated?.Invoke(this, EventArgs.Empty); + } } } diff --git a/source/Trek-12/Models/Game/Map.cs b/source/Trek-12/Models/Game/Map.cs index de9ae3f..c599ddc 100644 --- a/source/Trek-12/Models/Game/Map.cs +++ b/source/Trek-12/Models/Game/Map.cs @@ -5,7 +5,7 @@ public class Map /// /// It is the list of cells on the map. /// - public List Boards { get; set; } + public List Boards { get; private set; } /// /// It is the backgrond image of the map @@ -33,10 +33,9 @@ public class Map /// The background of the map. public Map(string background) { - Boards = new List(); Boards = InitializeBoards(); Background = background; - OperationGrid = new List(); + OperationGrid = InitializeOperationGrid(); RopePaths = new List>(); Zones = new List>(); } @@ -45,87 +44,30 @@ public class Map /// Initializes the boards of the map. /// /// Return the boards - public static List InitializeBoards() + private List InitializeBoards() { - Cell pos01 = new Cell(0, 0); - Cell pos02 = new Cell(1, 0); - Cell pos03 = new Cell(2, 0); - Cell pos04 = new Cell(3, 0); - Cell pos05 = new Cell(4, 0); - - Cell pos11 = new Cell(0, 1); - Cell pos12 = new Cell(1, 1); - Cell pos13 = new Cell(2, 1); - Cell pos14 = new Cell(3, 1); - Cell pos15 = new Cell(4, 1); - - Cell pos21 = new Cell(0, 2); - Cell pos22 = new Cell(1, 2); - Cell pos23 = new Cell(2, 2); - Cell pos24 = new Cell(3, 2); - Cell pos25 = new Cell(4, 2); - - Cell pos31 = new Cell(0, 3); - Cell pos32 = new Cell(1, 3); - Cell pos33 = new Cell(2, 3); - Cell pos34 = new Cell(3, 3); - Cell pos35 = new Cell(4, 3); - - - List list = new List(); - list.Add(pos01); - list.Add(pos02); - list.Add(pos03); - list.Add(pos04); - list.Add(pos05); - - list.Add(pos11); - list.Add(pos12); - list.Add(pos13); - list.Add(pos14); - list.Add(pos15); - - list.Add(pos21); - list.Add(pos22); - list.Add(pos23); - list.Add(pos24); - list.Add(pos25); - - list.Add(pos31); - list.Add(pos32); - list.Add(pos33); - list.Add(pos34); - list.Add(pos35); - - return list; - } - - /// - /// Dislpay the boards of the map in the console. - /// - public void DisplayBoards() - { - int cpt = 0; - for (int i = 0; i < Boards.Count; i++) + var boards = new List(); + for (int i = 0; i < 36; i++) // 6x6 board { - Console.Write(" | "); - Console.Write(Boards[i].Value); - cpt++; - if (cpt % 5 == 0) Console.Write(" |\n"); + boards.Add(new Cell(i / 6, i % 6)); } + return boards; } - + /// - /// Ask the player to choose a cell to play. + /// Initializes the operation grid of the map. /// - /// - public Cell PlayerChoiceOfPosition() + /// Return the operation grid + private List InitializeOperationGrid() { - Console.WriteLine("Enter the position of the cell you want to play"); - Console.WriteLine("Enter the row number"); - int row = Convert.ToInt32(Console.ReadLine()); - Console.WriteLine("Enter the column number"); - int column = Convert.ToInt32(Console.ReadLine()); - return new Cell(row, column); + var operationGrid = new List(); + for (int i = 0; i < 5; i++) // 5 operations + { + for (int j = 0; j < 4; j++) // 4 cells per operation + { + operationGrid.Add(new OperationCell(i, j)); + } + } + return operationGrid; } } diff --git a/source/Trek-12/Models/Game/OperationCell.cs b/source/Trek-12/Models/Game/OperationCell.cs index 6efcb9c..550beae 100644 --- a/source/Trek-12/Models/Game/OperationCell.cs +++ b/source/Trek-12/Models/Game/OperationCell.cs @@ -8,6 +8,19 @@ public class OperationCell : Position /// public bool IsChecked { get; private set; } + /// + /// Constructor of the OperationCell class. + /// + /// + /// public OperationCell (int x,int y) :base (x,y) { } + + /// + /// Check the operation cell. + /// + public void Check() + { + IsChecked = true; + } } \ No newline at end of file diff --git a/source/Trek-12/Models/Models.csproj b/source/Trek-12/Models/Models.csproj index e2b0aa0..0119668 100644 --- a/source/Trek-12/Models/Models.csproj +++ b/source/Trek-12/Models/Models.csproj @@ -8,10 +8,8 @@ - - diff --git a/source/Trek-12/Tests/DiceTests.cs b/source/Trek-12/Tests/DiceTests.cs index 3b85879..7831db6 100644 --- a/source/Trek-12/Tests/DiceTests.cs +++ b/source/Trek-12/Tests/DiceTests.cs @@ -46,33 +46,4 @@ public class DiceTests dice.Roll(); Assert.True(dice.Value >= dice.NbMin && dice.Value <= dice.NbMax); } - - [Fact] - public void IsLower_WhenOtherDiceHasHigherValue_ReturnsTrue() - { - Dice dice1 = new Dice(); - Dice dice2 = new Dice(); - dice1.Roll(); - dice2.Roll(); - while (dice1.Value >= dice2.Value) - { - dice2.Roll(); - } - Assert.True(dice1.IsLower(dice2)); - } - - [Fact] - public void IsLower_WhenOtherDiceHasLowerValue_ReturnsFalse() - { - Dice dice1 = new Dice(); - Dice dice2 = new Dice(); - dice1.Roll(); - dice2.Roll(); - while (dice1.Value <= dice2.Value) - { - dice1.Roll(); - } - Assert.False(dice1.IsLower(dice2)); - } - } diff --git a/source/Trek-12/Tests/MapTests.cs b/source/Trek-12/Tests/MapTests.cs index 5257433..027ed94 100644 --- a/source/Trek-12/Tests/MapTests.cs +++ b/source/Trek-12/Tests/MapTests.cs @@ -1,34 +1,44 @@ -using Models.Game; +using Models; +using Models.Game; namespace Tests; public class MapTests { [Fact] - public void Constructor_WithValidBackground_SetsBackgroundCorrectly() + public void Map_Initialization_SetsBackground() { - var map = new Map("background"); - Assert.Equal("background", map.Background); + string background = "test_background"; + + var map = new Map(background); + + Assert.Equal(background, map.Background); } [Fact] - public void Constructor_InitializesEmptyBoards() + public void Map_Initialization_InitializesBoards() { - var map = new Map("background"); - Assert.Empty(map.Boards); + string background = "test_background"; + + var map = new Map(background); + + Assert.Equal(36, map.Boards.Count); + for (int i = 0; i < 36; i++) + { + Assert.Equal(new Cell(i / 6, i % 6), map.Boards[i]); + } } [Fact] - public void Constructor_InitializesEmptyOperationGrid() + public void Map_Initialization_InitializesRopePathsAndZones() { - var map = new Map("background"); - Assert.Empty(map.OperationGrid); - } - - [Fact] - public void Constructor_InitializesEmptyZones() - { - var map = new Map("background"); + string background = "test_background"; + + var map = new Map(background); + + Assert.NotNull(map.RopePaths); + Assert.NotNull(map.Zones); + Assert.Empty(map.RopePaths); Assert.Empty(map.Zones); } }