-
Notifications
You must be signed in to change notification settings - Fork 1
/
freecell.py
executable file
·1000 lines (878 loc) · 33.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import copy
import os
import sqlite3
import pprint
from colorize import colorize
from carddeck import Card, CardStack, Deck, CardSuit, CardRank
BANNER = """
.*.* .* .*
.* .* .*.* .*.* .*.* .*.*.* .*.* .* .*
.*.*.*.* .*.* .*.*.*.* .*.*.*.* .* .*.*.*.* .* .*
.* .* .* .* .* .* .* .*
.* .* .*.*.* .*.*.* .*.*.* .*.*.* .* .*
by @katylava
"""
class FreecellCard(Card):
def __init__(self, rank, suit):
super(FreecellCard, self).__init__(rank, suit)
def draw(self, width=8):
color = self.suit.color
text = '{:>2}{:<2} '.format(self.rank.c, self.suit.filled_symbol)
return colorize(text.rjust(width), fg=color, bg='white', var='und', bgalt=True)
class FreecellDeck(Deck):
"""
>>> deck = FreecellDeck()
>>> len(deck.cards)
52
>>> card = deck.next()
>>> isinstance(card, FreecellCard)
True
"""
def __init__(self):
super(FreecellDeck, self).__init__()
self._mapcards()
def loads(self, string):
super(FreecellDeck, self).loads(string)
self._mapcards()
def _mapcards(self):
basic_cards = self.cards
self.cards = [FreecellCard(c.rank, c.suit) for c in basic_cards]
class NoFreecellsError(Exception):
pass
class FreeCellOccupiedError(Exception):
pass
class InvalidColumnCardError(Exception):
pass
class InvalidColumnStackError(Exception):
pass
class InvalidFoundationCardError(Exception):
pass
class FreecellInvalidMoveError(Exception):
pass
class FoundationPile(CardStack):
"""
>>> deck = FreecellDeck()
>>> fp = FoundationPile('Hearts')
>>> ace_hearts = FreecellCard(rank=CardRank('Ace'), suit=CardSuit('Hearts'))
>>> two_hearts = FreecellCard(rank=CardRank('Two'), suit=CardSuit('Hearts'))
>>> fp.next_card() == ace_hearts
True
>>> fp.add_card(ace_hearts)
>>> fp.next_card() == two_hearts
True
>>> fp.add_card(two_hearts)
>>> fp.rank() == CardRank('Two')
True
"""
def __init__(self, suit):
super(FoundationPile, self).__init__([])
self.suit = CardSuit(suit)
def rank(self):
top = self.top_card()
if top:
return top.rank
else:
return None
def add_card(self, card, force=False):
if isinstance(card, CardStack):
card = card.top_card()
if card == self.next_card():
self = self + card
else:
msg = "{} on {}".format(card, self.top_card())
raise InvalidFoundationCardError(msg)
def next_card(self):
top = self.top_card()
if not top:
return FreecellCard(rank=CardRank('Ace'), suit=self.suit)
else:
if top.rank.label == 'King':
return None
else:
return FreecellCard(top.rank.next_rank(), suit=self.suit)
def __repr__(self):
if self.length == 0:
return self.suit.symbol
else:
return self.top_card().__repr__()
class AltDescCardColumn(CardStack):
"""
>>> jc = FreecellCard('J','c')
>>> qh = FreecellCard('Q','h')
>>> col = AltDescCardColumn()
>>> col.add_card(qh)
>>> col.top_card().rank.prev_rank().label
'Jack'
>>> jc.rank == col.top_card().rank.prev_rank()
True
>>> col.add_card(jc)
>>> col.valid()
True
>>> td = FreecellCard(10,'d')
>>> col.add_card(td)
>>> col.valid()
True
>>> stack = col.slice_stack(jc)
>>> stack.top_card() == td
True
>>> stack.bottom_card() == jc
True
>>> deck = Deck()
>>> deck.shuffle()
>>> fresh = AltDescCardColumn(deck.cards[:5])
>>> fresh.add_stack(stack, force=True)
>>> fresh.top_stack() == stack
True
>>> fresh.add_card(FreecellCard(9,'s'))
>>> fresh.top_stack().length
3
>>> fresh.top_stack().bottom_card() == jc
True
>>> js = FreecellCard('J','s')
>>> fresh.top_stack_for(js).bottom_card() == td
True
>>> print(fresh.top_stack_for(FreecellCard(2,'s')))
None
>>> print(fresh.top_stack_for(FreecellCard('k','s')))
None
>>> fresh.remove_top_stack_for(js)
10♢, 9♠
>>> fresh.top_card() == jc
True
"""
def top_stack(self, length=None):
clone = copy.deepcopy(self)
while not clone.valid() or (length and clone.length > length):
clone.cards.pop(0)
return clone
def add_card(self, card, force=False):
if force or self.allow_card(card):
self = self + card
else:
msg = "{} on {}".format(card, self.top_card())
raise InvalidColumnCardError(msg)
def add_stack(self, stack, force=False):
if isinstance(stack, AltDescCardColumn):
if force or self.allow_stack(stack):
self = self + stack
else:
raise InvalidColumnStackError
def allow_stack(self, stack):
return self.allow_card(stack.bottom_card()) and stack.valid()
def allow_card(self, card):
top = self.top_card()
if not top:
return True
else:
return card.rank == top.rank.prev_rank() \
and not card.is_same_color_as(top)
def top_stack_for(self, card):
stack = self.top_stack()
if card:
temp = self.__class__([card])
while stack.length > 0 and not temp.allow_stack(stack):
stack.cards.pop(0)
return None if stack.length == 0 else stack
def remove_top_stack(self, length=None):
bottom_card = self.top_stack(length).bottom_card()
return self.slice_stack(bottom_card)
def remove_top_stack_for(self, card):
bottom_card = self.top_stack_for(card).bottom_card()
return self.slice_stack(bottom_card)
def valid(self):
temp = self.__class__([])
for c in self.cards:
try:
temp.add_card(c)
except InvalidColumnCardError:
return False
return True
class Freecells:
"""
>>> fcs = Freecells()
>>> fcs.free()
4
>>> five_hearts = FreecellCard('5', 'Hearts')
>>> fcs.add_card(five_hearts) # returns position card was added to
0
>>> fcs.get_card_at_position(0) == fcs.empty_cell
False
>>> fcs.cells
[5♡, , , ]
>>> fcs.empty_cell == CardStack([], 1)
True
>>> fcs.free()
3
>>> fcs.get_card_at_position(0) == five_hearts
True
>>> two_hearts = FreecellCard('Two', 'Hearts')
>>> fcs.add_card(two_hearts, 3)
3
>>> fcs.free()
2
>>> fcs.first_open()
1
>>> fcs.cells
[5♡, , , 2♡]
>>> fcs.all_cards()
[5♡ , None, None, 2♡ ]
"""
empty_cell = CardStack([], 1)
def __init__(self):
self.cells = []
for n in range(0,4):
self.cells.append(copy.deepcopy(self.empty_cell))
def free(self):
return self.cells.count(self.empty_cell)
def first_open(self):
return self.cells.index(self.empty_cell)
def all_cards(self):
return [stack.top_card() for stack in self.cells]
def add_card(self, card, position=None):
if self.free():
if not position:
position = self.first_open()
if self.cells[position].length == 0:
self.cells[position].add_card(card)
return position
else:
raise FreeCellOccupiedError
else:
raise NoFreecellsError
def get_card_at_position(self, position):
return self.cells[position].top_card()
def remove_card_from_position(self, position):
card = self.cells[position].top_card()
self.cells[position].remove_top_card()
return card
def remove_card(self, card):
position = self.cells.index(CardStack([card], 1))
self.cells[position].remove_top_card()
class FreecellGame(object):
"""
>>> game = FreecellGame()
>>> game.columns[0].length
7
>>> game.columns[7].length
6
>>> game.freecell_count()
4
>>> game.move('ag')
True
>>> game.freecell_count()
3
>>> game.columns[7].cards = []
>>> game.freecell_count()
4
>>> game.move('q;') # test moving from freecell to empty column
True
>>> game.freecell_count()
4
>>> cards = '8C,7D,10H,6D,KS,4S,5S,9C,7H,6H,AC,JS,7C,5H,AD,2D,3S,AS,QS,10S,2S,8H,8S,9S,9D,3C,10C,9H,3D,2H,QD,5C,KD,JD,JC,AH,6C,4H,7S,QC,5D,4C,10D,4D,QH,8D,3H,6S,KH,KC,2C,JH'
>>> game = FreecellGame(cards)
>>> game.deck.reset()
>>> game.deck.__repr__() == cards
True
>>> game.columns[0].bottom_card().label
'Jack of Hearts'
>>> game.columns[3].top_card().label
'Eight of Clubs'
"""
mv_cols = list('asdfjkl;')
mv_cells = list('qwertg')
mv_found = list('uiopyh')
mv_found_order = list('SHDC')
def __init__(self, deck=None):
if isinstance(deck, str):
self.deck = FreecellDeck()
self.deck.loads(deck)
elif isinstance(deck, FreecellDeck):
self.deck = deck
else:
self.deck = FreecellDeck()
self.deck.shuffle()
self.history = []
self.replay = []
initial_state = {
'columns': self.deck.deal(8),
'foundation': {'S':[], 'H':[], 'D':[], 'C':[]},
'freecells': [],
}
self.set_state(initial_state)
self.add_history()
def complete(self):
all_kings = True
for f in self.foundation.values():
if not f.rank() or not f.rank().label == 'King':
all_kings = False
return all_kings
def get_state(self):
return self.__getstate__()
def __getstate__(self):
foundation = {}
for s in self.foundation.keys():
foundation[s] = self.foundation[s].cards[:]
return {
'columns': [c.cards[:] for c in self.columns],
'foundation': foundation,
'freecells': self.freecells.all_cards()
}
def set_state(self, state):
self.__setstate__(state)
def __setstate__(self, state):
self.columns = []
self.freecells = Freecells()
self.foundation = {
'S': FoundationPile('Spades'),
'H': FoundationPile('Hearts'),
'D': FoundationPile('Diamonds'),
'C': FoundationPile('Clubs'),
}
for col in state['columns']:
self.columns.append(AltDescCardColumn(col))
for suit in self.foundation.keys():
self.foundation[suit].cards = state['foundation'][suit]
for pos, card in enumerate(state['freecells']):
if card:
self.freecells.add_card(card, pos)
def add_history(self):
self.history.append(self.get_state())
def undo(self):
if len(self.history) == 1:
self.set_state(self.history[0])
else:
if self.get_state() == self.history[-1]:
self.history.pop()
self.set_state(self.history.pop())
def top_cards(self):
return [c.top_card() for c in self.columns]
def parse_moves(self, moves):
movelist = []
if moves in ['z', 'zz']:
movelist.append(('z','z'))
elif moves in ['m', 'mm']:
self.move_all_to_foundation()
else:
moves = list(moves)
while len(moves) >= 2:
movelist.append((moves.pop(0), moves.pop(0)))
return movelist
def move(self, moves):
movelist = self.parse_moves(moves)
for fr, to in movelist:
card = None
if fr in self.mv_cols:
move_from = self.columns[self.mv_cols.index(fr)]
elif fr in self.mv_cells[:-2]: # the last ones are only for moving 'to'
move_from = self.freecells.cells[self.mv_cells.index(fr)]
card = move_from.top_card()
elif fr in self.mv_found[:-2]: # the last ones are only for moving 'to' 'y', is only for moving to
key = self.mv_found_order[self.mv_found.index(fr)]
move_from = self.foundation[key]
card = move_from.top_card()
elif fr == 'z':
pass
else:
raise FreecellInvalidMoveError("'{}' is not a move".format(fr))
if to in self.mv_cols:
move_to = self.columns[self.mv_cols.index(to)]
elif to in self.mv_cells:
if to in 'tg':
index = self.freecells.first_open()
else:
index = self.mv_cells.index(to)
card = move_from.top_card()
move_to = self.freecells.cells[index]
elif to in self.mv_found:
if to in 'yh':
key = move_from.top_card().suit.c
else:
key = self.mv_found_order[self.mv_found.index(to)]
card = move_from.top_card()
move_to = self.foundation[key]
elif to == 'z':
self.undo()
else:
raise FreecellInvalidMoveError("'{}' is not a move".format(to))
success = False
if card:
try:
success = self.move_card(card, move_from, move_to)
except:
import traceback
message = traceback.format_exc().splitlines()[-1]
print(colorize(message, fg='red'))
print(colorize('move was {}{}'.format(fr,to), fg='cyan'))
success = False
elif to == 'z':
success = True
else:
success = self.move_stack(move_from, move_to)
if success:
self.replay.append('{}{}'.format(fr, to))
self.add_history()
else:
self.set_state(self.history[-1])
return False
return True
def move_card(self, card, move_from, move_to):
try:
move_to.add_card(card)
except:
raise
else:
move_from.remove_top_card()
return True
def move_stack(self, move_from, move_to):
onto_card = move_to.top_card()
if onto_card:
stack = move_from.top_stack_for(onto_card)
else:
length = self.freecell_count()
if move_to.length > 0:
length = length + 1
stack = move_from.top_stack(length)
if not stack:
raise FreecellInvalidMoveError(
"No cards in '{}' for {}".format(move_from, onto_card)
)
if stack.length <= self.freecell_count()+1:
try:
move_to.add_stack(stack)
except InvalidColumnStackError:
raise FreecellInvalidMoveError(
"Can't move stack '{}' to {}".format(stack, onto_card)
)
else:
if onto_card:
move_from.remove_top_stack_for(onto_card)
else:
move_from.remove_top_stack(length)
return True
else:
raise FreecellInvalidMoveError(
"Not enough freecells to move stack '{}'".format(stack)
)
def move_all_to_foundation(self):
def recurse():
next_on_founds = [f.next_card() for f in self.foundation.values()]
for i, card in enumerate(self.top_cards()):
if card and card in next_on_founds:
self.move('{}y'.format(self.mv_cols[i]))
return True
for i, card in enumerate(self.freecells.all_cards()):
if card and card in next_on_founds:
self.move('{}y'.format(self.mv_cells[i]))
return True
while recurse():
recurse()
def freecell_count(self):
return self.freecells.free() + self.top_cards().count(None)
def draw_board(self, cardwidth=8, suit_offset=2):
# the suit symbols take up less than one character, so the offset
# is how much you have to subtract from the width in spots that
# do not have a suit character
if cardwidth - suit_offset < 5:
cardwidth = 5 + suit_offset
longest_column = max([c.length for c in self.columns])
space = " "*((cardwidth - suit_offset)+1)
blank = "_"*(cardwidth - suit_offset) + " "
found = "{}" + "_"*(cardwidth - suit_offset - 1) + " "
movel = "%".center(cardwidth - suit_offset) + " "
board = ''
# empty space before foundations
board = space*4
# foundations
for s in ['S','H','D','C']:
f = self.foundation[s]
if f.length == 0:
board = board + found.format(f.suit.symbol)
else:
board = board + f.top_card().draw(cardwidth) + " "
board = board + "\n"
# free cell move hints
for l in self.mv_cells[:4]:
board = board + movel.replace('%',l)
# foundation move hints
for l in self.mv_found[:4]:
board = board + movel.replace('%',l)
board = board + "\n"
# freecells
for c in self.freecells.cells:
if c.length == 0:
board = board + blank
else:
board = board + c.top_card().draw(cardwidth) + " "
# empty space after freecells
board = board + space*4
board = board + "\n" + space*8
# rows
for n in range(0, longest_column+2):
board = board + "\n"
for letter, c in zip(self.mv_cols, self.columns):
card = c.card_at(n)
if card:
board = board + card.draw(cardwidth) + " "
elif n == c.length + 1:
board = board + movel.replace('%',letter)
else:
board = board + space
return board
class GameHistory(object):
"""
>>> import random
>>> gh = GameHistory('/tmp/testgamehistoryclass{}.db'.format(random.random()))
>>> gh.add({'deck':'blah','time':689832,'moves':98,'replay':'asas','complete':1})
1
>>> gh.add({'deck':'blah2','time':0,'moves':0,'replay':'fgfg','complete':0})
2
>>> unf = gh.unfinished()
>>> len(unf)
1
>>> unf[0][2]
'blah2'
>>> game = FreecellGame()
>>> game.move('aqswdefr')
True
>>> gh.save(game, 30.847, None)
3
>>> game.deck.reset()
>>> record = gh.get(3)
>>> record[0][gh.I_DECK] == str(game.deck)
True
clean up
>>> gh.conn.execute("drop table gamehistory") and True
True
>>> gh.conn.commit()
>>> gh.conn.close()
"""
I_ID = 0
I_DATE = 1
I_DECK = 2
I_TIME = 3
I_MOVES = 4
I_REPLAY = 5
I_COMPL = 6
def __init__(self, db):
db = os.path.expanduser(db)
self.conn = sqlite3.connect(db)
self.conn.execute("""
create table if not exists gamehistory(
id integer primary key,
datetime text,
deck text,
time integer,
moves integer,
replay text,
complete integer
)
""")
self.conn.commit()
def add(self, values):
gameid = values.get('gameid', None)
exists = gameid and self.get(values['gameid'])
if exists:
query = """
update gamehistory
set time={time}, moves={moves}, replay='{replay}', complete={complete}
where id={gameid}
""".format(**values)
else:
query = """
insert into gamehistory
(datetime, deck, time, moves, replay, complete) values
(datetime('now'), '{deck}', {time}, {moves}, '{replay}', {complete})
""".format(**values)
cursor = self.conn.execute(query)
self.conn.commit()
return gameid or cursor.lastrowid
def get(self, gameid):
return self.select("where id={}".format(gameid))
def remove(self, gameid):
query = "delete from gamehistory where id={}".format(gameid)
self.conn.execute(query)
self.conn.commit()
def save(self, game, time=None, gameid=None):
game.deck.reset()
return self.add({
'gameid': gameid or 0,
'deck': game.deck.__repr__(),
'time': int(time) or 0,
'moves': len(game.replay),
'replay': ''.join(game.replay),
'complete': 1 if game.complete() else 0,
})
def besttimes(self, count=10):
return self.select(
"where complete=1 order by time asc, moves asc limit {}".format(count)
)
def leastmoves(self, count=10):
return self.select(
"where complete=1 order by moves asc, time asc limit {}".format(count)
)
def unfinished(self):
return self.select("where complete=0")
def pp(self, results, mark=None):
order = ['I_ID', 'I_DATE', 'I_TIME', 'I_MOVES', 'I_COMPL']
widths = [8, 20, 10, 5, 10]
headings = ['Game #', 'Date/Time', 'Time', 'Moves', 'Complete']
total_width = sum(widths) + 3*len(widths) + 1
col_fmts = ['{N:>{w[N]}}'.replace('N', str(n)) for n in range(0,5)]
line_fmt = '| ' + ' | '.join(col_fmts) + ' |'
border = '-' * total_width
table = [border, line_fmt.format(*headings, w=widths), border]
for r in results:
args = []
highlight = mark and r[mark[0]] == mark[1]
for prop in order:
value = r[getattr(self, prop)]
width = widths[order.index(prop)]
if prop == 'I_TIME' and value not in headings:
value = '{}:{:02}:{:02}'.format(
value // 3600, value // 60 % 60, value % 60
)
elif prop == 'I_COMPL' and value not in headings:
value = 'X' if value else ' '
elif highlight and prop == 'I_ID':
value = '{:*>{w}}'.format(value, w=width)
args.append(value)
line = line_fmt.format(*args, w=widths)
if highlight:
line = colorize(line, var='rev')
table.append(line)
table.append(border)
print('\n'.join(table))
def select(self, query):
c = self.conn.execute(
"select id, datetime(datetime, 'localtime'), deck, time, moves, "
"replay, complete from gamehistory {}".format(query)
)
result = c.fetchall()
c.close()
return result
if __name__ == '__main__':
import readline, rlcompleter
from subprocess import call
from datetime import datetime, timedelta
from optparse import OptionParser
usage = "Usage: %prog [--test]"
parser = OptionParser(usage=usage)
parser.add_option('-t', '--test', action='store_true', default=False,
help="Run doctests")
parser.add_option('-d', '--db', default="~/.pyfreecell.db",
help="Path to game history database")
parser.add_option('-w', '--width', type="int", default=7, help="Card width")
parser.add_option('-o', '--offset', default=0, type="int",
help="The difference between the --width and the actual"
" card width when the suit symbols take up less"
" room on screen than their actual width. You will"
" most likely have to set this to 2.")
options, args = parser.parse_args()
if options.test:
import doctest
doctest.testmod()
else:
readline.parse_and_bind('tab: complete')
history = GameHistory(options.db)
start = datetime.now()
game = None
move = None
gameid = None
call(['clear'])
gamehelp = "Type 'n' to start a game. Type 2 letters to move card from\n" \
"spot to another, first the letter near the 'from' pile, then\n" \
"the letter near the 'to' pile, then hit Enter.\n" \
"You can type more letter pairs before you hit Enter and\n" \
"it will do them all in sequence.\n" \
" n -- new game\n" \
" save -- save game and quit\n" \
" q -- quit without saving\n" \
" a/s/d/f/j/k/l/; -- from/to column\n" \
" q/w/e/r -- from/to specific free cell\n" \
" g/t -- to first free cell\n" \
" u/i/o/p -- from/to specific foundation\n" \
" h/t -- to appropriate foundation for from card\n" \
" m -- make all possible foundation moves\n" \
" z -- undo (zz to use in same string as other moves)\n" \
" show -- enter cmd with no args for 'show' help\n" \
" play n restart|resume -- restart/resume game # n\n" \
" ?/help -- show this help\n" \
" py -- enter python interpreter... mostly for inspecting\n" \
" game and history objects\n"
print(colorize(BANNER, fg='green'))
print(colorize(gamehelp, fg='yel'))
saved = history.unfinished()
if saved:
print(colorize('Saved', fg='cyan', var='und'))
history.pp(saved)
while True:
try:
raw_move = input(colorize('move> ', fg='mag'))
except KeyboardInterrupt:
move='q'
else:
move = raw_move.lower().strip()
if move in ['q','quit','exit']:
break
if move in ['?', 'help']:
print(gamehelp)
continue
if move in ['save']:
duration = datetime.now() - start
print(history.save(game, duration.total_seconds(), gameid))
break
if move == 'py':
intro = "python interpreter\n" \
"enter 'q' to quit and return to move interpreter\n"
print(colorize(intro, fg='yel'))
while True:
move = input(colorize('>>> ', fg='blu'))
if move.lower() in ['q','quit','exit']:
if game and not game.complete():
print(colorize("press enter again to see board", fg='yel'))
break
if move:
try:
result = eval(move.strip())
result = pprint.pformat(result)
print(colorize(result, fg='green'))
except Exception:
import traceback
exc = traceback.format_exc().splitlines()
if len(exc) > 5:
message = '\n'.join(exc)
else:
message = exc[-1]
print(colorize(message, fg='red'))
continue
if move.startswith('show'):
usage = ("'show' options are 'saved', 'bt', 'lm', 'last' or 'q'."
"\n'bt' = best times "
"\n'lm' = least moves "
"\n'last' = recent "
"\n'q' = query ('where...' or 'order by...') "
"\n'bt', 'last', and 'lm' take a 3rd optional arg -- "
"\nnumber of rows to display\n")
opts = raw_move.split(' ')
allow = ['saved','bt','lm','last','q']
if len(opts) < 2 or opts[1] not in allow:
print(colorize(usage, fg='yel'))
opts = ['q', 'last']
action = opts[1]
count = 5 if len(opts) < 3 else opts[2]
mark = None if not gameid else (0, gameid)
if action == 'saved':
result = history.unfinished()
heading = 'Saved Games'
elif action == 'bt':
result = history.besttimes(count)
heading = 'Best Times'
elif action == 'lm':
result = history.leastmoves(count)
heading = 'Least Moves'
elif action == 'last':
result = history.select(
"order by datetime desc limit {}".format(count)
)
heading = 'Recent Games'
elif action == 'q':
action = ' '.join(opts[2:])
firstword = opts[2]
if firstword not in ['where','order','limit']:
try:
z, where, order, limit = action.split(action[0])
except (IndexError, ValueError):
print(colorize('wrong # of params', fg='red'))
print(colorize(usage, fg='yel'))
continue
else:
where = where and 'where {}'.format(where)
order = order and 'order by {}'.format(order)
limit = 'limit {}'.format(limit or 20)
action = '{} {} {}'.format(where, order, limit)
elif 'limit' not in action:
action = '{} limit 20'.format(action)
try:
result = history.select(action)
heading = 'games {}'.format(action)
except Exception:
import traceback
exc = traceback.format_exc().splitlines()
print(colorize(exc[-1], fg='red'))
print(colorize('Query: {}'.format(action), fg='cyan'))
print(colorize(usage, fg='yel'))
continue
print(colorize(heading, fg='cyan', var='und'))
history.pp(result, mark=mark)
continue
if not move and not game:
continue
if move in ['n','new']:
start = datetime.now()
gameid = None
game = FreecellGame()
elif move.startswith('play'):
try:
z, gameid, begin = move.split()
except:
print("To play a saved game type: 'play <gameid> <restart|resume>'")
continue
else:
record = history.get(gameid)
if record:
gameid = int(gameid) # for 'mark' arg to history.pp()
gdeck = record[0][history.I_DECK]
game = FreecellGame(gdeck)
if begin == 'resume':
greplay = record[0][history.I_REPLAY]
gtime = record[0][history.I_TIME]
game.move(greplay)
start = datetime.now() - timedelta(seconds=gtime)
else:
print("Game {} does not exist".format(gameid))
continue
else:
try:
if game:
game.move(move)
else:
raise Exception(
'No active game. Enter "n" to start, "?" for help.'
)
except Exception as e:
print(colorize("Error: {}".format(e), fg='red'))
print(colorize(
"If you're getting strange errors try"
" hitting Enter again, or save and resume",
fg='cyan'
))
continue
if game.complete():
# new games don't have a gameid
# resumed games have a gameid but need duration updated
# ... so need to make sure we save same game not new game
# empty/invalid moves after end of game need to do nothing
saved = gameid and history.get(gameid)[0][history.I_COMPL]
if not saved:
finish = datetime.now()
duration = finish - start
# this will save resumed game if gameid, otherwise it
# will save a new game
gameid = history.save(game, duration.total_seconds(), gameid)
print(colorize(
"\nCompleted Game #{}!\nTime: {}\nMoves: {}" \
.format(gameid, duration, len(game.replay)),
fg='yel'
))
print(colorize("\nBest Times:", fg='yel'))
history.pp(history.besttimes(5), mark=(0, gameid))
print(colorize("\nLeast Moves:", fg='yel'))
history.pp(history.leastmoves(5), mark=(0, gameid))
continue
else:
call(['clear'])
print("--------")
print(game.draw_board(options.width, options.offset))
print("--------")