-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
232 lines (165 loc) · 7.54 KB
/
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
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
"""Class for encoding order of gameplay, in coordination with the client script."""
from random import shuffle
from itertools import chain, cycle
from Card import Card
from ClientClasses import *
from Player import Player
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from pygame.time import delay
class Game(object):
"""Class for encoding order of gameplay, in coordination with the client script."""
__slots__ = 'StartCardPositions', 'CardPositions', 'RepeatGame', 'Attributes', 'GameAttributes', 'Triggers', \
'StartPlay'
def __init__(self, PlayerNumber):
self.StartCardPositions = [i for i in range(PlayerNumber)]
self.CardPositions = self.StartCardPositions
self.StartPlay = False
self.RepeatGame = True
self.Attributes = AttributeTracker(True, PlayerNumber)
self.Triggers = Triggers()
# A few functions to be accessed by the threaded-client function.
@staticmethod
def AddPlayerName(name, playerindex):
Player.AllPlayers[playerindex].AddName(name)
def TimeToStart(self):
self.StartPlay = True
@staticmethod
def PlayerActionCompleted(playerindex):
Player.AllPlayers[playerindex].ActionComplete = True
def SetCardNumber(self, number):
self.Attributes.Game['StartCardNumber'] = int(number)
def PlayerMakesBid(self, playerindex, bid):
Player.AllPlayers[playerindex].MakeBid(int(bid))
self.Triggers.Surfaces['CurrentBoard'] += 1
def ExecutePlay(self, cardID, playerindex):
player = Player.AllPlayers[playerindex]
card = next(card for card in player.Hand if card.ID == cardID)
player.PlayCard(card, self.Attributes.Round['trumpsuit'])
card.SetPos(self.CardPositions[len(self.Attributes.Trick['PlayedCards'])])
self.Attributes.Trick['PlayedCards'].append(card)
self.Triggers.Surfaces['CurrentBoard'] += 1
def RepeatQuestionAnswer(self):
self.RepeatGame = True
# The remaining functions relate to the order of gameplay.
def WaitForPlayers(self, attribute):
self.Triggers.Events[attribute] += 1
while any(not player.ActionComplete for player in Player.AllPlayers):
delay(60)
Player.AllPlayers = [player.NextStage() for player in Player.AllPlayers]
def PlayGame(self):
# Wait until the opening sequence is complete
self.WaitForPlayers('GameInitialisation')
WhichRound = range(1, (self.Attributes.Game['StartCardNumber'] + 1))
HowManyCards = range(self.Attributes.Game['StartCardNumber'], 0, -1)
WhoLeads = cycle(Player.AllPlayers)
for roundnumber, cardnumber, RoundLeader in zip(WhichRound, HowManyCards, WhoLeads):
self.PlayRound(roundnumber, cardnumber, RoundLeader)
self.Attributes.Game['MaxPoints'] = max(player.Points for player in Player.AllPlayers)
self.Attributes.Game['Winners'] = [
player for player in Player.AllPlayers
if player.Points == self.Attributes.Game['MaxPoints']
]
for player in self.Attributes.Game['Winners']:
player.GamesWon += 1
self.Triggers.Surfaces['Scoreboard'] += 1
# Wait until all players have finished announcing the game winners.
self.WaitForPlayers('WinnersAnnounced')
if self.Attributes.Tournament['GamesPlayed']:
self.Attributes.Tournament['MaxGamesWon'] = max(player.GamesWon for player in Player.AllPlayers)
self.Attributes.Tournament['TournamentLeaders'] = [
player for player in Player.AllPlayers
if player.GamesWon == self.Attributes.Tournament['MaxGamesWon']
]
self.Triggers.Events['TournamentLeaders'] += 1
self.RepeatGame = False
while not self.RepeatGame:
delay(1)
self.NewGameReset()
# Wait until all players have logged their new playerindex.
self.WaitForPlayers('NewGameReset')
self.PlayGame()
def PlayRound(self, roundnumber, cardnumber, RoundLeader):
self.Attributes.Round['CardNumberThisRound'] = cardnumber
self.Attributes.Round['RoundNumber'] = roundnumber
self.Attributes.Round['RoundLeader'] = RoundLeader
RoundLeader.RoundLeader = True
self.Triggers.Surfaces['Scoreboard'] += 1
self.WaitForPlayers('RoundStart')
# the trumpcard is set as part of this function call.
self.NewPack()
self.WaitForPlayers('NewPack')
# Deal cards
Pack, trumsuit = self.Attributes.Round['PackOfCards'], self.Attributes.Round['trumpsuit']
Player.AllPlayers = [
player.ReceiveCards([Pack.pop() for i in range(cardnumber)], trumsuit)
for player in Player.AllPlayers
]
self.Triggers.Surfaces['TrumpCard'] += 1
self.Triggers.Surfaces['CurrentBoard'] += 1
self.WaitForPlayers('CardsDealt')
FirstPlayer = RoundLeader
RoundLeader.RoundLeader = False
for i in range(cardnumber):
FirstPlayerIndex = self.TrickStart(i + 1, FirstPlayer)
self.TrickMiddle(FirstPlayerIndex)
FirstPlayer = self.TrickEnd(self.Attributes.Trick['PlayedCards'])
self.WaitForPlayers('RoundEnd')
Player.AllPlayers = [player.ReceivePoints() for player in Player.AllPlayers]
self.Triggers.Surfaces['Scoreboard'] += 1
self.WaitForPlayers('PointsAwarded')
Player.AllPlayers = [player.EndOfRound() for player in Player.AllPlayers]
self.Attributes.Round['TrumpCard'] = None
self.Attributes.Round['trumpsuit'] = ''
if self.Attributes.Round['RoundNumber'] != self.Attributes.Game['StartCardNumber']:
self.Attributes.Round['RoundNumber'] += 1
self.Attributes.Round['CardNumberThisRound'] -= 1
self.Attributes.Trick['TrickNumber'] = 1
self.Triggers.Surfaces['CurrentBoard'] += 1
self.Triggers.Surfaces['Scoreboard'] += 1
def NewPack(self):
PackOfCards = [Card(value, suit) for value in range(2, 15) for suit in ('D', 'S', 'C', 'H')]
shuffle(PackOfCards)
TrumpCard = PackOfCards.pop()
self.Attributes.Round['trumpsuit'] = TrumpCard.ActualSuit
self.Attributes.Round['PackOfCards'] = PackOfCards
self.Attributes.Round['TrumpCard'] = TrumpCard
self.Triggers.Surfaces['TrumpCard'] += 1
def TrickStart(self, TrickNumber, FirstPlayer):
self.Attributes.Trick['TrickInProgress'] = True
self.Attributes.Trick['TrickNumber'] = TrickNumber
FirstPlayerIndex = FirstPlayer.playerindex
self.Attributes.Trick['FirstPlayerIndex'] = FirstPlayerIndex
self.CardPositions = (self.StartCardPositions[FirstPlayerIndex:]
+ self.StartCardPositions[:FirstPlayerIndex])
return FirstPlayerIndex
def TrickMiddle(self, FirstPlayerIndex):
self.WaitForPlayers('TrickStart')
for i in chain(range(FirstPlayerIndex, self.Attributes.Tournament['PlayerNumber']), range(FirstPlayerIndex)):
currentnumber = len(self.Attributes.Trick['PlayedCards'])
self.Attributes.Trick['WhoseTurnPlayerIndex'] = i
self.Triggers.Surfaces['CurrentBoard'] += 1
while len(self.Attributes.Trick['PlayedCards']) == currentnumber:
delay(60)
def TrickEnd(self, PlayedCards):
self.Attributes.Trick['WhoseTurnPlayerIndex'] = -1
self.Attributes.Trick['TrickInProgress'] = False
self.Attributes.Trick['Winner'] = (
max((card.DetermineWinValue(PlayedCards[0].ActualSuit, self.Attributes.Round['trumpsuit'])
for card in PlayedCards), key=Card.GetWinValue)).PlayedBy
self.Attributes.Trick['Winner'].WinsTrick()
self.Attributes.Trick['FirstPlayerIndex'] = 0
delay(500)
PlayedCards.clear()
self.Triggers.Surfaces['CurrentBoard'] += 1
self.WaitForPlayers('TrickEnd')
return self.Attributes.Trick['Winner']
def NewGameReset(self):
self.Attributes.Game['StartCardNumber'] = 0
self.Attributes.Round['RoundNumber'] = 1
Player.AllPlayers = Player.AllPlayers[1:] + Player.AllPlayers[:1]
Player.AllPlayers = [player.ResetPlayer(Player.AllPlayers.index(player)) for player in Player.AllPlayers]
self.Attributes.Tournament['gameplayers'] = Player.AllPlayers
self.Attributes.Tournament['GamesPlayed'] += 1
self.Triggers.Surfaces['Scoreboard'] += 1
self.StartPlay = False