From c4bec0e3acb3cc0f62ef83357f9cbe3e2c2b2229 Mon Sep 17 00:00:00 2001 From: Knose Date: Sat, 31 Jul 2021 20:44:52 +0200 Subject: [PATCH] Add list-command --- Classes/TarotDeck.cs | 46 +++++++++++------------ Options.cs | 16 +++++--- Program.cs | 87 +++++++++++++++++++++++++++++--------------- TODO.md | 2 +- 4 files changed, 93 insertions(+), 58 deletions(-) diff --git a/Classes/TarotDeck.cs b/Classes/TarotDeck.cs index cb3bd29..017cd50 100644 --- a/Classes/TarotDeck.cs +++ b/Classes/TarotDeck.cs @@ -9,28 +9,28 @@ public enum ShuffleType{ } public class TarotDeck : IDeck{ - private List _deck; + public readonly List Cards; public TarotDeck(){ - this._deck = new List(); + this.Cards = new List(); } public void DeserializeDeck(string filePath){ - _deck.AddRange(Utilities.Deserialize>(filePath)); + Cards.AddRange(Utilities.Deserialize>(filePath)); } public string SerializeDeck(){ - return Utilities.Serialize>(_deck); + return Utilities.Serialize>(Cards); } public TarotCard RequeueCard() { - TarotCard card = _deck.Dequeue(); - _deck.Add(card); + TarotCard card = Cards.Dequeue(); + Cards.Add(card); return card; } public void PrintDeck(){ - foreach(TarotCard card in this._deck){ + foreach(TarotCard card in this.Cards){ Console.WriteLine(card.GetName()); } } @@ -50,39 +50,39 @@ public void ShuffleDeck(ShuffleType shuffleType){ } private void ShuffleFisherYates(){ - for (int i = _deck.Count - 1; i >= 0; i--){ + for (int i = Cards.Count - 1; i >= 0; i--){ int k = Utilities.RNG.Next(i + 1); - TarotCard card = _deck[k]; - _deck[k] = _deck[i]; - _deck[i] = card; + TarotCard card = Cards[k]; + Cards[k] = Cards[i]; + Cards[i] = card; } } private void ShuffleOverhand(){ - int cut = Utilities.RNG.Next(_deck.Count); + int cut = Utilities.RNG.Next(Cards.Count); for (int i = 0; i < cut; i++){ - TarotCard card = _deck.Dequeue(); - _deck.Add(card); + TarotCard card = Cards.Dequeue(); + Cards.Add(card); } } 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); - _deck.Clear(); + int cut = Utilities.RNG.Next(Cards.Count); + int count = Cards.Count; + List left = Cards.GetRange(0, cut); + List right = Cards.GetRange(cut, count - cut); + Cards.Clear(); while(left.Count > 0 && right.Count > 0){ - _deck.Add(left.Dequeue()); - _deck.Add(right.Dequeue()); + Cards.Add(left.Dequeue()); + Cards.Add(right.Dequeue()); } if(left.Count == 0){ - _deck.AddRange(right); + Cards.AddRange(right); } else if(right.Count == 0){ - _deck.AddRange(left); + Cards.AddRange(left); } } } diff --git a/Options.cs b/Options.cs index 42cc5c4..0807a71 100644 --- a/Options.cs +++ b/Options.cs @@ -36,13 +36,19 @@ public class ResetOptions{ [Verb("spread", HelpText = "Perform a tarot spread.")] public class SpreadOptions{ - [Option('l',"list", SetName = "one", MetaValue = "string", Required = false, HelpText = "List a specific spread.")] - public bool List{get; set;} + [Value(0, MetaName = "name", MetaValue = "string", Required = true, HelpText = "Name of spread to perform.")] + public string Name{get; set;} + } + + [Verb("list", HelpText = "List all available cards or spreads.")] + public class ListOption{ + [Option('c',"card", SetName = "cards", MetaValue = "string", Required = true, HelpText = "List all, or a specific card.")] + public bool Card{get; set;} - [Option('a',"list-all", SetName = "all", Required = false, HelpText = "List all spreads.")] - public bool ListAll{get; set;} + [Option('s',"spread", SetName = "spread", MetaValue = "string", Required = true, HelpText = "List all, or a specific spread.")] + public bool Spread{get; set;} - [Value(0, MetaName = "name", MetaValue = "string", Required = false, HelpText = "Name of spread to perform.")] + [Value(0, MetaName = "name", MetaValue = "string", Required = false, HelpText = "Name of card or spread to list. Leave empty for all.")] public string Name{get; set;} } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 295e18d..29da8d6 100644 --- a/Program.cs +++ b/Program.cs @@ -27,7 +27,7 @@ class Program{ static int Main(string[] args){ TarotDeck deck = new TarotDeck(); TarotSpreads spreads = new TarotSpreads(); - Type[] types = {typeof(ShuffleOptions), typeof(GetOptions), typeof(ResetOptions), typeof(SpreadOptions)}; + Type[] types = {typeof(ShuffleOptions), typeof(GetOptions), typeof(ResetOptions), typeof(SpreadOptions), typeof(ListOption)}; HandleFiles(deck, spreads); @@ -36,6 +36,7 @@ static int Main(string[] args){ (ShuffleOptions options) => Shuffle(options, deck), (ResetOptions options) => Reset(options, deck, spreads), (SpreadOptions options) => Spread(options, deck, spreads), + (ListOption options) => List(options, deck, spreads), errors => 1 ); } @@ -159,37 +160,65 @@ private static int Reset(ResetOptions options, TarotDeck deck, TarotSpreads spre private static int Spread(SpreadOptions options, TarotDeck deck, TarotSpreads spreads){ List names = new List(spreads.Keys); - if(options.ListAll){ - names.Sort(); - for (int i = 0; i < names.Count; i++){ - string name = names[i]; - Console.WriteLine($"{i+1}. {name}:"); - TarotSpread spread = spreads[name]; - for (int j = 0; j < spread.Length(); j++){ - Console.WriteLine($"\t{j+1}) {spread.Positions[j]}"); - } - } - }else if(options.Name != default){ - if(names.Contains(options.Name)){ - TarotSpread spread = spreads[options.Name]; - if(options.List){ - Console.WriteLine($"{options.Name}:"); - for (int j = 0; j < spread.Length(); j++){ - Console.WriteLine($"\t{j+1}) {spread.Positions[j]}"); - } - }else{ - spread.AddCards(deck); - spread.PrintSpread(); - } - }else{ - Console.WriteLine("Spread does not exist."); - return 1; - } + if(names.Contains(options.Name)){ + TarotSpread spread = spreads[options.Name]; + spread.AddCards(deck); + spread.PrintSpread(); + SaveDeck(deck); + return 0; }else{ + Console.WriteLine("Spread does not exist."); return 1; } - SaveDeck(deck); - return 0; } + + private static int List(ListOption options, TarotDeck deck, TarotSpreads spreads){ + if(options.Card){ + if(options.Name != default){ + TarotCard card = deck.Cards.Find((TarotCard c) => c.Name.ToLower() == options.Name.ToLower()); + + if(card is not null){ + Console.WriteLine($"{card.GetName()}:\n\t{card.GetKeywords()}"); + }else{ + Console.WriteLine("That card does not exist."); + return 1; + } + }else{ + foreach (TarotCard card in deck.Cards) + { + Console.WriteLine($"{card.GetName()}:\n\t{card.GetKeywords()}"); + } + } + }else if(options.Spread){ + List names = new List(spreads.Keys); + names.Sort(); + + if(options.Name != default){ + string name = names.Find((string n) => n.ToLower() == options.Name.ToLower()); + + if(name is not null){ + TarotSpread spread = spreads[name]; + + Console.WriteLine($"{name}:"); + for (int i = 0; i < spread.Length(); i++){ + Console.WriteLine($"\t{i+1}. {spread.Positions[i]}"); + } + }else{ + Console.WriteLine("That spread does not exist."); + return 1; + } + }else{ + foreach(string name in names){ + TarotSpread spread = spreads[name]; + + Console.WriteLine($"{name}:"); + for (int i = 0; i < spread.Length(); i++){ + Console.WriteLine($"\t{i+1}. {spread.Positions[i]}"); + } + } + } + } + return 0; + } } } \ No newline at end of file diff --git a/TODO.md b/TODO.md index fc231ea..8b6da38 100644 --- a/TODO.md +++ b/TODO.md @@ -22,10 +22,10 @@ C# Tarot-reading application ### In Progress -- [ ] Move listing spreads and viewing one single spread to a verb that also allows viewing a specific card or all cards ### Done ✓ +- [x] Move listing spreads and viewing one single spread to a verb that also allows viewing a specific card or all cards - [x] Add tarot card meanings/keywords. - [x] Function to reset deck and/or spreads, either from original or from own chosen file. - [x] Current Spreads-file, similar to current deck.