From 4ef292fc5636107d9ae8741d15bab4b1fb85f9aa Mon Sep 17 00:00:00 2001 From: Knose Date: Tue, 27 Jul 2021 22:17:30 +0200 Subject: [PATCH] Save deck between sessions --- Class/IDeck.cs | 15 +++++++++++++++ Class/TarotCard.cs | 8 ++++---- Class/{Deck.cs => TarotDeck.cs} | 29 +++++++++++++++++------------ Class/Utilities.cs | 4 ++++ Program.cs | 20 ++++++++++++++------ 5 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 Class/IDeck.cs rename Class/{Deck.cs => TarotDeck.cs} (71%) diff --git a/Class/IDeck.cs b/Class/IDeck.cs new file mode 100644 index 0000000..2a5a7f7 --- /dev/null +++ b/Class/IDeck.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace tarot{ + public interface IDeck where T : ICard{ + public void DeserializeDeck(string filePath); + + public string SerializeDeck(); + + public T RequeueCard(); + + public void PrintDeck(); + + public void ShuffleDeck(ShuffleType shuffleType); + } +} \ No newline at end of file diff --git a/Class/TarotCard.cs b/Class/TarotCard.cs index 07937bc..b11c23e 100644 --- a/Class/TarotCard.cs +++ b/Class/TarotCard.cs @@ -1,12 +1,12 @@ namespace tarot{ public class TarotCard : ICard{ - private readonly string _name; - public TarotCard(int id, string name){ - this._name = name; + public readonly string Name; + public TarotCard(string name){ + this.Name = name; } public override string ToString(){ - return this._name; + return Name; } } } \ No newline at end of file diff --git a/Class/Deck.cs b/Class/TarotDeck.cs similarity index 71% rename from Class/Deck.cs rename to Class/TarotDeck.cs index cf95ab3..a72bf7a 100644 --- a/Class/Deck.cs +++ b/Class/TarotDeck.cs @@ -8,25 +8,30 @@ public enum ShuffleType{ Riffle } - public class Deck{ - private List _deck; + public class TarotDeck : IDeck{ + private List _deck; - public Deck(){ - this._deck = new List(); + public TarotDeck(){ + this._deck = new List(); } - public void AddToDeck(string filePath){ + public void DeserializeDeck(string filePath){ + _deck = new List(); _deck.AddRange(Utilities.Deserialize>(filePath)); } - public ICard RequeueCard() { - ICard card = _deck.Dequeue(); + public string SerializeDeck(){ + return Utilities.Serialize>(_deck); + } + + public TarotCard RequeueCard() { + TarotCard card = _deck.Dequeue(); _deck.Add(card); return card; } public void PrintDeck(){ - foreach(ICard card in this._deck){ + foreach(TarotCard card in this._deck){ Console.WriteLine(card.ToString()); } } @@ -48,7 +53,7 @@ public void ShuffleDeck(ShuffleType shuffleType){ private void ShuffleFisherYates(){ for (int i = _deck.Count - 1; i >= 0; i--){ int k = Utilities.RNG.Next(i + 1); - ICard card = _deck[k]; + TarotCard card = _deck[k]; _deck[k] = _deck[i]; _deck[i] = card; } @@ -57,7 +62,7 @@ private void ShuffleFisherYates(){ private void ShuffleOverhand(){ int cut = Utilities.RNG.Next(_deck.Count); for (int i = 0; i < cut; i++){ - ICard card = _deck.Dequeue(); + TarotCard card = _deck.Dequeue(); _deck.Add(card); } } @@ -65,8 +70,8 @@ private void ShuffleOverhand(){ private void ShuffleRiffle(){ int cut = Utilities.RNG.Next(_deck.Count); int count = _deck.Count; - List left = _deck.GetRange(0, cut); - List right = _deck.GetRange(cut, count - cut); + List left = _deck.GetRange(0, cut); + List right = _deck.GetRange(cut, count - cut); _deck.Clear(); while(left.Count > 0 && right.Count > 0){ diff --git a/Class/Utilities.cs b/Class/Utilities.cs index f2e452f..e76e5b2 100644 --- a/Class/Utilities.cs +++ b/Class/Utilities.cs @@ -16,6 +16,10 @@ public static T Deserialize(string filePath){ return JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } + public static string Serialize(T obj){ + return JsonConvert.SerializeObject(obj, Formatting.Indented); + } + public static string ToRoman(int number){ number = Math.Max(number, 0); diff --git a/Program.cs b/Program.cs index 88b732e..8c5f617 100644 --- a/Program.cs +++ b/Program.cs @@ -4,16 +4,22 @@ namespace tarot{ class Program{ + static string userPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToString(); + static string configPath = Path.Combine(userPath,@"tarot/"); + static string filePath = Path.Combine(configPath,@"current_deck.json"); + static string defaultFilePath = @"Data/default_cards.json"; + static int Main(string[] args){ - string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - string configPath = Path.Combine(userPath,@".config/tarot/"); Directory.CreateDirectory(configPath); + TarotDeck deck = new TarotDeck(); + if(File.Exists(filePath)){ + deck.DeserializeDeck(filePath); + }else{ + deck.DeserializeDeck(defaultFilePath); + File.WriteAllText(filePath, deck.SerializeDeck()); + } Type[] types = {typeof(ShuffleOptions), typeof(GetOptions)}; - - Deck deck = new Deck(); - deck.AddToDeck(@"Data/default_cards.json"); - return Parser.Default.ParseArguments(args, types) .MapResult( (GetOptions options) => { @@ -21,6 +27,7 @@ static int Main(string[] args){ for (int i = 0; i < options.Amount; i++){ Console.WriteLine(deck.RequeueCard().ToString()); } + File.WriteAllText(filePath, deck.SerializeDeck()); return 0; }, (ShuffleOptions options) => { @@ -54,6 +61,7 @@ static int Main(string[] args){ Console.WriteLine("Performed {0} shuffle.", options.Type.ToLower()); } } + File.WriteAllText(filePath, deck.SerializeDeck()); return 0; }, errors => 1);