diff --git a/source/Trek-12/ConsoleApp/Program.cs b/source/Trek-12/ConsoleApp/Program.cs index 38d2c35..13ca4b4 100644 --- a/source/Trek-12/ConsoleApp/Program.cs +++ b/source/Trek-12/ConsoleApp/Program.cs @@ -1,4 +1,4 @@ -// See https://aka.ms/new-console-template for more information +// See https://aka.ms/new-console-template for more information using Models; using Models.Events; @@ -45,6 +45,7 @@ class Program // Initialisation Game.InitializeGame(map, player); + Game.GameLoop(); } diff --git a/source/Trek-12/Models/Game/Game.cs b/source/Trek-12/Models/Game/Game.cs index be48c4b..e0e76c3 100644 --- a/source/Trek-12/Models/Game/Game.cs +++ b/source/Trek-12/Models/Game/Game.cs @@ -23,6 +23,8 @@ namespace Models.Game [DataContract] public class Game : INotifyPropertyChanged { + public bool IsPreviousGameNotFinished { get; private set; } + /* Persistence Interface */ public IPersistence PersistenceManager { get; set; } @@ -104,6 +106,7 @@ namespace Models.Game public Operation PlayerOperation { get; set; } public Cell PlayerCell { get; set; } + public int Resultat { get; set; } public Rules.Rules GameRules { get; } @@ -134,23 +137,57 @@ namespace Models.Game { Maps.Add(map); } + + /// + /// Deletes the last game in the list of games. Use for avoiding stack not finished games. + /// + /// + public bool DeleteGame() + { + if (Games.Count == 0) + { + return false; + } + Games.RemoveAt(Games.Count - 1); // Remove the last game + return true; + } + /// + /// Adds a new best score to the list of best scores. Or updates it if it already exists. + /// + /// The final score of the game. public void AddBestScore(int finalScore) { BestScore bs = new BestScore(UsedMap.Name, CurrentPlayer, 1, finalScore); - foreach (var score in BestScores) - { - if (!bs.Equals(score)) continue; + var existingScore = BestScores.FirstOrDefault(score => score.Equals(bs)); - score.IncrGamesPlayed(); - score.UpdateScore(finalScore); - return; + if (existingScore != null) + { + existingScore.IncrGamesPlayed(); + existingScore.UpdateScore(finalScore); + } + else + { + BestScores.Add(bs); } - BestScores.Add(bs); - BestScores.OrderByDescending(p => p.Score); + // Sorting the best scores + List sortedScores = BestScores.OrderByDescending(score => score.Score).ToList(); + + for (int i = 0; i < sortedScores.Count; i++) + { + if (!BestScores[i].Equals(sortedScores[i])) + { + BestScores.Move(BestScores.IndexOf(sortedScores[i]), i); + } + } } + /// + /// Removes a player from the list of players. + /// + /// + /// True if the player was removed successfully, false otherwise. public bool RemovePlayer(string playerName) { Player player = Players.FirstOrDefault(p => p.Pseudo == playerName); @@ -163,6 +200,12 @@ namespace Models.Game return true; } + /// + /// Modifies the pseudo of a player. + /// + /// + /// + /// public bool ModifyPlayer(string pseudo, string newpseudo) { foreach (var index in Players) @@ -178,6 +221,10 @@ namespace Models.Game return false; } + /// + /// Removes a game from the list of games. + /// + /// public void CheckAndRemoveBestScoresDependencies(string playerName) { List bs = new List(); @@ -193,7 +240,12 @@ namespace Models.Game BestScores.Remove(score); } } - + + /// + /// Modifies the pseudo of a player in the best scores. + /// + /// + /// public void CheckAndChangeBestScoresDependencies(string playerName, string newPlayerName) { foreach (var bestScore in BestScores) @@ -213,6 +265,10 @@ namespace Models.Game } foreach (var game in data.Item2) { + if (game.IsRunning) + { + IsPreviousGameNotFinished = true; + } Games.Add(game); } foreach (var map in data.Item3) @@ -317,7 +373,7 @@ namespace Models.Game /// /// The cell chosen by the player to place the result. /// The result of the dice operation to be placed in the cell. - private void PlaceResult(Cell playerChoice) + private void PlaceResult(Cell playerChoice, int result) { IEnumerable ValidCell = from cell in UsedMap.Boards @@ -328,14 +384,16 @@ namespace Models.Game { if (item.X == playerChoice.X && item.Y == playerChoice.Y) { - if (PlayerCell.Value > 12 || (PlayerCell.Value > 6 && item.IsDangerous == true)) + if (result > 12 || (result > 6 && item.IsDangerous == true)) { item.SetPenalty(); PlayerCell.SetPenalty(); } - item.Value = PlayerCell.Value; + item.Value = result; return; } + + } } @@ -398,7 +456,6 @@ namespace Models.Game { IsRunning = true; GameStarted?.Invoke(this, new GameStartedEventArgs(CurrentPlayer)); - //GameLoop(); } /// @@ -413,17 +470,13 @@ namespace Models.Game /// /// The main game loop that runs while the game is active. /// - private void GameLoop() + public void GameLoop() { while (IsRunning) { RollAllDice(); - int res = PlayerChooseOperation(); - PlayerOption?.Invoke(this,new PlayerOptionEventArgs(UsedMap.Boards.ToList(),res,Turn)); + Resultat = PlayerChooseOperation(); PlayerSelectionCell(); - PlayerCell.Value = res; - AddToRopePath(PlayerCell,UsedMap.Boards.ToList()); - PlaceResult(PlayerCell); BoardUpdated?.Invoke(this, new BoardsUpdateEventArgs(UsedMap.Boards.ToList())); Turn++; } @@ -436,6 +489,7 @@ namespace Models.Game { PlayerChooseOp?.Invoke(this, new PlayerChooseOperationEventArgs(PlayerOperation)); } + PlayerOption?.Invoke(this, new PlayerOptionEventArgs(UsedMap.Boards.ToList(), ResultOperation(PlayerOperation), Turn)); return ResultOperation(PlayerOperation); } @@ -447,6 +501,7 @@ namespace Models.Game PlayerChooseCell?.Invoke(this, new PlayerChooseCellEventArgs(PlayerCell)); } MarkOperationAsChecked(PlayerOperation); + PlaceResult(PlayerCell, Resultat); } /// diff --git a/source/Trek-12/Models/Rules/Rules.cs b/source/Trek-12/Models/Rules/Rules.cs index 42b28e7..6852e74 100644 --- a/source/Trek-12/Models/Rules/Rules.cs +++ b/source/Trek-12/Models/Rules/Rules.cs @@ -25,6 +25,8 @@ namespace Models.Rules { if (!IsCellEmpty(playerChoicePosition)) return false; + if (playerChoicePosition.Valid == false) return false; + if (EveryAdjacentCells(playerChoicePosition, cells).Count == 1) return false; return true; diff --git a/source/Trek-12/Tests/GameTests.cs b/source/Trek-12/Tests/GameTests.cs index 589031e..242f80b 100644 --- a/source/Trek-12/Tests/GameTests.cs +++ b/source/Trek-12/Tests/GameTests.cs @@ -260,7 +260,7 @@ public class GameTests _game.PlayerCell = new Cell(2, 0); _game.PlayerCell.Value = 3; - methodInfo.Invoke(_game, new object[] { _game.PlayerCell }); + methodInfo.Invoke(_game, new object[] { _game.PlayerCell, 3 }); //_game.UsedMap.Boards[2].Value = _game.PlayerCell.Value; Assert.Equal(3, _game.UsedMap.Boards[2].Value); @@ -274,8 +274,11 @@ public class GameTests _game.InitializeGame(map, player, false); - var cell = new Cell(0, 1); + var cell = new Cell(1, 0); + cell.Valid = true; + _game.UsedMap.Boards[0].Valid = true; _game.UsedMap.Boards[0].Value = 1; + _game.UsedMap.Boards[1].Valid = true; bool result = _game.HandlePlayerChoice(cell, 1); Assert.True(result); } @@ -342,7 +345,7 @@ public class GameTests _game.PlayerCell = new Cell(2, 0); _game.PlayerCell.Value = 14; - methodInfo.Invoke(_game, new object[] { _game.PlayerCell }); + methodInfo.Invoke(_game, new object[] { _game.PlayerCell, 14 }); Assert.True(_game.UsedMap.Boards[2].Penalty); } @@ -365,7 +368,7 @@ public class GameTests _game.PlayerCell = new Cell(2, 0); _game.PlayerCell.Value = 7; - methodInfo.Invoke(_game, new object[] { _game.PlayerCell }); + methodInfo.Invoke(_game, new object[] { _game.PlayerCell, 7 }); Assert.True(_game.UsedMap.Boards[2].Penalty); } @@ -424,39 +427,6 @@ public class GameTests } - [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, false); - var methodInfo = typeof(Game).GetMethod("AddToRopePath", BindingFlags.NonPublic | BindingFlags.Instance); - Assert.NotNull(methodInfo); - - _game.UsedMap.Boards[0].Valid = true; - _game.UsedMap.Boards[1].Valid = true; - _game.UsedMap.Boards[2].Valid = true; - _game.UsedMap.Boards[3].Valid = true; - - - _game.UsedMap.Boards[0].Value = 0; - //methodInfo.Invoke(_game, new object[] { _game.UsedMap.Boards[0], _game.UsedMap.Boards.ToList() }); - _game.UsedMap.Boards[1].Value = 1; - //methodInfo.Invoke(_game, new object[] { _game.UsedMap.Boards[1], _game.UsedMap.Boards.ToList() }); - _game.UsedMap.Boards[2].Value = 2; - //methodInfo.Invoke(_game, new object[] { _game.UsedMap.Boards[2], _game.UsedMap.Boards.ToList() }); - _game.UsedMap.Boards[3].Value = 5; - - methodInfo.Invoke(_game, new object[] { _game.UsedMap.Boards[2], _game.UsedMap.Boards.ToList() }); - - _game.PutPenaltyForLostCells(_game.UsedMap.Boards); - - Assert.True(_game.UsedMap.Boards[3].Penalty); - - Assert.Equal(3, _game.CalculusOfPenalty(_game.UsedMap.Boards)); - - } - [Fact] public void CalculusOfPenalty_ReallyCalculusPenalty_ForZonesAndDangerousCellsAndOverTwelve() { @@ -483,12 +453,12 @@ public class GameTests _game.PlayerCell.Value = 7; _game.PlayerCell.Valid = true; _game.PlayerCell.IsDangerous = true; - place.Invoke(_game, new object[] { _game.PlayerCell }); //One penalty + place.Invoke(_game, new object[] { _game.PlayerCell, 7 }); //One penalty _game.PlayerCell = new Cell(5, 1); _game.PlayerCell.Value = 14; _game.PlayerCell.Valid = true; - place.Invoke(_game, new object[] { _game.PlayerCell }); + place.Invoke(_game, new object[] { _game.PlayerCell, 14 }); foreach (var cells in _game.UsedMap.Boards.ToList()) diff --git a/source/Trek-12/Tests/RulesTests.cs b/source/Trek-12/Tests/RulesTests.cs index b493df7..ee82959 100644 --- a/source/Trek-12/Tests/RulesTests.cs +++ b/source/Trek-12/Tests/RulesTests.cs @@ -44,6 +44,7 @@ public class RulesTests { Rules rules = new Rules(); Map map = new Map("test", "background"); + map.Boards[0].Valid = true; Cell selectedCell = map.Boards[0]; Assert.True(rules.IsCellValid(selectedCell, map.Boards.ToList())); } diff --git a/source/Trek-12/Trek-12.sln b/source/Trek-12/Trek-12.sln index 4ce1243..2c7ceca 100644 --- a/source/Trek-12/Trek-12.sln +++ b/source/Trek-12/Trek-12.sln @@ -5,15 +5,15 @@ VisualStudioVersion = 17.8.34408.163 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Trek-12", "Trek-12\Trek-12.csproj", "{41EE7BF8-DDE6-4B00-9434-076589C0B419}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Models", "Models\Models.csproj", "{807AB723-7AD3-42DD-9DA6-7AA5B0A9AAB4}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Models", "Models\Models.csproj", "{807AB723-7AD3-42DD-9DA6-7AA5B0A9AAB4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{795F2C88-3C43-4795-9764-E52F7330888D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{795F2C88-3C43-4795-9764-E52F7330888D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{383C4215-C680-4C2E-BC7E-B62F0B164370}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tests", "Tests\Tests.csproj", "{383C4215-C680-4C2E-BC7E-B62F0B164370}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataContractPersistence", "DataContractPersistence\DataContractPersistence.csproj", "{FC6A23C3-A1E3-4BF4-85B0-404D8574E190}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DataContractPersistence", "DataContractPersistence\DataContractPersistence.csproj", "{FC6A23C3-A1E3-4BF4-85B0-404D8574E190}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stub", "Stub\Stub.csproj", "{49360F7D-C59D-4B4F-AF5A-73FF61D9EF9B}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Stub", "Stub\Stub.csproj", "{49360F7D-C59D-4B4F-AF5A-73FF61D9EF9B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/source/Trek-12/Trek-12/App.xaml.cs b/source/Trek-12/Trek-12/App.xaml.cs index f9a7703..e4167f7 100644 --- a/source/Trek-12/Trek-12/App.xaml.cs +++ b/source/Trek-12/Trek-12/App.xaml.cs @@ -29,7 +29,7 @@ namespace Trek_12 Directory.CreateDirectory(FilePath); } - File.Delete(Path.Combine(FilePath, FileName)); + //File.Delete(Path.Combine(FilePath, FileName)); string fullPath = Path.Combine(FilePath, FileName); if (File.Exists(fullPath)) diff --git a/source/Trek-12/Trek-12/Trek-12.csproj b/source/Trek-12/Trek-12/Trek-12.csproj index 4d2aac5..75e56ef 100644 --- a/source/Trek-12/Trek-12/Trek-12.csproj +++ b/source/Trek-12/Trek-12/Trek-12.csproj @@ -2,7 +2,7 @@ net8.0-android;net8.0-ios;net8.0-maccatalyst - $(TargetFrameworks);net8.0-windows10.0.19041.0 + @@ -38,6 +38,14 @@ 6.5 + + $(TargetFrameworks);net8.0-windows10.0.19041.0 + MSIX + 404032fa5d4dc4c8bbf036505d2409963f355ebd + + + + diff --git a/source/Trek-12/Trek-12/Views/PageBoard.xaml b/source/Trek-12/Trek-12/Views/PageBoard.xaml index 397b0eb..d9aef40 100644 --- a/source/Trek-12/Trek-12/Views/PageBoard.xaml +++ b/source/Trek-12/Trek-12/Views/PageBoard.xaml @@ -6,52 +6,70 @@ BackgroundColor="Bisque"> - - + + -