-
Notifications
You must be signed in to change notification settings - Fork 0
/
freecell.py
397 lines (331 loc) · 13.3 KB
/
freecell.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
from card import Card
from deck import Deck
class Freecell:
pile = [[], [], [], [], [], [], [], []]
freecell_deck = Deck(1, 13, 4)
freecell_deck.shuffle()
column = 0
while not freecell_deck.is_empty():
pile[column].append(freecell_deck.draw_card())
column += 1
if column % 8 == 0:
column = 0
foundation = [[], [], [], []]
cell = [[], [], [], []]
def __int__(self, foundation, pile, cell):
"""
Build a deck containing 52 cards
Deal the cards into 8 cascades
Build 4 foundations and 4 cells
"""
self.foundation = foundation
self.pile = pile
self.cell = cell
def print_game(self):
print()
print(" Cells: Foundation:")
#cell and foundation
for i in range(4):
print('%8d' % (i + 1), end='')
print(' ', end='')
for i in range(4):
print('%8d' % (i + 1), end='')
print()
for c in self.cell:
try:
print("%8s" % (c[0]), end='')
except IndexError:
print("%8s" % (''), end='')
print(' ', end='')
for stack in self.foundation:
try:
print("%8s" % (stack[-1]), end='')
except IndexError:
print("%8s" % (''), end='')
print()
print('------------------------------------------------------------------------')
print(" pile")
for i in range(len(self.pile)):
print("%8d" % (i + 1), end='')
print()
max_length = max([len(stack) for stack in self.pile])
for i in range(max_length):
print(' ', end='')
for stack in self.pile:
try:
print("%8s" % (stack[i]), end='')
except IndexError:
print("%8s" % (''), end='')
print()
print('------------------------------------------------------------------------')
########################################
# Check the move whether valid or not
def move_to_foundation(self, p_col, f_col):
'''
moves a card at the end of a column of pile to a foundation
True if the move is valid, False otherwise
'''
the_pile = self.pile[p_col - 1]
the_foundation = self.foundation[f_col - 1]
if the_pile == []:
return False
else:
if the_foundation == []:
if the_pile[-1].face == "A":
return True
else:
return False
else:
if the_foundation[-1].suit == the_pile[-1].suit and the_foundation[
-1].get_face_index() == the_pile[-1].get_face_index() - 1:
return True
else:
return False
def move_to_cell(self, p_col, c_col):
'''
moves a card at the end of a column of pile to a cell
True if the move is valid, False otherwise
'''
the_card = self.pile[p_col - 1]
the_cell = self.cell[c_col - 1]
if the_cell == []:
return True
else:
return False
def move_to_pile(self, c_col, p_col):
'''
moves a card in the cell to a column of pile
True if the move is valid, False otherwise
'''
the_cell = self.cell[c_col - 1]
the_pile = self.pile[p_col - 1]
if the_cell == []:
return False
else:
if the_pile == []:
return True
else:
cell_card = self.cell[c_col - 1][-1]
the_card = self.pile[p_col - 1][-1]
if cell_card.get_face_index() == the_card.get_face_index() - 1 and cell_card.get_color() != the_card.get_color():
return True
else:
return False
def move_in_pile(self, p_col_source, p_col_dest):
'''
move card from one pile column to another
True if the move is valid, False otherwise
'''
the_source = self.pile[p_col_source - 1]
the_dest = self.pile[p_col_dest - 1]
if the_source == []:
return False
else:
if the_dest == []:
return True
else:
the_card = the_source[-1]
dest_card = the_dest[-1]
if the_card.get_face_index() == dest_card.get_face_index() - 1 and dest_card.get_color() != the_card.get_color():
return True
else:
return False
def cell_to_foundation(self, c_col, f_col):
'''
moves a card of cell to a foundation
returns: Boolean (True if the move is valid, False otherwise)
'''
the_cell = self.cell[c_col - 1]
the_foundation = self.foundation[f_col - 1]
if the_cell == []:
return False
else:
if the_foundation == []:
if the_cell[-1].face == "A":
return True
else:
return False
else:
if the_foundation[-1].suit == the_cell[-1].suit and the_foundation[
-1].get_face_index() == the_cell[-1].get_face_index() - 1:
return True
else:
return False
########################################
# Move Card
def p2f(self, p_col, f_col):
if self.move_to_foundation(p_col, f_col) == True:
the_card = self.pile[p_col - 1].pop()
self.foundation[f_col - 1].append(the_card)
else:
print('Invalid move to foundation')
def p2p(self, p_col_source, p_col_dest):
if self.move_in_pile(p_col_source, p_col_dest) == True:
the_card = self.pile[p_col_source - 1].pop()
self.pile[p_col_dest - 1].append(the_card)
else:
print('Invalid move to pile')
def p2c(self, p_col, c_col):
if self.move_to_cell(p_col, c_col) == True:
the_card = self.pile[p_col - 1].pop()
self.cell[c_col - 1].append(the_card)
else:
print('Invalid move to cell')
def c2p(self, c_col, p_col):
if self.move_to_pile(c_col, p_col) == True:
the_card = self.cell[c_col - 1].pop()
self.pile[p_col - 1].append(the_card)
else:
print('Invalid move to pile')
def c2f(self, c_col, f_col):
if self.cell_to_foundation(c_col, f_col) == True:
the_card = self.cell[c_col - 1].pop()
self.foundation[f_col - 1].append(the_card)
else:
print('Invalid move to pile')
def win_game(self):
'''
Check whether the player wins
'''
for i in range(0, 4):
if len(self.foundation[i]) == 13:
print('''
.----------------. .----------------. .-----------------.
| .--------------. || .--------------. || .--------------. |
| | _____ _____ | || | _____ | || | ____ _____ | |
| ||_ _||_ _|| || | |_ _| | || ||_ \|_ _| | |
| | | | /\ | | | || | | | | || | | \ | | | |
| | | |/ \| | | || | | | | || | | |\ \| | | |
| | | /\ | | || | _| |_ | || | _| |_\ |_ | |
| | |__/ \__| | || | |_____| | || ||_____|\____| | |
| | | || | | || | | |
| '--------------' || '--------------' || '--------------' |
'----------------' '----------------' '---------------
''')
return True
else:
return False
def welcome_game(self):
'''
print freecell banner
state the rules of freecell
'''
print('''
o__ __o__/_ o o
<| v <|> <|>
< > / \ / \
| \o__ __o o__ __o o__ __o __o__ o__ __o \o/ \o/
o__/_ | |> /v |> /v |> /> \ /v |> | |
| / \ < > /> // /> // o/ /> // / \ / \
<o> \o/ \o o/ \o o/ <| \o o/ \o/ \o/
| | v\ /v __o v\ /v __o \\ v\ /v __o | |
/ \ / \ <\/> __/> <\/> __/> _\o__</ <\/> __/> / \ / \
''')
print("To Win")
print("\tMove all the cards to the Foundations")
print("Foundation")
print("\tSorted Cards in a suit from Ace to King")
print("pile")
print("\tShuffled 8 piles of cards")
print("Cell")
print("\tEmpty 4 slots to put 1 card each")
print("Rules")
print("\tOnly 1 card be moved a time")
print("\tCard placed in pile should be different color")
print("\tand one less rank e.g. red 3 be placed on black 4")
print("\tAn empty pile can put any card ")
def game_help(self):
'''
parameters: none
returns: nothing
prints the supported commands
'''
print("Input should be")
print("\t p2f #P #F -- Pile to Foundation 1: ")
print("\t e.g. Pile 2 to Foundation: p2f 2 1 ")
print("\t p2p #P #P -- Pile to Pile: ")
print("\t e.g. Pile 2 to Pile 1: p2f 2 1 ")
print("\t p2c #P #C -- Pile to Cell: ")
print("\t e.g. Pile 2 to Cell 1: p2c 2 1 ")
print("\t c2p #C #P -- Cell to Pile: ")
print("\t e.g. Cell 2 to Pile 1: c2p 2 1 ")
print("\t c2f #C #F -- Cell to Foundation: ")
print("\t e.g. Cell 2 to Foundation 1: c2f 2 1 ")
print("\t 'h' for help")
print("\t 'q' to quit")
def main():
import time
print('''
┬ ┬┌─┐┬ ┌─┐┌─┐┌┬┐┌─┐
│││├┤ │ │ │ ││││├┤
└┴┘└─┘┴─┘└─┘└─┘┴ ┴└─┘ ......
''')
time.sleep(2)
newgame = Freecell()
newgame.welcome_game()
time.sleep(2)
newgame.game_help()
time.sleep(2)
while newgame.win_game() == False:
newgame.print_game()
user_input = input("Please type in your move: ")
user_input = user_input.strip()
user_input_list = user_input.split()
if len(user_input_list) > 0:
mode = user_input_list[0]
if mode == 'p2f':
if user_input_list[1] in "1 2 3 4 5 6 7 8".split() and user_input_list[
2] in "1 2 3 4".split():
p_col = int(user_input_list[1])
f_col = int(user_input_list[2])
newgame.p2f(p_col, f_col)
else:
print("Invalid column range")
elif mode == 'p2p':
if user_input_list[1] in "1 2 3 4 5 6 7 8".split() and user_input_list[
2] in "1 2 3 4 5 6 7 8".split():
p_col_source = int(user_input_list[1])
p_col_dest = int(user_input_list[2])
newgame.p2p(p_col_source, p_col_dest)
else:
print("Invalid column range")
elif mode == 'p2c':
if user_input_list[1] in "1 2 3 4 5 6 7 8".split() and user_input_list[
2] in "1 2 3 4".split():
p_col = int(user_input_list[1])
c_col = int(user_input_list[2])
newgame.p2c(p_col, c_col)
else:
print("Invalid column range")
elif mode == 'c2p':
if user_input_list[1] in "1 2 3 4".split() and \
user_input_list[2] in "1 2 3 4 5 6 7 8".split():
c_col = int(user_input_list[1])
p_col = int(user_input_list[2])
newgame.c2p(c_col,p_col)
else:
print("Invalid column range")
elif mode == 'c2f':
if user_input_list[1] in "1 2 3 4".split() and user_input_list[2] in "1 2 3 4".split():
c_col = int(user_input_list[1])
f_col = int(user_input_list[2])
newgame.c2f(c_col,f_col)
else:
print("Invalid column range")
elif mode == 'q':
break
elif mode == 'h':
newgame.game_help()
else:
print('Unknown command:', mode)
else:
print("Unknown Command:", user_input)
print('''
________ __ __ __ __
/_ __/ /_ ____ _____ / /__ \ \/ /___ __ __ / /
/ / / __ \/ __ `/ __ \/ //_/ \ / __ \/ / / / / /
/ / / / / / /_/ / / / / ,< / / /_/ / /_/ / /_/
/_/ /_/ /_/\__,_/_/ /_/_/|_| /_/\____/\__,_/ (_)
''')
if __name__ == '__main__':
main()