-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardstock.py
1321 lines (1100 loc) · 37.2 KB
/
cardstock.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
# coding=UTF-8
# -*- coding: UTF-8 -*-
# vim: set fileencoding=UTF-8 :
"""
Shared objects and functions for many card games, especially those
where you take tricks
"""
import itertools
from enum import Enum, unique
from itertools import cycle
import random
from math import ceil
from pathlib import Path
from typing import (
List,
Optional,
Tuple,
Iterable,
Set,
Callable,
Dict,
NamedTuple,
TextIO,
Union,
Generic,
TypeVar,
Type,
Iterator,
Any,
)
from copy import deepcopy
from datetime import datetime
import configparser
import os
import click
import abc
import inspect
base_log_dir: str = "logs/"
def game_out_dir(gamename: Union[str, bytes]) -> str:
return os.path.join(base_log_dir, gamename)
@abc.abstractmethod
def get_game_name() -> str:
return os.path.basename(__file__).split(".py")[0]
def p0(*msg):
"""
dummy function for use when you don't want any output
"""
pass
def now() -> str:
return str(datetime.now()).split(".")[0]
def make_players(
handles: List[str],
player_type: "Union[Iterable[Type[PlayerType]], Type[PlayerType]]",
calling_game: "GameType",
) -> "List[PlayerType]":
if not isinstance(player_type, Iterable):
player_type = {player_type}
player_type = cycle(player_type)
return [pt(calling_game, h) for pt, h in zip(player_type, handles)]
class Color:
WHITE = (0, "FFF")
BLACK = (1, "000")
RED = (2, "F00")
YES = (99, "00F")
BLUE = (-5, "00F")
INTERNATIONAL_KLEIN_BLUE = (153, "002FA7")
GREEN = (3, "0F0")
YELLOW = (5, "FF0")
def __init__(self, value: int, hexa: str):
self.v = value
self.hex = hexa
def __hash__(self) -> int:
return self.v
@property
def hex_code(self) -> str:
return f"#{self.hex}"
@unique
class Suit(Enum):
JOKER_WHITE = (0, "🃏", "JOKER_WHITE", Color.WHITE)
JOKER = (0, "🃏", "JOKER", Color.YES)
CLUB = (1, "♣", "SPADE", Color.BLACK)
DIAMOND = (2, "♢", "HEART", Color.RED)
SPADE = (3, "♠", "CLUB", Color.BLACK)
HEART = (4, "♡", "DIAMOND", Color.RED)
TRUMP = (5, "T", "TRUMP", Color.YES)
JOKER_RED = (0, "🃏", "JOKER_BLACK", Color.RED)
JOKER_BLACK = (0, "🃏", "JOKER_RED", Color.BLACK)
opposite: "Suit"
v: int
def __init__(self, value: int, symbol: str, opposite: str, color: Color):
self.v = value
self.symbol = symbol
self.color = color
try:
self.opposite = self._member_map_[opposite]
self._member_map_[opposite].opposite = self
except KeyError:
pass
@property
def plural_name(self) -> str:
if self != self.TRUMP:
return self.name + "S"
return self.name
def __str__(self):
return self.plural_name
def __repr__(self):
return self.symbol
def __lt__(self, other):
return self.v < other.v
@unique
class Rank(Enum):
JOKER = (0, "🃟")
ACE_LO = (1, "a")
TWO = (2, "2")
THREE = (3, "3")
FOUR = (4, "4")
FIVE = (5, "5")
SIX = (6, "6")
SEVEN = (7, "7")
EIGHT = (8, "8")
NINE = (9, "9")
TEN = (10, "⒑")
JACK = (11, "J")
QUEEN = (12, "Q")
KING = (13, "K")
ACE_HI = (14, "A")
LEFT_BOWER = (15, "L")
RIGHT_BOWER = (16, "R")
SUPER_JOKER = (17, "*")
def __init__(self, value: int, char: str):
self.v = value
self.char = char
@property
def long_display_name(self) -> str:
if self == self.ACE_HI:
return "ACE"
if self == self.ACE_LO:
return "ACE"
return self.name
def __repr__(self):
return self.char
def __str__(self):
return self.long_display_name
def __lt__(self, other):
return self.v < other.v
class Cardstock:
"""
Thought the name would be punny. Think of this as BaseCard.
"""
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
self.d_rank = rank
self.d_suit = suit
@property
def card_name(self) -> str:
return self.rank.long_display_name + " of " + self.suit.plural_name
def __str__(self) -> str:
return self.card_name
def __repr__(self):
return f"{repr(self.d_rank)}{repr(self.d_suit)}"
# return f"{repr(self.rank)}{repr(self.suit)}" # debugging
def __eq__(self, other):
return True if (self.rank == other.rank and self.suit == other.suit) else False
def __lt__(self, other):
if self.suit < other.suit:
return True
if self.suit > other.suit:
return False
if self.rank < other.rank:
return True
return False
def __hash__(self) -> int:
return key_trump_power(self)
def beats(self, other: "Cardstock", is_low: bool) -> bool:
"""Assumes the first played card of equal value wins"""
if self.suit not in {other.suit, Suit.TRUMP}:
return False # must follow suit
return (self < other) if is_low else (self > other)
def follows_suit(self, s: Optional[Suit], strict: bool = True) -> bool:
return s is None or (self.suit == s or self.suit == Suit.TRUMP and not strict)
@property
def value(self) -> int:
return 1
class Card(Cardstock):
pass
CardType = TypeVar("CardType", bound=Cardstock)
def key_display4human(c: Cardstock) -> int:
return (
c.d_suit.opposite.v * 100 + 15
if c.rank == Rank.LEFT_BOWER
else c.d_suit.v * 100 + c.rank.v
)
def match_by_rank(c: Iterable[CardType], r: Rank) -> List[CardType]:
return [x for x in c if (x.rank == r)]
def key_rank_first(c: CardType) -> int:
return c.rank.v * 10 + c.suit.v
def key_suit_first(c: CardType) -> int:
return c.suit.v * 100 + c.rank.v
def key_trump_power(c: CardType) -> int:
return key_rank_first(c) + (1000 if c.suit == Suit.TRUMP else 0)
class TrickPlay(NamedTuple):
card: CardType
played_by: "PlayerType"
def beats(self, other: "TrickPlay", is_low: bool = False):
return self.card.beats(other.card, is_low)
class Trick(List[TrickPlay]):
def winner(self, is_low: bool = False) -> Optional[TrickPlay]:
if not self:
return None
w: TrickPlay = self[0]
for cpt in self:
if cpt.beats(w, is_low):
w = cpt
return w
def winning_card(self, is_low: bool = False) -> CardType:
return self.winner(is_low).card
def __add__(self, other):
if isinstance(other, Iterable):
self.extend(other)
elif isinstance(other, TrickPlay):
self.append(other)
return self
@property
def points(self) -> int:
return 1
@property
def cards(self) -> "Hand":
return Hand(c.card for c in self)
def follow_suit(
self, strict: bool = False, ot: "Optional[Type[Trick]]" = None
) -> "TrickType":
if not ot:
ot: Type[Trick] = Trick
return ot(x for x in self if x.card.follows_suit(self.lead_suit, strict))
@property
def lead(self) -> Optional[CardType]:
return self[0].card if self else None
@property
def lead_suit(self) -> Optional[Suit]:
return self.lead.suit if self.lead else None
TrickType = TypeVar("TrickType", bound=Trick)
suits: List[Suit] = [Suit.HEART, Suit.SPADE, Suit.DIAMOND, Suit.CLUB]
euchre_ranks: List[Rank] = [
Rank.NINE,
Rank.TEN,
Rank.JACK,
Rank.QUEEN,
Rank.KING,
Rank.ACE_HI,
]
poker_ranks: List[Rank] = [
Rank.TWO,
Rank.THREE,
Rank.FOUR,
Rank.FIVE,
Rank.SIX,
Rank.SEVEN,
Rank.EIGHT,
] + euchre_ranks
class Hand(List[CardType]):
def __str__(self) -> str:
return " ".join([repr(x) for x in self])
def __add__(self, other: Union[Iterable[CardType], CardType]) -> "Hand":
if isinstance(other, Iterable):
self.extend(other)
if isinstance(other, Cardstock):
self.append(other)
return self
def trumpify(self, trump_suit: Optional[Suit]) -> "Hand":
if not trump_suit:
return self
for card in self:
if card.suit == trump_suit:
card.suit = Suit.TRUMP
if card.rank == Rank.JACK:
card.rank = Rank.RIGHT_BOWER
if card.rank == Rank.JACK and card.suit == trump_suit.opposite:
card.suit = Suit.TRUMP
card.rank = Rank.LEFT_BOWER
return self
def __mul__(self, other) -> "Hand":
assert isinstance(other, int)
return Hand([c for c in self for _ in range(other)])
@property
def points(self) -> int:
return sum([c.value for c in self])
@property
def pointable(self) -> "Hand":
"""
:return: Hand of cards with positive point values, intended for Hearts
"""
return Hand(c for c in self if c.value > 0)
@property
def point_free(self) -> "Hand":
return Hand(c for c in self if c.value < 1)
@property
def pcv(self) -> int:
return self.pointable.points
def add_jokers(self, j: int = 0, c: Type[Cardstock] = Card) -> "Hand":
if j < 1:
return self
if j % 2:
self.append(c(Rank.JOKER, random.choice([Suit.JOKER, Suit.JOKER_WHITE])))
for _ in range(j // 2):
self.append(c(Rank.JOKER, Suit.JOKER_RED))
self.append(c(Rank.JOKER, Suit.JOKER_BLACK))
return self
def make_deck(
r: List[Rank], s: List[Suit], c: Type[Cardstock] = Card, jokers: int = 0
) -> Tuple[Hand, int]:
return (
Hand(c(rank, suit) for rank in r for suit in s).add_jokers(jokers, c),
len(r) * len(s) + jokers,
)
def make_euchre_deck(c: Type[Cardstock] = Card, jokers: int = 0) -> Tuple[Hand, int]:
"""Single euchre deck"""
return make_deck(euchre_ranks, suits, c, jokers)
def make_pinochle_deck(c: Type[Cardstock] = Card, jokers: int = 0) -> Tuple[Hand, int]:
"""a.k.a. a double euchre deck"""
return (make_euchre_deck(c) * 2)[0].add_jokers(jokers, c), 48 + jokers
def make_standard_deck(c: Type[Cardstock] = Card, jokers: int = 0) -> Tuple[Hand, int]:
"""
Standard 52 card deck
Perfect for 52 pick-up
"""
return make_deck(poker_ranks, suits, c, jokers)
def make_minimum_sized_deck(
m: Callable[[Type[Cardstock], int], Tuple[Hand, int]],
c: Type[Cardstock] = Card,
jokers: int = 0,
minimum_size: int = 0,
minimum_copies: int = 1,
) -> Hand:
"""
multiples the deck's size until it is greater or equal to the minimum number of cards
Jokers are added at the very end unless the value is negative
"""
deck, size = m(c, 0 if jokers > 0 else -jokers)
if minimum_size < 1:
return deck
multiplicity: int = max(ceil((minimum_size - jokers) / size), minimum_copies)
return (deck * multiplicity).add_jokers(jokers)
def follow_suit(
s: Optional[Suit],
cs: Iterable[CardType],
strict: Optional[bool] = True,
allow_points: bool = True,
ok_empty: bool = False,
**kwargs,
) -> Hand:
"""
:param s: suit to follow
:param cs: cards to filter
:param strict: count trump cards as following suit?
:param allow_points: (for Hearts), used on first trick
or when leading before heartbreak
:param ok_empty: return an empty Hand if no cards match if true
else return all input cards
:return: cards that follow suit
"""
# print(s, cs, strict, allow_points, ok_empty) # debugging
if not cs:
# stop here on an empty input
return Hand(cs)
if strict is None:
# activate a preset
strict = True
ok_empty = False
if not allow_points:
# filter out the pointable cards and try again
return follow_suit(s, [c for c in cs if (c.value < 1)], strict, True, ok_empty)
if not ok_empty:
# take 1
valid_cards = follow_suit(s, cs, strict, allow_points, True)
if valid_cards:
return valid_cards
# try again with no suit requirements
valid_cards = follow_suit(None, cs, strict, allow_points, True)
if valid_cards:
return valid_cards
# final try where you allow pointable cards
# this should return the whole input set
return follow_suit(None, cs, strict, True, True)
# return cards that follow suit
return Hand(c for c in cs if (c.follows_suit(s, strict)))
@unique
class Bid(Enum):
LOW_NO = (None, True)
CLUBS = (Suit.CLUB, False)
DIAMONDS = (Suit.DIAMOND, False)
SPADES = (Suit.SPADE, False)
HEARTS = (Suit.HEART, False)
HI_NO = (None, False)
def __init__(self, s: Optional[Suit], lo: bool):
self.trump_suit: Optional[Suit] = s
self.is_low: bool = lo
self.short_name: str = self.name[:-1].split("_")[0].lower()
class WithScore:
def __init__(self):
self.score_changes: List[int] = []
@property
def score(self) -> int:
return sum(self.score_changes)
@score.setter
def score(self, value: int):
self.score_changes.append(value)
@abc.abstractmethod
def hand_tab(self, hand: Optional[int], tab: str = "\t") -> str:
"""
:param hand: hand number
:param tab: separator
:return: an expanded list of score changes for the hand
"""
if hand is None:
return str(self.score)
return str(self.score_changes[hand])
class MakesBid:
bid: int = 0
class BasePlayer(abc.ABC):
next_player: "PlayerType"
team: "TeamType"
previous_player: "PlayerType"
opposite_player: "Optional[PlayerType]" = None
def __init__(self, g: "GameType", /, name: str, bot: int = 1):
self.name: str = name
self.is_bot: int = bot
self.hand = Hand()
self.in_game: GameType = g # allow access to the game in which you're playing
self.tricks_taken: List[Hand] = []
self.sort_key: Callable[[CardType], int] = key_display4human
self.passed_cards = Hand()
@property
def deck(self) -> Hand:
return self.in_game.deck
@property
def card_count(self) -> Hand:
"""
:return: list of remaining cards to be played
"""
o = deepcopy(self.in_game.unplayed_cards)
for c in self.hand:
o.remove(c)
return o
def __str__(self):
return f"{self.name}"
def __repr__(self):
return f"Player {self.name}"
@abc.abstractmethod
def pick_card(
self, valid_cards: Hand, **kwargs,
):
pass
def play_card(self, trick_in_progress: "TrickType", /, **kwargs,) -> CardType:
vr: Optional[Rank] = kwargs.get("valid_rank")
c: CardType = self.pick_card(
follow_suit( # valid cards
trick_in_progress.lead_suit
if trick_in_progress
else kwargs.get("force_suit"),
Hand(c for c in self.hand if c.rank == vr) if vr else self.hand,
strict=None,
allow_points=kwargs.get("points_ok", True),
ok_empty=False,
),
trick_in_progress=trick_in_progress, # current trick
**kwargs, # any other useful info
)
# below line is for debugging trump calls
# print(c, self.in_game.played_cards, self.in_game.unplayed_cards)
self.in_game.played_cards.append(c)
self.in_game.unplayed_cards.remove(c)
self.hand.remove(c)
return c
@property
def teammates(self) -> "Set[PlayerType]":
return self.team.players - {self}
def send_shooter(
self, cards: int, p_word: str = "send", prompt="Send a card to your friend."
) -> List[Card]:
"""
Passes cards
Name is from its original use in euchre
:param cards: number of cards to send
:param p_word: changes the word in the inline prompt for humans
:param prompt: replaces "choose the lead" at the top of the prompt for humans
:return: a list of cards to be sent
"""
if not cards or not self.hand:
return []
out: List[CardType] = (
self.hand[-cards:]
if self.is_bot
else [
self.pick_card(self.hand, p_word=p_word, prompt=prompt)
for _ in range(cards)
]
)
for c in out:
self.hand.remove(c)
self.passed_cards.append(c)
return out
def sort_hand(self, is_low: bool = False) -> None:
"""
Sorts your current hand
bots have [-1] as the "best" card
"""
self.hand.sort(key=self.sort_key, reverse=is_low if self.is_bot else False)
def receive_cards(
self, cards_in: Iterable[CardType], /, *, sort_low: bool = False, **kwargs
) -> None:
self.hand += cards_in
self.sort_hand(sort_low)
PlayerType = TypeVar("PlayerType", bound=BasePlayer)
def get_play_order(lead: PlayerType) -> List[PlayerType]:
p_c = lead
out = []
while p_c not in out:
out.append(p_c)
p_c = p_c.next_player
return out
class BaseComputer(BasePlayer, abc.ABC):
def __init__(self, g: "GameType", /, name: str, **kwargs):
BasePlayer.__init__(self, g, name, 1)
class BaseHuman(BasePlayer, abc.ABC):
def __init__(self, g: "GameType", /, name: str, **kwargs):
BasePlayer.__init__(self, g, name, 0)
def pick_card(self, valid_cards: Hand, **kwargs,) -> CardType:
trick_in_progress = kwargs.get("trick_in_progress")
if not trick_in_progress:
print(kwargs.get("prompt", "Choose the lead."))
proper_picks: List[int] = [
i for i in range(len(self.hand)) if self.hand[i] in valid_cards
]
print(" ".join([repr(c) for c in self.hand]))
print(
" ".join(
[f"{j:2}" if j in proper_picks else " " for j in range(len(self.hand))]
)
)
return self.hand[
int(
click.prompt(
f"Index of card to {kwargs.get('p_word', 'play')}",
type=click.Choice([str(pp) for pp in proper_picks], False),
show_choices=False,
default=proper_picks[0] if len(proper_picks) == 1 else None,
)
)
]
def receive_cards(
self,
cards_in: Iterable[CardType],
/,
*,
sort_low: bool = False,
from_player: Optional[PlayerType] = None,
**kwargs,
) -> None:
super(BaseHuman, self).receive_cards(cards_in, sort_low=sort_low, **kwargs)
summary: str = f"Received {Hand(cards_in)}"
if from_player:
summary += f" from {from_player}"
print(summary)
class BaseTeam:
def __init__(self, players: "Iterable[PlayerType]"):
for player in players:
player.team = self
self.players: Set[PlayerType] = set(players)
def __repr__(self):
return "/".join([pl.name for pl in self.players])
TeamType = TypeVar("TeamType", bound=BaseTeam)
def deal(
players: List[PlayerType],
deck: Hand,
minimum_kitty_size: int = 0,
shuffled: bool = True,
replace: bool = True,
fixed_hand_size: Optional[int] = None,
) -> Hand:
"""
Enforces an equal-size deal
:param fixed_hand_size: deal this many cards instead of dealing until you run out
:param players: list of players to whom to deal
:param deck: the deck from which the cards are dealt
:param shuffled: is the deck shuffled first?
:param minimum_kitty_size: the minimum size of the kitty
:param replace: replaces the player's hand if true; appends if false
:return: the kitty
"""
deck_size: int = len(deck)
handedness = len(players)
# for card in self.deck: # for debugging
# assert card.suit != Suit.TRUMP, f"{card} {repr(card)}"
if shuffled:
random.shuffle(deck)
k_size: int = minimum_kitty_size + (deck_size - minimum_kitty_size) % handedness
k_dex: int = deck_size if k_size == 0 else -k_size
kitty: Hand = Hand(deck[k_dex:])
for i in range(handedness):
p: PlayerType = players[i]
h: Hand = deepcopy(Hand(deck[i:k_dex:handedness][:fixed_hand_size]))
if replace:
p.hand = h
else:
p.hand += h
p.sort_hand()
return kitty
_global_options = [
click.option(
"--humans",
"-p",
multiple=True,
default=[],
type=click.IntRange(0, 8),
help="List index of a human player, repeatable",
),
click.option(
"--all-bots",
type=click.BOOL,
is_flag=True,
help="All-bot action for testing and demos",
),
click.option(
"--required-points",
"-v",
"points",
type=click.IntRange(1, None),
help="""
Victory threshold (v): positive integer
\b
team.score > v: victory
team.score < -v: mercy rule loss
all team scores < -v/2: everyone loses
""",
),
click.option(
"--handedness",
"-h",
type=click.IntRange(3, 10),
default=4,
help="Number of players in the game",
),
click.option(
"--team-size",
"-t",
type=click.IntRange(1, 5),
help="""
Number of players per team.
\b
1 = normal hearts
2 = standard 4-player euchre
""",
),
click.option(
"--pass-size",
"-z",
type=click.IntRange(1, 5),
help="(maximum) Number of cards to pass",
),
click.option(
"--minimum-hand-size",
type=click.IntRange(1, None),
help="Minimum size of the hand",
),
click.option(
"--add-jokers",
type=click.IntRange(0, None),
help="number of jokers to add to the deck",
),
click.option(
"--minimum-kitty-size",
type=click.IntRange(0, None),
help="minimum number of cards in the kitty",
),
click.option(
"--deck-replication",
type=click.IntRange(1, None),
help="Minimum number of times the deck gets replicated",
),
]
def common_options(func):
return add_options(func, *_global_options)
def add_options(func, *options):
for o in options:
func = o(func)
return func
def make_and_play_game(
game: "Type[BaseGame]",
logging_directory: Optional[str],
start_time: Optional[str] = None,
**kwargs,
):
start_time = start_time if start_time else now()
g = game(start=start_time, **kwargs)
g.play()
g.write_log(logging_directory)
class BaseGame(abc.ABC):
def __init__(
self,
*,
deck_generator: Callable[
[Type[Cardstock], int], Tuple[Hand, int]
] = make_standard_deck,
human_player_type: Type[BaseHuman],
computer_player_type: Type[BaseComputer],
card_type: Type[Cardstock] = Card,
team_type: Type[BaseTeam],
game_name: str,
force_multiple_teams: bool = True,
handedness: int = 4,
team_size: int = 1,
points: int,
fixed_hand_size: Optional[int] = None,
start: str = now(),
pass_size: int = None,
add_jokers: int = None,
minimum_kitty_size: int = 0,
minimum_hand_size: int = 10,
deck_replication: int = 1,
single_human_name: str = "You",
all_bots: bool = False,
humans: List[int] = None,
shuffle_deck: bool = True,
**kwargs,
):
"""
The basic setup for a trick-taking card game.
Most of these params should either be from the Click kwargs or
left at their default values rather than parsing them in the
constructor of child classes.
:param deck_generator: function that generates the deck of cards
:param human_player_type: Class of human players
:param computer_player_type: Class for computer players
:param card_type: Class of card to use
:param team_type: Class of team
:param game_name: string of the game's name, should be the same as the filename
:param force_multiple_teams: make sure all the players aren't on the same team
:param handedness: number of players in the game
:param team_size: number of players on each team
:param points: victory threshold
:param single_human_name: the name override for games with one human player
:param start: string of game start time, used for logging
:param pass_size: number of cards to pass in passing games
:param add_jokers: number of jokers to add to the deck
:param fixed_hand_size: fix hand size instead of dealing all available cards
:param minimum_kitty_size: minimum number of leftover cards on each deal
:param minimum_hand_size: minimum hand size, determines deck replication
:param deck_replication: at least this many copies of the deck are created
:param all_bots: set True for all-bot testing action
:param humans: index locations of human players
:param shuffle_deck: shuffle before dealing?
:param kwargs: additional arguments to alter the game's setup and behavior
"""
"""
Basic setup and checks
"""
self.handedness: int = 4 if handedness is None else handedness
# sanity checking
if not team_size:
team_size = 1
assert (
self.handedness % team_size == 0
), f"{self.handedness} players can't divide into teams of {team_size}"
if force_multiple_teams:
assert ( # also handles negative team sizes
self.handedness // team_size > 1
), f"There's no game if everyone is on the same team"
# constants
self.victory_threshold: int = points
self.start_time: str = start if start else now()
c = configparser.ConfigParser()
c.read("constants.cfg")
def g_h() -> str:
return f"{game_name.capitalize()}-{self.handedness}"
if pass_size is None:
try:
pass_size = c.getint("Shoot Strength", g_h())
except configparser.NoOptionError:
try:
pass_size = c.getint("Shoot Strength", game_name)
except configparser.NoOptionError:
pass_size = c.getint("Shoot Strength", "Default")
self.pass_size: int = pass_size if pass_size else 0
"""
Make the deck
"""
# the initial constant configuration
self.kitty: Hand = Hand()
j = 0 if add_jokers is None else 0
self.minimum_kitty_size = (
0 if minimum_kitty_size is None else minimum_kitty_size
)
# actually make the deck
self.deck = make_minimum_sized_deck(
deck_generator,
card_type,
j,
self.handedness * (minimum_hand_size if minimum_hand_size else 1),
deck_replication if deck_replication else 1,
)
# calculate hand sizes and card counts
self.hand_size: int = fixed_hand_size if fixed_hand_size else (
len(self.deck) - self.minimum_kitty_size
) // self.handedness
self.fhs: Optional[int] = fixed_hand_size
self.suit_safety: Dict[Suit, Union[None, bool, TeamType, PlayerType]] = {}
self.reset_suit_safety()
"""
Setup players and teams
"""
# Get names
try: # preset name schema
player_names = c["Names"][g_h()].strip().split("\n")
except KeyError: # names randomly from a list
pph = c["Names"]["Grand Name Gallery"].strip().split("\n")
est: str = f"{self.handedness} is way too many players!\n"
est += "Edit the grand name gallery in constants.cfg to have "
est += f"at least {self.handedness-len(pph)} more names"
assert self.handedness <= len(pph), est
player_names = pph[
(j := random.randrange(len(pph) - self.handedness)) : j
+ self.handedness
]
# calculate player types
if not humans: # assume one human player as default
humans = [random.randrange(self.handedness)]
if all_bots:
humans = []
if len(humans) == 1 and humans[0] < self.handedness:
player_names[humans[0]] = single_human_name if single_human_name else "You"
self.players: List[PlayerType] = make_players(
player_names,
[
(human_player_type if i in humans else computer_player_type)
for i in range(self.handedness)
],
self,
)