forked from pgwhalen/euchre_sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zachsim_game.py
150 lines (119 loc) · 4.06 KB
/
zachsim_game.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
from random import shuffle, randrange
from functools import reduce
from game import Game
import utils
SUITS = ['s', 'h', 'd', 'c']
VALUES = ['9','T','J','Q','K','A']
MAX_SCORE = 100
class ZachSimGame(Game):
# override so that we can set MAX_SCORE
def play_game(self):
while (self._game_score[1] < MAX_SCORE and self._game_score[2] < MAX_SCORE):
self.play_hand()
print("--------------------")
print("Score:", self._game_score)
print("--------------------")
print("GAME OVER!")
# override so that we fix the dealer (doesn't change each hand)
# override so that we always play highest lead suit / lowest non-trump
def play_hand(self):
# dealer is the "last" player in order
self._dealer = self._players[3]
# deal
self.deal_hand()
# call trump
self.call_trump()
# display trump, top card and hands
print("====================")
self.print_hand()
# play starts with the "first" player in order
leader = 0
# number of players
numplayers = len(self._players)
# play tricks
print("Tricks:")
for _ in range(5):
# cards played in order from [leader..leader+numplayers-1]%numplayers
trick = []
# loop through players, starting with the leader
for n in range(numplayers):
p = self._players[(leader+n)%numplayers]
card = p.action(trick)
if p not in self._inactives:
# must follow suit
if len(trick) > 0 and not utils.did_follow_suit(self._hands[p], card, self._trump, trick[0]):
print("DEBUG: lead card: ", trick[0], " card: ", card, "hand: ", self._hands[p])
raise IllegalPlayException("Must follow suit")
# internal logic error
if card not in self._hands[p]:
raise IllegalPlayException("Player doesn't have that card to play")
trick.append(card)
self._hands[p].remove(card) # Game
winning_card = utils.best_card(trick, self._trump, trick[0])
winning_player_index = (trick.index(winning_card) + leader) % numplayers
winning_player = self._players[winning_player_index]
self._tricks_score[self._teams[winning_player]] += 1
print(winning_player.name, winning_card, trick)
# move leader to winning player
leader = winning_player_index
# score
self.score_hand()
# reset
self._trump = None
self._leftbower = None
self._top_card = None
self._inactives = []
for team_num in range(1, 3):
self._tricks_score[team_num] = 0
for p in self._players:
self._hands[p] = [] # Game
p.active = True
# don't change dealer each hand
# override so that we can rig the deal
def deal_hand(self):
self.__deck = [val + suit for val in VALUES for suit in SUITS]
shuffle(self.__deck)
# rig the game!
# 0) set trump (before we "deal")
self.call_trump()
# 1) give the first player some trump
# A) Jh, Kh, Th, 9h
# B) Jd, Kh, Th, 9h
self.__deck.remove("Jh")
self._hands[self._players[0]].append("Jh")
self.__deck.remove("Kh")
self._hands[self._players[0]].append("Kh")
self.__deck.remove("Th")
self._hands[self._players[0]].append("Th")
self.__deck.remove("9h")
self._hands[self._players[0]].append("9h")
# 2) give the dealer (4th player) one trump
# Either Qh or Ah or Jh, but never Jd, since that would force trump to be diamons
self.__deck.remove("Qh")
self._hands[self._players[3]].append("Qh")
# 3) deal the rest
count = 0
while (len(self.__deck) > 4):
for p in self._players:
if (len(self._hands[p]) < 5):
card = self.__deck.pop(0)
# p0 cannot pick up any more trump
if (p == self._players[0]):
while (card[1] == 'h' or card == 'Jd'):
count=count+1
self.__deck.append(card)
card = self.__deck.pop(0)
if (count>10):
print(card)
print(self.__deck)
self._hands[p].append(card)
self._top_card = self.__deck.pop()
# override so that we can force trump, called by dealer
def call_trump(self):
# dealer always chooses trump to be hearts
self._trump = 'h'
self._caller = self._players[3]
# set up left bower
self._leftbower = 'J' + utils.same_color(self._trump)
class IllegalPlayException(Exception):
pass