diff --git a/source/Trek-12/ConsoleApp/ConsoleApp.csproj b/source/Trek-12/ConsoleApp/ConsoleApp.csproj
index 229387b..5d6a28e 100644
--- a/source/Trek-12/ConsoleApp/ConsoleApp.csproj
+++ b/source/Trek-12/ConsoleApp/ConsoleApp.csproj
@@ -8,6 +8,7 @@
+
diff --git a/source/Trek-12/ConsoleApp/Program.cs b/source/Trek-12/ConsoleApp/Program.cs
index cb57385..5f96c6c 100644
--- a/source/Trek-12/ConsoleApp/Program.cs
+++ b/source/Trek-12/ConsoleApp/Program.cs
@@ -4,6 +4,8 @@ using Models;
using Models.Events;
using Models.Exceptions;
using Models.Game;
+using Models.Interfaces;
+using DataContractPersistence;
namespace ConsoleApp;
@@ -19,10 +21,11 @@ class Program
string? pseudo = Console.ReadLine();
if (pseudo != null)
{
+ IPersistence persistence = new DataContractXml();
Player player = new Player(pseudo);
Map map = new Map("background");
- Game game = new Game(player, map);
+ Game game = new Game(persistence);
// Abonnement aux événements
game.GameStarted += OnGameStarted!;
@@ -33,7 +36,7 @@ class Program
game.CellChosen += OnCellChosen!;
// Initialisation
- game.InitializeGame();
+ game.InitializeGame(map, player);
}
}
diff --git a/source/Trek-12/Models/Game/Dice.cs b/source/Trek-12/Models/Game/Dice.cs
index 2d47fe3..36bfd33 100644
--- a/source/Trek-12/Models/Game/Dice.cs
+++ b/source/Trek-12/Models/Game/Dice.cs
@@ -14,12 +14,12 @@ namespace Models.Game
///
/// Lowest number on the dice.
///
- public int NbMin { get; private set; }
+ public int MinVal { get; private set; }
///
/// Highest number on the dice.
///
- public int NbMax { get; private set; }
+ public int MaxVal { get; private set; }
///
/// Value of the dice.
@@ -29,9 +29,9 @@ namespace Models.Game
get => _value;
private set
{
- if (value < NbMin || value > NbMax)
+ if (value < MinVal || value > MaxVal)
{
- value = NbMin;
+ value = MinVal;
}
_value = value;
}
@@ -40,13 +40,13 @@ namespace Models.Game
///
/// Initializes a new instance of the class.
///
- /// The lowest number on the dice, on which the highest number is set
- public Dice(int nbmin)
+ /// The lowest number on the dice, on which the highest number is set
+ public Dice(int minval)
{
- if (nbmin < 0) nbmin = 0;
- if (nbmin > 1) nbmin = 1;
- NbMin = nbmin;
- NbMax = nbmin + 5;
+ if (minval < 0) minval = 0;
+ if (minval > 1) minval = 1;
+ MinVal = minval;
+ MaxVal = minval + 5;
}
///
@@ -54,13 +54,13 @@ namespace Models.Game
///
public Dice()
{
- NbMin = 0;
- NbMax = 5;
+ MinVal = 0;
+ MaxVal = 5;
}
public override string ToString()
{
- return $"Ce dé a pour valeur {Value} et est entre {NbMin} et {NbMax}";
+ return $"Ce dé a pour valeur {Value} et est entre {MinVal} et {MaxVal}";
}
///
@@ -68,7 +68,7 @@ namespace Models.Game
///
public void Roll()
{
- Value = new Random().Next(NbMin, NbMax + 1);
+ Value = new Random().Next(MinVal, MaxVal + 1);
}
///
diff --git a/source/Trek-12/Models/Game/Game.cs b/source/Trek-12/Models/Game/Game.cs
index 982b2af..c4f4460 100644
--- a/source/Trek-12/Models/Game/Game.cs
+++ b/source/Trek-12/Models/Game/Game.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -27,14 +28,20 @@ namespace Models.Game
public List BestScores { get; set; }
private bool _isRunning;
+ public bool IsRunning
+ {
+ get => _isRunning;
+ private set => _isRunning = value;
+ }
+
public Player CurrentPlayer { get; private set; }
public Map UsedMap { get; private set; }
- private Dice Dice1 { get; }
- private Dice Dice2 { get; }
+ public Dice Dice1 { get; private set; }
+ public Dice Dice2 { get; private set; }
- private int Turn { get; set; }
+ public int Turn { get; private set; }
public Rules.Rules GameRules { get; }
@@ -98,25 +105,10 @@ namespace Models.Game
Games = new List();
Maps = new List
private void GameLoop()
{
- while (_isRunning)
+ while (IsRunning)
{
if (Turn == 20)
{
diff --git a/source/Trek-12/Models/Rules/Rules.cs b/source/Trek-12/Models/Rules/Rules.cs
index bd8c8cf..341d1ff 100644
--- a/source/Trek-12/Models/Rules/Rules.cs
+++ b/source/Trek-12/Models/Rules/Rules.cs
@@ -209,6 +209,8 @@ namespace Models.Rules
public List EveryAdjacentCells(Cell choosenCell, List cells)
{
List adjacentCells = new List();
+
+ if (choosenCell == null || cells == null) return adjacentCells;
foreach (var cell in cells)
{
@@ -230,7 +232,7 @@ namespace Models.Rules
calculus += zones[i].Count - 1 + zones[i][0].Value;
if (zones[i].Count > 9)
{
- calculus += (zones[i].Count - 9) * 5;
+ calculus += (zones[i].Count - 9) * 5 - (zones[i].Count - 9);
}
}
return calculus;
diff --git a/source/Trek-12/Tests/DiceTests.cs b/source/Trek-12/Tests/DiceTests.cs
index 7831db6..22ded18 100644
--- a/source/Trek-12/Tests/DiceTests.cs
+++ b/source/Trek-12/Tests/DiceTests.cs
@@ -13,30 +13,30 @@ public class DiceTests
public void Constructor_WithNegativeNbMin_SetsNbMinToZero()
{
Dice dice = new Dice(-1);
- Assert.Equal(0, dice.NbMin);
+ Assert.Equal(0, dice.MinVal);
}
[Fact]
public void Constructor_WithNbMinGreaterThanOne_SetsNbMinToOne()
{
Dice dice = new Dice(2);
- Assert.Equal(1, dice.NbMin);
+ Assert.Equal(1, dice.MinVal);
}
[Fact]
public void Constructor_WithValidNbMin_SetsNbMinAndNbMaxCorrectly()
{
Dice dice = new Dice(1);
- Assert.Equal(1, dice.NbMin);
- Assert.Equal(6, dice.NbMax);
+ Assert.Equal(1, dice.MinVal);
+ Assert.Equal(6, dice.MaxVal);
}
[Fact]
public void DefaultConstructor_SetsNbMinToZeroAndNbMaxToFive()
{
Dice dice = new Dice();
- Assert.Equal(0, dice.NbMin);
- Assert.Equal(5, dice.NbMax);
+ Assert.Equal(0, dice.MinVal);
+ Assert.Equal(5, dice.MaxVal);
}
[Fact]
@@ -44,6 +44,6 @@ public class DiceTests
{
Dice dice = new Dice();
dice.Roll();
- Assert.True(dice.Value >= dice.NbMin && dice.Value <= dice.NbMax);
+ Assert.True(dice.Value >= dice.MinVal && dice.Value <= dice.MaxVal);
}
}
diff --git a/source/Trek-12/Tests/GameTests.cs b/source/Trek-12/Tests/GameTests.cs
new file mode 100644
index 0000000..066aeff
--- /dev/null
+++ b/source/Trek-12/Tests/GameTests.cs
@@ -0,0 +1,246 @@
+using Moq;
+using Models.Game;
+using Models.Interfaces;
+using System.Reflection;
+using static System.Type;
+
+namespace Tests;
+
+public class GameTests
+{
+ private readonly Mock _mockPersistence; // Mocking (faking) the persistence layer to allow for testing without a persistent connection
+ private readonly Game _game;
+
+ public GameTests()
+ {
+ _mockPersistence = new Mock();
+ _game = new Game(_mockPersistence.Object);
+ }
+
+ private void SetDiceValues(Game game, int value1, int value2)
+ {
+ var dice1Field = typeof(Game).GetProperty("Dice1", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
+ var dice2Field = typeof(Game).GetProperty("Dice2", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
+
+ if (dice1Field != null && dice2Field != null)
+ {
+ var dice1 = (Dice)dice1Field?.GetValue(game)!;
+ var dice2 = (Dice)dice2Field?.GetValue(game)!;
+
+ var valueField = typeof(Dice).GetField("_value", BindingFlags.Instance | BindingFlags.NonPublic);
+ valueField?.SetValue(dice1, value1);
+ valueField?.SetValue(dice2, value2);
+ }
+ }
+
+ [Fact]
+ public void AddPlayer_ShouldAddPlayerToList()
+ {
+ var player = new Player();
+
+ _game.AddPlayer(player);
+
+ Assert.Contains(player, _game.Players);
+ }
+
+ [Fact]
+ public void AddGame_ShouldAddGameToList()
+ {
+ var game = new Game(_mockPersistence.Object);
+
+ _game.AddGame(game);
+
+ Assert.Contains(game, _game.Games);
+ }
+
+ [Fact]
+ public void AddMap_ShouldAddMapToList()
+ {
+ var map = new Map("test_background.png");
+
+ _game.AddMap(map);
+
+ Assert.Contains(map, _game.Maps);
+ }
+
+ [Fact]
+ public void AddBestScore_ShouldAddBestScoreToList()
+ {
+ var bestScore = new BestScore(3, 125);
+
+ _game.AddBestScore(bestScore);
+
+ Assert.Contains(bestScore, _game.BestScores);
+ }
+
+ [Fact]
+ public void LoadData_ShouldLoadDataFromPersistence()
+ {
+ var players = new List { new Player() };
+ var games = new List { new Game(_mockPersistence.Object) };
+ var maps = new List | | | |