⚙️ Correction de bugs dans le jeu

pull/112/head
Rémi LAVERGNE 11 months ago
parent 62ff6c130f
commit 103fd02652

@ -1,4 +1,6 @@
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace Models.Game
{
@ -6,7 +8,7 @@ namespace Models.Game
/// The Cell class represents a cell in the application.
/// </summary>
[DataContract]
public class Cell : Position, IEquatable<Cell>
public class Cell : Position, IEquatable<Cell>, INotifyPropertyChanged
{
/// <summary>
/// The value of the cell.
@ -22,6 +24,8 @@ namespace Models.Game
throw new Exception("La valeur doit être supérieure à 0");
}
this._value = value;
OnPropertyChanged();
}
}
@ -76,5 +80,13 @@ namespace Models.Game
if (this.X == other.X && this.Y == other.Y) return true;
return false;
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

@ -105,8 +105,28 @@ namespace Models.Game
public Operation PlayerOperation { get; set; }
public Cell PlayerCell { get; set; }
public int Resultat { get; set; }
private Cell _playerCell;
public Cell PlayerCell {
get => _playerCell;
set
{
_playerCell = value;
OnPropertyChanged(nameof(PlayerCell));
}
}
private int _resultat;
public int Resultat {
get => _resultat;
set
{
_resultat = value;
OnPropertyChanged(nameof(Resultat));
}
}
public bool DiceRolledFlag { get; private set; }
public bool OperationChosenFlag { get; private set; }
public Rules.Rules GameRules { get; }
@ -137,20 +157,6 @@ namespace Models.Game
{
Maps.Add(map);
}
/// <summary>
/// Deletes the last game in the list of games. Use for avoiding stack not finished games.
/// </summary>
/// <returns></returns>
public bool DeleteGame()
{
if (Games.Count == 0)
{
return false;
}
Games.RemoveAt(Games.Count - 1); // Remove the last game
return true;
}
/// <summary>
/// Adds a new best score to the list of best scores. Or updates it if it already exists.
@ -316,7 +322,11 @@ namespace Models.Game
{
Dice1.Roll();
Dice2.Roll();
DiceRolledFlag = true;
OperationChosenFlag = false;
DiceRolled?.Invoke(this, new DiceRolledEventArgs(Dice1.Value, Dice2.Value));
OnPropertyChanged(nameof(Dice1));
OnPropertyChanged(nameof(Dice2));
}
/// <summary>
@ -385,6 +395,7 @@ namespace Models.Game
if (item.X == playerChoice.X && item.Y == playerChoice.Y)
{
item.Value = result;
OnPropertyChanged(nameof(UsedMap.Boards));
return;
}
@ -432,16 +443,30 @@ namespace Models.Game
/// </summary>
public void InitializeGame(Map map, Player player, bool startImmediately = true)
{
var runningGames = Games.Where(g => g.IsRunning).ToList();
foreach (var game in runningGames)
{
Games.Remove(game);
}
OnPropertyChanged(nameof(Games));
UsedMap = map;
CurrentPlayer = player;
Turn = 1;
Dice1 = new Dice();
Dice2 = new Dice(1);
IsPreviousGameNotFinished = false;
OnPropertyChanged(nameof(UsedMap));
OnPropertyChanged(nameof(CurrentPlayer));
OnPropertyChanged(nameof(Turn));
if (startImmediately)
{
StartGame();
}
SaveData();
}
/// <summary>
@ -451,6 +476,8 @@ namespace Models.Game
{
IsRunning = true;
GameStarted?.Invoke(this, new GameStartedEventArgs(CurrentPlayer));
SaveData();
}
/// <summary>
@ -485,6 +512,7 @@ namespace Models.Game
PlayerChooseOp?.Invoke(this, new PlayerChooseOperationEventArgs(PlayerOperation));
}
PlayerOption?.Invoke(this, new PlayerOptionEventArgs(UsedMap.Boards.ToList(), ResultOperation(PlayerOperation), Turn));
OperationChosenFlag = true;
return ResultOperation(PlayerOperation);
}
@ -497,6 +525,8 @@ namespace Models.Game
}
MarkOperationAsChecked(PlayerOperation);
PlaceResult(PlayerCell, Resultat);
DiceRolledFlag = false;
OperationChosenFlag = false;
}
/// <summary>
@ -506,6 +536,7 @@ namespace Models.Game
public void HandlePlayerOperation(Operation operation)
{
int result = ResultOperation(operation);
OperationChosenFlag = true;
OperationChosen?.Invoke(this, new OperationChosenEventArgs(operation, result));
}

@ -64,6 +64,16 @@ namespace Models.Game
Zones = new List<List<Cell>>();
}
/// <summary>
/// Clone the map to avoid reference problems.
/// </summary>
/// <returns></returns>
public Map Clone()
{
Map map = new Map(Name, Background);
return map;
}
/// <summary>
/// Initializes the boards of the map.
/// </summary>

