-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.go
285 lines (240 loc) · 5.87 KB
/
game.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import "time"
// Stack represents a number of cards in a particular order
type Stack struct {
Cards []*Card
}
// Push adds a new card to the top of the stack
func (s *Stack) Push(card *Card) {
s.Cards = append(s.Cards, card)
}
// Top gets the top card of a stack, or nil if the stack is empty
func (s *Stack) Top() *Card {
if len(s.Cards) == 0 {
return nil
}
return s.Cards[len(s.Cards)-1]
}
// Pop removes the top card of a stack, turning up any card immediately underneath and returning the removed card
func (s *Stack) Pop() *Card {
if len(s.Cards) == 0 {
return nil
}
ret := s.Top()
s.Cards = s.Cards[0 : len(s.Cards)-1]
if len(s.Cards) > 0 {
s.Cards[len(s.Cards)-1].TurnFaceUp()
}
return ret
}
// Contains will return true if the stack contains the specified card
func (s *Stack) Contains(card *Card) bool {
for _, c := range s.Cards {
if cardEquals(c, card) {
return true
}
}
return false
}
// Game represents a full solitaire game, starting from a standard draw
type Game struct {
Hand *Deck
Draw1, Draw2, Draw3 *Card
Drawn *Deck
Build1 *Stack
Build2 *Stack
Build3 *Stack
Build4 *Stack
Stack1 *Stack
Stack2 *Stack
Stack3 *Stack
Stack4 *Stack
Stack5 *Stack
Stack6 *Stack
Stack7 *Stack
OnWin func()
}
func pushToStack(s *Stack, d *Deck, count int) {
for i := 0; i < count; i++ {
card := d.Pop()
if i == count-1 {
card.FaceUp = true
}
s.Push(card)
}
}
func (g *Game) deal() {
pushToStack(g.Stack1, g.Hand, 1)
pushToStack(g.Stack2, g.Hand, 2)
pushToStack(g.Stack3, g.Hand, 3)
pushToStack(g.Stack4, g.Hand, 4)
pushToStack(g.Stack5, g.Hand, 5)
pushToStack(g.Stack6, g.Hand, 6)
pushToStack(g.Stack7, g.Hand, 7)
}
// AutoBuild attempts to place the passed card onto one of the build stacks
func (g *Game) AutoBuild(c *Card) {
for _, b := range []*Stack{g.Build1, g.Build2, g.Build3, g.Build4} {
if !g.ruleCanMoveToBuild(b, c) {
continue
}
g.MoveCardToBuild(b, c)
break
}
}
// ResetDraw resets the draw pile to be completely available (no cards drawn)
func (g *Game) ResetDraw() {
for ; len(g.Hand.Cards) > 0; g.DrawThree() {
}
// Reset the draw pile
g.DrawThree()
}
func (g *Game) drawCard() *Card {
if len(g.Hand.Cards) == 0 {
return nil
}
popped := g.Hand.Pop()
popped.FaceUp = true
g.Drawn.Push(popped)
return popped
}
// DrawThree draws three cards from the deck and adds them to the draw pile(s).
// If there are no cards available to be drawn it will cycle back to the beginning and draw the first three.
func (g *Game) DrawThree() {
if len(g.Hand.Cards) == 0 {
g.Draw1 = nil
g.Draw2 = nil
g.Draw3 = nil
g.Hand = g.Drawn
for _, card := range g.Hand.Cards {
card.TurnFaceDown()
}
g.Drawn = &Deck{}
return
}
g.Draw1 = g.drawCard()
g.Draw2 = g.drawCard()
g.Draw3 = g.drawCard()
}
// MoveCardToBuild attempts to move the currently selected card to a build stack.
// If the move is not possible it will return.
func (g *Game) MoveCardToBuild(build *Stack, card *Card) {
if !g.ruleCanMoveToBuild(build, card) {
return
}
g.removeCard(card)
build.Push(card)
if len(g.Build1.Cards) == 13 && len(g.Build2.Cards) == 13 &&
len(g.Build3.Cards) == 13 && len(g.Build4.Cards) == 13 {
if g.OnWin != nil {
g.OnWin()
}
}
}
// MoveCardToStack attempts to move the currently selected card to a table stack.
// If the move is not possible it will return.
func (g *Game) MoveCardToStack(stack *Stack, card *Card) {
if !g.ruleCanMoveToStack(stack, card) {
return
}
oldStack := g.stackForCard(card)
if oldStack == nil {
g.removeCard(card)
stack.Push(card)
return
}
found := false
for _, c := range oldStack.Cards {
if cardEquals(c, card) {
found = true
}
if found {
// noinspection GoDeferInLoop
defer oldStack.Pop()
stack.Push(c)
}
}
}
func (g *Game) stackForCard(card *Card) *Stack {
if g.Stack1.Contains(card) {
return g.Stack1
}
if g.Stack2.Contains(card) {
return g.Stack2
}
if g.Stack3.Contains(card) {
return g.Stack3
}
if g.Stack4.Contains(card) {
return g.Stack4
}
if g.Stack5.Contains(card) {
return g.Stack5
}
if g.Stack6.Contains(card) {
return g.Stack6
}
if g.Stack7.Contains(card) {
return g.Stack7
}
return nil
}
func (g *Game) removeCard(card *Card) {
if cardEquals(card, g.Draw3) {
g.Drawn.Remove(card)
g.Draw3 = nil
} else if cardEquals(card, g.Draw2) {
g.Drawn.Remove(card)
g.Draw2 = nil
} else if cardEquals(card, g.Draw1) {
// TODO what if it's empty - the previous draw?
g.Drawn.Remove(card)
g.Draw1 = nil
} else if cardEquals(card, g.Build1.Top()) {
g.Build1.Pop()
} else if cardEquals(card, g.Build2.Top()) {
g.Build2.Pop()
} else if cardEquals(card, g.Build3.Top()) {
g.Build3.Pop()
} else if cardEquals(card, g.Build4.Top()) {
g.Build4.Pop()
} else if cardEquals(card, g.Stack1.Top()) {
g.Stack1.Pop()
} else if cardEquals(card, g.Stack2.Top()) {
g.Stack2.Pop()
} else if cardEquals(card, g.Stack3.Top()) {
g.Stack3.Pop()
} else if cardEquals(card, g.Stack4.Top()) {
g.Stack4.Pop()
} else if cardEquals(card, g.Stack5.Top()) {
g.Stack5.Pop()
} else if cardEquals(card, g.Stack6.Top()) {
g.Stack6.Pop()
} else if cardEquals(card, g.Stack7.Top()) {
g.Stack7.Pop()
}
}
// NewGame starts a new solitaire game and draws to the standard configuration.
func NewGame() *Game {
return NewGameFromSeed(time.Now().UnixNano())
}
// NewGameFromSeed starts a new solitaire game and draws to the standard configuration.
// The randomness of the desk is seeded using the specified value.
func NewGameFromSeed(seed int64) *Game {
game := &Game{}
game.Hand = NewShuffledDeckFromSeed(seed)
game.Drawn = &Deck{}
game.Stack1 = &Stack{}
game.Stack2 = &Stack{}
game.Stack3 = &Stack{}
game.Stack4 = &Stack{}
game.Stack5 = &Stack{}
game.Stack6 = &Stack{}
game.Stack7 = &Stack{}
game.Build1 = &Stack{}
game.Build2 = &Stack{}
game.Build3 = &Stack{}
game.Build4 = &Stack{}
game.deal()
return game
}