-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
277 lines (234 loc) · 7.15 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
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
from deck import Deck
from hand import Hand
from player import Player
from card import Card
class Game(object):
MAXPOINTS = 16
SPEED = 100
def __init__(self):
self.__players = []
self.__board = Hand()
self.__deck = Deck()
self.nextInTurn = 0
self.nextDealer = -1
self.extraCardPoints=0
self.extraSpadesPoints=0
self.lastRaise=None
def initForNewRound(self):
self.__deck = Deck()
for pl in self.__players:
pl.setHand(self.deal(4))
pl.clearStack()
pl.clearCottages()
self.lastRaise=None
self.nextDealer += 1
self.nextInTurn = self.nextDealer+1
self.initBoard()
def addPlayer(self,playerName, tyyppi):
player = Player(playerName,tyyppi)
self.__players.append(player)
def addPlayer2(self,player):
self.__players.append(player)
def getPlayers(self):
return self.__players
def getNextInTurn(self):
pl = self.__players[self.nextInTurn%len(self.__players)]
self.nextInTurn+=1
return pl
def initBoard(self):
cards = self.__deck.deal(4)
self.__board.addCards(cards)
def getBoard(self):
return self.__board
'''
Checks if game is over/ someone has 16 or more points
'''
def gameOver(self):
for player in self.__players:
if player.getPoints()>=Game.MAXPOINTS:
return True
return False
def deal(self,amount):
return self.__deck.deal(amount)
def nextInTurnType(self):
return self.__players[self.nextInTurn%len(self.__players)].getType()
def getDeck(self):
return self.__deck
'''
returns boolean value determining if round should be continued i.e. if cards are left on deck or on players hands
'''
def continueRound(self):
if self.getDeck().cardsLeft()>0:
return True
for pl in self.getPlayers():
if pl.hasCards():
return True
return False
'''
This function deals the card on board to the player who was the last one to raise cards
'''
def dealBoardToLatest(self):
if len(self.getBoard().getCards())>0:
try:
self.lastRaise.addCardsToStack(self.getBoard().getCards())
self.getBoard().clear()
except AttributeError:
self.getBoard().clear()
'''
THIS FUNCTION IS NOT USED ANYMORE FOR ANYTHING
This function returns the minimum limit used in computerPlayerIO's chooseCardToTable function to determine the MIVALUE-limit
'''
def getMinLimit(self):
cards=[]
c10=Card(10,Card.DIAMONDS)
s2=Card(2,Card.SPADES)
for pl in self.__players:
cards.extend(pl.getStack().getCards())
#cards.sort()
am=[0]*13
for c in cards:
num=c.getHandValue()-2
if c==c10:
num=8 #10-2
elif c==s2:
num=0 #2-2
am[num]+=1
if c10 not in cards:
return 16
if s2 not in cards:
return 15
for i in range(12,-1,-1):
if am[i]!=4:
return i+2
return 16
def getAllStackCardsFromPlayers(self):
cards=[]
for pl in self.__players:
cards.extend(pl.getStack().getCards())
return cards
'''
Window's saveGame method uses this method for saving the game
'''
def saveGame(self):
final_text=''
#players
plrs=''
for i in self.getPlayers():
text=''
text+='NAM'
name=i.getName()
pit=len(name)
if pit<10:
text+='00'
elif pit<100:
text+='0'
text+=str(pit)
text+=name
text+='COT'
cot=i.getCottages()
if cot<10:
text+='001'
elif cot<100:
text+='002'
elif cot>100:
text+='003'
text+=str(cot)
points=i.getPoints()
text+='POI'
if points<10:
text+='001'
elif points<100:
text+='002'
elif points>100:
text+='003'
text+=str(points)
hand=i.getHand().getCards()
am=len(hand)
text+='HAN'
text+='00'
text+=str(am)
for card in hand:
text+=card.cardSave()
text+='TYP'
text+='001'
typ=i.getType()
text+=str(typ)
stack=i.getStack().getCards()
text+='STA'
if len(stack)<10:
text+='00'
elif len(stack)<100:
text+='0'
text+=str(len(stack))
for card in stack:
text+=card.cardSave()
text+='END001'
text2='PLR'
if len(text)<10:
text2+='00'
elif len(text)<100:
text2+='0'
text2+=str(len(text))
text2+=text
plrs+=text2
final_text=plrs
#board
board=self.getBoard().getCards()
txt='BOA'
if len(board)<10:
txt+='00'
elif len(board)<100:
txt+='0'
txt+=str(len(board))
for card in board:
txt+=card.cardSave()
final_text+=txt
#deck
deck=self.getDeck().getCards()
txt='DEC'
if len(deck)<10:
txt+='00'
elif len(deck)<100:
txt+='0'
txt+=str(len(deck))
for card in deck:
txt+=card.cardSave()
final_text+=txt
#nextInTurn
txt='NTU001'+str(self.nextInTurn%len(self.__players))
final_text+=txt
#nextDealer
txt='NDE001'+str(self.nextDealer%len(self.__players)%len(self.__players))
final_text+=txt
#extraCardPoints
extraCardPoints=self.extraCardPoints
txt='ECP'
if extraCardPoints<10:
txt+='001'
elif extraCardPoints<100:
txt+='002'
else:
txt+='003'
txt+=str(extraCardPoints)
final_text+=txt
#extraSpadesPoints
extraSpadesPoints=self.extraSpadesPoints
txt='ESP'
if extraSpadesPoints<10:
txt+='001'
elif extraSpadesPoints<100:
txt+='002'
else:
txt+='003'
txt+=str(extraSpadesPoints)
final_text+=txt
#lastRaise
lastRaise=9
txt='LRA001'
if self.lastRaise!=None:
lastRaise=self.__players.index(self.lastRaise)
txt+=str(lastRaise%len(self.__players))
else:
txt+=str(lastRaise)
final_text+=txt
return final_text