diff --git a/source/Trek-12/Models/Game/Game.cs b/source/Trek-12/Models/Game/Game.cs index 290e579..3f4335e 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -459,5 +459,41 @@ namespace Models.Game { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } + + public int CalculusOfPenalty(ReadOnlyCollection Boards) + { + int result = 0; + foreach (var cells in Boards) + if (cells.Penalty) + { + if (cells.Valid == false || cells.Value == null) + continue; + result += 3; + } + return result; + } + + public void PutPenaltyForLostCells(ReadOnlyCollection Boards) + { + foreach (var cells in Boards) + { + if (cells == null || cells.Value == null || cells.Valid == false) + continue; + if (!UsedMap.IsCellInZones(cells) || !UsedMap.IsCellInRopePath(cells)) + cells.SetPenalty(); + } + } + + public int FinalCalculusOfPoints() + { + int? points = GameRules.FinalCalculusOfZones(UsedMap.Zones); + for (int i = 0; i < UsedMap.RopePaths.Count; i++) + { + points += GameRules.ScoreRopePaths(UsedMap.RopePaths[i]); + } + points += CalculusOfPenalty(UsedMap.Boards); + return points ?? 0; + } + } } diff --git a/source/Trek-12/Models/Game/Map.cs b/source/Trek-12/Models/Game/Map.cs index 6acd93a..8b034b6 100644 --- a/source/Trek-12/Models/Game/Map.cs +++ b/source/Trek-12/Models/Game/Map.cs @@ -118,5 +118,29 @@ namespace Models.Game { return OperationGrid[x * 4 + 3].IsChecked; } + + public bool IsCellInZones(Cell cell) + { + foreach (var zone in Zones) + { + if (zone.Contains(cell)) + { + return true; + } + } + return false; + } + + public bool IsCellInRopePath(Cell cell) + { + foreach (var path in RopePaths) + { + if (path.Contains(cell)) + { + return true; + } + } + return false; + } } } \ No newline at end of file diff --git a/source/Trek-12/Tests/GameTests.cs b/source/Trek-12/Tests/GameTests.cs index 980531a..96d1bdf 100644 --- a/source/Trek-12/Tests/GameTests.cs +++ b/source/Trek-12/Tests/GameTests.cs @@ -397,4 +397,110 @@ public class GameTests Assert.True(_game.UsedMap.Boards[2].Penalty); } + [Fact] + public void PutPenaltyForLostCells_ReallyPutPenalty_ForZones() + { + var player = new Player("test_player", "DefaultProfilePicture"); + var map = new Map("test_name", "test_background.png"); + _game.InitializeGame(map, player); + + _game.UsedMap.Boards[1].Value = 5; + _game.UsedMap.Boards[2].Value = 5; + _game.UsedMap.Boards[3].Value = 5; + _game.UsedMap.Boards[0].Value = 0; + + foreach (var cells in _game.UsedMap.Boards.ToList()) + { + _game.GameRules.IsZoneValidAndAddToZones(cells, _game.UsedMap); + } + + _game.PutPenaltyForLostCells(_game.UsedMap.Boards); + + Assert.True(_game.UsedMap.Boards[0].Penalty); + + } + + [Fact] + public void PutPenaltyForLostCells_ReallyPutPenalty_ForRopePathes() + { + var player = new Player("test_player", "DefaultProfilePicture"); + var map = new Map("test_name", "test_background.png"); + _game.InitializeGame(map, player); + + _game.UsedMap.Boards[1].Value = 1; + _game.UsedMap.Boards[2].Value = 2; + _game.UsedMap.Boards[3].Value = 5; + _game.UsedMap.Boards[0].Value = 0; + + var methodInfo = typeof(Game).GetMethod("AddToRopePath", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(methodInfo); + + foreach (var cells in _game.UsedMap.Boards.ToList()) + { + methodInfo.Invoke(_game, new object[] { cells, _game.UsedMap.Boards.ToList() }); + } + + _game.PutPenaltyForLostCells(_game.UsedMap.Boards); + + Assert.True(_game.UsedMap.Boards[3].Penalty); + + } + + [Fact] + public void CalculusOfPenalty_ReallyCalculusPenalty() + { + var player = new Player("test_player", "DefaultProfilePicture"); + var map = new Map("test_name", "test_background.png"); + _game.InitializeGame(map, player); + + _game.UsedMap.Boards[0].Valid = true; + _game.UsedMap.Boards[1].Valid = true; + _game.UsedMap.Boards[2].Valid = true; + _game.UsedMap.Boards[7].Valid = true; + _game.UsedMap.Boards[8].Valid = true; + _game.UsedMap.Boards[9].Valid = true; + _game.UsedMap.Boards[10].Valid = true; + _game.UsedMap.Boards[11].Valid = true; + _game.UsedMap.Boards[12].Valid = true; + _game.UsedMap.Boards[13].Valid = true; + + + _game.UsedMap.Boards[0].Value = 0; + _game.UsedMap.Boards[1].Value = 1; + _game.UsedMap.Boards[2].Value = 2; + _game.UsedMap.Boards[8].Value = 5; + _game.UsedMap.Boards[7].Value = 5; + _game.UsedMap.Boards[9].Value = 5; + _game.UsedMap.Boards[10].Value = 6; + _game.UsedMap.Boards[11].IsDangerous = true; + _game.UsedMap.Boards[12].Value = 13; //One penalty because it is over 12 + _game.UsedMap.Boards[13].Value = 9; //One penalty because it is lost + + var place = typeof(Game).GetMethod("PlaceResult", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(place); + + var cell = new Cell(1, 4); + cell.Value = 7; + cell.Valid = true; + cell.IsDangerous = true; + place.Invoke(_game, new object[] { cell, 7 }); //One penalty + + var methodInfo = typeof(Game).GetMethod("AddToRopePath", BindingFlags.NonPublic | BindingFlags.Instance); + Assert.NotNull(methodInfo); + + foreach (var cells in _game.UsedMap.Boards.ToList()) + { + _game.GameRules.IsZoneValidAndAddToZones(cells, _game.UsedMap); + methodInfo.Invoke(_game, new object[] { cells, _game.GameRules.EveryAdjacentCells(cells, _game.UsedMap.Boards.ToList()) }); + } + + _game.PutPenaltyForLostCells(_game.UsedMap.Boards); + + Assert.True(_game.UsedMap.Boards[10].Penalty); + Assert.True(_game.UsedMap.Boards[11].Penalty); + Assert.True(_game.UsedMap.Boards[12].Penalty); + + Assert.Equal(9, _game.CalculusOfPenalty(_game.UsedMap.Boards)); + + } } diff --git a/source/Trek-12/Tests/MapTests.cs b/source/Trek-12/Tests/MapTests.cs index cc6c98c..4915c11 100644 --- a/source/Trek-12/Tests/MapTests.cs +++ b/source/Trek-12/Tests/MapTests.cs @@ -10,9 +10,9 @@ public class MapTests { string name = "test_name"; string background = "test_background"; - - var map = new Map(name,background); - + + var map = new Map(name, background); + Assert.Equal(background, map.Background); } @@ -44,4 +44,54 @@ public class MapTests Assert.Empty(map.RopePaths); Assert.Empty(map.Zones); } + + [Fact] + public void IsCellInZones_FoundTheCell() + { + var map = new Map("test_name", "test_background.png"); + + var cell = new Cell(0, 0); + var zone = new List { cell }; + map.Zones.Add(zone); + Assert.True(map.IsCellInZones(cell)); + + } + + [Fact] + public void IsCellInZones_DoNotFoundTheCell() + { + var map = new Map("test_name", "test_background.png"); + + var cell = new Cell(0, 0); + var othercell = new Cell(1, 1); + var zone = new List { cell }; + map.Zones.Add(zone); + Assert.False(map.IsCellInZones(othercell)); + + } + + [Fact] + public void IsCellInRopePath_FoundTheCell() + { + var map = new Map("test_name", "test_background.png"); + + var cell = new Cell(0, 0); + var paths = new List { cell }; + map.RopePaths.Add(paths); + Assert.True(map.IsCellInRopePath(cell)); + + } + + [Fact] + public void IsCellInRopePath_DoNotFoundTheCell() + { + var map = new Map("test_name", "test_background.png"); + + var cell = new Cell(0, 0); + var othercell = new Cell(1, 1); + var paths = new List { cell }; + map.RopePaths.Add(paths); + Assert.False(map.IsCellInRopePath(othercell)); + + } }