fin de l'initialisation d'un match et ajout du random aux commandes #29

Merged
mcmuzzle merged 1 commits from 22_finalPreparation into main 2026-02-05 10:20:33 +00:00
11 changed files with 163 additions and 32 deletions

View File

@@ -6,6 +6,7 @@ indent_style = space
indent_size = 4 indent_size = 4
charset = utf-8 charset = utf-8
dotnet_diagnostic.CA1307.severity = none dotnet_diagnostic.CA1307.severity = none
dotnet_diagnostic.CA5394.severity = none //Pour l'utilisation de random
[Src/HexagonalLib/**] [Src/HexagonalLib/**]
generated_code = true generated_code = true

View File

@@ -8,6 +8,9 @@ public abstract class BaseCommand<T> where T : BaseCommandResult
{ {
protected abstract T LocalExecute(); protected abstract T LocalExecute();
public required Random Random { get; init; }
/// <summary> Executer la commande, si nécessaire il faut avoir rempli les parametres en amont </summary> /// <summary> Executer la commande, si nécessaire il faut avoir rempli les parametres en amont </summary>
public T Execute() public T Execute()
{ {

View File

@@ -27,7 +27,7 @@ public class PrepareMatchCommand : BaseMatchCommand<PrepareMatchResult>
{ {
if (nbSocle > 0) if (nbSocle > 0)
{ {
player.PutPieceInVisibleArea(socle); player.PutPieceInHiddenArea(socle);
nbSocle--; nbSocle--;
} }
else else
@@ -43,7 +43,7 @@ public class PrepareMatchCommand : BaseMatchCommand<PrepareMatchResult>
{ {
if (i < 2) if (i < 2)
{ {
player.PutPieceInVisibleArea(marker); player.PutPieceInHiddenArea(marker);
} }
else else
{ {
@@ -53,8 +53,37 @@ public class PrepareMatchCommand : BaseMatchCommand<PrepareMatchResult>
} }
//assigner les ouvriers (1 chef, 1 shaman et 1 worker chacun) //assigner les ouvriers (1 chef, 1 shaman et 1 worker chacun)
match.AssignPiece(player.Chef, player.VisiblePosition); match.AssignPiece(player.Chef, player.HiddenPosition);
match.AssignPiece(player.Shaman, player.VisiblePosition); match.AssignPiece(player.Shaman, player.HiddenPosition);
int nbWorker = 1;
foreach (PieceIndex worker in player.Workers)
{
if (nbWorker > 0)
{
match.AssignPiece(worker, player.HiddenPosition);
nbWorker--;
}
else
{
match.AssignPiece(worker, PiecePosition.Urne);
}
}
match.AssignPiece(player.Workers.First(), player.HiddenPosition);
//score a zero
for (int iteScore = 0; iteScore < match.NbPlayer; iteScore++)
{
match.SetScore(iteScore, 0);
}
//choix d'un 1er joueur
Player? firstPlayer = match.GetPlayer(Random.Next(match.NbPlayer));
if (null == firstPlayer)
throw new InitialisationException("Cannot select first player, indexOutOfRange");
match.AssignPiece(PieceIndex.StartPlayer, firstPlayer.VisiblePosition);
} }
return new PrepareMatchResult() return new PrepareMatchResult()
{ {

View File

@@ -36,11 +36,15 @@ public class Match
/// </summary> /// </summary>
int[] dices = [0, 0, 0, 0, 0,]; int[] dices = [0, 0, 0, 0, 0,];
/// <summary>
/// Indique si pour une foret donnée le bois a ete ramassé, l'index et les quantités sont dans le boardLayout
/// </summary>
bool[] forestUsed = [false, false, false, false, false, false, false];
/// <summary> /// <summary>
/// un tableau indiquant ou sont placé les pieces, l'index est l'id de la piece et la valeur la position de la piece /// /// un tableau indiquant ou sont placé les pieces, l'index est l'id de la piece et la valeur la position de la piece ///
/// </summary> /// </summary>
PiecePosition[] pieces = new PiecePosition[(int)Enum.GetValues(typeof(PieceIndex)).Cast<PieceIndex>().Max() + 1]; PiecePosition[] pieces = new PiecePosition[(int)Enum.GetValues(typeof(PieceIndex)).Cast<PieceIndex>().Max() + 1];
#endregion #endregion
#region Getters #region Getters
@@ -72,6 +76,16 @@ public class Match
players[index] = playerId; players[index] = playerId;
} }
public void SetScore(int index, int score)
{
scores[index] = score;
}
public int GetScore(int index)
{
return scores[index];
}
public PiecePosition GetPiece(PieceIndex PieceIndex) public PiecePosition GetPiece(PieceIndex PieceIndex)
{ {
return pieces[(int)PieceIndex]; return pieces[(int)PieceIndex];
@@ -95,6 +109,8 @@ public class Match
Buffer.BlockCopy(scores, 0, result.scores, 0, scores.Length * sizeof(int)); Buffer.BlockCopy(scores, 0, result.scores, 0, scores.Length * sizeof(int));
Buffer.BlockCopy(dices, 0, result.dices, 0, dices.Length * sizeof(int)); Buffer.BlockCopy(dices, 0, result.dices, 0, dices.Length * sizeof(int));
Buffer.BlockCopy(pieces, 0, result.pieces, 0, pieces.Length * sizeof(PieceIndex)); Buffer.BlockCopy(pieces, 0, result.pieces, 0, pieces.Length * sizeof(PieceIndex));
Buffer.BlockCopy(forestUsed, 0, result.forestUsed, 0, forestUsed.Length * sizeof(bool));
return result; return result;
@@ -110,7 +126,8 @@ public class Match
return Enumerable.SequenceEqual(players, match.players) && return Enumerable.SequenceEqual(players, match.players) &&
Enumerable.SequenceEqual(scores, match.scores) && Enumerable.SequenceEqual(scores, match.scores) &&
Enumerable.SequenceEqual(dices, match.dices) && Enumerable.SequenceEqual(dices, match.dices) &&
Enumerable.SequenceEqual(pieces, match.pieces); Enumerable.SequenceEqual(pieces, match.pieces) &&
Enumerable.SequenceEqual(forestUsed, match.forestUsed);
} }
return false; return false;
} }

View File

@@ -48,16 +48,21 @@ public class Player
{ {
_match.AssignPiece(piece, HiddenPosition); _match.AssignPiece(piece, HiddenPosition);
} }
public int Score => _match.GetScore(Index);
#endregion #endregion
#region workers #region workers
public PieceIndex Chef => PlayerPieces[Index].Where(p => p.ToString().Contains("Chief")).FirstOrDefault(); public PieceIndex Chef => PlayerPieces[Index].Where(p => p.ToString().Contains("Chief")).FirstOrDefault();
public PieceIndex Shaman => PlayerPieces[Index].Where(p => p.ToString().Contains("Shaman")).FirstOrDefault(); public PieceIndex Shaman => PlayerPieces[Index].Where(p => p.ToString().Contains("Shaman")).FirstOrDefault();
public ICollection<PieceIndex> Workers => [.. PlayerPieces[Index].Where(p => p.ToString().Contains("Worker"))];
#endregion #endregion
#region socles #region socles
public ICollection<PieceIndex> Bases => PlayerPieces[Index]?.Where(p => p.ToString().Contains("Base")).ToList() ?? []; public ICollection<PieceIndex> Bases => PlayerPieces[Index]?.Where(p => p.ToString().Contains("Base")).ToList() ?? [];
public int NbVisibleBase => Bases.Count(p => _match.GetPiece(p) == VisiblePosition); public int NbVisibleBase => Bases.Count(p => _match.GetPiece(p) == VisiblePosition);
public int NbHiddenBase => Bases.Count(p => _match.GetPiece(p) == HiddenPosition);
#endregion #endregion
#region Tribal markers #region Tribal markers

View File

@@ -0,0 +1,14 @@
namespace Giants.Core.Exceptions;
/// <summary>
/// Exception levée lors d'un probleme pendant l'initialisation de la partie
/// </summary>
public class InitialisationException : System.Exception
{
/// <inheritdoc/>>
public InitialisationException() { }
/// <inheritdoc/>>
public InitialisationException(string message) : base(message) { }
/// <inheritdoc/>>
public InitialisationException(string message, System.Exception inner) : base(message, inner) { }
}

View File

@@ -16,6 +16,18 @@ public class BoardLayout
Dictionary<int, TileInfos> _tiles; Dictionary<int, TileInfos> _tiles;
Dictionary<AxialCoords, int> _forest = new Dictionary<AxialCoords, int>(){
{new AxialCoords(4,2),0},
{new AxialCoords(4,1),1},
{new AxialCoords(5,0),2},
{new AxialCoords(5,1),3},
{new AxialCoords(5,2),4},
{new AxialCoords(6,0),5},
{new AxialCoords(6,1),6},
};
int[] _forestQte = [5, 5, 4, 4, 3, 3, 3];
int width = 16; int width = 16;
public BoardLayout(IHexagonalGrid gridSystem) public BoardLayout(IHexagonalGrid gridSystem)
@@ -31,6 +43,21 @@ public class BoardLayout
return tmp.Where(t => _tiles.ContainsKey(Index(t.Q, t.R))).ToList(); return tmp.Where(t => _tiles.ContainsKey(Index(t.Q, t.R))).ToList();
} }
public int ForestQte(AxialCoords coords)
{
int index = _forest[coords];
return ForestQte(index);
}
public int ForestIndex(AxialCoords coords)
{
if (_forest.TryGetValue(coords, out int value))
{
return value;
}
return -1;
}
public int ForestQte(int index) => _forestQte[index];
[MemberNotNull(nameof(_tiles))] [MemberNotNull(nameof(_tiles))]
private void BuildDefaultBoard() private void BuildDefaultBoard()
{ {

View File

@@ -21,10 +21,13 @@ public class GetMatchCommandTest
[Fact] [Fact]
public void GetMatchNotExisting() public void GetMatchNotExisting()
{ {
Random random = new Random(1);
GetMatchCommand command = new GetMatchCommand(_repo) GetMatchCommand command = new GetMatchCommand(_repo)
{ {
MatchId = 2 MatchId = 2,
Random = random
}; };
GetMatchResult result = command.Execute(); GetMatchResult result = command.Execute();
Assert.False(result.Success); Assert.False(result.Success);
Assert.Null(result.Match); Assert.Null(result.Match);
@@ -33,9 +36,11 @@ public class GetMatchCommandTest
[Fact] [Fact]
public void GetMatchSimple() public void GetMatchSimple()
{ {
Random random = new Random(1);
GetMatchCommand command = new GetMatchCommand(_repo) GetMatchCommand command = new GetMatchCommand(_repo)
{ {
MatchId = 1 MatchId = 1,
Random = random
}; };
GetMatchResult result = command.Execute(); GetMatchResult result = command.Execute();
Assert.True(result.Success); Assert.True(result.Success);

View File

@@ -19,7 +19,8 @@ public class NewMatchCommandTest
[Fact] [Fact]
public void CreateBasicMatch() public void CreateBasicMatch()
{ {
NewMatchCommand command = new NewMatchCommand(_repo); Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result = command.Execute(); NewMatchResult result = command.Execute();
Assert.True(result.Success); Assert.True(result.Success);
} }
@@ -27,12 +28,13 @@ public class NewMatchCommandTest
[Fact] [Fact]
public void TwoNewMatchShouldHaveDifferentID() public void TwoNewMatchShouldHaveDifferentID()
{ {
NewMatchCommand command = new NewMatchCommand(_repo); Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result = command.Execute(); NewMatchResult result = command.Execute();
Assert.True(result.Success); Assert.True(result.Success);
Assert.NotNull(result.Match); Assert.NotNull(result.Match);
NewMatchCommand command2 = new NewMatchCommand(_repo); NewMatchCommand command2 = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result2 = command2.Execute(); NewMatchResult result2 = command2.Execute();
Assert.True(result2.Success); Assert.True(result2.Success);
Assert.NotNull(result2.Match); Assert.NotNull(result2.Match);

View File

@@ -1,5 +1,6 @@
using Giants.Application; using Giants.Application;
using Giants.Core.Commands; using Giants.Core.Commands;
using Giants.Core.Enums;
using Giants.Core.Interfaces; using Giants.Core.Interfaces;
using Giants.Infrastructure; using Giants.Infrastructure;
@@ -19,12 +20,13 @@ public class PrepareMatchCommandTest
[Fact] [Fact]
void SimpleInit5Players() void SimpleInit5Players()
{ {
NewMatchCommand command = new NewMatchCommand(_repo); Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result = command.Execute(); NewMatchResult result = command.Execute();
Match? m = result.Match; Match? m = result.Match;
Assert.NotNull(m); Assert.NotNull(m);
PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5, 14, 9 } }; PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5, 14, 9 }, Random = random };
var resultPrep = prepCommand.Execute(); var resultPrep = prepCommand.Execute();
Assert.True(resultPrep.Success); Assert.True(resultPrep.Success);
Assert.NotNull(resultPrep?.Match); Assert.NotNull(resultPrep?.Match);
@@ -34,24 +36,29 @@ public class PrepareMatchCommandTest
Player? p = resultPrep.Match.GetPlayer(i); Player? p = resultPrep.Match.GetPlayer(i);
Assert.NotNull(p); Assert.NotNull(p);
Assert.Equal(2, p.NbVisibleTribalTokenCount); Assert.Equal(0, p.NbVisibleTribalTokenCount);
Assert.Equal(0, p.NbHiddenTribalTokenCount); Assert.Equal(2, p.NbHiddenTribalTokenCount);
Assert.Equal(4, p.NbUrnTribalTokenCount); Assert.Equal(4, p.NbUrnTribalTokenCount);
Assert.Equal(5, p.NbVisibleBase); Assert.Equal(5, p.NbHiddenBase);
Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Chef)); Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef));
Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Shaman)); Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Shaman));
Assert.Equal(1, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == p.HiddenPosition));
Assert.Equal(5, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == Enums.PiecePosition.Urne));
Assert.Equal(0, p.Score);
Assert.NotEqual(PiecePosition.boite, resultPrep.Match.GetPiece(PieceIndex.StartPlayer));
} }
} }
[Fact] [Fact]
void SimpleInit4Players() void SimpleInit4Players()
{ {
NewMatchCommand command = new NewMatchCommand(_repo); Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result = command.Execute(); NewMatchResult result = command.Execute();
Match? m = result.Match; Match? m = result.Match;
Assert.NotNull(m); Assert.NotNull(m);
PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5, 14 } }; PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5, 14 }, Random = random };
var resultPrep = prepCommand.Execute(); var resultPrep = prepCommand.Execute();
Assert.True(resultPrep.Success); Assert.True(resultPrep.Success);
Assert.NotNull(resultPrep?.Match); Assert.NotNull(resultPrep?.Match);
@@ -61,24 +68,29 @@ public class PrepareMatchCommandTest
Player? p = resultPrep.Match.GetPlayer(i); Player? p = resultPrep.Match.GetPlayer(i);
Assert.NotNull(p); Assert.NotNull(p);
Assert.Equal(2, p.NbVisibleTribalTokenCount); Assert.Equal(0, p.NbVisibleTribalTokenCount);
Assert.Equal(0, p.NbHiddenTribalTokenCount); Assert.Equal(2, p.NbHiddenTribalTokenCount);
Assert.Equal(4, p.NbUrnTribalTokenCount); Assert.Equal(4, p.NbUrnTribalTokenCount);
Assert.Equal(6, p.NbVisibleBase); Assert.Equal(6, p.NbHiddenBase);
Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Chef)); Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef));
Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Shaman)); Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Shaman));
Assert.Equal(1, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == p.HiddenPosition));
Assert.Equal(5, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == Enums.PiecePosition.Urne));
Assert.Equal(0, p.Score);
Assert.NotEqual(PiecePosition.boite, resultPrep.Match.GetPiece(PieceIndex.StartPlayer));
} }
} }
[Fact] [Fact]
void SimpleInit3Players() void SimpleInit3Players()
{ {
NewMatchCommand command = new NewMatchCommand(_repo); Random random = new Random(1);
NewMatchCommand command = new NewMatchCommand(_repo) { Random = random };
NewMatchResult result = command.Execute(); NewMatchResult result = command.Execute();
Match? m = result.Match; Match? m = result.Match;
Assert.NotNull(m); Assert.NotNull(m);
PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5 } }; PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List<int>() { 12, 15, 5 }, Random = random };
var resultPrep = prepCommand.Execute(); var resultPrep = prepCommand.Execute();
Assert.True(resultPrep.Success); Assert.True(resultPrep.Success);
Assert.NotNull(resultPrep?.Match); Assert.NotNull(resultPrep?.Match);
@@ -88,12 +100,16 @@ public class PrepareMatchCommandTest
Player? p = resultPrep.Match.GetPlayer(i); Player? p = resultPrep.Match.GetPlayer(i);
Assert.NotNull(p); Assert.NotNull(p);
Assert.Equal(2, p.NbVisibleTribalTokenCount); Assert.Equal(0, p.NbVisibleTribalTokenCount);
Assert.Equal(0, p.NbHiddenTribalTokenCount); Assert.Equal(2, p.NbHiddenTribalTokenCount);
Assert.Equal(4, p.NbUrnTribalTokenCount); Assert.Equal(4, p.NbUrnTribalTokenCount);
Assert.Equal(7, p.NbVisibleBase); Assert.Equal(7, p.NbHiddenBase);
Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Chef)); Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef));
Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Shaman)); Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Shaman));
Assert.Equal(1, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == p.HiddenPosition));
Assert.Equal(5, p.Workers.Count(w => resultPrep.Match.GetPiece(w) == Enums.PiecePosition.Urne));
Assert.Equal(0, p.Score);
Assert.NotEqual(PiecePosition.boite, resultPrep.Match.GetPiece(PieceIndex.StartPlayer));
} }
} }
} }

View File

@@ -52,4 +52,16 @@ public class BoardLayoutTests
BoardLayout layout = new BoardLayout(grid); BoardLayout layout = new BoardLayout(grid);
Assert.Equal(PiecePosition.tile144, layout.FromTileIndex(144)); Assert.Equal(PiecePosition.tile144, layout.FromTileIndex(144));
} }
[Fact]
public void CheckForestQuantities()
{
IHexagonalGrid grid = new HexagonalGridImpl();
BoardLayout layout = new BoardLayout(grid);
int forest1Index = layout.ForestIndex(new AxialCoords(4, 2));
Assert.Equal(0, forest1Index);
int qteForest1 = layout.ForestQte(forest1Index);
Assert.Equal(5, qteForest1);
}
} }