From 3dba66344d0221b4494ca572d084b4b20b3556b4 Mon Sep 17 00:00:00 2001 From: mcmuzzle Date: Thu, 24 Apr 2025 22:31:19 +0200 Subject: [PATCH] fin de l'initialisation d'un match et ajout du random aux commandes --- .editorconfig | 1 + Src/Giants.Core/Src/Commands/BaseCommand.cs | 3 + .../Src/Commands/PrepareMatchCommand.cs | 37 ++++++++++-- Src/Giants.Core/Src/Entities/Match.cs | 21 ++++++- Src/Giants.Core/Src/Entities/Player.cs | 5 ++ .../Src/Exceptions/InitialisationException.cs | 14 +++++ .../Src/ValuesObjects/BoardLayout.cs | 27 +++++++++ .../Commands/GetMatchCommandTest.cs | 9 ++- .../Commands/NewMatchCommandTest.cs | 8 ++- .../Commands/PrepareMatchCommandTest.cs | 58 ++++++++++++------- .../Entities/BoardLayoutTests.cs | 12 ++++ 11 files changed, 163 insertions(+), 32 deletions(-) create mode 100644 Src/Giants.Core/Src/Exceptions/InitialisationException.cs diff --git a/.editorconfig b/.editorconfig index 505cfcf..9c06c69 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,6 +6,7 @@ indent_style = space indent_size = 4 charset = utf-8 dotnet_diagnostic.CA1307.severity = none +dotnet_diagnostic.CA5394.severity = none //Pour l'utilisation de random [Src/HexagonalLib/**] generated_code = true \ No newline at end of file diff --git a/Src/Giants.Core/Src/Commands/BaseCommand.cs b/Src/Giants.Core/Src/Commands/BaseCommand.cs index 38c62e9..f089e2e 100644 --- a/Src/Giants.Core/Src/Commands/BaseCommand.cs +++ b/Src/Giants.Core/Src/Commands/BaseCommand.cs @@ -8,6 +8,9 @@ public abstract class BaseCommand where T : BaseCommandResult { protected abstract T LocalExecute(); + public required Random Random { get; init; } + + /// Executer la commande, si nécessaire il faut avoir rempli les parametres en amont public T Execute() { diff --git a/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs b/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs index 20bd115..ca40d4f 100644 --- a/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs +++ b/Src/Giants.Core/Src/Commands/PrepareMatchCommand.cs @@ -27,7 +27,7 @@ public class PrepareMatchCommand : BaseMatchCommand { if (nbSocle > 0) { - player.PutPieceInVisibleArea(socle); + player.PutPieceInHiddenArea(socle); nbSocle--; } else @@ -43,7 +43,7 @@ public class PrepareMatchCommand : BaseMatchCommand { if (i < 2) { - player.PutPieceInVisibleArea(marker); + player.PutPieceInHiddenArea(marker); } else { @@ -53,8 +53,37 @@ public class PrepareMatchCommand : BaseMatchCommand } //assigner les ouvriers (1 chef, 1 shaman et 1 worker chacun) - match.AssignPiece(player.Chef, player.VisiblePosition); - match.AssignPiece(player.Shaman, player.VisiblePosition); + match.AssignPiece(player.Chef, player.HiddenPosition); + 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() { diff --git a/Src/Giants.Core/Src/Entities/Match.cs b/Src/Giants.Core/Src/Entities/Match.cs index 40bc533..0843073 100644 --- a/Src/Giants.Core/Src/Entities/Match.cs +++ b/Src/Giants.Core/Src/Entities/Match.cs @@ -36,11 +36,15 @@ public class Match /// int[] dices = [0, 0, 0, 0, 0,]; + /// + /// Indique si pour une foret donnée le bois a ete ramassé, l'index et les quantités sont dans le boardLayout + /// + bool[] forestUsed = [false, false, false, false, false, false, false]; + /// /// un tableau indiquant ou sont placé les pieces, l'index est l'id de la piece et la valeur la position de la piece /// /// PiecePosition[] pieces = new PiecePosition[(int)Enum.GetValues(typeof(PieceIndex)).Cast().Max() + 1]; - #endregion #region Getters @@ -72,6 +76,16 @@ public class Match 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) { return pieces[(int)PieceIndex]; @@ -95,6 +109,8 @@ public class Match Buffer.BlockCopy(scores, 0, result.scores, 0, scores.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(forestUsed, 0, result.forestUsed, 0, forestUsed.Length * sizeof(bool)); + return result; @@ -110,7 +126,8 @@ public class Match return Enumerable.SequenceEqual(players, match.players) && Enumerable.SequenceEqual(scores, match.scores) && Enumerable.SequenceEqual(dices, match.dices) && - Enumerable.SequenceEqual(pieces, match.pieces); + Enumerable.SequenceEqual(pieces, match.pieces) && + Enumerable.SequenceEqual(forestUsed, match.forestUsed); } return false; } diff --git a/Src/Giants.Core/Src/Entities/Player.cs b/Src/Giants.Core/Src/Entities/Player.cs index 29b0103..192c2ad 100644 --- a/Src/Giants.Core/Src/Entities/Player.cs +++ b/Src/Giants.Core/Src/Entities/Player.cs @@ -48,16 +48,21 @@ public class Player { _match.AssignPiece(piece, HiddenPosition); } + + public int Score => _match.GetScore(Index); + #endregion #region workers 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 ICollection Workers => [.. PlayerPieces[Index].Where(p => p.ToString().Contains("Worker"))]; #endregion #region socles public ICollection Bases => PlayerPieces[Index]?.Where(p => p.ToString().Contains("Base")).ToList() ?? []; public int NbVisibleBase => Bases.Count(p => _match.GetPiece(p) == VisiblePosition); + public int NbHiddenBase => Bases.Count(p => _match.GetPiece(p) == HiddenPosition); #endregion #region Tribal markers diff --git a/Src/Giants.Core/Src/Exceptions/InitialisationException.cs b/Src/Giants.Core/Src/Exceptions/InitialisationException.cs new file mode 100644 index 0000000..fb6a573 --- /dev/null +++ b/Src/Giants.Core/Src/Exceptions/InitialisationException.cs @@ -0,0 +1,14 @@ +namespace Giants.Core.Exceptions; + +/// +/// Exception levée lors d'un probleme pendant l'initialisation de la partie +/// +public class InitialisationException : System.Exception +{ + /// > + public InitialisationException() { } + /// > + public InitialisationException(string message) : base(message) { } + /// > + public InitialisationException(string message, System.Exception inner) : base(message, inner) { } +} \ No newline at end of file diff --git a/Src/Giants.Core/Src/ValuesObjects/BoardLayout.cs b/Src/Giants.Core/Src/ValuesObjects/BoardLayout.cs index 4a967a1..be02025 100644 --- a/Src/Giants.Core/Src/ValuesObjects/BoardLayout.cs +++ b/Src/Giants.Core/Src/ValuesObjects/BoardLayout.cs @@ -16,6 +16,18 @@ public class BoardLayout Dictionary _tiles; + Dictionary _forest = new Dictionary(){ + {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; public BoardLayout(IHexagonalGrid gridSystem) @@ -31,6 +43,21 @@ public class BoardLayout 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))] private void BuildDefaultBoard() { diff --git a/Tests/Giants.Core.Tests/Commands/GetMatchCommandTest.cs b/Tests/Giants.Core.Tests/Commands/GetMatchCommandTest.cs index 028c951..3b5a268 100644 --- a/Tests/Giants.Core.Tests/Commands/GetMatchCommandTest.cs +++ b/Tests/Giants.Core.Tests/Commands/GetMatchCommandTest.cs @@ -21,10 +21,13 @@ public class GetMatchCommandTest [Fact] public void GetMatchNotExisting() { + Random random = new Random(1); GetMatchCommand command = new GetMatchCommand(_repo) { - MatchId = 2 + MatchId = 2, + Random = random }; + GetMatchResult result = command.Execute(); Assert.False(result.Success); Assert.Null(result.Match); @@ -33,9 +36,11 @@ public class GetMatchCommandTest [Fact] public void GetMatchSimple() { + Random random = new Random(1); GetMatchCommand command = new GetMatchCommand(_repo) { - MatchId = 1 + MatchId = 1, + Random = random }; GetMatchResult result = command.Execute(); Assert.True(result.Success); diff --git a/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs b/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs index 8b323f4..50df1ae 100644 --- a/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs +++ b/Tests/Giants.Core.Tests/Commands/NewMatchCommandTest.cs @@ -19,7 +19,8 @@ public class NewMatchCommandTest [Fact] public void CreateBasicMatch() { - NewMatchCommand command = new NewMatchCommand(_repo); + Random random = new Random(1); + NewMatchCommand command = new NewMatchCommand(_repo) { Random = random }; NewMatchResult result = command.Execute(); Assert.True(result.Success); } @@ -27,12 +28,13 @@ public class NewMatchCommandTest [Fact] public void TwoNewMatchShouldHaveDifferentID() { - NewMatchCommand command = new NewMatchCommand(_repo); + Random random = new Random(1); + NewMatchCommand command = new NewMatchCommand(_repo) { Random = random }; NewMatchResult result = command.Execute(); Assert.True(result.Success); Assert.NotNull(result.Match); - NewMatchCommand command2 = new NewMatchCommand(_repo); + NewMatchCommand command2 = new NewMatchCommand(_repo) { Random = random }; NewMatchResult result2 = command2.Execute(); Assert.True(result2.Success); Assert.NotNull(result2.Match); diff --git a/Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs b/Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs index 80d20d6..997aacb 100644 --- a/Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs +++ b/Tests/Giants.Core.Tests/Commands/PrepareMatchCommandTest.cs @@ -1,5 +1,6 @@ using Giants.Application; using Giants.Core.Commands; +using Giants.Core.Enums; using Giants.Core.Interfaces; using Giants.Infrastructure; @@ -19,12 +20,13 @@ public class PrepareMatchCommandTest [Fact] void SimpleInit5Players() { - NewMatchCommand command = new NewMatchCommand(_repo); + Random random = new Random(1); + NewMatchCommand command = new NewMatchCommand(_repo) { Random = random }; NewMatchResult result = command.Execute(); Match? m = result.Match; Assert.NotNull(m); - PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List() { 12, 15, 5, 14, 9 } }; + PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List() { 12, 15, 5, 14, 9 }, Random = random }; var resultPrep = prepCommand.Execute(); Assert.True(resultPrep.Success); Assert.NotNull(resultPrep?.Match); @@ -34,24 +36,29 @@ public class PrepareMatchCommandTest Player? p = resultPrep.Match.GetPlayer(i); Assert.NotNull(p); - Assert.Equal(2, p.NbVisibleTribalTokenCount); - Assert.Equal(0, p.NbHiddenTribalTokenCount); + Assert.Equal(0, p.NbVisibleTribalTokenCount); + Assert.Equal(2, p.NbHiddenTribalTokenCount); Assert.Equal(4, p.NbUrnTribalTokenCount); - Assert.Equal(5, p.NbVisibleBase); - Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Chef)); - Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Shaman)); + Assert.Equal(5, p.NbHiddenBase); + Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef)); + 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] void SimpleInit4Players() { - NewMatchCommand command = new NewMatchCommand(_repo); + Random random = new Random(1); + NewMatchCommand command = new NewMatchCommand(_repo) { Random = random }; NewMatchResult result = command.Execute(); Match? m = result.Match; Assert.NotNull(m); - PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List() { 12, 15, 5, 14 } }; + PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List() { 12, 15, 5, 14 }, Random = random }; var resultPrep = prepCommand.Execute(); Assert.True(resultPrep.Success); Assert.NotNull(resultPrep?.Match); @@ -61,24 +68,29 @@ public class PrepareMatchCommandTest Player? p = resultPrep.Match.GetPlayer(i); Assert.NotNull(p); - Assert.Equal(2, p.NbVisibleTribalTokenCount); - Assert.Equal(0, p.NbHiddenTribalTokenCount); + Assert.Equal(0, p.NbVisibleTribalTokenCount); + Assert.Equal(2, p.NbHiddenTribalTokenCount); Assert.Equal(4, p.NbUrnTribalTokenCount); - Assert.Equal(6, p.NbVisibleBase); - Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Chef)); - Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Shaman)); + Assert.Equal(6, p.NbHiddenBase); + Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef)); + 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] void SimpleInit3Players() { - NewMatchCommand command = new NewMatchCommand(_repo); + Random random = new Random(1); + NewMatchCommand command = new NewMatchCommand(_repo) { Random = random }; NewMatchResult result = command.Execute(); Match? m = result.Match; Assert.NotNull(m); - PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List() { 12, 15, 5 } }; + PrepareMatchCommand prepCommand = new PrepareMatchCommand() { InputMatch = m, PlayerIDs = new List() { 12, 15, 5 }, Random = random }; var resultPrep = prepCommand.Execute(); Assert.True(resultPrep.Success); Assert.NotNull(resultPrep?.Match); @@ -88,12 +100,16 @@ public class PrepareMatchCommandTest Player? p = resultPrep.Match.GetPlayer(i); Assert.NotNull(p); - Assert.Equal(2, p.NbVisibleTribalTokenCount); - Assert.Equal(0, p.NbHiddenTribalTokenCount); + Assert.Equal(0, p.NbVisibleTribalTokenCount); + Assert.Equal(2, p.NbHiddenTribalTokenCount); Assert.Equal(4, p.NbUrnTribalTokenCount); - Assert.Equal(7, p.NbVisibleBase); - Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Chef)); - Assert.Equal(p.VisiblePosition, resultPrep.Match.GetPiece(p.Shaman)); + Assert.Equal(7, p.NbHiddenBase); + Assert.Equal(p.HiddenPosition, resultPrep.Match.GetPiece(p.Chef)); + 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)); } } } \ No newline at end of file diff --git a/Tests/Giants.Core.Tests/Entities/BoardLayoutTests.cs b/Tests/Giants.Core.Tests/Entities/BoardLayoutTests.cs index 17a05c1..d58714f 100644 --- a/Tests/Giants.Core.Tests/Entities/BoardLayoutTests.cs +++ b/Tests/Giants.Core.Tests/Entities/BoardLayoutTests.cs @@ -52,4 +52,16 @@ public class BoardLayoutTests BoardLayout layout = new BoardLayout(grid); 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); + } } \ No newline at end of file -- 2.49.1