diff --git a/source/Trek-12/ConsoleApp/Program.cs b/source/Trek-12/ConsoleApp/Program.cs index 5f96c6c..930eb61 100644 --- a/source/Trek-12/ConsoleApp/Program.cs +++ b/source/Trek-12/ConsoleApp/Program.cs @@ -69,7 +69,7 @@ class Program static void OnBoardUpdated(object sender, EventArgs e) { DisplayBoard(((Game)sender).UsedMap); - DisplayOperationTable(((Game)sender).UsedMap.OperationGrid); + DisplayOperationTable(((Game)sender).UsedMap.OperationGrid.ToList()); } /// @@ -92,7 +92,7 @@ class Program static void OnOperationChosen(object sender, OperationChosenEventArgs e) { Console.WriteLine($"Operation: {e.Operation}, Result: {e.Result}"); - DisplayOperationTable(((Game)sender).UsedMap.OperationGrid); + DisplayOperationTable(((Game)sender).UsedMap.OperationGrid.ToList()); Cell playerChoice = GetPlayerChoice(); bool test = ((Game)sender).HandlePlayerChoice(playerChoice, e.Result); if(!test) diff --git a/source/Trek-12/Models/Game/Cell.cs b/source/Trek-12/Models/Game/Cell.cs index 49fe4bb..b2eb151 100644 --- a/source/Trek-12/Models/Game/Cell.cs +++ b/source/Trek-12/Models/Game/Cell.cs @@ -24,7 +24,10 @@ /// /// The fact that the cell is dangerous or not. /// - private bool IsDangerous { get; set; } + public bool IsDangerous { get; set; } + + public bool Valid { get; set; } + /// /// Atribute to know if the cell is a penalty cell. @@ -41,6 +44,7 @@ { IsDangerous = isDangerous; Penalty = false; + Valid = false; } /// diff --git a/source/Trek-12/Models/Game/Game.cs b/source/Trek-12/Models/Game/Game.cs index c4f4460..6c732ff 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -176,7 +176,7 @@ namespace Models.Game /// The result of the dice operation to be placed in the cell. private bool PlaceResult(Cell playerChoice, int result) { - if (Turn == 1 || GameRules.NearCellIsValid(playerChoice, UsedMap.Boards)) + if (Turn == 1 || GameRules.NearCellIsValid(playerChoice, UsedMap.Boards.ToList())) { for (int i = 0; i < UsedMap.Boards.Count; i++) { @@ -323,7 +323,7 @@ namespace Models.Game //throw new InvalidCellCoordinatesException("Invalid cell coordinates. Please choose again."); } - if (!GameRules.IsCellValid(cell, UsedMap.Boards)) + if (!GameRules.IsCellValid(cell, UsedMap.Boards.ToList())) { return false; //throw new InvalidCellException("Cell is not valid. Please choose again."); @@ -336,7 +336,7 @@ namespace Models.Game //throw new InvalidPlaceResultException("Cell is not valid for place result. Please choose again."); } GameRules.IsZoneValidAndAddToZones(cell, UsedMap); - AddToRopePath(cell, GameRules.EveryAdjacentCells(cell, UsedMap.Boards)); + AddToRopePath(cell, GameRules.EveryAdjacentCells(cell, UsedMap.Boards.ToList())); CellChosen?.Invoke(this, new CellChosenEventArgs(cell, result)); return true; //BoardUpdated?.Invoke(this, EventArgs.Empty); diff --git a/source/Trek-12/Models/Game/Map.cs b/source/Trek-12/Models/Game/Map.cs index ccc327b..e046601 100644 --- a/source/Trek-12/Models/Game/Map.cs +++ b/source/Trek-12/Models/Game/Map.cs @@ -1,4 +1,6 @@ -namespace Models.Game +using System.Collections.ObjectModel; + +namespace Models.Game { /// @@ -9,7 +11,8 @@ /// /// It is the list of cells on the map. /// - public List Boards { get; private set; } + public ReadOnlyObservableCollection Boards { get; private set; } + ObservableCollection board = new ObservableCollection(); /// /// It is the backgrond image of the map @@ -19,7 +22,8 @@ /// /// It is the grid of the possible operation in the game /// - public List OperationGrid { get; private set; } + public ReadOnlyObservableCollection OperationGrid { get; private set; } + ObservableCollection operationGrid = new ObservableCollection(); /// /// It is a list of a list containing user's rope paths in the current game @@ -37,9 +41,11 @@ /// The background of the map. public Map(string background) { - Boards = InitializeBoards(); + Boards = new ReadOnlyObservableCollection(board); + InitializeBoards(board); Background = background; - OperationGrid = InitializeOperationGrid(); + OperationGrid = new ReadOnlyObservableCollection(operationGrid); + InitializeOperationGrid(operationGrid); RopePaths = new List>(); Zones = new List>(); } @@ -48,23 +54,43 @@ /// Initializes the boards of the map. /// /// Return the boards - private List InitializeBoards() + private void InitializeBoards(ObservableCollection board) { - var boards = new List(); - for (int i = 0; i < 36; i++) // 6x6 board + for (int i = 0; i < 49; i++) // 6x6 board { - boards.Add(new Cell(i / 6, i % 6)); + board.Add(new Cell(i / 7, i % 7)); } - return boards; + board[1].Valid = true; + board[3].Valid = true; + board[4].Valid = true; + board[5].Valid = true; + board[9].Valid = true; + board[12].Valid = true; + board[15].Valid = true; + board[16].Valid = true; + board[17].Valid = true; + board[21].Valid = true; + board[24].Valid = true; + board[25].Valid = true; + board[28].Valid = true; + board[30].Valid = true; + board[31].Valid = true; + board[32].Valid = true; + board[40].Valid = true; + board[41].Valid = true; + board[48].Valid = true; + + board[3].IsDangerous = true; + board[15].IsDangerous = true; + board[16].IsDangerous = true; } /// /// Initializes the operation grid of the map. /// /// Return the operation grid - private List InitializeOperationGrid() + private void InitializeOperationGrid(ObservableCollectionoperationGrid) { - var operationGrid = new List(); for (int i = 0; i < 5; i++) // 5 operations { for (int j = 0; j < 4; j++) // 4 cells per operation @@ -72,7 +98,6 @@ operationGrid.Add(new OperationCell(i, j)); } } - return operationGrid; } public bool CheckOperationPossible(int x) diff --git a/source/Trek-12/Models/Game/OperationCell.cs b/source/Trek-12/Models/Game/OperationCell.cs index 4667ff0..459f325 100644 --- a/source/Trek-12/Models/Game/OperationCell.cs +++ b/source/Trek-12/Models/Game/OperationCell.cs @@ -1,3 +1,5 @@ +using System.ComponentModel; + namespace Models.Game { /// @@ -5,10 +7,20 @@ namespace Models.Game /// public class OperationCell : Position { + + private bool isChecked; /// /// It tells if the operation is checked or not in the operation grid of the game. /// - public bool IsChecked { get; private set; } + public bool IsChecked + { + get => isChecked; + private set + { + isChecked = value; + OnPropertyChanged(nameof(IsChecked)); + } + } /// /// Constructor of the OperationCell class. diff --git a/source/Trek-12/Models/Game/Position.cs b/source/Trek-12/Models/Game/Position.cs index c1b75da..d34ece5 100644 --- a/source/Trek-12/Models/Game/Position.cs +++ b/source/Trek-12/Models/Game/Position.cs @@ -1,10 +1,13 @@ -namespace Models.Game +using System.ComponentModel; +using System.Runtime.CompilerServices; + +namespace Models.Game { /// /// The Position (x,y) of a cell in the game. /// - public class Position + public class Position : INotifyPropertyChanged { /// /// The X coordinate. @@ -26,5 +29,19 @@ X = x; Y = y; } + + /// + /// Event raised when a property is changed to notify the view. + /// + public event PropertyChangedEventHandler PropertyChanged; + + /// + /// Trigger the PropertyChanged event for a specific property. + /// + /// Name of the property that changed. + protected virtual void OnPropertyChanged(string propertyName) + { + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } } } \ 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 341d1ff..7e17b3e 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -130,7 +131,7 @@ namespace Models.Rules List adjacentCells = new List(); - adjacentCells = EveryAdjacentCells(chosenCell, map.Boards); + adjacentCells = EveryAdjacentCells(chosenCell, map.Boards.ToList()); foreach(var cells in adjacentCells) { diff --git a/source/Trek-12/Stub/Stub.cs b/source/Trek-12/Stub/Stub.cs index e7f3292..d933f4a 100644 --- a/source/Trek-12/Stub/Stub.cs +++ b/source/Trek-12/Stub/Stub.cs @@ -1,4 +1,4 @@ -using System.Collections.ObjectModel; +using System.Collections.ObjectModel; using System.Data; using System.Numerics; using Models.Game; @@ -14,9 +14,9 @@ namespace Stub public Stub() { - LoadPlayer(); ListPlayer = new ReadOnlyObservableCollection(listplayer); ListMap = new ReadOnlyObservableCollection(listmap); + LoadPlayer(); LoadMap(); } @@ -27,14 +27,8 @@ namespace Stub listplayer.Add(new Player("Lucas", "profile.jpg")); listplayer.Add(new Player("relavergne", "profile.jpg")); listplayer.Add(new Player("reneveu", "profile.jpg")); - } - - public void Add(string pseudo, string profilePicture) - { - listplayer.Add(new Player(pseudo, profilePicture)); - } - + public void LoadMap() { @@ -43,14 +37,19 @@ namespace Stub listmap.Add(new Map("tmp1.jpeg")); } + public void Add(string pseudo, string profilePicture) + { + listplayer.Add(new Player(pseudo, profilePicture)); + } + public void Pop(string pseudo, string profilePicture) { listplayer.Remove(new Player(pseudo, profilePicture)); } - + public bool Modify(string pseudo, string newpseudo) { - foreach(var index in listplayer) + foreach (var index in listplayer) { if (index.Pseudo == pseudo) { @@ -58,6 +57,7 @@ namespace Stub return true; } } + return false; } } diff --git a/source/Trek-12/Tests/RulesTests.cs b/source/Trek-12/Tests/RulesTests.cs index 7cfe05a..653a3da 100644 --- a/source/Trek-12/Tests/RulesTests.cs +++ b/source/Trek-12/Tests/RulesTests.cs @@ -35,7 +35,7 @@ public class RulesTests Map map = new Map("background"); Cell selectedCell = map.Boards[0]; selectedCell.Value = 5; - Assert.False(rules.IsCellValid(selectedCell, map.Boards)); + Assert.False(rules.IsCellValid(selectedCell, map.Boards.ToList())); } [Fact] @@ -44,7 +44,7 @@ public class RulesTests Rules rules = new Rules(); Map map = new Map("background"); Cell selectedCell = map.Boards[0]; - Assert.True(rules.IsCellValid(selectedCell, map.Boards)); + Assert.True(rules.IsCellValid(selectedCell, map.Boards.ToList())); } [Fact] @@ -211,7 +211,7 @@ public class RulesTests Cell adjacentCell = new Cell(0, 1); adjacentCell.Value = 1; - map.Boards.Add(adjacentCell); + map.Boards.ToList().Add(adjacentCell); map.Zones.Add(new List { adjacentCell }); rules.IsZoneValidAndAddToZones(cell, map); @@ -228,7 +228,7 @@ public class RulesTests Cell adjacentCell = new Cell(0, 1); adjacentCell.Value = 1; - map.Boards.Add(adjacentCell); + map.Boards.ToList().Add(adjacentCell); rules.IsZoneValidAndAddToZones(cell, map); Assert.Contains(cell, map.Zones[0]); } diff --git a/source/Trek-12/Trek-12/AppShell.xaml b/source/Trek-12/Trek-12/AppShell.xaml index 9b1b669..0179a5a 100644 --- a/source/Trek-12/Trek-12/AppShell.xaml +++ b/source/Trek-12/Trek-12/AppShell.xaml @@ -34,9 +34,9 @@ ContentTemplate="{DataTemplate views:PageSelectMap}" Route="PageSelectMap"/> - + diff --git a/source/Trek-12/Trek-12/Resources/Images/checked.png b/source/Trek-12/Trek-12/Resources/Images/checked.png new file mode 100644 index 0000000..fdac7db Binary files /dev/null and b/source/Trek-12/Trek-12/Resources/Images/checked.png differ diff --git a/source/Trek-12/Trek-12/Resources/Images/maptest.png b/source/Trek-12/Trek-12/Resources/Images/maptest.png new file mode 100644 index 0000000..03dd7c4 Binary files /dev/null and b/source/Trek-12/Trek-12/Resources/Images/maptest.png differ diff --git a/source/Trek-12/Trek-12/Trek-12.csproj b/source/Trek-12/Trek-12/Trek-12.csproj index 6fa80dd..58891bc 100644 --- a/source/Trek-12/Trek-12/Trek-12.csproj +++ b/source/Trek-12/Trek-12/Trek-12.csproj @@ -56,6 +56,11 @@ + + + + + @@ -84,6 +89,9 @@ MSBuild:Compile + + MSBuild:Compile + MSBuild:Compile diff --git a/source/Trek-12/Trek-12/Views/PageBoard.xaml b/source/Trek-12/Trek-12/Views/PageBoard.xaml new file mode 100644 index 0000000..7ac0747 --- /dev/null +++ b/source/Trek-12/Trek-12/Views/PageBoard.xaml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/source/Trek-12/Trek-12/Views/PageBoard.xaml.cs b/source/Trek-12/Trek-12/Views/PageBoard.xaml.cs new file mode 100644 index 0000000..1f66020 --- /dev/null +++ b/source/Trek-12/Trek-12/Views/PageBoard.xaml.cs @@ -0,0 +1,32 @@ +using System.Diagnostics; + +namespace Trek_12.Views; + +using Models.Game; +using Stub; + +public partial class PageBoard : ContentPage +{ + public Stub MyStub { get; set; } = new Stub(); + + public PageBoard() + { + InitializeComponent(); + BindingContext = MyStub; + } + + private void OnOperationCellSelected(object sender, SelectionChangedEventArgs e) + { + Debug.WriteLine("OnOperationCellSelected"); // Debug + if (e.CurrentSelection.Count > 0) // Si un élément est sélectionné + { + var selectedCell = (OperationCell)e.CurrentSelection[0]; + if (selectedCell != null && !selectedCell.IsChecked) + { + selectedCell.Check(); + Debug.WriteLine("OperationCell at ({0}, {1}) is checked", selectedCell.X, selectedCell.Y); // Debug + } + ((CollectionView)sender).SelectedItem = null; // Déselectionne l'élément pour la CollectionView + } + } +} \ No newline at end of file