@ -40,9 +40,9 @@ namespace Trek_12
/* Add the permanent maps if they are not already in the game */
if (Manager.Maps.Count == 0)
{
Manager.AddMap(new Map("Dunai","profile.jpg"));
Manager.AddMap(new Map("Kagkot","montagne1.png"));
Manager.AddMap(new Map("Dhaulagiri","tmp1.jpeg"));
Manager.AddMap(new Map("Dunai","montagne2.png"));
Manager.AddMap(new Map("Kagkot", "montagne3.png"));
Manager.AddMap(new Map("Dhaulagiri", "montagne4.png"));
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 978 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 972 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.9 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 MiB

@ -55,7 +55,6 @@
<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
@ -69,6 +68,9 @@
<None Remove="Resources\Images\back_arrow.png" />
<None Remove="Resources\Images\checked.png" />
<None Remove="Resources\Images\maptest.png" />
<None Remove="Resources\Images\montagne2.png" />
<None Remove="Resources\Images\montagne3.png" />
<None Remove="Resources\Images\montagne4.png" />
<None Remove="Resources\Images\user.png" />
</ItemGroup>

@ -1,5 +1,6 @@
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.VisualBasic;
namespace Trek_12.Views;
@ -10,7 +11,7 @@ public partial class PageBoard : ContentPage
{
public Game GameManager => (App.Current as App).Manager;
public int Resultat { get; set; }
public int Result { get; set; }
public Cell ChoosenCell { get; set; }
@ -26,13 +27,46 @@ public partial class PageBoard : ContentPage
GameManager.DiceRolled += ResultSubstraction;
GameManager.DiceRolled += ResultMultiplication;
GameManager.PlayerOption += GameManager_PlayerOption;
// We add this game to the list of games
GameManager.CellChosen += HandleCellChosen;
GameManager.AddGame(GameManager);
GameManager.OnPropertyChanged(nameof(GameManager.Games));
GameManager.SaveData();
}
private void HandleCellChosen(object sender, CellChosenEventArgs e)
{
YellowDice.IsVisible = false;
RedDice.IsVisible = false;
RollButton.IsEnabled = true;
}
private void ResetOperationButtonsAndDice()
{
Lower.IsVisible = false;
Higher.IsVisible = false;
Substraction.IsVisible = false;
Addition.IsVisible = false;
Multiplication.IsVisible = false;
RollButton.IsEnabled = true;
YellowDice.IsVisible = false;
RedDice.IsVisible = false;
}
private void SetOperationButtonState(Button selectedButton)
{
// Deselect all buttons
Lower.BackgroundColor = Colors.DarkSalmon;
Higher.BackgroundColor = Colors.DarkSalmon;
Substraction.BackgroundColor = Colors.DarkSalmon;
Addition.BackgroundColor = Colors.DarkSalmon;
Multiplication.BackgroundColor = Colors.DarkSalmon;
// Select the clicked button
selectedButton.BackgroundColor = Colors.LightCoral;
}
private void GameManager_PlayerOption(object? sender, PlayerOptionEventArgs e)
{
/* IEnumerable<Cell> PlayedCellsQuery =
@ -92,8 +126,7 @@ public partial class PageBoard : ContentPage
private void OnOperationCellSelected(object sender, SelectionChangedEventArgs e)
{
Debug.WriteLine("OnOperationCellSelected"); // Debug
if (e.CurrentSelection.Count > 0) // Si un <20>l<EFBFBD>ment est s<>lectionn<6E>
if (e.CurrentSelection.Count > 0) // Si un élément est sélectionné
{
var selectedCell = (OperationCell)e.CurrentSelection[0];
if (selectedCell != null && !selectedCell.IsChecked)
@ -101,38 +134,48 @@ public partial class PageBoard : ContentPage
selectedCell.Check();
Debug.WriteLine("OperationCell at ({0}, {1}) is checked", selectedCell.X, selectedCell.Y); // Debug
}
((CollectionView)sender).SelectedItem = null; // D<EFBFBD>selectionne l'<27>l<EFBFBD>ment pour la CollectionView
((CollectionView)sender).SelectedItem = null; // Déselectionne l'élément pour la CollectionView
}
}
private void HigherClicked(object sender, EventArgs e)
{
GameManager.PlayerOperation = Operation.HIGHER;
Resultat = GameManager.PlayerChooseOperation();
SetOperationButtonState((Button)sender);
Result = GameManager.ResultOperation(Operation.HIGHER);
GameManager.HandlePlayerOperation(Operation.HIGHER);
}
private void LowerClicked(object sender, EventArgs e)
{
GameManager.PlayerOperation = Operation.LOWER;
Resultat = GameManager.PlayerChooseOperation();
SetOperationButtonState((Button)sender);
Result = GameManager.ResultOperation(Operation.LOWER);
GameManager.HandlePlayerOperation(Operation.LOWER);
}
private void AdditionClicked(object sender, EventArgs e)
{
GameManager.PlayerOperation = Operation.ADDITION;
Resultat = GameManager.PlayerChooseOperation();
SetOperationButtonState((Button)sender);
Result = GameManager.ResultOperation(Operation.ADDITION);
GameManager.HandlePlayerOperation(Operation.ADDITION);
}
private void SubstractionClicked(object sender, EventArgs e)
{
GameManager.PlayerOperation = Operation.SUBTRACTION;
Resultat = GameManager.PlayerChooseOperation();
SetOperationButtonState((Button)sender);
Result = GameManager.ResultOperation(Operation.SUBTRACTION);
GameManager.HandlePlayerOperation(Operation.SUBTRACTION);
}
private void MultiplicationClicked(object sender, EventArgs e)
{
GameManager.PlayerOperation = Operation.MULTIPLICATION;
Resultat = GameManager.PlayerChooseOperation();
SetOperationButtonState((Button)sender);
Result = GameManager.ResultOperation(Operation.MULTIPLICATION);
GameManager.HandlePlayerOperation(Operation.MULTIPLICATION);
}
private void DiceButton_Clicked(object sender, EventArgs e)
@ -140,10 +183,33 @@ public partial class PageBoard : ContentPage
GameManager.RollAllDice();
}
private void OnCellSelected(object sender, SelectionChangedEventArgs e)
private async void OnCellSelected(object sender, SelectionChangedEventArgs e)
{
ChoosenCell = (Cell)e.CurrentSelection[0];
GameManager.PlayerCell = ChoosenCell;
GameManager.PlayerSelectionCell();
if (!GameManager.DiceRolledFlag)
{
await DisplayAlert("Action Required", "You must roll the dice first.", "OK");
return;
}
if (!GameManager.OperationChosenFlag)
{
await DisplayAlert("Action Required", "You must choose an operation first.", "OK");
return;
}
if (e.CurrentSelection.Count > 0)
{
ChoosenCell = (Cell)e.CurrentSelection[0];
GameManager.PlayerCell = ChoosenCell;
GameManager.Resultat = Result;
OnPropertyChanged(nameof(GameManager.PlayerCell));
OnPropertyChanged(nameof(GameManager.Resultat));
GameManager.PlayerSelectionCell();
((CollectionView)sender).SelectedItem = null;
ResetOperationButtonsAndDice();
}
}
}

@ -10,8 +10,6 @@ public partial class PageSelectMap : ContentPage
public Game SelectMapManager => (App.Current as App).Manager;
private Map? _selectedMap;
private bool previousGameRunning = false;
private bool isVisibleContinueButton = false;
protected override async void OnAppearing()
@ -19,7 +17,7 @@ public partial class PageSelectMap : ContentPage
base.OnAppearing();
if (SelectMapManager.Games.Any(g => g.IsRunning))
{
previousGameRunning = true;
isVisibleContinueButton = true;
await DisplayAlert("Warning", "You've previously quit in the middle of a game.\nIf you start a new game, this one will be permanently lost.", "I understand");
}
@ -61,18 +59,27 @@ public partial class PageSelectMap : ContentPage
Player chosenPlayer = GetProfileByName(choosenPlayerName);
SelectMapManager.InitializeGame(_selectedMap, chosenPlayer);
var runningGames = SelectMapManager.Games.Where(g => g.IsRunning).ToList();
bool delete = false;
foreach (var game in runningGames)
{
SelectMapManager.Games.Remove(game);
delete = true;
}
if (delete)
{
await DisplayAlert("Game deleted", "The previous game has been deleted because you started a new one.", "OK");
SelectMapManager.OnPropertyChanged(nameof(SelectMapManager.Games));
SelectMapManager.SaveData();
}
SelectMapManager.InitializeGame(_selectedMap.Clone(), chosenPlayer);
if (SelectMapManager.UsedMap == _selectedMap && Equals(SelectMapManager.CurrentPlayer, chosenPlayer))
if (SelectMapManager.UsedMap != null && Equals(SelectMapManager.CurrentPlayer, chosenPlayer))
{
// Delete the previous game if it was running
if (previousGameRunning)
{
bool rep = SelectMapManager.DeleteGame();
SelectMapManager.SaveData();
if (rep) await DisplayAlert("Game deleted", "The previous game has been deleted because you started a new one.", "OK");
}
await Shell.Current.GoToAsync(nameof(PageBoard));
}
else
@ -100,7 +107,7 @@ public partial class PageSelectMap : ContentPage
return;
}
SelectMapManager.InitializeGame(game.UsedMap, game.CurrentPlayer);
SelectMapManager.InitializeGame(game.UsedMap, game.CurrentPlayer, false);
await Shell.Current.GoToAsync(nameof(PageBoard));
}

Loading…
Cancel
Save