-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCard.go
122 lines (108 loc) · 2.46 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
package main
import (
"fmt"
"strconv"
"strings"
)
// Card - type that has a suit, value, and rank (AJQK or numeric).
type Card struct {
value int
suit string
rank string
}
// CreateDeckOfCards - does a linear map of suits and values and returns a deck
// of 52 cards.
func CreateDeckOfCards() (deck Deck) {
suits := []string{"Clubs", "Diamonds", "Hearts", "Spades"}
values := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
deck = make(Deck, 0)
for _, suit := range suits {
for _, value := range values {
deck = append(deck, GetCardWithRank(value, suit))
}
}
return
}
// GetCardWithRank - appends the face value to the Card object.
func GetCardWithRank(value int, suit string) Card {
switch value {
case 1:
return Card{value, suit, "A"}
case 11:
return Card{value, suit, "J"}
case 12:
return Card{value, suit, "Q"}
case 13:
return Card{value, suit, "K"}
default:
return Card{value, suit, strconv.Itoa(value)}
}
}
// PrettyPrintCard - pretty prints a card into "RANK-SUIT".
func (c Card) PrettyPrintCard() string {
return c.rank + c.suit[:1]
}
func (c *Card) String() string {
return c.PrettyPrintCard()
}
// GetCardFromPrettyPrint - transforms a card in the format "RANK-SUIT" into
// a card.
func GetCardFromPrettyPrint(p string) (card Card, err error) {
var suit string
var value int
if len(p) < 2 {
err = fmt.Errorf("unidentifed card. Try again")
return
}
if p[:2] == "10" {
suit, err = GetCardSuit(p[2:])
if err != nil {
err = fmt.Errorf("unidentified card suit. Try again")
}
value, err = GetCardValue(p[:2])
if err != nil {
err = fmt.Errorf("could not get the value of this card. Try again")
}
card = GetCardWithRank(value, suit)
return
}
s := strings.Split(p, "")
value, err = GetCardValue(s[0])
suit, err = GetCardSuit(s[1])
card = GetCardWithRank(value, suit)
return
}
// GetCardSuit - gets the card's suit from the pretty printed string.
func GetCardSuit(s string) (suit string, err error) {
switch s {
case "C":
suit = "Clubs"
case "D":
suit = "Diamonds"
case "H":
suit = "Hearts"
case "S":
suit = "Spades"
default:
return suit, fmt.Errorf("unidentified card suit. Try again")
}
return
}
// GetCardValue - gets the card's value from the pretty printed string.
func GetCardValue(s string) (value int, err error) {
switch s {
case "A":
value = 1
case "J":
value = 11
case "Q":
value = 12
case "K":
value = 13
case "10":
value = 10
default:
value, err = strconv.Atoi(s)
}
return
}