-
Notifications
You must be signed in to change notification settings - Fork 0
/
JZgombic_Assignment8.py
358 lines (252 loc) · 11.3 KB
/
JZgombic_Assignment8.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import sys
import time
random.seed(0)
def main():
number_of_players = int(input("\nPlease enter the number of players: "))
if (number_of_players<2):
print ("\nThis is not a one player game, please invite some friends!")
main()
else:
try:
number_of_players
except ValueError:
print ("Kindly input an integer.")
main()
try:
computer_players = int(input("\nHow many of these are Computer players: "))
except ValueError:
print ("\nKindly input an integer.")
main()
timed()
human_players = number_of_players - computer_players
playing = PlayerFactory()
for num in range(human_players):
playing.playerNum.append('human')
for num in range(computer_players):
playing.playerNum.append('computer')
playing.getPlayers()
players = playing.export_list
TimedGameProxy(players)
class Player(object):
def __init__(self, playerX):
self.playerID = playerX
self.roll = True
self.total_score = 0
self.type = 'human'
def choice(self):
try:
choice = input("\nWhat would you like to do: Hold (h) or Roll (r)?")
if choice not in ('h', 'r'):
raise ValueError
if choice.lower() == 'h':
self.roll = False
print ("\nYour have chosen to Hold. This ends your turn.\n")
if choice.lower() == 'r':
self.roll = True
print ("Rolling.....\n")
except ValueError:
print ("\nPlease enter a valid selection: h for Hold or r for Roll.\n")
self.choice()
class ComputerPlayer(Player):
def __init__(self, playerX):
Player.__init__(self, playerX)
self.type = 'computer'
def Choice(self, choice):
if choice.lower() == 'h':
self.roll = False
print ("\nThe Computer has chosen to Hold. This ends their turn.\n")
if choice.lower() == 'r':
self.roll = True
print ("Rolling.....\n")
class HumanPlayer(Player):
pass
class Dice(object):
def __init__(self):
self.roll_value = None
def roll(self):
self.roll_value = random.randint(1, 6)
return self.roll_value
class Game(object):
def __init__(self, playerNum):
pass
def change_turn(self, player):
pass
def start_turn(self, player):
pass
class TimedGameProxy(Game):
def __init__(self, playerNum):
self.timed = True
self.starting_time = time.time()
self.time_limit = timed
Game.__init__(self, playerNum)
self.players = playerNum
self.goal = 100
self.first_player = random.randint(1, (len(self.players) - 1))
self.next_player = None
self.dice = Dice()
self.current_player = self.players[self.first_player]
print ("\nPlayer {} will go first\n".format(self.current_player.playerID))
self.start_turn(self.current_player)
def start_turn(self, player):
turn_score = 0
count = len(self.players) - 1
print (" The updated scores are as follows:")
while count >= 0:
print (" Player {} has {} points.".format(self.players[count].playerID, self.players[count].total_score))
count -= 1
print ("\n\n Player {}:".format(player.playerID))
if player.type == 'human':
player.roll = True
while player.roll:
die_num = self.dice.roll()
if die_num == 1:
print ("\n You rolled a 1, your turn score is 0 and the next player will now roll.\n")
player.roll = False
elif (turn_score + player.total_score) >= self.goal:
player.roll = False
elif timer == True and (self.time_limit - (time.time() - self.starting_time) <= 0):
player.roll = False
elif timer == True and (self.time_limit - (time.time() - self.starting_time) > 0):
turn_score = turn_score + die_num
print ("\nYou now have {:0.2f} seconds left until the game ends.".format(self.time_limit - (time.time() - self.starting_time)))
print ("\n You rolled a {} ".format(self.dice.roll_value))
print (" Your score for this round is {}".format(turn_score))
print (" Your total score for the game is {}".format(player.total_score))
print (" If you decide to hold, your total score will be {}".format((turn_score + player.total_score)))
player.choice()
else:
turn_score = turn_score + die_num
print ("\n You rolled a {} ".format(self.dice.roll_value))
print (" Your score for this round is {}".format(turn_score))
print (" Your total score for the game is {}".format(player.total_score))
print (" If you decide to hold, your total score will be {}".format((turn_score + player.total_score)))
player.choice()
player.total_score += turn_score
print (" Your total score is now {}.\n".format(player.total_score))
self.change_turn(player)
if player.type == 'computer':
player.roll = True
if 25 < (100 - (turn_score + player.total_score)):
hold = 25
elif (100 - (turn_score + player.total_score)) < 25:
hold = (100 - (turn_score + player.total_score))
while player.roll:
die_num = self.dice.roll()
if die_num == 1:
print ("\n You rolled a 1, your turn score is 0 and the next player will now roll.\n")
player.roll = False
elif (turn_score + player.total_score) >= self.goal:
player.roll = False
elif (turn_score + player.total_score) == hold:
player.roll = False
elif timer == True and (self.time_limit - (time.time() - self.starting_time) <= 0):
player.roll = False
elif timer == True and (self.time_limit - (time.time() - self.starting_time) > 0):
turn_score = turn_score + die_num
print ("\nYou now have {:0.2f} seconds left until the game ends.".format(self.time_limit - (time.time() - self.starting_time)))
print ("\n You rolled a {} ".format(self.dice.roll_value))
print (" Your score for this round is {}".format(turn_score))
print (" Your total score for the game is {}".format(player.total_score))
else:
turn_score = turn_score + die_num
print ("\n You rolled a {} ".format(self.dice.roll_value))
print (" Your score for this round is {}".format(turn_score))
print (" Your total score for the game is {}".format(player.total_score))
player.total_score += turn_score
print (" Your total score is now {}.\n".format(player.total_score))
self.change_turn(player)
def change_turn(self, player):
if player.total_score >= self.goal:
print ("\n Player {} has won!!!!!!!!".format(player.playerID))
print (" Player {} has won!!!!!!!!".format(player.playerID))
print (" Player {} has won!!!!!!!!".format(player.playerID))
self.proceed = False
try:
play = input("\n\nWould you like to play again: y for Yes or n for No ")
if play.lower() not in ('y', 'n'):
raise ValueError
if play.lower() == 'y':
number_of_players = ""
computer_players = ""
main()
if play.lower() == 'n':
sys.exit
except ValueError:
print ("Please enter a valid selection: either y for Yes or n for No.")
self.replay()
elif timer == True and (self.time_limit - (time.time() - self.starting_time) <= 0):
print (" Time has expired.\n")
print (" The final scores are as follows:")
count = len(self.players) - 1
while count >= 0:
print (" Player {} has {} points.".format(self.players[count].playerID, self.players[count].total_score))
count -= 1
self.proceed = False
try:
play = input("\n\nWould you like to play again: y for Yes or n for No ")
if play.lower() not in ('y', 'n'):
raise ValueError
if play.lower() == 'y':
number_of_players = ""
computer_players = ""
main()
if play.lower() == 'n':
sys.exit
except ValueError:
print ("Please enter a valid selection: either y for Yes or n for No.")
self.replay()
else:
if player.playerID == len(self.players):
self.first_player = 0
self.current_player = self.players[0]
self.start_turn(self.current_player)
else:
self.first_player += 1
self.current_player = self.players[self.first_player]
self.start_turn(self.current_player)
def timed():
global timer
global timed
timer = False
try:
time = input("\n\nWould you like this game to be timed: y for Yes or n for No ")
if time.lower() not in ('y', 'n'):
raise ValueError
if time.lower() == 'y':
timer = True
try:
timed = int(input("\nPlease enter a time limit (Enter 0 to accept the default of 60 seconds): "))
if timed == 0:
timed = 60
elif (timed < 0):
raise ValueError
else:
timed
except ValueError:
print ("Kindly input an integer.")
timed()
return timer
if time.lower() == 'n':
timer = False
return timer
except ValueError:
print ("Please enter a valid selection: either y for Yes or n for No.")
timer()
class PlayerFactory(object):
def __init__(self):
self.playerNum = []
self.export_list = []
def getPlayers(self):
count = 1
for item in self.playerNum:
if item == 'human':
self.export_list.append(HumanPlayer(count))
count += 1
if item == 'computer':
self.export_list.append(ComputerPlayer(count))
count += 1
if __name__ == '__main__':
main()