forked from coding-horror/basic-computer-games
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasketball.py
More file actions
389 lines (348 loc) · 13.6 KB
/
basketball.py
File metadata and controls
389 lines (348 loc) · 13.6 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
"""
The basketball class is a computer game that allows you to play as
Dartmouth College's captain and playmaker
The game uses set probabilites to simulate outcomes of each posession
You are able to choose your shot types as well as defensive formations
"""
import random
from typing import List, Literal, Optional
def print_intro() -> None:
print("\t\t\t Basketball")
print("\t Creative Computing Morristown, New Jersey\n\n\n")
print("This is Dartmouth College basketball. ")
print("Υou will be Dartmouth captain and playmaker.")
print("Call shots as follows:")
print(
"1. Long (30ft.) Jump Shot; "
"2. Short (15 ft.) Jump Shot; "
"3. Lay up; 4. Set Shot"
)
print("Both teams will use the same defense. Call Defense as follows:")
print("6. Press; 6.5 Man-to-Man; 7. Zone; 7.5 None.")
print("To change defense, just type 0 as your next shot.")
print("Your starting defense will be? ", end="")
class Basketball:
def __init__(self) -> None:
self.time = 0
self.score = [0, 0] # first value is opponents score, second is home
self.defense_choices: List[float] = [6, 6.5, 7, 7.5]
self.shot: Optional[int] = None
self.shot_choices: List[Literal[0, 1, 2, 3, 4]] = [0, 1, 2, 3, 4]
self.z1: Optional[float] = None
print_intro()
self.defense = get_defense_choice(self.defense_choices)
self.opponent = get_opponents_name()
self.start_of_period()
def add_points(self, team: Literal[0, 1], points: Literal[0, 1, 2]) -> None:
"""
Add points to the score.
Team can take 0 or 1, for opponent or Dartmouth, respectively
"""
self.score[team] += points
self.print_score()
def ball_passed_back(self) -> None:
print("Ball passed back to you. ", end="")
self.dartmouth_ball()
def change_defense(self) -> None:
"""change defense, called when the user enters 0 for their shot"""
defense = None
while defense not in self.defense_choices:
print("Your new defensive allignment is? ")
try:
defense = float(input())
except ValueError:
continue
assert isinstance(defense, float)
self.defense = defense
self.dartmouth_ball()
def foul_shots(self, team: Literal[0, 1]) -> None:
"""Simulate two foul shots for a player and adds the points."""
print("Shooter fouled. Two shots.")
if random.random() > 0.49:
if random.random() > 0.75:
print("Both shots missed.")
else:
print("Shooter makes one shot and misses one.")
self.score[team] += 1
else:
print("Shooter makes both shots.")
self.score[team] += 2
self.print_score()
def halftime(self) -> None:
"""called when t = 50, starts a new period"""
print("\n ***** End of first half *****\n")
self.print_score()
self.start_of_period()
def print_score(self) -> None:
"""Print the current score"""
print(f"Score: {self.score[1]} to {self.score[0]}\n")
def start_of_period(self) -> None:
"""Simulate a center jump for posession at the beginning of a period"""
print("Center jump")
if random.random() > 0.6:
print("Dartmouth controls the tap.\n")
self.dartmouth_ball()
else:
print(self.opponent + " controls the tap.\n")
self.opponent_ball()
def two_minute_warning(self) -> None:
"""called when t = 92"""
print(" *** Two minutes left in the game ***")
def dartmouth_jump_shot(self) -> None:
"""called when the user enters 1 or 2 for their shot"""
self.time += 1
if self.time == 50:
self.halftime()
elif self.time == 92:
self.two_minute_warning()
print("Jump Shot.")
# simulates chances of different possible outcomes
if random.random() > 0.341 * self.defense / 8:
if random.random() > 0.682 * self.defense / 8:
if random.random() > 0.782 * self.defense / 8:
if random.random() > 0.843 * self.defense / 8:
print("Charging foul. Dartmouth loses ball.\n")
else:
# player is fouled
self.foul_shots(1)
self.opponent_ball()
elif random.random() > 0.5:
print(
"Shot is blocked. Ball controlled by "
+ self.opponent
+ ".\n"
)
self.opponent_ball()
else:
print("Shot is blocked. Ball controlled by Dartmouth.")
self.dartmouth_ball()
else:
print("Shot is off target.")
if self.defense / 6 * random.random() > 0.45:
print(f"Rebound to {self.opponent}\n")
self.opponent_ball()
else:
print("Dartmouth controls the rebound.")
if random.random() > 0.4:
if self.defense == 6 and random.random() > 0.6:
print(f"Pass stolen by {self.opponent}, easy lay up")
self.add_points(0, 2)
self.dartmouth_ball()
else:
# ball is passed back to you
self.ball_passed_back()
else:
print()
self.dartmouth_non_jump_shot()
else:
print("Shot is good.")
self.add_points(1, 2)
self.opponent_ball()
def dartmouth_non_jump_shot(self) -> None:
"""
Lay up, set shot, or defense change
called when the user enters 0, 3, or 4
"""
self.time += 1
if self.time == 50:
self.halftime()
elif self.time == 92:
self.two_minute_warning()
if self.shot == 4:
print("Set shot.")
elif self.shot == 3:
print("Lay up.")
elif self.shot == 0:
self.change_defense()
# simulates different outcomes after a lay up or set shot
if 7 / self.defense * random.random() > 0.4:
if 7 / self.defense * random.random() > 0.7:
if 7 / self.defense * random.random() > 0.875:
if 7 / self.defense * random.random() > 0.925:
print("Charging foul. Dartmouth loses the ball.\n")
else:
print(f"Shot blocked. {self.opponent}'s ball.\n")
else:
self.foul_shots(1)
self.opponent_ball()
else:
print("Shot is off the rim.")
if random.random() > 2 / 3:
print("Dartmouth controls the rebound.")
if random.random() > 0.4:
print("Ball passed back to you.\n")
self.dartmouth_ball()
else:
self.dartmouth_non_jump_shot()
else:
print(self.opponent + " controls the rebound.\n")
self.opponent_ball()
else:
print("Shot is good. Two points.")
self.add_points(1, 2)
self.opponent_ball()
def dartmouth_ball(self) -> None:
"""plays out a Dartmouth posession, starting with your choice of shot"""
shot = get_dartmouth_ball_choice(self.shot_choices)
self.shot = shot
if self.time < 100 or random.random() < 0.5:
if self.shot in [1, 2]:
self.dartmouth_jump_shot()
else:
self.dartmouth_non_jump_shot()
elif self.score[0] == self.score[1]:
print("\n ***** End Of Second Half *****")
print("Score at end of regulation time:")
print(
" Dartmouth: "
+ str(self.score[1])
+ " "
+ self.opponent
+ ": "
+ str(self.score[0])
)
print("Begin two minute overtime period")
self.time = 93
self.start_of_period()
else:
print("\n ***** End Of Game *****")
print(
"Final Score: Dartmouth: "
+ str(self.score[1])
+ " "
+ self.opponent
+ ": "
+ str(self.score[0])
)
def opponent_jumpshot(self) -> None:
"""Simulate the opponents jumpshot"""
print("Jump Shot.")
if 8 / self.defense * random.random() > 0.35:
if 8 / self.defense * random.random() > 0.75:
if 8 / self.defense * random.random() > 0.9:
print("Offensive foul. Dartmouth's ball.\n")
else:
self.foul_shots(0)
self.dartmouth_ball()
else:
print("Shot is off the rim.")
if self.defense / 6 * random.random() > 0.5:
print(f"{self.opponent} controls the rebound.")
if (
self.defense == 6
and random.random() <= 0.75
and random.random() > 0.5
):
print()
self.opponent_non_jumpshot()
elif (
self.defense == 6
and random.random() <= 0.75
and random.random() <= 0.5
or self.defense != 6
and random.random() <= 0.5
):
print(f"Pass back to {self.opponent} guard.\n")
self.opponent_ball()
elif self.defense == 6 and random.random() > 0.75:
print("Ball stolen. Easy lay up for Dartmouth.")
self.add_points(1, 2)
self.opponent_ball()
else:
self.opponent_non_jumpshot()
else:
print("Dartmouth controls the rebound.\n")
self.dartmouth_ball()
else:
print("Shot is good.")
self.add_points(0, 2)
self.dartmouth_ball()
def opponent_non_jumpshot(self) -> None:
"""Simulate opponents lay up or set shot."""
if self.z1 > 3: # type: ignore
print("Set shot.")
else:
print("Lay up")
if 7 / self.defense * random.random() > 0.413:
print("Shot is missed.")
if self.defense / 6 * random.random() > 0.5:
print(f"{self.opponent} controls the rebound.")
if (
self.defense == 6
and random.random() <= 0.75
and random.random() > 0.5
or self.defense != 6
and random.random() > 0.5
):
print()
self.opponent_non_jumpshot()
elif (
self.defense == 6
and random.random() <= 0.75
and random.random() <= 0.5
):
print(f"Pass back to {self.opponent} guard.\n")
self.opponent_ball()
elif self.defense == 6 and random.random() > 0.75:
print("Ball stolen. Easy lay up for Dartmouth.")
self.add_points(1, 2)
self.opponent_ball()
else:
print(f"Pass back to {self.opponent} guard\n")
self.opponent_ball()
else:
print("Dartmouth controls the rebound.\n")
self.dartmouth_ball()
else:
print("Shot is good.")
self.add_points(0, 2)
self.dartmouth_ball()
def opponent_ball(self) -> None:
"""
Simulate an opponents possesion
Randomly picks jump shot or lay up / set shot.
"""
self.time += 1
if self.time == 50:
self.halftime()
self.z1 = 10 / 4 * random.random() + 1
if self.z1 > 2:
self.opponent_non_jumpshot()
else:
self.opponent_jumpshot()
def get_defense_choice(defense_choices: List[float]) -> float:
"""Takes input for a defense"""
try:
defense = float(input())
except ValueError:
defense = None
# if the input wasn't a valid defense, takes input again
while defense not in defense_choices:
print("Your new defensive allignment is? ", end="")
try:
defense = float(input())
except ValueError:
continue
assert isinstance(defense, float)
return defense
def get_dartmouth_ball_choice(shot_choices: List[Literal[0, 1, 2, 3, 4]]) -> int:
print("Your shot? ", end="")
shot = None
try:
shot = int(input())
except ValueError:
shot = None
while shot not in shot_choices:
print("Incorrect answer. Retype it. Your shot? ", end="")
try:
shot = int(input())
except Exception:
continue
assert isinstance(shot, int)
return shot
def get_opponents_name() -> str:
"""Take input for opponent's name"""
print("\nChoose your opponent? ", end="")
return input()
if __name__ == "__main__":
Basketball()