Skip to content

Commit

Permalink
Save deck between sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarcederberg committed Jul 27, 2021
1 parent 0f6a9d3 commit 4ef292f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
15 changes: 15 additions & 0 deletions Class/IDeck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace tarot{
public interface IDeck<T> where T : ICard{
public void DeserializeDeck(string filePath);

public string SerializeDeck();

public T RequeueCard();

public void PrintDeck();

public void ShuffleDeck(ShuffleType shuffleType);
}
}
8 changes: 4 additions & 4 deletions Class/TarotCard.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
29 changes: 17 additions & 12 deletions Class/Deck.cs → Class/TarotDeck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@ public enum ShuffleType{
Riffle
}

public class Deck{
private List<ICard> _deck;
public class TarotDeck : IDeck<TarotCard>{
private List<TarotCard> _deck;

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

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

public ICard RequeueCard() {
ICard card = _deck.Dequeue();
public string SerializeDeck(){
return Utilities.Serialize<List<TarotCard>>(_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());
}
}
Expand All @@ -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;
}
Expand All @@ -57,16 +62,16 @@ 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);
}
}

private void ShuffleRiffle(){
int cut = Utilities.RNG.Next(_deck.Count);
int count = _deck.Count;
List<ICard> left = _deck.GetRange(0, cut);
List<ICard> right = _deck.GetRange(cut, count - cut);
List<TarotCard> left = _deck.GetRange(0, cut);
List<TarotCard> right = _deck.GetRange(cut, count - cut);
_deck.Clear();

while(left.Count > 0 && right.Count > 0){
Expand Down
4 changes: 4 additions & 0 deletions Class/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public static T Deserialize<T>(string filePath){
return JsonConvert.DeserializeObject<T>(File.ReadAllText(filePath));
}

public static string Serialize<T>(T obj){
return JsonConvert.SerializeObject(obj, Formatting.Indented);
}

public static string ToRoman(int number){
number = Math.Max(number, 0);

Expand Down
20 changes: 14 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,30 @@

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) => {
options.Amount = Math.Max(1, options.Amount);
for (int i = 0; i < options.Amount; i++){
Console.WriteLine(deck.RequeueCard().ToString());
}
File.WriteAllText(filePath, deck.SerializeDeck());
return 0;
},
(ShuffleOptions options) => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4ef292f

Please sign in to comment.