Skip to content

Instantly share code, notes, and snippets.

@adam-phillipps
Created February 7, 2016 07:10
Show Gist options
  • Save adam-phillipps/bd1d6e8117043855ac5d to your computer and use it in GitHub Desktop.
Save adam-phillipps/bd1d6e8117043855ac5d to your computer and use it in GitHub Desktop.
8 minutes late resubmission
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A03
{
enum Suit { Clubs = 9827, Diamonds = 9830, Hearts = 9829, Spades = 9824 }
enum Rank { Ace = 1, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King }
struct Card : IComparable
{
#region Props
public Suit Suit { get; }
public Rank Rank { get; }
#endregion Props
#region Constructor
public Card(Rank rank, Suit suite)
{
Suit = suite;
Rank = rank;
}
#endregion Constructor
#region Public methods
public override string ToString()
{
return string.Format($"{(char)Suit} {Rank}");
}
public int CompareTo(object obj)
{
if (obj.GetType() != typeof(Card)) throw new ArgumentException();
Card rightHandCard = (Card)obj;
if (this.Equals(rightHandCard)) return 0;
else if (Rank == rightHandCard.Rank)
{
return (Suit > rightHandCard.Suit) ? 1 : -1;
}
else
{
return (Rank > rightHandCard.Rank) ? 1 : -1;
}
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
Card c = (Card)obj;
if ((int)Rank == (int)c.Rank)
return true;
return false;
//return base.Equals(obj);
}
public static bool operator ==(Card c1, Card c2)
{
return c1.Equals(c2);
}
public static bool operator !=(Card c1, Card c2)
{
return !(c1 == c2);
}
public static bool operator >(Card c1, Card c2)
{
if ((int)c1.Rank == (int)c2.Rank)
return false;
else if ((int)c1.Rank > (int)c2.Rank)
return true;
else
return ((int)c1.Suit > (int)c2.Suit) ? true : false;
}
public static bool operator <(Card c1, Card c2)
{
return ((int)c1.Rank == (int)c2.Rank) ? false : !(c1 > c2);
}
public override int GetHashCode()
{
int r = (int)Rank;
int s = (int)Suit;
return (r * s) * 17;
}
#endregion Public methods
#region Private methods
private bool Equals(Card c)
{
if (c == null || GetType() != c.GetType())
return false;
else
return (c.GetHashCode() == GetHashCode()) ? true : false;
}
#endregion Private methods
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A03
{
class Deck
{
#region Props
public List<Card> Cards { get; private set; }
#endregion Props
#region Constructors
public Deck() { Cards = NewDeck(); }
public Deck(List<Card> cardDeck) { Cards = cardDeck; }
#endregion Constructors
#region Public methods
public static List<Card> NewDeck()
{
List<Card> requestedDeck = new List<Card>();
foreach (Rank r in Enum.GetValues(typeof(Rank)))
foreach (Suit s in Enum.GetValues(typeof(Suit)))
requestedDeck.Add(new Card(r, s));
requestedDeck.Reverse();
return requestedDeck;
}
public void Shuffle()
{
Random rnd = new Random();
for (int i = Cards.Count() - 1; i > 1; i--)
{
int rndNumber = rnd.Next(0, i);
Card placeHolderCard = Cards[rndNumber];
Cards[rndNumber] = Cards[i];
Cards[i] = placeHolderCard;
}
}
public List<Card> Deal(int i = 5)
{
if (i > Cards.Count || i < 0) throw new ArgumentOutOfRangeException();
List<Card> cardsToDeal = Cards.GetRange(0, i);
Cards.RemoveRange(0, i);
return cardsToDeal;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Cards.Count() / 4; i++)
{
for (int j = 0; j < 4; j++)
{
string insert = string.Format($"{Cards[j + (i * 4)],-9:-9}");
sb.Insert(0, insert);
}
sb.Insert(0, "\n");
}
return sb.ToString();
}
#endregion Public methods
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment