diff --git a/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs b/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs new file mode 100644 index 0000000..8831ee9 --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/Events/SwapTilesNotifiedEventArgs.cs @@ -0,0 +1,6 @@ +namespace QwirkleClassLibrary.Events; + +public class SwapTilesNotifiedEventArgs(string reason) +{ + public string Reason { get; private set; } = reason; +} \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index 8487113..fc176b5 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -58,13 +58,18 @@ namespace QwirkleClassLibrary.Games protected virtual void OnEndOfGame(EndOfGameNotifiedEventArgs args) => EndOfGameNotified?.Invoke(this, args); + + public event EventHandler? SwapTilesNotified; + + protected virtual void OnSwapTiles(SwapTilesNotifiedEventArgs args) + => SwapTilesNotified?.Invoke(this, args); /// /// Adds a player in the game if the game is not running, if the name is correct, if the game is not full and if the name is not already taken. /// - /// + /// /// boolean to check it - public bool AddPlayerInGame(List PlayersTag) + public bool AddPlayerInGame(List playersTag) { if (GameRunning) { @@ -72,30 +77,30 @@ namespace QwirkleClassLibrary.Games return false; } - for (int i = PlayersTag.Count - 1; i >= 0; i--) + for (int i = playersTag.Count - 1; i >= 0; i--) { - if (string.IsNullOrWhiteSpace(PlayersTag[i])) + if (string.IsNullOrWhiteSpace(playersTag[i])) { - PlayersTag.RemoveAt(i); + playersTag.RemoveAt(i); } } - if (PlayersTag.Count <= 1 || PlayersTag.Count > 4) + if (playersTag.Count <= 1 || playersTag.Count > 4) { - PlayersTag.Clear(); + playersTag.Clear(); OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : It takes a minimum of 2 players and a maximum of 4 players to start a game.")); return false; } - for (int i = PlayersTag.Count - 1; i >= 0; i--) + for (int i = playersTag.Count - 1; i >= 0; i--) { - if (!CheckPlayerTag(PlayersTag, i)) + if (!CheckPlayerTag(playersTag, i)) { - PlayersTag.RemoveAt(i); + playersTag.RemoveAt(i); return false; } } - foreach (var tag in PlayersTag) + foreach (var tag in playersTag) { Player pl = CreatePlayer(tag); players.Add(pl); @@ -106,27 +111,30 @@ namespace QwirkleClassLibrary.Games return true; } - public bool CheckPlayerTag(List PlayersTag, int pos) + public bool CheckPlayerTag(List playersTag, int pos) { - if (string.IsNullOrWhiteSpace(PlayersTag[pos])) + if (string.IsNullOrWhiteSpace(playersTag[pos])) { OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR with " + (pos + 1) + " entry : The name is null or white space.")); return false; } - for(int i=0; i { b.GetCell(x + 1, y), diff --git a/cm10.cs b/cm10.cs new file mode 100644 index 0000000..165164f --- /dev/null +++ b/cm10.cs @@ -0,0 +1,122 @@ +// ==== Persistance ==== + +// Enregistrer les meilleurs scores des parties (déjà prévu) +// Enregistrer la partie en cours, à chaque coup et/ou quand on veut quitter le jeu (déjà prévu) +// Faire un replay de la partie avec 2 joueurs (pas terrible, jeu déjà bien lent) + +// json(prefered 'cause lighter), binary, text, xml(prefered 'cause cleaner) files + +[DataContract(Name = "Player")] +public class Player +{ + [DataMember(Name = "Name")] + public string Name { get; set; } + + [DataMember(EmitDefaultValue = false)] + public int Score { get; set; } + + public Player(string name, int score) + { + Name = name; + Score = score; + } +} + +// Save +var serializer = new DataContractSerializer(typeof(Player)); + +using (Stream s = File.Create("player.xml")) +{ + serializer.WriteObject(s, new Player("John", 42)); +} + +// Load +var serializer = new DataContractSerializer(typeof(Player)); + +using (Stream s = File.OpenRead("player.xml")) +{ + var player = serializer.ReadObject(s) as Player; +} + +// =================== +// ===== Example ===== +// =================== + +[DataContract] +public class Player +{ + [DataMember] + public string Name { get; set; } + + [DataMember] + public int Score { get; set; } + + public Player(string name, int score) + { + Name = name; + Score = score; + } +} + +[DataContract] +public class Result +{ + [DataMember] + public DateTime Date { get; set; } + + [DataMember] + public Player[] Players { get; set; } + + [DataMember] + public int WinnerId { get; set; } + + public Result(DateTime date, Player[] players, int winnerId) + { + Players = new Player[players.Length]; + for (int i = 0; i < players.Length; i++) + { + Players[i] = new Player(players[i].Name, players[i].Score); + } + Date = date; + WinnerId = winnerId; + } + + public Result(Player[] players, int winnerId) : this(DateTime.Now, players, winnerId){} +} + +public class ResultsManage +{ + public ReadOnlyCollection Results { get; private set; } + private readonly List results = new List(); + + public ResultsManage() + { + Results = new ReadOnlyCollection(results); + } + + public void AddResult(int WinnerId, params Player[] players) + { + result.Add(new Result(players, WinnerId)); + } +} + + +Result r = new Result([new Player("John", 42), new Player("Jane", 24)], 24); + +Directory.SetCurrentDirectory(Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\..\\..")); +Directory.CreateDirectory("Files"); + +DataContractSerializer serializer = new DataContractSerializer(typeof(Result), new DataContractSerializerSettings +{ + PreserveObjectReferences = true +}); + +using (Stream s = File.Create(Path.Combine("Files", "result.xml"))) +{ + serializer.WriteObject(s, r); +} + +using (Stream s2 = File.OpenRead(Path.Combine("Files", "result.xml"))) +{ + Result r2 = serializer.ReadObject(s2) as Result; +} \ No newline at end of file