Skip to content

Commit

Permalink
Add list-command
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarcederberg committed Jul 31, 2021
1 parent 61c253f commit c4bec0e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 58 deletions.
46 changes: 23 additions & 23 deletions Classes/TarotDeck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ public enum ShuffleType{
}

public class TarotDeck : IDeck<TarotCard>{
private List<TarotCard> _deck;
public readonly List<TarotCard> Cards;

public TarotDeck(){
this._deck = new List<TarotCard>();
this.Cards = new List<TarotCard>();
}

public void DeserializeDeck(string filePath){
_deck.AddRange(Utilities.Deserialize<List<TarotCard>>(filePath));
Cards.AddRange(Utilities.Deserialize<List<TarotCard>>(filePath));
}

public string SerializeDeck(){
return Utilities.Serialize<List<TarotCard>>(_deck);
return Utilities.Serialize<List<TarotCard>>(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());
}
}
Expand All @@ -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<TarotCard> left = _deck.GetRange(0, cut);
List<TarotCard> right = _deck.GetRange(cut, count - cut);
_deck.Clear();
int cut = Utilities.RNG.Next(Cards.Count);
int count = Cards.Count;
List<TarotCard> left = Cards.GetRange(0, cut);
List<TarotCard> 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);
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
}
}
87 changes: 58 additions & 29 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
);
}
Expand Down Expand Up @@ -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<string> names = new List<string>(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<string> names = new List<string>(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;
}
}
}
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c4bec0e

Please sign in to comment.