Skip to content

Commit

Permalink
Implement Move and Swap verbs
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarcederberg committed Aug 2, 2021
1 parent 005eb05 commit 5db4bd0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Classes/IDeck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface IDeck<T> where T : ICard{

public void ShuffleDeck(ShuffleType shuffleType);

public void SwapCards(int firstIndex, int otherIndex);
public void SwapCards(int firstIndex, int secondIndex);

public void MoveCard(int cardIndex, int newIndexx);
}
Expand Down
6 changes: 3 additions & 3 deletions Classes/TarotDeck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public void ShuffleDeck(ShuffleType shuffleType){
}
}

public void SwapCards(int firstIndex, int otherIndex){
public void SwapCards(int firstIndex, int secondIndex){
TarotCard temp = Cards[firstIndex];
Cards[firstIndex] = Cards[otherIndex];
Cards[otherIndex] = temp;
Cards[firstIndex] = Cards[secondIndex];
Cards[secondIndex] = temp;
}

public void MoveCard(int oldIndex, int newIndex){
Expand Down
18 changes: 18 additions & 0 deletions Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,22 @@ public class ListOption{
[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;}
}

[Verb("move", HelpText = "Move a card to a new position in the deck.")]
public class MoveOptions{
[Value(0, MetaName = "card index", MetaValue = "int", Required = true, HelpText = "Position of card to move.")]
public int OldIndex{get; set;}

[Value(0, MetaName = "new index", MetaValue = "int", Required = true, HelpText = "Position of where to move the card to.")]
public int NewIndex{get; set;}
}

[Verb("swap", HelpText = "Swap the positions of two cards in the deck")]
public class SwapOptions{
[Value(0, MetaName = "first card index", MetaValue = "int", Required = true, HelpText = "Position of first card to swap.")]
public int FirstIndex{get; set;}

[Value(0, MetaName = "second card index", MetaValue = "int", Required = true, HelpText = "Position of second card to swap.")]
public int SecondIndex{get; set;}
}
}
27 changes: 26 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ 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), typeof(ListOption)};
Type[] types = {typeof(ShuffleOptions), typeof(GetOptions), typeof(ResetOptions), typeof(SpreadOptions), typeof(ListOption),
typeof(MoveOptions), typeof(SwapOptions)};

HandleFiles(deck, spreads);

Expand All @@ -37,6 +38,8 @@ static int Main(string[] args){
(ResetOptions options) => Reset(options, deck, spreads),
(SpreadOptions options) => Spread(options, deck, spreads),
(ListOption options) => List(options, deck, spreads),
(MoveOptions options) => Move(options, deck),
(SwapOptions options) => Swap(options, deck),
errors => 1
);
}
Expand Down Expand Up @@ -220,5 +223,27 @@ private static int List(ListOption options, TarotDeck deck, TarotSpreads spreads
}
return 0;
}

private static int Move(MoveOptions options, TarotDeck deck){
if(options.OldIndex > deck.Cards.Count -1 || options.OldIndex < 0 ||
options.NewIndex > deck.Cards.Count -1 || options.NewIndex < 0){
Console.WriteLine($"Positional index is outside of the deck range [0,{deck.Cards.Count -1}].");
return 1;
}else{
deck.MoveCard(options.OldIndex, options.NewIndex);
return 0;
}
}

private static int Swap(SwapOptions options, TarotDeck deck){
if(options.FirstIndex > deck.Cards.Count -1 || options.FirstIndex < 0 ||
options.SecondIndex > deck.Cards.Count -1 || options.SecondIndex < 0){
Console.WriteLine($"Positional index is outside of the deck range [0,{deck.Cards.Count -1}].");
return 1;
}else{
deck.SwapCards(options.FirstIndex, options.SecondIndex);
return 0;
}
}
}
}

0 comments on commit 5db4bd0

Please sign in to comment.