From 005546e43b0c57fa67adc9da296f46ca90664212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Thu, 30 May 2024 23:37:37 +0200 Subject: [PATCH 01/14] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Test=20des=20derni?= =?UTF-8?q?=C3=A8res=20r=C3=A8gles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Tests/RulesTests.cs | 243 +++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) diff --git a/source/Trek-12/Tests/RulesTests.cs b/source/Trek-12/Tests/RulesTests.cs index 56ec387..630cca2 100644 --- a/source/Trek-12/Tests/RulesTests.cs +++ b/source/Trek-12/Tests/RulesTests.cs @@ -232,4 +232,247 @@ public class RulesTests rules.IsZoneValidAndAddToZones(cell, map); Assert.Contains(cell, map.Zones[0]); } + + [Fact] + public void IsValueInZones_ReturnsFalse_WhenCellIsNull() + { + Rules rules = new Rules(); + Assert.False(rules.IsValueInZones(null, new List>())); + } + + [Fact] + public void IsValueInZones_ReturnsFalse_WhenZonesAreEmpty() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + Assert.False(rules.IsValueInZones(cell, new List>())); + } + + [Fact] + public void IsValueInZones_ReturnsFalse_WhenNoZoneContainsCellWithSameValue() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + List> zones = new List> + { + new List { new Cell(0, 1) { Value = 2 } }, + new List { new Cell(1, 0) { Value = 3 } } + }; + Assert.False(rules.IsValueInZones(cell, zones)); + } + + [Fact] + public void IsValueInZones_ReturnsTrue_WhenZoneContainsCellWithSameValue() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + List> zones = new List> + { + new List { new Cell(0, 1) { Value = 1 } }, + new List { new Cell(1, 0) { Value = 3 } } + }; + Assert.True(rules.IsValueInZones(cell, zones)); + } + + [Fact] + public void IsCellInZone_ReturnsFalse_WhenCellIsNull() + { + Rules rules = new Rules(); + Assert.False(rules.IsCellInZone(null, new List>())); + } + + [Fact] + public void IsCellInZone_ReturnsFalse_WhenZonesAreEmpty() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + Assert.False(rules.IsCellInZone(cell, new List>())); + } + + [Fact] + public void IsCellInZone_ReturnsFalse_WhenNoZoneContainsCell() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List> zones = new List> + { + new List { new Cell(0, 1) }, + new List { new Cell(1, 0) } + }; + Assert.False(rules.IsCellInZone(cell, zones)); + } + + [Fact] + public void IsCellInZone_ReturnsTrue_WhenZoneContainsCell() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List> zones = new List> + { + new List { new Cell(0, 1) }, + new List { cell } + }; + Assert.True(rules.IsCellInZone(cell, zones)); + } + + [Fact] + public void AddToZone_DoesNothing_WhenCellIsNull() + { + Rules rules = new Rules(); + List> zones = new List>(); + rules.AddToZone(null, zones); + Assert.Empty(zones); + } + + [Fact] + public void AddToZone_DoesNothing_WhenCellValueIsNull() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List> zones = new List>(); + rules.AddToZone(cell, zones); + Assert.Empty(zones); + } + + [Fact] + public void AddToZone_DoesNothing_WhenCellIsAlreadyInZone() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + List> zones = new List> + { + new List { cell } + }; + rules.AddToZone(cell, zones); + Assert.Single(zones[0]); // Verify if the List has still only one element + } + + [Fact] + public void AddToZone_AddsCellToExistingZone_WhenCellHasSameValueAsZone() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + List> zones = new List> + { + new List { new Cell(0, 1) { Value = 1 } } + }; + rules.AddToZone(cell, zones); + Assert.Contains(cell, zones[0]); + } + + [Fact] + public void NewZoneIsCreated_DoesNothing_WhenFirstCellIsNull() + { + Rules rules = new Rules(); + Map map = new Map("background"); + rules.NewZoneIsCreated(null, new Cell(0, 0), map); + Assert.Empty(map.Zones); + } + + [Fact] + public void NewZoneIsCreated_DoesNothing_WhenSecondCellIsNull() + { + Rules rules = new Rules(); + Map map = new Map("background"); + rules.NewZoneIsCreated(new Cell(0, 0), null, map); + Assert.Empty(map.Zones); + } + + [Fact] + public void NewZoneIsCreated_DoesNothing_WhenFirstCellValueIsNull() + { + Rules rules = new Rules(); + Map map = new Map("background"); + Cell firstCell = new Cell(0, 0); + rules.NewZoneIsCreated(firstCell, new Cell(0, 1), map); + Assert.Empty(map.Zones); + } + + [Fact] + public void NewZoneIsCreated_DoesNothing_WhenSecondCellValueIsNull() + { + Rules rules = new Rules(); + Map map = new Map("background"); + Cell secondCell = new Cell(0, 0); + rules.NewZoneIsCreated(new Cell(0, 1), secondCell, map); + Assert.Empty(map.Zones); + } + + [Fact] + public void NewZoneIsCreated_CreatesNewZone_WhenBothCellsAreNotNullAndHaveValues() + { + Rules rules = new Rules(); + Map map = new Map("background"); + Cell firstCell = new Cell(0, 0); + firstCell.Value = 1; + Cell secondCell = new Cell(0, 1); + secondCell.Value = 1; + rules.NewZoneIsCreated(firstCell, secondCell, map); + Assert.Single(map.Zones); + Assert.Contains(firstCell, map.Zones[0]); + Assert.Contains(secondCell, map.Zones[0]); + } + + [Fact] + public void EveryAdjacentCells_ReturnsEmptyList_WhenCellIsNull() + { + Rules rules = new Rules(); + List cells = new List { new Cell(0, 0), new Cell(0, 1) }; + Assert.Empty(rules.EveryAdjacentCells(null, cells)); + } + + [Fact] + public void EveryAdjacentCells_ReturnsEmptyList_WhenCellsAreEmpty() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + Assert.Empty(rules.EveryAdjacentCells(cell, new List())); + } + + [Fact] + public void EveryAdjacentCells_ReturnsEmptyList_WhenNoAdjacentCells() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List cells = new List { new Cell(2, 2), new Cell(3, 3) }; + Assert.Empty(rules.EveryAdjacentCells(cell, cells)); + } + + [Fact] + public void EveryAdjacentCells_ReturnsListWithAdjacentCells_WhenAdjacentCellsExist() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List cells = new List { new Cell(0, 1), new Cell(1, 0), new Cell(2, 2) }; + List result = rules.EveryAdjacentCells(cell, cells); + Assert.Equal(2, result.Count); + Assert.Contains(new Cell(0, 1), result); + Assert.Contains(new Cell(1, 0), result); + } + + [Fact] + public void FinalCalculusOfZones_ReturnsZero_WhenZonesAreEmpty() + { + Rules rules = new Rules(); + Assert.Equal(0, rules.FinalCalculusOfZones(new List>())); + } + + /* + [Fact] + public void FinalCalculusOfZones_ReturnsCorrectValue_WhenZonesAreNotEmpty() + { + // TODO + } + + [Fact] + public void FinalCalculusOfZones_ReturnsCorrectValue_WhenZoneCountIsGreaterThanNine() + { + // TODO + } + */ } \ No newline at end of file From 636cc3b72244ee4827d8d86ab0a8b465c0a91391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Thu, 30 May 2024 23:38:48 +0200 Subject: [PATCH 02/14] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20VS=C3=83V=C3=A9ri?= =?UTF-8?q?fication=20si=20param=C3=A8tres=20null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Rules/Rules.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/Trek-12/Models/Rules/Rules.cs b/source/Trek-12/Models/Rules/Rules.cs index 8a7dc43..73d4467 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -174,6 +174,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) { From d999520a9aac299b167f9ed803533b6e1b2a7b31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Fri, 31 May 2024 08:28:15 +0200 Subject: [PATCH 03/14] =?UTF-8?q?Ajout=20de=20deux=20tests=20sur=20le=20ca?= =?UTF-8?q?lcul=20des=20zones=20+=20r=C3=A9solution=20d'un=20probl=C3=A8me?= =?UTF-8?q?=20sur=20le=20code=20de=20cette=20m=C3=AAme=20fonction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Rules/Rules.cs | 2 +- source/Trek-12/Tests/RulesTests.cs | 55 ++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/source/Trek-12/Models/Rules/Rules.cs b/source/Trek-12/Models/Rules/Rules.cs index 73d4467..95859d7 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -197,7 +197,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/RulesTests.cs b/source/Trek-12/Tests/RulesTests.cs index 630cca2..7cfe05a 100644 --- a/source/Trek-12/Tests/RulesTests.cs +++ b/source/Trek-12/Tests/RulesTests.cs @@ -462,17 +462,64 @@ public class RulesTests Assert.Equal(0, rules.FinalCalculusOfZones(new List>())); } - /* [Fact] public void FinalCalculusOfZones_ReturnsCorrectValue_WhenZonesAreNotEmpty() { - // TODO + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + Cell cell1 = new Cell(0, 1); + cell1.Value = 1; + Cell cell2 = new Cell(0, 2); + cell2.Value = 1; + Cell uncell = new Cell(0, 0); + uncell.Value = 4; + Cell deuxcell = new Cell(0, 1); + deuxcell.Value = 4; + Cell troiscell = new Cell(0, 2); + troiscell.Value = 4; + List> zones = new List> + { + new List { cell, cell1, cell2 }, + new List { uncell, deuxcell, troiscell } + }; + + Assert.Equal(9, rules.FinalCalculusOfZones(zones)); } [Fact] public void FinalCalculusOfZones_ReturnsCorrectValue_WhenZoneCountIsGreaterThanNine() { - // TODO + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 5; + Cell cell1 = new Cell(0, 1); + cell1.Value = 5; + Cell cell2 = new Cell(0, 2); + cell2.Value = 5; + Cell cell3 = new Cell(0, 3); + cell3.Value = 5; + Cell cell4 = new Cell(0, 4); + cell4.Value = 5; + Cell cell5 = new Cell(0, 5); + cell5.Value = 5; + Cell cell6 = new Cell(1, 0); + cell6.Value = 5; + Cell cell7 = new Cell(1, 1); + cell7.Value = 5; + Cell cell8 = new Cell(1, 2); + cell8.Value = 5; + Cell cell9 = new Cell(1, 3); + cell9.Value = 5; + Cell cell10 = new Cell(1, 4); + cell10.Value = 5; + + List> zones = new List> + { + new List { cell, cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, cell10 } + }; + + Assert.Equal(23, rules.FinalCalculusOfZones(zones)); } - */ + } \ No newline at end of file From be61f9dd7ef4ceb40deaf65b9eb3224acb6ad16d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Fri, 31 May 2024 09:53:17 +0200 Subject: [PATCH 04/14] =?UTF-8?q?Ajout=20d'=C3=A9bauches=20de=20tests=20po?= =?UTF-8?q?ur=20GameTests=20mais=20non=20fonctionnelles=20(check=20pour=20?= =?UTF-8?q?utiliser=20reflection)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Tests/GameTests.cs | 96 +++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 source/Trek-12/Tests/GameTests.cs diff --git a/source/Trek-12/Tests/GameTests.cs b/source/Trek-12/Tests/GameTests.cs new file mode 100644 index 0000000..7f0abee --- /dev/null +++ b/source/Trek-12/Tests/GameTests.cs @@ -0,0 +1,96 @@ +namespace Tests; +using Models.Game; +using System.Reflection; +using static System.Type; +public class GameTests +{ + [Fact] + public void Game_Initialization_SetsPlayer() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + Assert.Equal(player, game.CurrentPlayer); + } + + [Fact] + public void Initialize_Turn_1() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + Game test = new Game(player, map); + + typeof(Game).GetProperty("Turn").SetValue(test, 1); + + + var result = test.GetResult(); + + Assert.Equal(typeof(Game).GetProperty("Turn").GetValue(test.Turn), 1); + } + /* + [Fact] + public void Game_Initialization_SetsMap() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + Assert.Equal(map, game.UsedMap); + } + + [Fact] + public void Game_Initialization_SetsDice() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + Assert.NotNull(game.Dice1); + Assert.NotNull(game.Dice2); + } + + [Fact] + public void Game_Initialization_SetsGameRules() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + Assert.NotNull(game.GameRules); + } + + [Fact] + public void RollAllDice_RollsAllDice() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + game.RollAllDice(); + + Assert.True(game.Dice1.Value >= 0 && game.Dice1.Value <= 5); + Assert.True(game.Dice2.Value >= 1 && game.Dice2.Value <= 6); + } + + [Fact] + public void MarkOperationAsChecked_Check_Well() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + game.MarkOperationAsChecked(Operation.LOWER); + + Assert.True(game.UsedMap.OperationGrid[0].IsChecked); + } + + */ +} From 0dc74f9aa13b466725a9f57878848e85d2b9b2d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Thu, 30 May 2024 23:37:37 +0200 Subject: [PATCH 05/14] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Test=20des=20derni?= =?UTF-8?q?=C3=A8res=20r=C3=A8gles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Tests/RulesTests.cs | 243 +++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) diff --git a/source/Trek-12/Tests/RulesTests.cs b/source/Trek-12/Tests/RulesTests.cs index 56ec387..630cca2 100644 --- a/source/Trek-12/Tests/RulesTests.cs +++ b/source/Trek-12/Tests/RulesTests.cs @@ -232,4 +232,247 @@ public class RulesTests rules.IsZoneValidAndAddToZones(cell, map); Assert.Contains(cell, map.Zones[0]); } + + [Fact] + public void IsValueInZones_ReturnsFalse_WhenCellIsNull() + { + Rules rules = new Rules(); + Assert.False(rules.IsValueInZones(null, new List>())); + } + + [Fact] + public void IsValueInZones_ReturnsFalse_WhenZonesAreEmpty() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + Assert.False(rules.IsValueInZones(cell, new List>())); + } + + [Fact] + public void IsValueInZones_ReturnsFalse_WhenNoZoneContainsCellWithSameValue() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + List> zones = new List> + { + new List { new Cell(0, 1) { Value = 2 } }, + new List { new Cell(1, 0) { Value = 3 } } + }; + Assert.False(rules.IsValueInZones(cell, zones)); + } + + [Fact] + public void IsValueInZones_ReturnsTrue_WhenZoneContainsCellWithSameValue() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + List> zones = new List> + { + new List { new Cell(0, 1) { Value = 1 } }, + new List { new Cell(1, 0) { Value = 3 } } + }; + Assert.True(rules.IsValueInZones(cell, zones)); + } + + [Fact] + public void IsCellInZone_ReturnsFalse_WhenCellIsNull() + { + Rules rules = new Rules(); + Assert.False(rules.IsCellInZone(null, new List>())); + } + + [Fact] + public void IsCellInZone_ReturnsFalse_WhenZonesAreEmpty() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + Assert.False(rules.IsCellInZone(cell, new List>())); + } + + [Fact] + public void IsCellInZone_ReturnsFalse_WhenNoZoneContainsCell() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List> zones = new List> + { + new List { new Cell(0, 1) }, + new List { new Cell(1, 0) } + }; + Assert.False(rules.IsCellInZone(cell, zones)); + } + + [Fact] + public void IsCellInZone_ReturnsTrue_WhenZoneContainsCell() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List> zones = new List> + { + new List { new Cell(0, 1) }, + new List { cell } + }; + Assert.True(rules.IsCellInZone(cell, zones)); + } + + [Fact] + public void AddToZone_DoesNothing_WhenCellIsNull() + { + Rules rules = new Rules(); + List> zones = new List>(); + rules.AddToZone(null, zones); + Assert.Empty(zones); + } + + [Fact] + public void AddToZone_DoesNothing_WhenCellValueIsNull() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List> zones = new List>(); + rules.AddToZone(cell, zones); + Assert.Empty(zones); + } + + [Fact] + public void AddToZone_DoesNothing_WhenCellIsAlreadyInZone() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + List> zones = new List> + { + new List { cell } + }; + rules.AddToZone(cell, zones); + Assert.Single(zones[0]); // Verify if the List has still only one element + } + + [Fact] + public void AddToZone_AddsCellToExistingZone_WhenCellHasSameValueAsZone() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + List> zones = new List> + { + new List { new Cell(0, 1) { Value = 1 } } + }; + rules.AddToZone(cell, zones); + Assert.Contains(cell, zones[0]); + } + + [Fact] + public void NewZoneIsCreated_DoesNothing_WhenFirstCellIsNull() + { + Rules rules = new Rules(); + Map map = new Map("background"); + rules.NewZoneIsCreated(null, new Cell(0, 0), map); + Assert.Empty(map.Zones); + } + + [Fact] + public void NewZoneIsCreated_DoesNothing_WhenSecondCellIsNull() + { + Rules rules = new Rules(); + Map map = new Map("background"); + rules.NewZoneIsCreated(new Cell(0, 0), null, map); + Assert.Empty(map.Zones); + } + + [Fact] + public void NewZoneIsCreated_DoesNothing_WhenFirstCellValueIsNull() + { + Rules rules = new Rules(); + Map map = new Map("background"); + Cell firstCell = new Cell(0, 0); + rules.NewZoneIsCreated(firstCell, new Cell(0, 1), map); + Assert.Empty(map.Zones); + } + + [Fact] + public void NewZoneIsCreated_DoesNothing_WhenSecondCellValueIsNull() + { + Rules rules = new Rules(); + Map map = new Map("background"); + Cell secondCell = new Cell(0, 0); + rules.NewZoneIsCreated(new Cell(0, 1), secondCell, map); + Assert.Empty(map.Zones); + } + + [Fact] + public void NewZoneIsCreated_CreatesNewZone_WhenBothCellsAreNotNullAndHaveValues() + { + Rules rules = new Rules(); + Map map = new Map("background"); + Cell firstCell = new Cell(0, 0); + firstCell.Value = 1; + Cell secondCell = new Cell(0, 1); + secondCell.Value = 1; + rules.NewZoneIsCreated(firstCell, secondCell, map); + Assert.Single(map.Zones); + Assert.Contains(firstCell, map.Zones[0]); + Assert.Contains(secondCell, map.Zones[0]); + } + + [Fact] + public void EveryAdjacentCells_ReturnsEmptyList_WhenCellIsNull() + { + Rules rules = new Rules(); + List cells = new List { new Cell(0, 0), new Cell(0, 1) }; + Assert.Empty(rules.EveryAdjacentCells(null, cells)); + } + + [Fact] + public void EveryAdjacentCells_ReturnsEmptyList_WhenCellsAreEmpty() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + Assert.Empty(rules.EveryAdjacentCells(cell, new List())); + } + + [Fact] + public void EveryAdjacentCells_ReturnsEmptyList_WhenNoAdjacentCells() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List cells = new List { new Cell(2, 2), new Cell(3, 3) }; + Assert.Empty(rules.EveryAdjacentCells(cell, cells)); + } + + [Fact] + public void EveryAdjacentCells_ReturnsListWithAdjacentCells_WhenAdjacentCellsExist() + { + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + List cells = new List { new Cell(0, 1), new Cell(1, 0), new Cell(2, 2) }; + List result = rules.EveryAdjacentCells(cell, cells); + Assert.Equal(2, result.Count); + Assert.Contains(new Cell(0, 1), result); + Assert.Contains(new Cell(1, 0), result); + } + + [Fact] + public void FinalCalculusOfZones_ReturnsZero_WhenZonesAreEmpty() + { + Rules rules = new Rules(); + Assert.Equal(0, rules.FinalCalculusOfZones(new List>())); + } + + /* + [Fact] + public void FinalCalculusOfZones_ReturnsCorrectValue_WhenZonesAreNotEmpty() + { + // TODO + } + + [Fact] + public void FinalCalculusOfZones_ReturnsCorrectValue_WhenZoneCountIsGreaterThanNine() + { + // TODO + } + */ } \ No newline at end of file From 8f3468c863563f2778766147bed6169ef553228f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Thu, 30 May 2024 23:38:48 +0200 Subject: [PATCH 06/14] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20VS=C3=83V=C3=A9ri?= =?UTF-8?q?fication=20si=20param=C3=A8tres=20null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Rules/Rules.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/Trek-12/Models/Rules/Rules.cs b/source/Trek-12/Models/Rules/Rules.cs index bd8c8cf..6fa645a 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) { From 407ded9e147bf2444f61d3950dd62c26977a26f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Fri, 31 May 2024 08:28:15 +0200 Subject: [PATCH 07/14] =?UTF-8?q?Ajout=20de=20deux=20tests=20sur=20le=20ca?= =?UTF-8?q?lcul=20des=20zones=20+=20r=C3=A9solution=20d'un=20probl=C3=A8me?= =?UTF-8?q?=20sur=20le=20code=20de=20cette=20m=C3=AAme=20fonction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Rules/Rules.cs | 2 +- source/Trek-12/Tests/RulesTests.cs | 55 ++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/source/Trek-12/Models/Rules/Rules.cs b/source/Trek-12/Models/Rules/Rules.cs index 6fa645a..341d1ff 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -232,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/RulesTests.cs b/source/Trek-12/Tests/RulesTests.cs index 630cca2..7cfe05a 100644 --- a/source/Trek-12/Tests/RulesTests.cs +++ b/source/Trek-12/Tests/RulesTests.cs @@ -462,17 +462,64 @@ public class RulesTests Assert.Equal(0, rules.FinalCalculusOfZones(new List>())); } - /* [Fact] public void FinalCalculusOfZones_ReturnsCorrectValue_WhenZonesAreNotEmpty() { - // TODO + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 1; + Cell cell1 = new Cell(0, 1); + cell1.Value = 1; + Cell cell2 = new Cell(0, 2); + cell2.Value = 1; + Cell uncell = new Cell(0, 0); + uncell.Value = 4; + Cell deuxcell = new Cell(0, 1); + deuxcell.Value = 4; + Cell troiscell = new Cell(0, 2); + troiscell.Value = 4; + List> zones = new List> + { + new List { cell, cell1, cell2 }, + new List { uncell, deuxcell, troiscell } + }; + + Assert.Equal(9, rules.FinalCalculusOfZones(zones)); } [Fact] public void FinalCalculusOfZones_ReturnsCorrectValue_WhenZoneCountIsGreaterThanNine() { - // TODO + Rules rules = new Rules(); + Cell cell = new Cell(0, 0); + cell.Value = 5; + Cell cell1 = new Cell(0, 1); + cell1.Value = 5; + Cell cell2 = new Cell(0, 2); + cell2.Value = 5; + Cell cell3 = new Cell(0, 3); + cell3.Value = 5; + Cell cell4 = new Cell(0, 4); + cell4.Value = 5; + Cell cell5 = new Cell(0, 5); + cell5.Value = 5; + Cell cell6 = new Cell(1, 0); + cell6.Value = 5; + Cell cell7 = new Cell(1, 1); + cell7.Value = 5; + Cell cell8 = new Cell(1, 2); + cell8.Value = 5; + Cell cell9 = new Cell(1, 3); + cell9.Value = 5; + Cell cell10 = new Cell(1, 4); + cell10.Value = 5; + + List> zones = new List> + { + new List { cell, cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9, cell10 } + }; + + Assert.Equal(23, rules.FinalCalculusOfZones(zones)); } - */ + } \ No newline at end of file From 45f142988661d9e539c9fc6ad7245ba94cf452e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Fri, 31 May 2024 09:53:17 +0200 Subject: [PATCH 08/14] =?UTF-8?q?Ajout=20d'=C3=A9bauches=20de=20tests=20po?= =?UTF-8?q?ur=20GameTests=20mais=20non=20fonctionnelles=20(check=20pour=20?= =?UTF-8?q?utiliser=20reflection)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Tests/GameTests.cs | 96 +++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 source/Trek-12/Tests/GameTests.cs diff --git a/source/Trek-12/Tests/GameTests.cs b/source/Trek-12/Tests/GameTests.cs new file mode 100644 index 0000000..7f0abee --- /dev/null +++ b/source/Trek-12/Tests/GameTests.cs @@ -0,0 +1,96 @@ +namespace Tests; +using Models.Game; +using System.Reflection; +using static System.Type; +public class GameTests +{ + [Fact] + public void Game_Initialization_SetsPlayer() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + Assert.Equal(player, game.CurrentPlayer); + } + + [Fact] + public void Initialize_Turn_1() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + Game test = new Game(player, map); + + typeof(Game).GetProperty("Turn").SetValue(test, 1); + + + var result = test.GetResult(); + + Assert.Equal(typeof(Game).GetProperty("Turn").GetValue(test.Turn), 1); + } + /* + [Fact] + public void Game_Initialization_SetsMap() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + Assert.Equal(map, game.UsedMap); + } + + [Fact] + public void Game_Initialization_SetsDice() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + Assert.NotNull(game.Dice1); + Assert.NotNull(game.Dice2); + } + + [Fact] + public void Game_Initialization_SetsGameRules() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + Assert.NotNull(game.GameRules); + } + + [Fact] + public void RollAllDice_RollsAllDice() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + game.RollAllDice(); + + Assert.True(game.Dice1.Value >= 0 && game.Dice1.Value <= 5); + Assert.True(game.Dice2.Value >= 1 && game.Dice2.Value <= 6); + } + + [Fact] + public void MarkOperationAsChecked_Check_Well() + { + Player player = new Player("test_player"); + Map map = new Map("test_background"); + + var game = new Game(player, map); + + game.MarkOperationAsChecked(Operation.LOWER); + + Assert.True(game.UsedMap.OperationGrid[0].IsChecked); + } + + */ +} From 1c4125aadb58f25aa7debd4e494f1235a027c535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Sun, 2 Jun 2024 14:10:43 +0200 Subject: [PATCH 09/14] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Ajout=20du=20paqu?= =?UTF-8?q?et=20NuGet=20pour=20les=20Mocks=20dans=20les=20tests=20unitaire?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Tests/Tests.csproj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/Trek-12/Tests/Tests.csproj b/source/Trek-12/Tests/Tests.csproj index 2fea905..4ea534a 100644 --- a/source/Trek-12/Tests/Tests.csproj +++ b/source/Trek-12/Tests/Tests.csproj @@ -10,8 +10,9 @@ - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all From 170f002e14aea2c3ca2a33ecb896e4058d7fbd37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Sun, 2 Jun 2024 14:27:41 +0200 Subject: [PATCH 10/14] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Ajout=20de=20Mock=20?= =?UTF-8?q?pour=20imiter=20l'interface=20de=20persistance=20et=20d=C3=A9bu?= =?UTF-8?q?t=20des=20tests=20de=20Game?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Tests/GameTests.cs | 72 +++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 13 deletions(-) diff --git a/source/Trek-12/Tests/GameTests.cs b/source/Trek-12/Tests/GameTests.cs index 7f0abee..a0d2080 100644 --- a/source/Trek-12/Tests/GameTests.cs +++ b/source/Trek-12/Tests/GameTests.cs @@ -1,35 +1,81 @@ -namespace Tests; +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); + } + [Fact] - public void Game_Initialization_SetsPlayer() + public void AddPlayer_ShouldAddPlayerToList() { - Player player = new Player("test_player"); - Map map = new Map("test_background"); + var player = new Player(); - var game = new Game(player, map); + _game.AddPlayer(player); - Assert.Equal(player, game.CurrentPlayer); + Assert.Contains(player, _game.Players); } [Fact] - public void Initialize_Turn_1() + public void AddGame_ShouldAddGameToList() { - Player player = new Player("test_player"); - Map map = new Map("test_background"); + var game = new Game(new Player(), new Map("test_background")); + + _game.AddGame(game); + + Assert.Contains(game, _game.Games); + } - Game test = new Game(player, map); + [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); - typeof(Game).GetProperty("Turn").SetValue(test, 1); + _game.AddBestScore(bestScore); + Assert.Contains(bestScore, _game.BestScores); + } - var result = test.GetResult(); + [Fact] + public void LoadData_ShouldLoadDataFromPersistence() + { + var players = new List { new Player() }; + var games = new List { new Game(new Player(), new Map("test_background")) }; + var maps = new List { new Map("test_background") }; + var bestScores = new List { new BestScore(1, 26) }; - Assert.Equal(typeof(Game).GetProperty("Turn").GetValue(test.Turn), 1); + _mockPersistence.Setup(p => p.LoadData()).Returns((players, games, maps, bestScores)); + + _game.LoadData(); + + Assert.Equal(players, _game.Players); + Assert.Equal(games, _game.Games); + Assert.Equal(maps, _game.Maps); + Assert.Equal(bestScores, _game.BestScores); } + + /* [Fact] public void Game_Initialization_SetsMap() From 756764f8ce8cecc2c75537d8a86854f86b9224cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Sun, 2 Jun 2024 16:44:17 +0200 Subject: [PATCH 11/14] =?UTF-8?q?=F0=9F=93=9D=20Traduction=20de=20propri?= =?UTF-8?q?=C3=A9t=C3=A9s=20pour=20les=20d=C3=A9s=20et=20traduction=20des?= =?UTF-8?q?=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Game/Dice.cs | 28 ++++++++++++++-------------- source/Trek-12/Tests/DiceTests.cs | 14 +++++++------- 2 files changed, 21 insertions(+), 21 deletions(-) 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/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); } } From 6bbf5c147aeaf49d59d33b872e8dab26d29fba3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Sun, 2 Jun 2024 16:47:51 +0200 Subject: [PATCH 12/14] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20Ajout=20des=20tests?= =?UTF-8?q?=20restants=20pour=20Game.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Tests/GameTests.cs | 170 ++++++++++++++++++++++++------ 1 file changed, 137 insertions(+), 33 deletions(-) diff --git a/source/Trek-12/Tests/GameTests.cs b/source/Trek-12/Tests/GameTests.cs index a0d2080..066aeff 100644 --- a/source/Trek-12/Tests/GameTests.cs +++ b/source/Trek-12/Tests/GameTests.cs @@ -17,6 +17,22 @@ public class GameTests _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() { @@ -30,7 +46,7 @@ public class GameTests [Fact] public void AddGame_ShouldAddGameToList() { - var game = new Game(new Player(), new Map("test_background")); + var game = new Game(_mockPersistence.Object); _game.AddGame(game); @@ -61,7 +77,7 @@ public class GameTests public void LoadData_ShouldLoadDataFromPersistence() { var players = new List { new Player() }; - var games = new List { new Game(new Player(), new Map("test_background")) }; + var games = new List { new Game(_mockPersistence.Object) }; var maps = new List { new Map("test_background") }; var bestScores = new List { new BestScore(1, 26) }; @@ -75,68 +91,156 @@ public class GameTests Assert.Equal(bestScores, _game.BestScores); } - - /* [Fact] - public void Game_Initialization_SetsMap() + public void SaveData_ShouldCallPersistenceSaveData() { - Player player = new Player("test_player"); - Map map = new Map("test_background"); + _game.SaveData(); - var game = new Game(player, map); + _mockPersistence.Verify(p => p.SaveData(_game.Players, _game.Games, _game.Maps, _game.BestScores), Times.Once); // Times.Once is to verify if the method is called exactly one time + } - Assert.Equal(map, game.UsedMap); + [Fact] + public void InitializeGame_ShouldInitializeGameAndNotTriggerEventWhenNotStarted() + { + var player = new Player(); + var map = new Map("test_background"); + bool eventTriggered = false; + + _game.GameStarted += (sender, args) => + { + eventTriggered = true; + }; + + _game.InitializeGame(map, player, false); + Assert.False(eventTriggered); + Assert.False(_game.IsRunning); + Assert.Equal(map, _game.UsedMap); + Assert.Equal(player, _game.CurrentPlayer); } [Fact] - public void Game_Initialization_SetsDice() + public void InitializeGame_ShouldInitializeGameAndTriggerEventWhenStarted() { - Player player = new Player("test_player"); - Map map = new Map("test_background"); + var player = new Player(); + var map = new Map("test_background"); + bool eventTriggered = false; + + _game.GameEnded += (sender, args) => + { + eventTriggered = true; + }; + + _game.InitializeGame(map, player, true); + Assert.True(eventTriggered); + Assert.False(_game.IsRunning); + Assert.Equal(map, _game.UsedMap); + Assert.Equal(player, _game.CurrentPlayer); + } + + [Theory] + [InlineData(Operation.ADDITION, 3, 4, 7)] + [InlineData(Operation.SUBTRACTION, 6, 4, 2)] + [InlineData(Operation.MULTIPLICATION, 2, 3, 6)] + [InlineData(Operation.LOWER, 1, 4, 1)] + [InlineData(Operation.HIGHER, 2, 5, 5)] + public void HandlePlayerOperation_ShouldPerformCorrectOperationAndTriggerEvent(Operation operation, int value1, int value2, int expectedResult) + { + var player = new Player(); + var map = new Map("background"); + _game.InitializeGame(map, player, false); + + bool eventTriggered = false; + int actualResult = 0; + + _game.OperationChosen += (sender, args) => + { + eventTriggered = true; + actualResult = args.Result; + }; - var game = new Game(player, map); + SetDiceValues(_game, value1, value2); - Assert.NotNull(game.Dice1); - Assert.NotNull(game.Dice2); + + _game.HandlePlayerOperation(operation); + + + Assert.True(eventTriggered); + Assert.Equal(expectedResult, actualResult); } [Fact] - public void Game_Initialization_SetsGameRules() + public void Game_Initialization_SetsMap() { - Player player = new Player("test_player"); - Map map = new Map("test_background"); + var player = new Player("test_player"); + var map = new Map("background"); - var game = new Game(player, map); + _game.InitializeGame(map, player); - Assert.NotNull(game.GameRules); + Assert.Equal(map, _game.UsedMap); + } + + [Fact] + public void Game_Initialization_SetsPlayer() + { + var player = new Player("test_player"); + var map = new Map("background"); + + _game.InitializeGame(map, player); + + Assert.Equal(player, _game.CurrentPlayer); } [Fact] - public void RollAllDice_RollsAllDice() + public void Game_Initialization_SetsDice() { - Player player = new Player("test_player"); + Player player = new Player(); Map map = new Map("test_background"); - var game = new Game(player, map); + _game.InitializeGame(map, player); - game.RollAllDice(); + Assert.NotNull(_game.Dice1); + Assert.NotNull(_game.Dice2); + } - Assert.True(game.Dice1.Value >= 0 && game.Dice1.Value <= 5); - Assert.True(game.Dice2.Value >= 1 && game.Dice2.Value <= 6); + [Fact] + public void Game_Initialization_SetsGameRules() + { + var game = new Game(_mockPersistence.Object); + + Assert.NotNull(game.GameRules); } [Fact] public void MarkOperationAsChecked_Check_Well() { - Player player = new Player("test_player"); - Map map = new Map("test_background"); + var player = new Player(); + var map = new Map("test_background"); - var game = new Game(player, map); + _game.InitializeGame(map, player); - game.MarkOperationAsChecked(Operation.LOWER); + // Use of reflection to call private method + var methodInfo = typeof(Game).GetMethod("MarkOperationAsChecked", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(methodInfo); + + var operation = Operation.ADDITION; - Assert.True(game.UsedMap.OperationGrid[0].IsChecked); - } - */ + methodInfo.Invoke(_game, new object[] { operation }); + + + int operationIndex = (int)operation; + int operationsPerType = 4; + bool isChecked = false; + + for (int i = operationIndex * operationsPerType; i < (operationIndex + 1) * operationsPerType; i++) + { + if (_game.UsedMap.OperationGrid[i].IsChecked) + { + isChecked = true; + break; + } + } + + Assert.True(isChecked); + } } From 4a3ab90bdcab5b984f2afb9f25df20f7009c84b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Sun, 2 Jun 2024 16:48:43 +0200 Subject: [PATCH 13/14] =?UTF-8?q?=F0=9F=9A=A7=20Suppression=20d'un=20const?= =?UTF-8?q?ructeur=20et=20d=C3=A9veloppement=20de=20la=20m=C3=A9thode=20d'?= =?UTF-8?q?initialisation=20=C3=A0=20la=20place?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Game/Game.cs | 61 +++++++++++++++++------------- 1 file changed, 35 insertions(+), 26 deletions(-) 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(); BestScores = new List(); - - _isRunning = false; - } - - - /// - /// Initializes a new instance of the class. - /// - /// The player who play the game. - /// The map to be used in the game. - public Game(Player player, Map map) - { - _isRunning = false; - UsedMap = map; - CurrentPlayer = player; - Dice1 = new Dice(); - Dice2 = new Dice(1); - Turn = 1; + GameRules = new Rules.Rules(); + + IsRunning = false; } /// @@ -240,13 +232,30 @@ namespace Models.Game } } } - + /// /// Initializes the game. /// - public void InitializeGame() + public void InitializeGame(Map map, Player player, bool startImmediately = true) + { + UsedMap = map; + CurrentPlayer = player; + Turn = 1; + Dice1 = new Dice(); + Dice2 = new Dice(1); + + if (startImmediately) + { + StartGame(); + } + } + + /// + /// Starts the game. + /// + private void StartGame() { - _isRunning = true; + IsRunning = true; GameStarted?.Invoke(this, new GameStartedEventArgs(CurrentPlayer)); GameLoop(); } @@ -256,7 +265,7 @@ namespace Models.Game /// private void EndGame(int? pts) { - _isRunning = false; + IsRunning = false; GameEnded?.Invoke(this, new GameEndedEventArgs(CurrentPlayer, pts)); } @@ -265,7 +274,7 @@ namespace Models.Game /// private void GameLoop() { - while (_isRunning) + while (IsRunning) { if (Turn == 20) { From b4aaff6ddee73b222350409e96a86d1c1739d3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Sun, 2 Jun 2024 16:53:41 +0200 Subject: [PATCH 14/14] =?UTF-8?q?=F0=9F=94=97=20Ajout=20des=20directives?= =?UTF-8?q?=20d'assembly=20et=20des=20changements=20de=20Game=20pour=20cor?= =?UTF-8?q?riger=20les=20erreurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/ConsoleApp/ConsoleApp.csproj | 1 + source/Trek-12/ConsoleApp/Program.cs | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) 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); } }