From 455755b6a433447cb3f74ec043908d981791c24a Mon Sep 17 00:00:00 2001 From: rbamforth <79797573+rbamforth@users.noreply.github.com> Date: Mon, 22 Mar 2021 12:00:26 +0000 Subject: [PATCH] Added Cards and Deck classes --- 94 War/csharp/War/War/Cards.cs | 113 +++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 94 War/csharp/War/War/Cards.cs diff --git a/94 War/csharp/War/War/Cards.cs b/94 War/csharp/War/War/Cards.cs new file mode 100644 index 00000000..ad84735a --- /dev/null +++ b/94 War/csharp/War/War/Cards.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace War +{ + public enum Suit + { + none = 0, + clubs, + diamonds, + hearts, + spades + } + + public enum Rank + { + none = 0, + // Skip 1 because ace is high. + two = 2, + three, + four, + five, + six, + seven, + eight, + nine, + ten, + jack, + queen, + king, + ace + } + + // TODO Testing + + class Card + { + // TODO Turn into properties, maybe?? + private Suit suit; + private Rank rank; + + public Card(Suit suit, Rank rank) // immutable + { + this.suit = suit; + this.rank = rank; + } + + // would normally consider suit and rank but in this case we only want to compare rank. + public static bool operator ==(Card lhs, Card rhs) + { + return lhs.rank == rhs.rank; + } + + public static bool operator !=(Card lhs, Card rhs) + { + return !(lhs == rhs); + } + + public static bool operator <(Card lhs, Card rhs) + { + return lhs.rank < rhs.rank; + } + public static bool operator >(Card lhs, Card rhs) + { + return rhs < lhs; + } + public static bool operator <=(Card lhs, Card rhs) + { + return !(lhs > rhs); + } + public static bool operator >=(Card lhs, Card rhs) + { + return !(lhs < rhs); + } + + public override string ToString() + { + // TODO No need to create the dictionaries each time this is called. + // Also make it static. + // Is there a C# equivalent of an initializer list? + var suitNames = new Dictionary(); + + suitNames[Suit.none] = "N"; + suitNames[Suit.clubs] = "C"; + suitNames[Suit.diamonds] = "D"; + suitNames[Suit.hearts] = "H"; + suitNames[Suit.spades] = "S"; + + var rankNames = new Dictionary(); + rankNames[Rank.none] = "0"; + rankNames[Rank.two] = "2"; + rankNames[Rank.three] = "3"; + rankNames[Rank.four] = "4"; + rankNames[Rank.five] = "5"; + rankNames[Rank.six] = "6"; + rankNames[Rank.seven] = "7"; + rankNames[Rank.eight] = "8"; + rankNames[Rank.nine] = "9"; + rankNames[Rank.ten] = "10"; + rankNames[Rank.jack] = "J"; + rankNames[Rank.queen] = "Q"; + rankNames[Rank.king] = "K"; + rankNames[Rank.ace] = "A"; + + return $"{suitNames[suit]}-{rankNames[rank]}"; // string interpolation + } + } + + class Deck + { + } +}