-
Notifications
You must be signed in to change notification settings - Fork 1
/
solitaire.py
403 lines (356 loc) · 13.7 KB
/
solitaire.py
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env python2
import pygame
from os import path
from random import shuffle
from pygame.locals import *
import numpy as np
CARDWIDTH = 73
CARDHEIGHT = 97
OFFSET = 16
MARGIN = 4
WIDTH = (MARGIN + CARDWIDTH) * 7 - MARGIN
HEIGHT = (7 + 14) * OFFSET + 2 * (MARGIN + CARDHEIGHT) - 3
CURSOR_COLOR = pygame.Color(0, 0, 255)
CURSOR_SELECTED_COLOR = pygame.Color(255, 0, 255)
SIZE = 100
class Card:
def __init__(self, suit, value, hidden=True):
self.suit = suit
self.value = value
self.hidden = hidden
def __repr__(self):
return "%s %s Hidden: %s" % (self.suit, self.value, self.hidden)
class RowStack:
def __init__(self):
self.cards = []
def add(self, cards):
if self.accept(cards[0]):
for c in cards: c.hidden = False
self.cards.extend(cards)
return True
def accept(self, card):
last = self.cards[-1] if len(self.cards) > 0 else None
return (not last and card.value == 13 or
last and
not last.hidden and
last.suit % 2 != card.suit % 2 and
last.value == card.value+1)
class GoalStack:
def __init__(self):
self.cards = []
self.suit = None
def add(self, card):
if self.accept(card):
card.hidden = False
self.cards.append(card)
return True
def accept(self, card):
if not self.suit and card.value == 1:
self.suit = card.suit
return True
else:
last = self.cards[-1] if len(self.cards) > 0 else None
return (not last and
card.value == 1 or
last and
last.suit == card.suit and
last.value == card.value-1)
class Deck:
def __init__(self):
cards = []
for suit in range(4):
for value in range(1, 14):
cards.append(Card(suit, value))
shuffle(cards)
self.rows = [RowStack() for _ in range(7)]
for i in range(7):
for j in range(i, 7):
self.rows[j].cards.append(cards.pop(0))
for row in self.rows:
row.cards[-1].hidden = False
self.goals = [GoalStack() for _ in range(4)]
self.deck = cards
self.showing = []
self.showed = []
def deal(self):
cards = self.deck[:3]
del self.deck[:3]
if self.showing:
self.showed.extend(self.showing)
self.showing = cards
if not cards:
self.deck = self.showed
self.showed = []
def stack(self, stack):
return self.rows[stack]
class Cursor:
def __init__(self, x, y):
self.x = x
self.y = y
self.nCards = 1
class Selected:
def __init__(self, x, y, nCards, cards):
self.x = x
self.y = y
self.nCards = nCards
self.cards = cards
class Solitaire:
def __init__(self, size=SIZE):
self.cards = [[pygame.image.load(path.join('cards', '{0:02d}'.format(value) + suit + ".gif"))
for value in range(1, 14)]
for suit in ['c', 'd', 's', 'h']]
self.backside = pygame.image.load(path.join('cards', 'back192.gif'))
self.bottom = pygame.image.load(path.join('cards', 'bottom01-n.gif'))
pygame.init()
self.screen = pygame.display.set_mode((size, size))
self.reset()
background = pygame.Surface((WIDTH, HEIGHT))
self.background = background.convert()
self.step()
def draw(self):
self.background.blit(self.backside, (0, 0))
if self.deck.showing:
x = 0
for c in self.deck.showing:
self.background.blit(self.cards[c.suit][c.value - 1], ((MARGIN + CARDWIDTH) + x, 0))
x += OFFSET
else:
self.background.blit(self.bottom, (MARGIN + CARDWIDTH, 0))
for i, s in enumerate(self.deck.goals):
if s.cards:
card = s.cards[-1]
img = self.cards[card.suit][card.value - 1]
else:
img = self.bottom
self.background.blit(img, ((MARGIN + CARDWIDTH) * (i + 3), 0))
for i, r in enumerate(self.deck.rows):
y = 0
for c in r.cards:
card = self.backside if c.hidden else self.cards[c.suit][c.value - 1]
self.background.blit(card, ((MARGIN + CARDWIDTH) * i, (2 * MARGIN + CARDHEIGHT) + y))
y += OFFSET
if(self.selected): self.draw_cursor(self.selected, CURSOR_SELECTED_COLOR)
self.draw_cursor(self.cursor)
def right(self):
if self.cursor.x == 0 and self.cursor.y == 0 and not self.deck.showing:
self.cursor.x = 3
elif self.cursor.x == 1 and self.cursor.y == 0:
self.cursor.x = 3
else:
self.cursor.x = min(self.cursor.x + 1, 6)
self.cursor.nCards = 1
def left(self):
if self.cursor.x == 3 and self.cursor.y == 0 and not self.deck.showing:
self.cursor.x = 0
elif self.cursor.x == 3 and self.cursor.y == 0:
self.cursor.x = 1
else:
self.cursor.x = max(self.cursor.x - 1, 0)
self.cursor.nCards = 1
def down(self):
if self.cursor.y == 1:
self.cursor.nCards = max(self.cursor.nCards - 1, 1)
self.cursor.y = 1
def up(self):
if (not self.selected and
self.cursor.nCards < len(self.deck.stack(self.cursor.x).cards) and
self.cursor.y == 1 and
not self.deck.stack(self.cursor.x).cards[-(self.cursor.nCards + 1)].hidden):
self.cursor.nCards += 1
else:
if not self.deck.showing and (self.cursor.x == 1 or self.cursor.x == 2):
self.cursor.x = 0
elif self.cursor.x == 2: # The gap between the main deck and the four goal piles
self.cursor.x = 1
self.cursor.y = 0
self.cursor.nCards = 1
def select(self):
if self.cursor.y == 0:
if self.cursor.x == 0:
self.deck.deal()
self.selected = None
return
elif self.cursor.x == 1:
self.selected = Selected(1, 0, 1, self.deck.showing)
return
if self.selected:
selected_cards = self.selected.cards[-self.selected.nCards:]
if self.cursor.y == 0 and len(selected_cards) == 1:
if self.deck.goals[self.cursor.x - 3].add(selected_cards[0]):
del self.selected.cards[-self.selected.nCards:]
if self.selected.cards != self.deck.goals[self.selected.x - 3].cards:
self.score += 10
else:
if self.deck.rows[self.cursor.x].add(selected_cards):
del self.selected.cards[-self.selected.nCards:]
if self.selected.cards and self.selected.cards == self.deck.showing:
self.score += 5
elif self.selected.cards and self.selected.cards == self.deck.goals[self.selected.x - 3].cards:
self.score -= 15
if self.score < 0: self.score = 0
self.selected = None
else:
cards = (self.deck.goals[self.cursor.x - 3].cards
if self.cursor.y == 0
else self.deck.rows[self.cursor.x].cards)
if cards:
if cards[-1].hidden:
cards[-1].hidden = False
self.score += 5
else:
self.selected = Selected(self.cursor.x,
self.cursor.y,
self.cursor.nCards,
cards)
def reset(self):
self.cursor = Cursor(0, 0)
self.selected = None
self.score = 0
self.deck = Deck()
self.last_move = ()
self.last_score = 0
self.fails = 0
def draw_cursor(self, cursor, color=CURSOR_COLOR):
y = cursor.y * (2 * MARGIN + CARDHEIGHT)
if cursor.y == 1:
y += OFFSET * max(len(self.deck.stack(cursor.x).cards) - cursor.nCards, 0)
x = cursor.x * (MARGIN + CARDWIDTH)
if cursor.y == 0 and cursor.x == 1:
x += OFFSET * (len(self.deck.showing) - 1)
pygame.draw.rect(self.background,
color,
pygame.Rect(x,
y,
CARDWIDTH,
CARDHEIGHT + OFFSET * (cursor.nCards - 1)),
5)
def step(self, action=None):
score = self.score
terminal = False
if self.fails >= 20:
terminal = True
elif not action:
pass
elif action == 'up':
self.up()
elif action == 'down':
self.down()
elif action == 'left':
self.left()
elif action == 'right':
self.right()
elif action == 'select':
self.select()
self.background.fill((0, 130, 0))
self.draw()
asd = pygame.transform.smoothscale(self.background, (SIZE, SIZE))
self.screen.blit(asd, (0, 0))
pygame.display.flip()
image = pygame.surfarray.array3d(self.screen)
return np.transpose(image, (1, 0, 2)), self.score-score, terminal
def play(self):
while 1:
event = pygame.event.wait()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.left()
elif event.key == pygame.K_RIGHT:
self.right()
elif event.key == pygame.K_UP:
self.up()
elif event.key == pygame.K_DOWN:
self.down()
elif event.key == pygame.K_SPACE:
self.select()
elif event.key == pygame.K_RETURN:
s = self.bot()
if s == 'left':
self.left()
elif s == 'right':
self.right()
elif s == 'down':
self.down()
elif s == 'up':
self.up()
elif s == 'select':
self.select()
elif event.type == pygame.QUIT:
pygame.quit()
exit()
self.background.fill((0, 130, 0))
self.draw()
self.screen.blit(self.background, (0, 0))
pygame.display.flip()
def bot(self):
if self.selected:
if self.selected.nCards == 1: # Move selected card to goal
card = self.selected.cards[-self.selected.nCards]
for i, g in enumerate(self.deck.goals):
if g.accept(card):
return self.botmove(3 + i, 0 , 1)
for i, r in enumerate(self.deck.rows):
if r.accept(card):
return self.botmove(i, 1, 1)
else: # Move selected cards to other stack
card = self.selected.cards[-self.selected.nCards]
for i, r in enumerate(self.deck.rows):
if r.accept(card):
return self.botmove(i, 1, 1)
else:
for i, r in enumerate(self.deck.rows):
if self.deck.rows[i].cards:
if self.deck.rows[i].cards[-1].hidden:
return self.botmove(i, 1, 1)
for i, r in enumerate(self.deck.rows):
for j, g in enumerate(self.deck.goals):
if self.deck.rows[i].cards:
card = self.deck.rows[i].cards[-1]
if g.accept(card):
return self.botmove(i, 1, 1)
if self.deck.showing:
card = self.deck.showing[-1]
for i, r in enumerate(self.deck.rows):
if r.accept(card):
return self.botmove(1, 0, 1)
for i, g in enumerate(self.deck.goals):
if g.accept(card):
return self.botmove(1, 0, 1)
for i, r in enumerate(self.deck.rows):
if self.deck.rows[i].cards:
for n, c in enumerate(self.deck.rows[i].cards):
if not c.hidden:
for j, rr in enumerate(self.deck.rows):
if j == i: continue
card = self.deck.rows[i].cards[n]
if rr.accept(card):
nCards = len(self.deck.rows[i].cards) - n
if self.last_move == (i, 1):
continue
return self.botmove(i, 1, nCards)
return self.botmove(0, 0, 1)
def botmove(self, x, y, nCards):
if self.cursor.y < y:
return 'down'
elif self.cursor.y > y:
return 'up'
else:
if self.cursor.x < x:
return 'right'
elif self.cursor.x > x:
return 'left'
else:
if self.cursor.nCards < nCards:
return 'up'
else:
self.last_move = (x, y)
if self.last_score == self.score:
self.fails += 1
else:
self.fails = 0
self.last_score = self.score
return 'select'
def main():
sol = Solitaire(WIDTH)
sol.play()
if __name__ == '__main__':
main()