-
Notifications
You must be signed in to change notification settings - Fork 0
/
pile.go
192 lines (161 loc) · 4.41 KB
/
pile.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package deckofcards
import (
"fmt"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/twinj/uuid"
)
//Pile is a type that implements the structure of a Draw.
type Pile struct {
cards Cards
Remaining int
PileID string
}
//AddCardsToPile adds all the cards in the cards parameter that are present in the Draw to the pile.
func (z *Pile) AddCardsToPile(draw *Draw, cards Cards) {
if draw != nil && draw.Success && len(draw.Cards) != 0 {
if len(draw.Cards) >= len(cards) && draw.AreAllCardsInThisDraw(cards) {
z.cards = append(z.cards, cards...)
}
}
z.Remaining = len(z.cards)
}
//NewPile creates a new Pile instance an returns a pointer to it.
func NewPile() *Pile {
return &Pile{
PileID: uuid.NewV4().String(),
cards: make(Cards, 0),
Remaining: 0,
}
}
func (z *Pile) String() string {
var printString []string
printString = append(printString, fmt.Sprintf("PileID: %s", z.PileID))
printString = append(printString, z.cards.String())
return strings.Join(printString, "\n")
}
//RetrieveCardsInPile returns a copy of all cards in the pile
func (z *Pile) RetrieveCardsInPile() (cards Cards) {
for _, pileCard := range z.cards {
cards = append(cards, pileCard.cloneCard())
}
return
}
//PickAmountOfCardsFromBottomOfPile returns a Draw of the amount selected from the bottom of the pile.
func (z *Pile) PickAmountOfCardsFromBottomOfPile(amount int) *Draw {
draw := &Draw{
Success: false,
Cards: make(Cards, 0),
Remaining: 0,
}
if amount <= 0 {
return draw
}
if amount >= z.Remaining {
amount = z.Remaining
}
sliceOfCards := z.cards[z.Remaining-amount:]
draw.Cards = append(draw.Cards, sliceOfCards...)
z.cards = z.cards[:z.Remaining-amount]
z.Remaining = len(z.cards)
draw.Remaining = len(draw.Cards)
draw.Success = true
return draw
}
//PickAmountOfCardsFromTopOfPile returns a Draw of the amount selected from the top of the pile.
func (z *Pile) PickAmountOfCardsFromTopOfPile(amount int) *Draw {
draw := &Draw{
Success: false,
Cards: make(Cards, 0),
Remaining: 0,
}
if amount <= 0 {
return draw
}
if amount >= z.Remaining {
amount = z.Remaining
}
sliceOfCards := z.cards[:amount]
draw.Cards = append(draw.Cards, sliceOfCards...)
z.cards = z.cards[amount:]
z.Remaining = len(z.cards)
draw.Remaining = len(draw.Cards)
draw.Success = true
return draw
}
//PickAllCardsFromPile returns all the cards in the pile as a Draw and clears the pile.
func (z *Pile) PickAllCardsFromPile() *Draw {
draw := &Draw{
Success: false,
Cards: make(Cards, 0),
Remaining: 0,
}
if len(z.cards) > 0 {
draw.Success = true
draw.Remaining = len(z.cards)
draw.Cards = append(draw.Cards, z.cards...)
}
z.cards = make(Cards, 0)
draw.Success = true
return draw
}
//GetCardsFromPile returns the specified cards from the pile as a Draw.
func (z *Pile) GetCardsFromPile(cards Cards) *Draw {
draw := &Draw{
Success: false,
Cards: make(Cards, 0),
Remaining: 0,
}
if len(z.cards) > 0 && len(cards) <= len(z.cards) {
var tempCards Cards
for _, card := range cards {
for i, pileCard := range z.cards {
if card.Equals(pileCard) {
tempCards = append(tempCards, pileCard)
z.cards = append(z.cards[:i], z.cards[i+1:]...)
z.Remaining--
}
}
}
if len(cards) == len(tempCards) {
draw.Success = true
draw.Remaining = len(cards)
draw.Cards = tempCards
}
}
return draw
}
//ShufflePile returns a shuffled pile
func ShufflePile(pile *Pile) *Pile {
pile.cards = shuffle(pile.cards)
return pile
}
//GetCardAtID returns the card at the id from the pile.
func (z *Pile) GetCardAtID(index int) (*Draw, error) {
if index > len(z.cards)-1 || index < 0 {
return nil, fmt.Errorf("Index out of bounds")
}
draw := &Draw{
Success: false,
Cards: make(Cards, 0),
Remaining: 0,
}
draw.Cards = append(draw.Cards, z.cards[index])
z.cards = append(z.cards[:index], z.cards[index+1:]...)
z.Remaining = len(z.cards)
draw.Remaining = len(draw.Cards)
draw.Success = true
return draw, nil
}
//UnmarshalPile unmarshals a byte array into a pointer to a Pile for internal use.
func UnmarshalPile(data []byte) (*Pile, error) {
var r Pile
json := jsoniter.ConfigCompatibleWithStandardLibrary
err := json.Unmarshal(data, &r)
return &r, err
}
//Marshal marshals a pointer to a Pile into a byte array for transmission.
func (z *Pile) Marshal() ([]byte, error) {
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(z)
}