-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
135 lines (111 loc) · 2.3 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
package deckofcards
import (
"fmt"
"regexp"
"strings"
)
//Card is a type that implements the structure of a Card.
type Card struct {
Value string `json:"value"`
Suit string `json:"suit"`
Code string `json:"code"`
DeckID string `json:"deckId"`
drawn bool
}
type cardError struct {
err string
value string
suit string
}
func newCard(deckID, value, suit string) (card *Card, err error) {
if !strings.EqualFold(deckID, "") {
card = &Card{
DeckID: deckID,
Code: "",
Value: "",
Suit: "",
drawn: false,
}
card.Value, err = getValue(value)
if err != nil {
return card, err
}
card.Suit, err = getSuit(suit)
if err != nil {
return card, err
}
card.Code = fmt.Sprintf("%s%s", value, suit)
}
return
}
func getValue(value string) (result string, err error) {
values := regexp.MustCompile(`[2-9]|0|A|K|Q|J|\*`)
if !values.MatchString(value) {
return "", &cardError{"invalid value.", value, ""}
}
switch value {
case "A":
result = "ACE"
case "K":
result = "KING"
case "Q":
result = "QUEEN"
case "J":
result = "JACK"
case "0":
result = "10"
case "*":
result = "JOKER"
default:
result = value
}
return
}
func getSuit(suit string) (result string, err error) {
suites := regexp.MustCompile(`S|D|C|H|\*`)
if !suites.MatchString(suit) {
return "", &cardError{"invalid suit.", "", suit}
}
switch suit {
case "S":
result = "SPADES"
case "D":
result = "DIAMONDS"
case "C":
result = "CLUBS"
case "H":
result = "HEARTS"
default:
result = "NONE"
}
return
}
//String function serializes the Card struct into a representable string output.
func (z *Card) String() string {
return fmt.Sprintf("DeckID: %s\n%s - %s", z.DeckID, z.Suit, z.Value)
}
func (z *Card) draw() *Card {
z.drawn = true
card := z.cloneCard()
return card
}
func (z *Card) cloneCard() *Card {
card := &Card{
DeckID: z.DeckID,
Code: z.Code,
Value: z.Value,
Suit: z.Suit,
drawn: z.drawn,
}
return card
}
//Equals function compares two cards with each other.
func (z *Card) Equals(card *Card) bool {
if z.Code == card.Code && z.Value == card.Value && z.Suit == card.Suit && z.drawn == card.drawn {
return true
}
return false
}
func (z *cardError) Error() string {
return fmt.Sprintf(`Card suit (%s), value (%s): %s`, z.suit, z.value, z.err)
}