This repository has been archived by the owner on Jan 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmy_classes.py
198 lines (167 loc) · 5.74 KB
/
my_classes.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
from my_globals import *
import AIs
import random
# holder and runner of the entire game
class Euchre():
def __init__(self):
# initialize AI's here
self.playerA1 = AIs.SimpleStat("Alex")
self.playerA2 = AIs.RandomPlay("Brian")
self.playerB1 = AIs.RandomPlay("Katrina")
self.playerB2 = AIs.RandomPlay("Sandy")
# make a list of the players for rotations
self.players = [self.playerA1, self.playerB1, self.playerA2, self.playerB2]
self.playerA1.setRelations(self.playerA2, self.playerB1, self.playerB2)
self.deck = list(allcards[:]) # get a deep copy of all_cards for dealing
def playGame(self): # begins the first round
# determine first dealer
# start at last player so that the first rotate in playRound has the first player deal
game.dealer = self.playerB2
# moved the multiple playRound calls into here
# iterative instead of recursive
while not self.hasWinner():
self.playRound()
for p in self.players:
p.reset()
self.endGame()
def dealCards(self):
random.shuffle(self.deck) # mutates deck
deal_index = self.players.index(game.dealer)
for x in range(1, 5):
self.players[(x+deal_index)%4].setHand(self.deck[:5])
self.deck = self.deck[5:]
def rotateDeal(self): # rotate the dealer
# use this if game.dealer is a direct reference
game.dealer = self.players[(self.players.index(game.dealer)+1)%4]
def playRound(self): # begins the next 5 tricks
game.resetRound()
self.rotateDeal()
self.deck = list(allcards[:])
self.dealCards()
# prepare for round
game.caller = self.orderUpDealerSec()
if game.caller:
# TODO
# have dealer pick up and discard
game.trump = self.deck[0].suit
self.deck[0] = game.dealer.pickUp(self.deck[0])
else:
game.caller, game.trump = self.pickSuitSec(self.deck[0].suit)
# TODO
# stick the dealer
pass
for p in self.players:
p.trumpIsSet()
# play 5 tricks
winner = self.players[(self.players.index(game.dealer) + 1)%4]
for x in range(5):
winner = self.playTrick(winner)
keys = game.center.keys()
txt = "End of trick:\n\t%s, %s, %s, %s\n\tWinner:%s\n" % (keys[0], keys[1], keys[2], keys[3],winner.name)
out.log(txt)
for p in self.players:
p.updateInfo(winner)
self.allotScore()
out.log("End of round:\n\tTrump: %s\n\tCaller: %s\n\tScores A: %d B: %d\n" % \
(game.trump, game.caller.name, game.scoreA, game.scoreB))
def playTrick(self, leader):
game.resetTrick()
# get the start card from leader
# assume cards played are legal
played = leader.playCard()
game.center[played] = leader
# handle the lead/left bower problem
if played.num == 11 and played.suit == offSuit(game.trump):
game.lead = offSuit(game.trump)
else:
game.lead = played.suit
# play the trick
leader_index = self.players.index(leader)
for x in range(1, 4):
played = self.players[(leader_index+x)%4].playCard()
game.center[played] = self.players[(leader_index+x)%4]
# return the winner of the trick
win_card = self.getWinningCard()
win_player = game.center[win_card]
if win_player == self.playerA1 or win_player == self.playerA2:
game.tricksA += 1
else:
game.tricksB += 1
return win_player
def allotScore(self):
# simple rules used, no going alone
if game.caller == self.playerA1 or game.caller == self.playerA2: # caller is in team A
c_team = "A"
c_tricks = game.tricksA
else:
c_tricks = game.tricksB
c_team = "B"
out.log("\t\tCaller: %s Tricks: A; %d B; %d C: %d" % (c_team, game.tricksA, game.tricksB, c_tricks))
if c_tricks == 0:
if c_team != "A":
game.scoreA += 4
else:
game.scoreB += 4
elif c_tricks == 1 or c_tricks == 2:
if c_team != "A":
game.scoreA += 2
else:
game.scoreB += 2
elif c_tricks == 3 or c_tricks == 4:
if c_team == "A":
game.scoreA += 1
else:
game.scoreB += 1
else:
if c_team == "A":
game.scoreA += 2
else:
game.scoreB += 2
def hasWinner(self):
if game.scoreA >= 10:
print "Team A Has won!"
return True
elif game.scoreB >= 10:
print "Team B Has won!"
return True
else:
return False
def endGame(self): # ends the game
if game.scoreA >= 10:
print "Players %s and %s have won!" % (self.playerA1.name, self.playerA2.name)
out.log("Players %s and %s have won!" % (self.playerA1.name, self.playerA2.name))
elif game.scoreB >= 10:
print "Players %s and %s have won!" % (self.playerB1.name, self.playerB2.name)
out.log("Players %s and %s have won!" % (self.playerB1.name, self.playerB2.name))
print "With a score of %d to %d." % (game.scoreA, game.scoreB)
out.log("With a score of %d to %d." % (game.scoreA, game.scoreB))
def getWinningCard(self):
return sorted(game.center, key=curCardVal, reverse=True)[0]
# temp = [(x, self.curCardVal(x)) for x in game.center]
# best = temp[0][1]
# bext_x = 0
# for x in range(1, len(temp)):
# if temp[x][1] > best:
# best = temp[x][1]
# best_x = x
# return temp[x][0]
def orderUpDealerSec(self):
deal_index = self.players.index(game.dealer)
for x in range(1, 5):
cur_player = self.players[(deal_index+x)%4]
if cur_player.orderUp(self.deck[0]):
placed = game.dealer.pickUp(self.deck[0])
self.deck[0] = placed
return cur_player
else:
pass # display that the player passed
return None
def pickSuitSec(self, out_suit):
for x in range(1, 5):
cur_player = self.players[(self.players.index(game.dealer)+x)%4]
picked = cur_player.pickSuit(out_suit)
if picked:
return self.players[(self.players.index(game.dealer)+x)%4], picked
else:
pass # display that the player passed
return None