forked from platatat/SnapCall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.cs
More file actions
73 lines (63 loc) · 2.16 KB
/
Card.cs
File metadata and controls
73 lines (63 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SnapCall
{
using Enums;
public class Card : IEquatable<Card>
{
public static ConsoleColor[] SuitColors = { ConsoleColor.Green, ConsoleColor.Red, ConsoleColor.Blue, ConsoleColor.Green };
public Rank Rank { get; set; }
public Suit Suit { get; set; }
private static int[] rankPrimes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 };
private static int[] suitPrimes = new int[] { 43, 47, 53, 59 };
public int PrimeRank { get { return rankPrimes[(int)Rank]; } }
public int PrimeSuit { get { return suitPrimes[(int)Suit]; } }
public bool Equals(Card other)
{
return this.Rank == other.Rank && this.Suit == other.Suit;
}
public int GetHashCode(Card c)
{
return c.PrimeRank * c.PrimeSuit;
}
public Card(string s)
{
var chars = s.ToUpper().ToCharArray();
if (chars.Length != 2) throw new ArgumentException("Card string must be length 2");
switch (chars[0])
{
case '2': this.Rank = Rank.Two; break;
case '3': this.Rank = Rank.Three; break;
case '4': this.Rank = Rank.Four; break;
case '5': this.Rank = Rank.Five; break;
case '6': this.Rank = Rank.Six; break;
case '7': this.Rank = Rank.Seven; break;
case '8': this.Rank = Rank.Eight; break;
case '9': this.Rank = Rank.Nine; break;
case 'T': this.Rank = Rank.Ten; break;
case 'J': this.Rank = Rank.Jack; break;
case 'Q': this.Rank = Rank.Queen; break;
case 'K': this.Rank = Rank.King; break;
case 'A': this.Rank = Rank.Ace; break;
default: throw new ArgumentException("Card string rank not valid");
}
switch (chars[1])
{
case 'S': this.Suit = Suit.Spades; break;
case 'H': this.Suit = Suit.Hearts; break;
case 'D': this.Suit = Suit.Diamonds; break;
case 'C': this.Suit = Suit.Clubs; break;
default: throw new ArgumentException("Card string suit not valid");
}
}
public override string ToString()
{
char[] ranks = "23456789TJQKA".ToCharArray();
char[] suits = { '♠', '♥', '♦', '♣' };
return ranks[(int)Rank].ToString() + suits[(int)Suit].ToString();
}
}
}