-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdreamium.py
executable file
·985 lines (819 loc) · 24.2 KB
/
dreamium.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
#!/usr/bin/python3
# Copyright (c) 2017 Santiago Piqueras
import random
import curses
from enum import Enum
VERSION = "0.1.1"
class Color(Enum):
RED = 1
BLUE = 2
GREEN = 3
YELLOW = 4
class CardType(Enum):
SUN = 1
MOON = 2
KEY = 3
DOOR = 4
NIGHTMARE = 5
BACK = 6
_ASCII_={}
_ASCII_[CardType.SUN]="""\
╭─┐
│☀│
└─╯""".splitlines()
_ASCII_[CardType.MOON]="""\
╭─┐
│☽│
└─╯""".splitlines()
_ASCII_[CardType.KEY]="""\
╭─┐
│⚷│
└─╯""".splitlines()
_ASCII_[CardType.DOOR]="""\
╭─┐
│╶│
└─╯""".splitlines()
_ASCII_[CardType.NIGHTMARE]="""\
╭─┐
│☠│
└─╯""".splitlines()
_ASCII_[CardType.BACK]="""\
╭╥┐
╞╬╡
└╨╯""".splitlines()
_ASCII_REDUCED_={}
_ASCII_REDUCED_[CardType.SUN]="""\
╭
☀
└""".splitlines()
_ASCII_REDUCED_[CardType.MOON]="""\
╭
│
☽""".splitlines()
_ASCII_REDUCED_[CardType.KEY]="""\
⚷
│
└""".splitlines()
_CURSES_COLORS_={}
_CURSES_COLORS_[Color.GREEN]=3
_CURSES_COLORS_[Color.RED]=2
_CURSES_COLORS_[Color.BLUE]=5
_CURSES_COLORS_[Color.YELLOW]=4
class Card:
def __init__(self, card_type, color=None):
self.color = color
self.card_type = card_type
def draw(self, window, x_offset, y_offset=0, options=0):
if self.card_type is CardType.NIGHTMARE:
for j in range(3):
window.addstr(y_offset+j, x_offset, _ASCII_[self.card_type][j],
curses.color_pair(6) | options)
return
for j in range(3):
window.addstr(y_offset+j, x_offset, _ASCII_[self.card_type][j],
curses.color_pair(_CURSES_COLORS_[self.color]) | options)
def draw_reduced(self, window, x_offset, y_offset=0, options=0):
for j in range(3):
window.addstr(y_offset+j, x_offset, _ASCII_REDUCED_[self.card_type][j],
curses.color_pair(_CURSES_COLORS_[self.color]) | options)
class Doors:
doors_counter = {}
doors = {}
doors_per_color = 2
def __init__(self):
for color in Color:
self.doors[color] = Card(CardType.DOOR, color)
self.reset()
def reset(self):
for color in Color:
self.doors_counter[color] = 0
self.doors_per_color = 2
def can_open(self, color):
return self.doors_counter[color] - self.doors_per_color
def open_door(self, color):
self.doors_counter[color] += 1
def close_door(self, color):
if not self.doors_counter[color]:
return False
self.doors_counter[color] -= 1
return True
def check_all_open(self):
for c in self.doors_counter.values():
if c != 2:
return False
return True
def draw(self, window):
for j, color in enumerate(Color):
self.doors[color].draw(window, j*4)
window.addstr(1, j*4 + 1, str(self.doors_counter[color]),
curses.color_pair(_CURSES_COLORS_[color]))
class Deck:
STANDARD = [(CardType.SUN, Color.RED, 9),
(CardType.MOON, Color.RED, 4),
(CardType.KEY, Color.RED, 3),
(CardType.DOOR, Color.RED, 2),
(CardType.SUN, Color.BLUE, 8),
(CardType.MOON, Color.BLUE, 4),
(CardType.KEY, Color.BLUE, 3),
(CardType.DOOR, Color.BLUE, 2),
(CardType.SUN, Color.GREEN, 7),
(CardType.MOON, Color.GREEN, 4),
(CardType.KEY, Color.GREEN, 3),
(CardType.DOOR, Color.GREEN, 2),
(CardType.SUN, Color.YELLOW, 6),
(CardType.MOON, Color.YELLOW, 4),
(CardType.KEY, Color.YELLOW, 3),
(CardType.DOOR, Color.YELLOW, 2),
(CardType.NIGHTMARE, None, 10)]
cards = []
num_nightmares = 0
def __init__(self, card_list = None):
self.reset(card_list)
def reset(self, card_list = None):
cards = self.STANDARD
self.cards = []
if card_list:
cards = card_list
for card_type, color, quantity in cards:
if card_type is CardType.NIGHTMARE:
self.num_nightmares = quantity
self.cards += [Card(card_type, color) for _ in range(quantity)]
def add_card(self, card):
if card.card_type is CardType.NIGHTMARE:
self.num_nightmares += 1
self.cards.append(card)
def next(self):
card = self.cards.pop(-1)
if card.card_type is CardType.NIGHTMARE:
self.num_nightmares -= 1
return card
def shuffle(self):
random.shuffle(self.cards)
def num_cards(self):
return len(self.cards)
def remove_door(self, color):
for card in self.cards:
if card.card_type is CardType.DOOR and card.color == color:
self.cards.remove(card)
break
def draw(self, window):
for j in range(3):
window.addstr(j, 0, _ASCII_[CardType.BACK][j],
curses.color_pair(6))
window.addstr(1, 4, str(self.num_cards()).rjust(2, " ") + " ╬")
window.addstr(2, 4, str(self.num_nightmares).rjust(2, " ") + " ☠")
class Path:
MAX_COMBO = 3
cards = []
combo = 0
def __init__(self):
self.reset()
def reset(self):
self.cards = []
self.combo = 0
def add_card(self, card):
if self.cards and self.cards[-1].card_type == card.card_type:
return False
if self.cards and self.cards[-1].color != card.color or \
self.combo == self.MAX_COMBO:
self.combo = 0
self.combo += 1
self.cards.append(card)
return True
def get_card(self, index):
return self.cards[index]
def has_combo(self):
return self.combo >= self.MAX_COMBO
def draw(self, window):
x = 0
for i, card in enumerate(self.cards):
if len(self.cards) - i >= 5:
card.draw_reduced(window, x)
x+=1
else:
card.draw(window, x)
x+=2
class Discard:
cards_counter = {}
cards = {}
last_card = None
draw_full = False
def __init__(self):
self.reset()
def reset(self):
draw_full = False
for card_type in (CardType.SUN, CardType.MOON, CardType.KEY):
for color in Color:
self.cards_counter[(card_type,color)] = 0
self.cards[(card_type,color)] = Card(card_type, color)
self.cards_counter[(CardType.NIGHTMARE,None)] = 0
self.cards[(CardType.NIGHTMARE,None)] = Card(CardType.NIGHTMARE, None)
self.last_card = None
def add_card(self, card):
self.cards_counter[(card.card_type,card.color)] += 1
self.last_card = card
def draw(self, window):
window.addstr(0,1,"Discard pile")
if not self.draw_full:
window.addstr(2,0,"9 Show/hide")
return
r, c = 0,0
for color in Color:
for i, card_type in enumerate((CardType.SUN, CardType.MOON, CardType.KEY)):
self.cards[(card_type,color)].draw(window, i*2 + 7*r, 1+c*4)
window.addstr(4+c*4, i*2 + 7*r + 1,
str(self.cards_counter[(card_type, color)]))
c+= 1 if r==1 else 0
r = 0 if r==1 else r+1
class KeyDiscard:
MAX_CARDS = 5
cards = []
selected = None
def __init__(self):
self.reset()
def reset(self):
self.cards = []
self.selected = None
self.highlight_keys = False
def add_card(self, card):
self.cards.append(card)
def get_card(self, index=None):
if index == None:
index = self.selected
return self.cards[index]
def remove_card(self):
return self.cards.pop()
def move_card_to(self, index):
card = self.cards.pop(self.selected)
self.cards.insert(index, card)
def all_doors(self):
for card in self.cards:
if card.card_type is not CardType.DOOR:
return False
return True
def num_cards(self):
return len(self.cards)
def draw(self, window):
if not self.cards:
return
x_offset = 0
for i, card in enumerate(self.cards):
if card is None:
continue
if i == len(self.cards) - 1:
x_offset += 3
if self.selected == i:
card.draw(window, x_offset+i*3, 0)
else:
card.draw(window, x_offset+i*3, 1)
if self.selected != i:
window.addstr(4, x_offset+i*3+1, str(i+1))
class Hand:
MAX_CARDS = 5
cards = []
selected = None
highlight_keys = False
def __init__(self):
self.reset()
def reset(self):
self.cards = [None for _ in range(self.MAX_CARDS)]
self.selected = None
self.highlight_keys = False
def add_card(self, card):
index = self.cards.index(None)
self.cards[index] = card
return index
def get_card(self, index=None):
if index == None:
index = self.selected
return self.cards[index]
def remove_card(self, index=None):
if index == None:
index = self.selected
self.selected = None
card = self.cards[index]
self.cards[index] = None
return card
def discard_hand(self):
cards = (card for card in self.cards if card is not None)
self.cards = [None for _ in range(self.MAX_CARDS)]
return cards
def remove_key_and_door(self):
# Assume that the door is selected
color = self.cards[self.selected].color
key_index = -1
for i, card in enumerate(self.cards):
if card is not None and card.card_type is CardType.KEY and card.color == color:
key_index = i
break
self.remove_card()
return self.remove_card(key_index)
def num_cards(self):
return self.MAX_CARDS - self.cards.count(None)
def has_key(self, color):
for card in self.cards:
if card and card.card_type is CardType.KEY and card.color == color:
return True
return False
def has_keys(self):
for card in self.cards:
if card and card.card_type is CardType.KEY:
return True
return False
def draw(self, window):
for i, card in enumerate(self.cards):
if card is None:
continue
if self.selected == i or (self.highlight_keys and card.card_type is CardType.KEY):
card.draw(window, i*3, 0)
else:
card.draw(window, i*3, 1)
if self.selected is None or (self.highlight_keys and card.card_type is CardType.KEY):
window.addstr(4, i*3+1, str(i+1))
class GameStateEnum(Enum):
NONE = 0
MAIN1 = 1
MAIN2 = 2
DOOR_DRAWN = 3
NIGHTMARE = 4
NIGHTMARE_KEY = 5
NIGHTMARE_DOOR = 6
KEY_DISCARD1 = 7
KEY_DISCARD2 = 8
WON = 9
LOST1 = 10
LOST2 = 11
class GameState():
state = GameStateEnum.NONE
message = ""
@staticmethod
def process(game, key):
pass
class Main1State(GameState):
state = GameStateEnum.MAIN1
message = \
"""Choose an option
1-5 Select a card""".splitlines()
@staticmethod
def process(game, opt):
if opt in range(1,6):
game.hand.selected = opt-1
return Main2State
return Main1State
class Main2State(GameState):
state = GameStateEnum.MAIN2
message = \
"""Card selected
1 Add card to the path
2 Discard card
6 Select another card""".splitlines()
@staticmethod
def process(game, opt):
if opt == 1:
card = game.hand.get_card()
success = game.path.add_card(card)
if success:
# Put card in the path
game.hand.remove_card()
if game.path.has_combo() and game.doors.can_open(card.color):
# Open door though combo
game.doors.open_door(card.color)
game.deck.remove_door(card.color)
game.deck.shuffle()
game.info = GameInfo.SHUFFLED
return game.fill_hand()
else:
game.hand.selected = None
game.error = GameError.PATH_SAME_SYMBOL
return Main1State
elif opt == 2:
card = game.hand.remove_card()
game.discard.add_card(card)
if card.card_type is CardType.KEY:
for _ in range(min(game.keydiscard.MAX_CARDS, game.deck.num_cards())):
card = game.deck.next()
game.keydiscard.add_card(card)
if game.keydiscard.all_doors() == True:
return Lost2State
game.hand.selected = -1
return KeyDiscard1State
else:
return game.fill_hand()
elif opt == 6:
game.hand.selected = None
return Main1State
return Main2State
class DoorDrawnState(GameState):
state = GameStateEnum.DOOR_DRAWN
message = \
"""You have drawn a DOOR
1 Use key to open the door
2 Return door to the deck""".splitlines()
@staticmethod
def process(game, opt):
if opt == 1:
# Open door through key
card = game.hand.remove_key_and_door()
game.discard.add_card(card)
game.doors.open_door(card.color)
return game.fill_hand()
elif opt == 2:
# Discard the door
card = game.hand.remove_card()
game.deck.add_card(card)
game.deck.shuffle()
game.info = GameInfo.SHUFFLED
return game.fill_hand()
return DoorDrawnState
class NightmareState(GameState):
state = GameStateEnum.NIGHTMARE
message = \
"""You have drawn a NIGHTMARE
1 Discard a key in hand
2 Discard an open door
3 Discard your hand
4 Discard top 5 cards from the deck""".splitlines()
@staticmethod
def process(game, opt):
if opt == 1:
if game.hand.has_keys():
game.hand.highlight_keys = True
return NightmareKeyState
else:
game.error = GameError.NO_KEYS
elif opt == 2:
return NightmareDoorState
elif opt == 3:
game.hand.remove_card()
cards = game.hand.discard_hand()
for card in cards:
game.discard.add_card(card)
return game.refill_hand()
elif opt == 4:
if game.deck.num_cards() >= 5:
shuffle_cards = []
for _ in range(5):
card = game.deck.next()
if card.card_type is not CardType.NIGHTMARE and \
card.card_type is not CardType.DOOR:
game.discard.add_card(card)
else:
shuffle_cards.append(card)
game.shuffle_cards(shuffle_cards)
game.hand.remove_card()
return game.fill_hand()
else:
game.error = GameError.NO_CARDS
return NightmareState
return NightmareState
class NightmareKeyState(GameState):
state = GameStateEnum.NIGHTMARE_KEY
message = \
"""Discard a key
1-5 Choose the key to discard
6 Select another option""".splitlines()
@staticmethod
def process(game, opt):
if opt in range(1,6):
index = opt-1
if game.hand.cards[index] and game.hand.cards[index].card_type is CardType.KEY:
card = game.hand.remove_card(index)
game.hand.remove_card()
game.discard.add_card(card)
game.hand.highlight_keys = False
return game.fill_hand()
elif opt == 6:
game.hand.highlight_keys = False
return NightmareState
return NightmareKeyState
class NightmareDoorState(GameState):
state = GameStateEnum.NIGHTMARE_DOOR
message = \
"""Discard a door
1 Discard red door
2 Discard blue door
3 Discard green door
4 Discard yellow door
6 Select another option""".splitlines()
@staticmethod
def process(game, opt):
if opt in range(1,5):
color = Color(opt)
success = game.doors.close_door(color)
if success:
game.hand.remove_card()
game.deck.add_card(Card(CardType.DOOR, color))
game.deck.shuffle()
game.info = GameInfo.SHUFFLED
return game.fill_hand()
else:
game.error = GameError.NO_DOORS
elif opt == 6:
return NightmareState
return NightmareDoorState
class KeyDiscard1State(GameState):
state = GameStateEnum.KEY_DISCARD1
message = \
"""You have discarded a key
1-5 Select a card
6 Confirm""".splitlines()
@staticmethod
def process(game, opt):
if opt in range(1,6):
index = opt-1
if index < game.keydiscard.num_cards():
game.keydiscard.selected = index
return KeyDiscard2State
elif opt == 6:
card = game.keydiscard.remove_card()
if card.card_type is CardType.DOOR:
game.keydiscard.add_card(card)
game.error = GameError.DISCARD_DOOR
else:
if card.card_type is not CardType.NIGHTMARE:
game.discard.add_card(card)
while game.keydiscard.num_cards() > 0:
card = game.keydiscard.remove_card()
game.deck.add_card(card)
game.hand.selected = None
return game.fill_hand()
return KeyDiscard1State
class KeyDiscard2State(GameState):
state = GameStateEnum.KEY_DISCARD2
message = \
"""Card selected
1-5 Choose its new position
6 Back""".splitlines()
@staticmethod
def process(game, opt):
if opt in range(1,6):
index = opt-1
if index < game.keydiscard.num_cards():
game.keydiscard.move_card_to(index)
game.keydiscard.selected = None
return KeyDiscard1State
elif opt == 6:
game.keydiscard.selected = None
return KeyDiscard1State
return KeyDiscard2State
class WonState(GameState):
state = GameStateEnum.WON
message = \
"""Congratulations! All doors opened
1 Play again
2 Quit""".splitlines()
@staticmethod
def process(game, opt):
if opt == 1:
game.reset()
return Main1State
elif opt == 2:
exit()
return WonState
class Lost1State(GameState):
state = GameStateEnum.LOST1
message = \
"""Oh no. You've run out of cards!
1 Play again
2 Quit""".splitlines()
@staticmethod
def process(game, opt):
if opt == 1:
game.reset()
return Main1State
elif opt == 2:
exit()
return Lost1State
class Lost2State(GameState):
state = GameStateEnum.LOST2
message = \
"""Oh no. All drawn cards are doors!
1 Play again
2 Quit""".splitlines()
@staticmethod
def process(game, opt):
if opt == 1:
game.reset()
return Main1State
elif opt == 2:
exit()
return Lost2State
class GameInfo(Enum):
NOTHING = 1
SHUFFLED = 2
class GameError(Enum):
NOTHING = 1
PATH_SAME_SYMBOL = 2
DISCARD_DOOR = 3
NO_KEYS = 4
NO_DOORS = 5
NO_CARDS = 6
class Game:
hand = None
path = None
deck = None
discard = None
doors = None
keydiscard = None
selected = -1
state = None
draw_discard = False
info = None
error = None
def __init__(self):
self.n_turn = 0
self.deck = Deck()
self.hand = Hand()
self.path = Path()
self.discard = Discard()
self.doors = Doors()
self.keydiscard = KeyDiscard()
self.info = GameInfo.NOTHING
self.error = GameError.NOTHING
def start(self):
self.deck.shuffle()
self.refill_hand()
self.state = Main1State
def reset(self):
self.deck.reset()
self.hand.reset()
self.path.reset()
self.discard.reset()
self.doors.reset()
self.keydiscard.reset()
self.start()
def shuffle_cards(self, cards, update_info=True):
if cards:
for card in cards:
self.deck.add_card(card)
self.deck.shuffle()
if update_info:
self.info = GameInfo.SHUFFLED
def refill_hand(self):
shuffle_cards = []
while self.hand.num_cards() != self.hand.MAX_CARDS:
if not self.deck.num_cards():
return Lost1State
card = self.deck.next()
if card.card_type != CardType.NIGHTMARE and \
card.card_type != CardType.DOOR:
self.hand.add_card(card)
else:
shuffle_cards.append(card)
self.shuffle_cards(shuffle_cards, False)
return Main1State
def fill_hand(self):
shuffle_cards = []
while self.hand.num_cards() != self.hand.MAX_CARDS:
if not self.deck.num_cards():
return Lost1State
card = self.deck.next()
index = self.hand.add_card(card)
if card.card_type is CardType.NIGHTMARE:
self.shuffle_cards(shuffle_cards)
self.hand.selected = index
return NightmareState
if card.card_type is CardType.DOOR:
self.hand.selected = index
if self.hand.has_key(card.color):
self.shuffle_cards(shuffle_cards)
return DoorDrawnState
else:
card = self.hand.remove_card()
shuffle_cards.append(card)
self.shuffle_cards(shuffle_cards)
return Main1State
def check_won(self):
return self.doors.check_all_open()
def process(self, key):
opt = -1
if key.isdigit():
opt = int(key)
elif key == "q":
exit(0)
if opt == 9:
self.discard.draw_full = not self.discard.draw_full
self.state = self.state.process(self, opt)
if self.check_won():
self.state = WonState
self.n_turn = 0
class UIMessage():
game = None
info = {}
error = {}
def __init__(self, game):
self.game = game
self.populate()
def populate(self):
self.info[GameInfo.SHUFFLED] = \
"""Deck has been shuffled"""
self.error[GameError.PATH_SAME_SYMBOL] = \
"""Repeated symbol"""
self.error[GameError.DISCARD_DOOR] = \
"""Can't discard a door"""
self.error[GameError.NO_KEYS] = \
"""You don't have any keys"""
self.error[GameError.NO_DOORS] = \
"""Not enough doors of that color"""
self.error[GameError.NO_CARDS] = \
"""There are less than 5 cards in the deck"""
def draw(self, window):
message = self.game.state.message
message_len = len(message)
for j in range(message_len):
window.addstr(j, 0, message[j])
if self.game.info != GameInfo.NOTHING:
window.addstr(message_len, 0, self.info[self.game.info], curses.color_pair(6))
self.game.info = GameInfo.NOTHING
if self.game.error != GameError.NOTHING:
window.addstr(message_len, 0, self.error[self.game.error], curses.color_pair(2))
self.game.error = GameError.NOTHING
class UI():
MIN_X = 70
MIN_Y = 20
game = None
stdscr = None
ui_message = None
error = False
wins = {}
def __init__(self, game, stdscr):
self.game = game
self.stdscr = stdscr
self.ui_message = UIMessage(game)
self.set_windows()
def set_windows(self):
"""
+-----------------+ +-------------------+
| DOORS | | |
+-----------------+ | KEYDISCARD |
+-------------------------------------| |
| PATH +-------------------+
+---------------------------------------------->
+-----------------+ +---------------+
| | +-------+ | |
| HAND | | DECK | | |
| | +-------+ | |
+-----------------+ | DISCARD |
| |
+---------------------------------------| |
| | |
| +---------------+
| MESSAGE
|
|
+---------------------------------------------->
"""
try:
self.wins = [self.stdscr.subwin(3, 65, 4, 2), # Path
self.stdscr.subwin(3, 16, 1, 5), # Doors
self.stdscr.subwin(5, 15, 7, 5), # Hand
self.stdscr.subwin(3, 9, 8, 22), # Deck
self.stdscr.subwin(9, 14, 8, 38), # Discard
self.stdscr.subwin(5, 20, 1, 36), # Keydiscard
self.stdscr.subwin(7, 60, 13, 1)] # Message
self.error = False
except curses.error:
self.error = True
def draw(self):
self.stdscr.clear()
if self.error:
y, x = self.stdscr.getmaxyx()
if y >= self.MIN_Y and x >= self.MIN_X:
self.set_windows()
if self.error:
self.stdscr.addstr("This game requires a minimum window size "
"of {}x{}\n".format(self.MIN_X,self.MIN_Y))
self.stdscr.addstr("Please resize the terminal\n")
self.stdscr.addstr("Or press Control+C to exit")
return
for win in self.wins:
win.clear()
self.game.path.draw(self.wins[0])
self.game.doors.draw(self.wins[1])
self.game.hand.draw(self.wins[2])
self.game.deck.draw(self.wins[3])
self.game.keydiscard.draw(self.wins[5])
self.ui_message.draw(self.wins[6])
self.game.discard.draw(self.wins[4])
self.stdscr.addstr(19, 49, "Dreamium V{}".format(VERSION), curses.color_pair(6))
for win in self.wins:
win.refresh()
self.stdscr.refresh()
def main(stdscr):
stdscr.clear()
stdscr.refresh()
curses.start_color()
curses.use_default_colors()
curses.curs_set(False)
for i in range(0, curses.COLORS):
curses.init_pair(i + 1, i, -1)
for i in range(0, curses.COLORS):
curses.init_pair(i + curses.COLORS + 1, i, 5)
game = Game()
game.start()
ui = UI(game, stdscr)
while True:
ui.draw()
key = stdscr.getkey()
game.process(key)
curses.wrapper(main)