MàJ révision du code
continuous-integration/drone/push Build is failing Details

pull/61/head
Rémi LAVERGNE 12 months ago
parent 07a93069e2
commit f73549b0d5

@ -2,51 +2,27 @@
public class Cell
{
public Position Pos { get; private set; }
private int? value;
private int? _value;
public int? Value {
get
{
return value;
}
get => _value;
set
{
if (value < 0)
{
throw new Exception("La valeur doit être supérieure à 0");
}
this.value = value;
this._value = value;
}
}
public bool IsDangerous { get; set; }
private bool IsDangerous { get; set; }
public string? Background { get; set; }
public Cell(int x, int y)
public Cell(bool isDangerous = false)
{
Pos = new Position(x, y);
IsDangerous = isDangerous;
}
public bool GetCellType() => IsDangerous;
// Redefinition de l'opérateur == pour comparer deux cellules
public override bool Equals(object? obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
Cell c = (Cell)obj;
return (Pos.X == c.Pos.X) && (Pos.Y == c.Pos.Y);
}
// Redefinition de la méthode de Hash pour l'opérateur ==
public override int GetHashCode()
{
return Pos.X * Pos.Y;
}
}

@ -0,0 +1,14 @@
using Models.Game;
namespace Models.Events;
public class GameStartedEventArgs : EventArgs
{
public Player Player { get; }
public Map Map { get; }
public GameStartedEventArgs(Player player, Map map)
{
Player = player;
Map = map;
}
}

@ -12,7 +12,7 @@ namespace Models.Game
public int NbMax { get; private set; }
public int Nb { get; private set; }
public int Value { get; private set; }
public Dice(int nbmin)
{
@ -30,17 +30,17 @@ namespace Models.Game
public override string ToString()
{
return $"Ce dé a pour valeur {Nb} et est entre {NbMin} et {NbMax}";
return $"Ce dé a pour valeur {Value} et est entre {NbMin} et {NbMax}";
}
public void Lancer()
{
Nb = new Random().Next(NbMin, NbMax + 1);
Value = new Random().Next(NbMin, NbMax + 1);
}
public bool IsLower(Dice dice2)
{
return dice2.Nb > this.Nb;
return dice2.Value > this.Value;
}
}
}

@ -19,8 +19,10 @@ namespace Models.Game
private Rules.Rules GameRules { get; }
private HashSet<RopePath> ropePaths { get; }
public Game(Player player,Map map)
public Game(Player player, Map map)
{
UsedMap = map;
CurrentPlayer = player;
@ -28,6 +30,7 @@ namespace Models.Game
Dice2 = new Dice(1);
Turn = 0;
GameRules = new Rules.Rules();
ropePaths = new HashSet<RopePath>();
}
public void RollAllDice()
@ -68,8 +71,10 @@ namespace Models.Game
}
}
public void AddRopeZone(Cell playerChoice,RopesZones list)
public void AddToRopePath(Cell cell)
{
if (GameRules.IsRopePath(cell, ropePaths)) return;
}
}

@ -2,13 +2,12 @@
public class Map
{
public List<Cell> Cells { get; private set; }
public SortedDictionary<Position, Cell> Cells { get; private set; }
public string Background { get; private set; }
public Map(List<Cell> cells, string background)
public Map(string background)
{
Cells = cells;
Cells = new SortedDictionary<Position, Cell>();
Background = background;
}
}

@ -0,0 +1,6 @@
namespace Models.Game;
public class RopePath
{
public HashSet<Cell> Cells { get; } = new HashSet<Cell>();
}

@ -7,9 +7,8 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Interfaces\" />
<Folder Include="Game\" />
<Folder Include="Exceptions\" />
<Folder Include="Game\" />
<Folder Include="Interfaces\" />
<Folder Include="Rules\" />
</ItemGroup>

