diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index ad12627..cab223f 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -384,6 +384,12 @@ namespace QwirkleClassLibrary.Games /// bool public bool SwapTiles(Player player, List tilesToSwap) { + if (cellUsed.Count != 0) + { + OnSwapTiles(new SwapTilesNotifiedEventArgs("You can't swap tiles after placing some !")); + return false; + } + if (tilesToSwap.Count == 0) { OnSwapTiles(new SwapTilesNotifiedEventArgs("You must select at least one tile to swap !")); diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs index 8325cc2..564134a 100644 --- a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs +++ b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceXml.cs @@ -20,9 +20,17 @@ public class GamePersistenceXml : IGamePersistence { var serializer = new DataContractSerializer(typeof(Game)); - using (Stream reader = File.OpenRead("Game.xml")) + try { - return serializer.ReadObject(reader) as Game ?? throw new InvalidOperationException(); + using (Stream reader = File.OpenRead("Game.xml")) + { + var newGame = serializer.ReadObject(reader) as Game; + return newGame!; + } + } + catch + { + return new Game(); } } } \ No newline at end of file diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index a6224bb..9961774 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -1,17 +1,9 @@ using QwirkleClassLibrary.Boards; -using QwirkleClassLibrary.Events; using QwirkleClassLibrary.Games; using QwirkleClassLibrary.Players; using QwirkleClassLibrary.Tiles; using QwirkleConsoleApp; -using System.Collections.Immutable; -using System.Collections.ObjectModel; -using System.Diagnostics.Metrics; -using System.Net.Quic; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Json; using System.Text; -using System.Transactions; using QwirkleClassLibrary.Persistences; using static System.Console; @@ -423,12 +415,33 @@ static void MainGame() Game loadedGame; IGamePersistence gameLoad = new GamePersistenceXml(); - loadedGame = gameLoad.LoadGame(); + + try + { + loadedGame = gameLoad.LoadGame(); + } + catch + { + ForegroundColor = ConsoleColor.Red; + WriteLine("ERROR : No game saved ! Creating a new game !"); + ResetColor(); + loadedGame = new Game(); + } ILeaderboardPersistence leaderboardLoad = new LeaderboardPersistenceJson(); leaderboard = leaderboardLoad.LoadLeaderboard(); - - MainMenuContinue(loadedGame); + + if (loadedGame.GameRunning) + { + MainMenuContinue(loadedGame); + } + else + { + AddPlayers(loadedGame); + loadedGame.StartGame(); + MainMenu(loadedGame); + } + leaderboard.AddScoreInLead(loadedGame.ScoreBoard); break;