-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.cs
More file actions
70 lines (60 loc) · 1.95 KB
/
Card.cs
File metadata and controls
70 lines (60 loc) · 1.95 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
using System;
namespace GoFish {
public class Card : IComparable<Card>, IEquatable<Card> {
/// <summary>
/// The card's "name," or textual representation
/// </summary>
public string Name { get; private set; }
/// <summary>
/// The card's numerical value, useful for sorting or comparison
/// </summary>
public int Value { get; private set; }
/// <summary>
/// A list of all possible card names
/// </summary>
public static readonly string[] CardNames = {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"J", "Q", "K",
};
/// <summary>
/// The number of valid suits of cards
/// </summary>
public const int SuitCount = 4;
/// <summary>
/// Creates a new Card instace
/// </summary>
/// <param name="value">The value of the card, from 1 - 13 (11, 12, 13 are J, Q, K respectively)</param>
public Card(int value) {
if (value < 1 || value > 13) {
throw new ArgumentOutOfRangeException($"{value} is outside of the possible range 1 - 13");
}
this.Value = value;
this.Name = CardNames[value - 1];
}
/// <summary>
/// Compares this Card to another one by value
/// </summary>
/// <param name="that">The Card to compare against</param>
/// <returns>< 0 if this < that, > 0 if this > that, 0 if this == that</returns>
public int CompareTo(Card that) {
return this.Value - that.Value;
}
/// <summary>
/// Tests for equivalency between two Cards by value
/// </summary>
/// <param name="that">The Card object to compare against</param>
/// <returns>Whether or not the two Card objects have the same value</returns>
public bool Equals(Card that) {
return that != null && that.Value == this.Value;
}
public override string ToString() {
return this.Name;
}
public override int GetHashCode() {
int hashName = this.Name == null ? 0 : this.Name.GetHashCode();
int hashValue = this.Value == 0 ? 0 : this.Value.GetHashCode();
return hashName ^ hashValue;
}
}
}