-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcard.go
62 lines (54 loc) · 1.31 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
package main
var (
cardValues = []string{"6", "7", "8", "9", "10", "J", "Q", "K", "A"}
cardSuits = []string{"♣", "♦", "♥", "♠"}
)
// Card represents a card from French playing cards with Value And Suit.
type Card struct {
Value string `json:"value"`
Suit string `json:"suit"`
}
func (c *Card) getValueIndex() int {
index := -1
for i, v := range cardValues {
if v == c.Value {
index = i
}
}
return index
}
func (c *Card) gt(otherCard *Card) bool {
if c.Suit != otherCard.Suit {
return false
}
thisIndex := c.getValueIndex()
otherIndex := otherCard.getValueIndex()
return thisIndex > otherIndex
}
func (c *Card) gte(otherCard *Card) bool {
if c.Suit != otherCard.Suit {
return false
}
thisIndex := c.getValueIndex()
otherIndex := otherCard.getValueIndex()
return thisIndex >= otherIndex
}
func (c *Card) lt(otherCard *Card) bool {
if c.Suit != otherCard.Suit {
return false
}
thisIndex := c.getValueIndex()
otherIndex := otherCard.getValueIndex()
return thisIndex < otherIndex
}
func (c *Card) lte(otherCard *Card) bool {
if c.Suit != otherCard.Suit {
return false
}
thisIndex := c.getValueIndex()
otherIndex := otherCard.getValueIndex()
return thisIndex <= otherIndex
}
func (c *Card) equals(otherCard *Card) bool {
return c.Value == otherCard.Value && c.Suit == otherCard.Suit
}