-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.go
174 lines (154 loc) · 3.08 KB
/
card.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package deck
import "fmt"
// Card is a playing card
type Card struct {
Suit
Value
}
// Suit is the suit of a card
type Suit string
// Color is the color of the card
type Color string
const (
// Red is the color red
Red Color = "Red"
// Black is the color black
Black Color = "Black"
)
const (
// Hearts is the hearts suit
Hearts Suit = "Hearts"
// Spades is the spades suit
Spades Suit = "Spades"
// Diamonds is the diamonds suit
Diamonds Suit = "Diamonds"
// Clubs is the clubs suit
Clubs Suit = "Clubs"
)
// Suits are all suits
var Suits = [4]Suit{Hearts, Spades, Diamonds, Clubs}
// Value is the value of a card
type Value int
const (
// Ace is an ace
Ace Value = iota
// Two is a 2
Two
// Three is a three
Three
// Four is a four
Four
// Five is a five
Five
// Six is a six
Six
// Seven is a seven
Seven
// Eight is an eight
Eight
// Nine is a nine
Nine
// Ten is a ten
Ten
// Jack is a jack
Jack
// Queen is a queen
Queen
// King is a king
King
)
// Values are the values in the deck
var Values = [13]Value{Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King}
// NewCard creates a new card of the given suit
// if the suit and value are not valid, a panic occurs
func NewCard(s Suit, v Value) *Card {
validate(s, v)
return &Card{
Suit: s,
Value: v,
}
}
// Validate validates the card, non-nil with valid suit and value
func Validate(card *Card) {
if card == nil {
panic(fmt.Sprintf("Card is nil"))
}
validate(card.Suit, card.Value)
}
func validate(s Suit, v Value) {
if s != Hearts && s != Spades && s != Diamonds && s != Clubs {
panic(fmt.Sprintf("Unknown suit %s", s))
}
if v > King || v < Ace {
panic(fmt.Sprintf("Unknown value %d", v))
}
}
func valueString(v Value) string {
switch v {
case Ace:
return "Ace"
case Two:
return "Two"
case Three:
return "Three"
case Four:
return "Four"
case Five:
return "Five"
case Six:
return "Six"
case Seven:
return "Seven"
case Eight:
return "Eight"
case Nine:
return "Nine"
case Ten:
return "Ten"
case Jack:
return "Jack"
case Queen:
return "Queen"
case King:
return "King"
default:
return ""
}
}
// String returns a string representation of this card
func (c Card) String() string {
return fmt.Sprintf("%s of %s", valueString(c.Value), c.Suit)
}
// Color returns the color of the card
func (c Card) Color() Color {
if c.Suit == Spades || c.Suit == Clubs {
return Black
}
return Red
}
// IsBlack returns if the card is black
func (c Card) IsBlack() bool {
return c.Color() == Black
}
// IsRed returns if the card is red
func (c Card) IsRed() bool {
return c.Color() == Red
}
// Compare returns negative is c < o, 0 if c = o and positive of c > o
func (c Card) Compare(o Card, acesLow ...bool) int {
if o.Value == c.Value {
return 0
}
aceHigh := true
if len(acesLow) > 0 {
aceHigh = !acesLow[0]
}
if (c.Value == Ace && aceHigh) || (o.Value == Ace && !aceHigh) || c.Value > o.Value {
return 1
}
return -1
}
// Equals returns if c is the same card as o
func (c Card) Equals(o Card) bool {
return c.Value == o.Value && c.Suit == o.Suit
}