diff --git a/Qwirkle/Files/LeaderBoard.json b/Qwirkle/Files/LeaderBoard.json index 232acb4..176dc08 100644 --- a/Qwirkle/Files/LeaderBoard.json +++ b/Qwirkle/Files/LeaderBoard.json @@ -1,16 +1 @@ -{ - "leaderboard": [ - { - "Date": "\/Date(1717106400000+0200)\/", - "PlayerName": "Jérémy", - "Points": 0, - "Victories": 1 - }, - { - "Date": "\/Date(1717106400000+0200)\/", - "PlayerName": "Jules", - "Points": 0, - "Victories": 0 - } - ] -} \ No newline at end of file +2024-05-31T00:00:00+02:00Jérémy012024-05-31T00:00:00+02:00Jules00 \ No newline at end of file diff --git a/Qwirkle/Files/game.json b/Qwirkle/Files/game.json index 3ddd603..9f0bf08 100644 --- a/Qwirkle/Files/game.json +++ b/Qwirkle/Files/game.json @@ -1 +1 @@ -{"GameRunning":true,"board":{"cells":[{"Tile":null,"x":0,"y":0},{"Tile":null,"x":0,"y":1},{"Tile":null,"x":0,"y":2},{"Tile":null,"x":0,"y":3},{"Tile":null,"x":0,"y":4},{"Tile":null,"x":0,"y":5},{"Tile":null,"x":0,"y":6},{"Tile":null,"x":0,"y":7},{"Tile":null,"x":0,"y":8},{"Tile":null,"x":0,"y":9},{"Tile":null,"x":0,"y":10},{"Tile":null,"x":0,"y":11},{"Tile":null,"x":0,"y":12},{"Tile":null,"x":0,"y":13},{"Tile":null,"x":0,"y":14},{"Tile":null,"x":1,"y":0},{"Tile":null,"x":1,"y":1},{"Tile":null,"x":1,"y":2},{"Tile":null,"x":1,"y":3},{"Tile":null,"x":1,"y":4},{"Tile":null,"x":1,"y":5},{"Tile":null,"x":1,"y":6},{"Tile":null,"x":1,"y":7},{"Tile":null,"x":1,"y":8},{"Tile":null,"x":1,"y":9},{"Tile":null,"x":1,"y":10},{"Tile":null,"x":1,"y":11},{"Tile":null,"x":1,"y":12},{"Tile":null,"x":1,"y":13},{"Tile":null,"x":1,"y":14},{"Tile":null,"x":2,"y":0},{"Tile":null,"x":2,"y":1},{"Tile":null,"x":2,"y":2},{"Tile":null,"x":2,"y":3},{"Tile":null,"x":2,"y":4},{"Tile":null,"x":2,"y":5},{"Tile":null,"x":2,"y":6},{"Tile":null,"x":2,"y":7},{"Tile":null,"x":2,"y":8},{"Tile":null,"x":2,"y":9},{"Tile":null,"x":2,"y":10},{"Tile":null,"x":2,"y":11},{"Tile":null,"x":2,"y":12},{"Tile":null,"x":2,"y":13},{"Tile":null,"x":2,"y":14},{"Tile":null,"x":3,"y":0},{"Tile":null,"x":3,"y":1},{"Tile":null,"x":3,"y":2},{"Tile":null,"x":3,"y":3},{"Tile":null,"x":3,"y":4},{"Tile":null,"x":3,"y":5},{"Tile":null,"x":3,"y":6},{"Tile":null,"x":3,"y":7},{"Tile":null,"x":3,"y":8},{"Tile":null,"x":3,"y":9},{"Tile":null,"x":3,"y":10},{"Tile":null,"x":3,"y":11},{"Tile":null,"x":3,"y":12},{"Tile":null,"x":3,"y":13},{"Tile":null,"x":3,"y":14},{"Tile":null,"x":4,"y":0},{"Tile":null,"x":4,"y":1},{"Tile":null,"x":4,"y":2},{"Tile":null,"x":4,"y":3},{"Tile":null,"x":4,"y":4},{"Tile":null,"x":4,"y":5},{"Tile":null,"x":4,"y":6},{"Tile":null,"x":4,"y":7},{"Tile":null,"x":4,"y":8},{"Tile":null,"x":4,"y":9},{"Tile":null,"x":4,"y":10},{"Tile":null,"x":4,"y":11},{"Tile":null,"x":4,"y":12},{"Tile":null,"x":4,"y":13},{"Tile":null,"x":4,"y":14},{"Tile":null,"x":5,"y":0},{"Tile":null,"x":5,"y":1},{"Tile":null,"x":5,"y":2},{"Tile":null,"x":5,"y":3},{"Tile":null,"x":5,"y":4},{"Tile":null,"x":5,"y":5},{"Tile":null,"x":5,"y":6},{"Tile":null,"x":5,"y":7},{"Tile":null,"x":5,"y":8},{"Tile":null,"x":5,"y":9},{"Tile":null,"x":5,"y":10},{"Tile":null,"x":5,"y":11},{"Tile":null,"x":5,"y":12},{"Tile":null,"x":5,"y":13},{"Tile":null,"x":5,"y":14},{"Tile":null,"x":6,"y":0},{"Tile":null,"x":6,"y":1},{"Tile":null,"x":6,"y":2},{"Tile":null,"x":6,"y":3},{"Tile":null,"x":6,"y":4},{"Tile":null,"x":6,"y":5},{"Tile":null,"x":6,"y":6},{"Tile":null,"x":6,"y":7},{"Tile":null,"x":6,"y":8},{"Tile":null,"x":6,"y":9},{"Tile":null,"x":6,"y":10},{"Tile":null,"x":6,"y":11},{"Tile":null,"x":6,"y":12},{"Tile":null,"x":6,"y":13},{"Tile":null,"x":6,"y":14},{"Tile":null,"x":7,"y":0},{"Tile":null,"x":7,"y":1},{"Tile":null,"x":7,"y":2},{"Tile":null,"x":7,"y":3},{"Tile":null,"x":7,"y":4},{"Tile":null,"x":7,"y":5},{"Tile":null,"x":7,"y":6},{"Tile":null,"x":7,"y":7},{"Tile":null,"x":7,"y":8},{"Tile":null,"x":7,"y":9},{"Tile":null,"x":7,"y":10},{"Tile":null,"x":7,"y":11},{"Tile":null,"x":7,"y":12},{"Tile":null,"x":7,"y":13},{"Tile":null,"x":7,"y":14},{"Tile":null,"x":8,"y":0},{"Tile":null,"x":8,"y":1},{"Tile":null,"x":8,"y":2},{"Tile":null,"x":8,"y":3},{"Tile":null,"x":8,"y":4},{"Tile":null,"x":8,"y":5},{"Tile":null,"x":8,"y":6},{"Tile":null,"x":8,"y":7},{"Tile":null,"x":8,"y":8},{"Tile":null,"x":8,"y":9},{"Tile":null,"x":8,"y":10},{"Tile":null,"x":8,"y":11},{"Tile":null,"x":8,"y":12},{"Tile":null,"x":8,"y":13},{"Tile":null,"x":8,"y":14},{"Tile":null,"x":9,"y":0},{"Tile":null,"x":9,"y":1},{"Tile":null,"x":9,"y":2},{"Tile":null,"x":9,"y":3},{"Tile":null,"x":9,"y":4},{"Tile":null,"x":9,"y":5},{"Tile":null,"x":9,"y":6},{"Tile":null,"x":9,"y":7},{"Tile":null,"x":9,"y":8},{"Tile":null,"x":9,"y":9},{"Tile":null,"x":9,"y":10},{"Tile":null,"x":9,"y":11},{"Tile":null,"x":9,"y":12},{"Tile":null,"x":9,"y":13},{"Tile":null,"x":9,"y":14},{"Tile":null,"x":10,"y":0},{"Tile":null,"x":10,"y":1},{"Tile":null,"x":10,"y":2},{"Tile":null,"x":10,"y":3},{"Tile":null,"x":10,"y":4},{"Tile":null,"x":10,"y":5},{"Tile":null,"x":10,"y":6},{"Tile":null,"x":10,"y":7},{"Tile":null,"x":10,"y":8},{"Tile":null,"x":10,"y":9},{"Tile":null,"x":10,"y":10},{"Tile":null,"x":10,"y":11},{"Tile":null,"x":10,"y":12},{"Tile":null,"x":10,"y":13},{"Tile":null,"x":10,"y":14},{"Tile":null,"x":11,"y":0},{"Tile":null,"x":11,"y":1},{"Tile":null,"x":11,"y":2},{"Tile":null,"x":11,"y":3},{"Tile":null,"x":11,"y":4},{"Tile":null,"x":11,"y":5},{"Tile":null,"x":11,"y":6},{"Tile":null,"x":11,"y":7},{"Tile":null,"x":11,"y":8},{"Tile":null,"x":11,"y":9},{"Tile":null,"x":11,"y":10},{"Tile":null,"x":11,"y":11},{"Tile":null,"x":11,"y":12},{"Tile":null,"x":11,"y":13},{"Tile":null,"x":11,"y":14}]},"players":[{"IsPlaying":true,"NameTag":"Jules","Tiles":[{"color":2,"shape":0},{"color":4,"shape":3},{"color":3,"shape":3},{"color":1,"shape":0},{"color":3,"shape":0},{"color":2,"shape":3}]},{"IsPlaying":false,"NameTag":"Jérémy","Tiles":[{"color":5,"shape":3},{"color":5,"shape":0},{"color":1,"shape":3},{"color":0,"shape":0},{"color":0,"shape":3},{"color":4,"shape":0}]}],"scoreBoard":[{"Key":{"IsPlaying":true,"NameTag":"Jules","Tiles":[{"color":2,"shape":0},{"color":4,"shape":3},{"color":3,"shape":3},{"color":1,"shape":0},{"color":3,"shape":0},{"color":2,"shape":3}]},"Value":0},{"Key":{"IsPlaying":false,"NameTag":"Jérémy","Tiles":[{"color":5,"shape":3},{"color":5,"shape":0},{"color":1,"shape":3},{"color":0,"shape":0},{"color":0,"shape":3},{"color":4,"shape":0}]},"Value":0}]} \ No newline at end of file +true000102030405060708090100110120130141011121314151617181911011111211311420212223242526272829210211212213214303132333435363738393103113123133144041424344454647484941041141241341450515253545556575859510511512513514606162636465666768696106116126136147071727374757677787971071171271371480818283848586878889810811812813814909192939495969798999109119129139141001011021031041051061071081091010101110121013101411011111211311411511611711811911101111111211131114falseJulesPurpleClubRedClubRedRhombusPurpleRoundPurpleStarPurpleStartrueJérémyGreenClubBlueShurikenGreenSquareOrangeStarOrangeClubBlueClubfalseJulesPurpleClubRedClubRedRhombusPurpleRoundPurpleStarPurpleStar0trueJérémyGreenClubBlueShurikenGreenSquareOrangeStarOrangeClubBlueClub0 \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Boards/Board.cs b/Qwirkle/QwirkleClassLibrary/Boards/Board.cs index b13c4f8..2f0df94 100644 --- a/Qwirkle/QwirkleClassLibrary/Boards/Board.cs +++ b/Qwirkle/QwirkleClassLibrary/Boards/Board.cs @@ -122,7 +122,7 @@ namespace QwirkleClassLibrary.Boards return null; } - protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } diff --git a/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs b/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs index ea9353b..0f470dc 100644 --- a/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs +++ b/Qwirkle/QwirkleClassLibrary/Boards/Cell.cs @@ -85,7 +85,7 @@ public class Cell : INotifyPropertyChanged } } - protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } diff --git a/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs b/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs index e59c3c1..0fefb18 100644 --- a/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs +++ b/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs @@ -1,7 +1,12 @@ namespace QwirkleClassLibrary.Events { - public class SwapTilesNotifiedEventArgs(string reason) + public class SwapTilesNotifiedEventArgs { - public string Reason { get; private set; } = reason; + public string Reason { get; private set; } + + public SwapTilesNotifiedEventArgs(string reason) + { + Reason = reason; + } } } \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index 94f8b32..6eee4f6 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -337,16 +337,14 @@ namespace QwirkleClassLibrary.Games return false; } - public bool TileInbag(Player player, Tile tile) + private bool TileInbag(Player player, Tile tile) { - for (int i = 0; i < player.Tiles.Count; i++) + foreach (var t in player.Tiles) { - Tile? t = player.Tiles[i]; - if (Object.ReferenceEquals(t, tile)) return true; - + if (ReferenceEquals(t, tile)) return true; } - return false; + return false; } diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistence.cs b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistence.cs deleted file mode 100644 index 542ba4d..0000000 --- a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistence.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace QwirkleClassLibrary.Persistences; - -public class GamePersistence -{ - -} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceJson.cs b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceJson.cs new file mode 100644 index 0000000..da90959 --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/Persistences/GamePersistenceJson.cs @@ -0,0 +1,27 @@ +using System.Runtime.Serialization; +using QwirkleClassLibrary.Games; + +namespace QwirkleClassLibrary.Persistences; + +public class GamePersistenceJson : IGamePersistence +{ + public void SaveGame(Game game) + { + var serializer = new DataContractSerializer(typeof(Game)); + + using (Stream writer = File.Create("Game.json")) + { + serializer.WriteObject(writer, game); + } + } + + public Game LoadGame() + { + var serializer = new DataContractSerializer(typeof(Game)); + + using (Stream reader = File.OpenRead("Game.json")) + { + return serializer.ReadObject(reader) as Game ?? throw new InvalidOperationException(); + } + } +} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/IGamePersistence.cs b/Qwirkle/QwirkleClassLibrary/Persistences/IGamePersistence.cs new file mode 100644 index 0000000..3eaa63a --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/Persistences/IGamePersistence.cs @@ -0,0 +1,10 @@ +using QwirkleClassLibrary.Games; + +namespace QwirkleClassLibrary.Persistences; + +public interface IGamePersistence +{ + void SaveGame(Game game); + + Game LoadGame(); +} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/ILeaderboardPersistence.cs b/Qwirkle/QwirkleClassLibrary/Persistences/ILeaderboardPersistence.cs new file mode 100644 index 0000000..e63ece0 --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/Persistences/ILeaderboardPersistence.cs @@ -0,0 +1,10 @@ +using QwirkleClassLibrary.Players; + +namespace QwirkleClassLibrary.Persistences; + +public interface ILeaderboardPersistence +{ + void SaveLeaderboard(Leaderboard leaderboard); + + Leaderboard LoadLeaderboard(); +} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/LeaderboardPersistence.cs b/Qwirkle/QwirkleClassLibrary/Persistences/LeaderboardPersistence.cs deleted file mode 100644 index db20a36..0000000 --- a/Qwirkle/QwirkleClassLibrary/Persistences/LeaderboardPersistence.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Runtime.Serialization; -using QwirkleClassLibrary.Players; - -namespace QwirkleClassLibrary.Persistences; - -public class LeaderboardPersistence -{ - public void SaveLeaderboard(Leaderboard leaderboard) - { - var leaderboardSerializer = new DataContractSerializer(typeof(Leaderboard)); - - using (Stream s = File.Create("LeaderBoard.json ")) - { - leaderboardSerializer.WriteObject(s, leaderboard); - } - } - - public Leaderboard LoadLeaderboard() - { - var leaderboardSerializer = new DataContractSerializer(typeof(Leaderboard)); - - using (Stream s = File.OpenRead("LeaderBoard.json")) - { - return leaderboardSerializer.ReadObject(s) as Leaderboard ?? throw new NullReferenceException(); - } - } -} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Persistences/LeaderboardPersistenceJson.cs b/Qwirkle/QwirkleClassLibrary/Persistences/LeaderboardPersistenceJson.cs new file mode 100644 index 0000000..decb648 --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/Persistences/LeaderboardPersistenceJson.cs @@ -0,0 +1,27 @@ +using System.Runtime.Serialization; +using QwirkleClassLibrary.Players; + +namespace QwirkleClassLibrary.Persistences; + +public class LeaderboardPersistenceJson : ILeaderboardPersistence +{ + public void SaveLeaderboard(Leaderboard leaderboard) + { + var serializer = new DataContractSerializer(typeof(Leaderboard)); + + using (Stream writer = File.Create("Leaderboard.json")) + { + serializer.WriteObject(writer, leaderboard); + } + } + + public Leaderboard LoadLeaderboard() + { + var serializer = new DataContractSerializer(typeof(Leaderboard)); + + using (Stream reader = File.OpenRead("Leaderboard.json")) + { + return serializer.ReadObject(reader) as Leaderboard ?? throw new InvalidOperationException(); + } + } +} \ No newline at end of file diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index c0a6e3a..49137d9 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -218,20 +218,14 @@ static void MenuSwitch(Game game) game.DrawTiles(game.GetPlayingPlayer()); game.CheckGameOver(game.GetPlayingPlayer()); - var inGameSerializer = new DataContractJsonSerializer(typeof(Game)); - using (Stream s = File.Create("game.json")) - { - inGameSerializer.WriteObject(s, game); - } + IGamePersistence gameSave = new GamePersistenceJson(); + gameSave.SaveGame(game); return; case 4: - var postGameSerializer = new DataContractJsonSerializer(typeof(Game)); - using (Stream s = File.Create("game.json")) - { - postGameSerializer.WriteObject(s, game); - } - + IGamePersistence endGameSave = new GamePersistenceJson(); + endGameSave.SaveGame(game); + game.GameRunning = false; break; } @@ -403,25 +397,19 @@ static void MainGame() leaderboard.AddScoreInLead(game.ScoreBoard); - LeaderboardPersistence lp = new LeaderboardPersistence(); - lp.SaveLeaderboard(leaderboard); + ILeaderboardPersistence leaderboardSave = new LeaderboardPersistenceJson(); + leaderboardSave.SaveLeaderboard(leaderboard); break; case 2: Game loadedGame; - var serializer = new DataContractJsonSerializer(typeof(Game)); - using (Stream s = File.OpenRead("game.json")) - { - loadedGame = (serializer.ReadObject(s) as Game)!; - } - - var loadedLeaderboardSerializer = new DataContractJsonSerializer(typeof(Leaderboard)); - using (Stream s = File.OpenRead("leaderboard.json")) - { - leaderboard = (loadedLeaderboardSerializer.ReadObject(s) as Leaderboard)!; - } + IGamePersistence gameLoad = new GamePersistenceJson(); + loadedGame = gameLoad.LoadGame(); + + ILeaderboardPersistence leaderboardLoad = new LeaderboardPersistenceJson(); + leaderboard = leaderboardLoad.LoadLeaderboard(); MainMenuContinue(loadedGame); leaderboard.AddScoreInLead(loadedGame.ScoreBoard);