@ -1,25 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models
{
public class Position
{
public Position(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
public override string ToString()
{
return $"{X} {Y}";
}
}
}
namespace Models;
public struct Position
{
public int X { get; set; }
public int Y { get; set; }
public Position(int x, int y)
{
X = x;
Y = y;
}
}

@ -3,31 +3,48 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models.Game;
namespace Models.Rules
{
public class Rules
{
public static bool NearCell(Cell playerChoice, List<Cell> cells)
public bool IsCellExist(Position playerChoicePosition, SortedDictionary<Position, Cell> cells)
{
return cells.Any(item => playerChoice.Pos.X == item.Pos.X + 1 || playerChoice.Pos.X == item.Pos.X - 1 || playerChoice.Pos.Y == item.Pos.Y + 1 || playerChoice.Pos.Y == item.Pos.Y - 1);
return cells.ContainsKey(playerChoicePosition);
}
public bool NearCellIsValid(Position playerChoicePosition, SortedDictionary<Position, Cell> cells)
{
if (!IsCellExist(playerChoicePosition, cells)) return false;
foreach (var cell in cells)
{
if (Math.Abs(playerChoicePosition.X - cell.Key.X) > 1 || Math.Abs(playerChoicePosition.Y - cell.Key.Y) > 1) continue;
if (!IsCellEmpty(cell.Value))
{
return true;
}
}
return false;
}
public static bool IsCellEmpty(Cell playerChoice)
public bool IsCellEmpty(Cell playerChoice)
{
return playerChoice.Value == null;
return !playerChoice.Value.HasValue;
}
public static bool IsCellValid(Cell playerChoice, List<Cell> cells)
public bool IsCellValid(Position playerChoicePosition, SortedDictionary<Position, Cell> cells)
{
return NearCell(playerChoice, cells) && IsCellEmpty(playerChoice);
return NearCellIsValid(playerChoicePosition, cells) && IsCellEmpty(cells.
}
public static bool IsZone(Cell playerChoice, List<Cell> cells)
public bool IsZone(Cell playerChoice, SortedDictionary<Position, Cell> cells)
{
foreach (var item in cells)
foreach (var k,v in cells)
{
if (playerChoice.Pos.X != item.Pos.X + 1 && playerChoice.Pos.X != item.Pos.X - 1
if (playerChoice.X != item.X + 1 && playerChoice.X != item.Pos.X - 1
&& playerChoice.Pos.Y != item.Pos.Y + 1 &&
playerChoice.Pos.Y != item.Pos.Y - 1) continue;
if (playerChoice.Value == item.Value)
@ -38,29 +55,32 @@ namespace Models.Rules
return false;
}
public static bool IsRopePath(Cell playerChoice, List<Cell> cells, List<Cell> ropePaths)
/// <summary>
/// Determines if a given cell is part of any rope path.
/// </summary>
/// <param name="playerChoice">The cell to check.</param>
/// <param name="ropePaths">A collection of rope paths to check against.</param>
/// <returns>True if the cell is part of any rope path, false otherwise.</returns>
public bool IsRopePath(Cell playerChoice, HashSet<RopePath> ropePaths)
{
foreach (var item in cells)
foreach (var path in ropePaths)
{
if (playerChoice.Pos.X != item.Pos.X + 1 && playerChoice.Pos.X != item.Pos.X - 1
&& playerChoice.Pos.Y != item.Pos.Y + 1 &&
playerChoice.Pos.Y != item.Pos.Y - 1) continue;
if (playerChoice.Value != item.Value + 1 && playerChoice.Value != item.Value - 1) continue;
foreach (var path in ropePaths)
if (path.Cells.Contains(playerChoice))
{
if (path.Equals(playerChoice))
{
return true;
}
return true;
}
return IsRopePath(item, cells, ropePaths);
}
return false;
}
public static int HowMany(Cell playerChoice, List<Cell> cells)
public bool IsAdjacent(Cell cell1, Cell cell2, SortedDictionary<Position, Cell> cells)
{
bool isSequence = cell1.Value.HasValue && cell2.Value.HasValue && Math.Abs(cell1.Value.Value - cell2.Value.Value) == 1;
return IsCellValid(cell1, cells) && isSequence;
}
public int HowMany(Cell playerChoice, SortedDictionary<Position, Cell> cells)
{
foreach(var pos in cells)
{
@ -72,7 +92,7 @@ namespace Models.Rules
return 0;
}
public static void SetValueAndPenalty(int valueChoice, Cell playerChoice, List<Cell> cells)
public void SetValueAndPenalty(int valueChoice, Cell playerChoice, SortedDictionary<Position, Cell> cells)
{
int val = HowMany(playerChoice, cells);

Loading…
Cancel
Save