-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
112 lines (98 loc) · 3.63 KB
/
models.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
package models
import (
"fmt"
"strings"
)
type Deck struct {
// ID uniquely identifies the deck from any other deck.
ID string
// Shuffled is whether the deck was shuffled or not when it was created.
Shuffled bool
// Cards that can still be drawn.
// The next card to be drawn is at position 0, if there is one,
// then the one at position 1, if there is one, and so on.
Cards []Card
}
type Card struct {
// Code is the unique identifier of the type of card,
// e.g. "AS" is Ace of Spades, "10H" is 10 of Hearts, etc.
Code string
// Value is the face value of the card e.g. "ACE", "10", "QUEEN".
Value string
// Suit can be "CLUBS", "HEARTS", "SPADES" or "DIAMONDS".
Suit string
}
var FullDeck52 = []Card{
{Code: "AS", Value: "ACE", Suit: "SPADES"},
{Code: "2S", Value: "2", Suit: "SPADES"},
{Code: "3S", Value: "3", Suit: "SPADES"},
{Code: "4S", Value: "4", Suit: "SPADES"},
{Code: "5S", Value: "5", Suit: "SPADES"},
{Code: "6S", Value: "6", Suit: "SPADES"},
{Code: "7S", Value: "7", Suit: "SPADES"},
{Code: "8S", Value: "8", Suit: "SPADES"},
{Code: "9S", Value: "9", Suit: "SPADES"},
{Code: "10S", Value: "10", Suit: "SPADES"},
{Code: "JS", Value: "JACK", Suit: "SPADES"},
{Code: "QS", Value: "QUEEN", Suit: "SPADES"},
{Code: "KS", Value: "KING", Suit: "SPADES"},
{Code: "AD", Value: "ACE", Suit: "DIAMONDS"},
{Code: "2D", Value: "2", Suit: "DIAMONDS"},
{Code: "3D", Value: "3", Suit: "DIAMONDS"},
{Code: "4D", Value: "4", Suit: "DIAMONDS"},
{Code: "5D", Value: "5", Suit: "DIAMONDS"},
{Code: "6D", Value: "6", Suit: "DIAMONDS"},
{Code: "7D", Value: "7", Suit: "DIAMONDS"},
{Code: "8D", Value: "8", Suit: "DIAMONDS"},
{Code: "9D", Value: "9", Suit: "DIAMONDS"},
{Code: "10D", Value: "10", Suit: "DIAMONDS"},
{Code: "JD", Value: "JACK", Suit: "DIAMONDS"},
{Code: "QD", Value: "QUEEN", Suit: "DIAMONDS"},
{Code: "KD", Value: "KING", Suit: "DIAMONDS"},
{Code: "AC", Value: "ACE", Suit: "CLUBS"},
{Code: "2C", Value: "2", Suit: "CLUBS"},
{Code: "3C", Value: "3", Suit: "CLUBS"},
{Code: "4C", Value: "4", Suit: "CLUBS"},
{Code: "5C", Value: "5", Suit: "CLUBS"},
{Code: "6C", Value: "6", Suit: "CLUBS"},
{Code: "7C", Value: "7", Suit: "CLUBS"},
{Code: "8C", Value: "8", Suit: "CLUBS"},
{Code: "9C", Value: "9", Suit: "CLUBS"},
{Code: "10C", Value: "10", Suit: "CLUBS"},
{Code: "JC", Value: "JACK", Suit: "CLUBS"},
{Code: "QC", Value: "QUEEN", Suit: "CLUBS"},
{Code: "KC", Value: "KING", Suit: "CLUBS"},
{Code: "AH", Value: "ACE", Suit: "HEARTS"},
{Code: "2H", Value: "2", Suit: "HEARTS"},
{Code: "3H", Value: "3", Suit: "HEARTS"},
{Code: "4H", Value: "4", Suit: "HEARTS"},
{Code: "5H", Value: "5", Suit: "HEARTS"},
{Code: "6H", Value: "6", Suit: "HEARTS"},
{Code: "7H", Value: "7", Suit: "HEARTS"},
{Code: "8H", Value: "8", Suit: "HEARTS"},
{Code: "9H", Value: "9", Suit: "HEARTS"},
{Code: "10H", Value: "10", Suit: "HEARTS"},
{Code: "JH", Value: "JACK", Suit: "HEARTS"},
{Code: "QH", Value: "QUEEN", Suit: "HEARTS"},
{Code: "KH", Value: "KING", Suit: "HEARTS"},
}
// Converts card-codes string to card objects,
// e.g. "AS,KD,AC" becomes those 3 cards.
// Errors if a card code is invalid.
func CardsFromCodes(cardCodes string) ([]Card, error) {
// TODO we could have this mapping calculated once in the package instead of every call; use init() func?
cardFromCode := make(map[string]Card, len(FullDeck52))
for _, card := range FullDeck52 {
cardFromCode[card.Code] = card
}
codes := strings.Split(cardCodes, ",")
cards := make([]Card, len(codes))
for i, code := range codes {
card, ok := cardFromCode[code]
if !ok {
return nil, fmt.Errorf("card invalid with code '%s'", code)
}
cards[i] = card
}
return cards, nil
}