-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcard.go
107 lines (95 loc) · 1.7 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
package deck
import "fmt"
// Suit represents the suit of the card (spade, heart, diamond, club)
type Suit int
// Face represents the face of the card (ace, two...queen, king)
type Face int
// Constants for Suit ♠♥♦♣
const (
CLUB Suit = iota
DIAMOND
HEART
SPADE
)
// Constants for Face
const (
ACE Face = iota
TWO
THREE
FOUR
FIVE
SIX
SEVEN
EIGHT
NINE
TEN
JACK
QUEEN
KING
)
// Global Variables representing the default suits and faces in a deck of cards
var (
SUITS = []Suit{CLUB, DIAMOND, HEART, SPADE}
FACES = []Face{ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}
)
// Card represents a playing card with a Face and a Suit
type Card int
func (c Card) String() string {
face := ""
switch c.Face() {
case 0:
face = "A"
case 1:
face = "2"
case 2:
face = "3"
case 3:
face = "4"
case 4:
face = "5"
case 5:
face = "6"
case 6:
face = "7"
case 7:
face = "8"
case 8:
face = "9"
case 9:
face = "T"
case 10:
face = "J"
case 11:
face = "Q"
case 12:
face = "K"
}
suit := ""
switch c.Suit() {
case 0:
suit = "♣"
case 1:
suit = "♦"
case 2:
suit = "♥"
case 3:
suit = "♠"
}
return fmt.Sprintf("%s%s", face, suit)
}
// Face is a utility function to get the face of a card
func (c Card) Face() int {
return int(c / 4)
}
// Suit is a utility function to get the suit of a card
func (c Card) Suit() int {
return int(c % 4)
}
// NewCard creates a new card with a face and suit
func NewCard(face Face, suit Suit) Card {
return Card(int(face)*4 + int(suit))
}
// GetSignature is the hex representation of the Face and Suit of the card
func (c *Card) GetSignature() string {
return fmt.Sprintf("%x%x", c.Face(), c.Suit())
}