-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBasketball.java
455 lines (421 loc) · 15.3 KB
/
Basketball.java
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import java.lang.Math;
import java.util.*;
import java.util.Scanner;
public class Basketball {
int time = 0;
int[] score = {0, 0};
double defense = -1;
List<Double> defense_choices = Arrays.asList(6.0, 6.5, 7.0, 7.5);
int shot = -1;
List<Integer> shot_choices = Arrays.asList(0, 1, 2, 3, 4);
double opponent_chance = 0;
String opponent = null;
public Basketball() {
// Explains the keyboard inputs
System.out.println("\t\t\t Basketball");
System.out.println("This is Hender College basketball. ");
System.out.println("Υou will be Hender captain and playmaker.");
System.out.println("Call shots as follows:");
System.out.println("1. Long (30ft.) Jump Shot; 2. Short (15 ft.) Jump Shot; "
+ "3. Lay up; 4. Set Shot");
System.out.println("Both teams will use the same defense. Call Defense as follows:");
System.out.println("6. Press; 6.5 Man-to-Man; 7. Zone; 7.5 None.");
System.out.println("To change defense, just type 0 as your next shot.");
System.out.print("Your starting defense will be? ");
Scanner scanner = new Scanner(System.in); // creates a scanner
// takes input for a defense
if (scanner.hasNextDouble()) {
defense = scanner.nextDouble();
}
else {
scanner.next();
}
// makes sure that input is legal
while (!defense_choices.contains(defense)) {
System.out.print("Your new defensive alignment is? ");
if (scanner.hasNextDouble()) {
defense = scanner.nextDouble();
}
else {
scanner.next();
continue;
}
}
// takes input for opponent's name
System.out.print("\nChoose your opponent? ");
opponent = scanner.next();
start_of_period();
}
// adds points to the score
// team can take 0 or 1, for opponent or Hender
private void add_points(int team, int points) {
score[team] += points;
print_score();
}
private void ball_passed_back() {
System.out.print("Ball passed back to you. ");
hender_ball();
}
// change defense, called when the user enters 0 for their shot
private void change_defense() {
defense = -1;
Scanner scanner = new Scanner(System.in);
while (!defense_choices.contains(defense)) {
System.out.println("Your new defensive alignment is? ");
if (scanner.hasNextDouble()) {
defense = (double)(scanner.nextDouble());
}
else {
continue;
}
}
hender_ball();
}
// simulates two foul shots for a player and adds the points
private void foul_shots(int team) {
System.out.println("Shooter fouled.Two shots.");
if (Math.random() > .49) {
if (Math.random() > .75) {
System.out.println("Both shots missed.");
}
else {
System.out.println("Shooter makes one shot and misses one.");
score[team] += 1;
}
}
else {
System.out.println("Shooter makes both shots.");
score[team] += 2;
}
print_score();
}
// called when time = 50, starts a new period
private void halftime() {
System.out.println("\n ***** End of first half *****\n");
print_score();
start_of_period();
}
// prints the current score
private void print_score() {
System.out.println("Score: " + score[1] + " to " + score[0] + "\n");
}
// simulates a center jump for possession at the beginning of a period
private void start_of_period() {
System.out.println("Center jump");
if (Math.random() > .6) {
System.out.println("Hender controls the tap.\n");
hender_ball();
}
else {
System.out.println(opponent + " controls the tap.\n");
opponent_ball();
}
}
// called when t = 92
private void two_minute_warning() {
System.out.println(" *** Two minutes left in the game ***");
}
// called when the user enters 1 or 2 for their shot
private void hender_jump_shot() {
time ++;
if (time == 50) {
halftime();
}
else if (time == 92) {
two_minute_warning();
}
System.out.println("Jump Shot.");
// simulates chances of different possible outcomes
if (Math.random() > .341 * defense / 8) {
if (Math.random() > .682 * defense / 8) {
if (Math.random() > .782 * defense / 8) {
if (Math.random() > .843 * defense / 8) {
System.out.println("Charging foul. Hender loses ball.\n");
}
else {
// player is fouled
foul_shots(1);
}
opponent_ball();
}
else {
if (Math.random() > .5) {
System.out.println("Shot is blocked. Ball controlled by " +
opponent + ".\n");
opponent_ball();
}
else {
System.out.println("Shot is blocked. Ball controlled by Hender.");
hender_ball();
}
}
}
else {
System.out.println("Shot is off target.");
if (defense / 6 * Math.random() > .45) {
System.out.println("Rebound to " + opponent + "\n");
opponent_ball();
}
else {
System.out.println("Hender controls the rebound.");
if (Math.random() > .4) {
if (defense == 6 && Math.random() > .6) {
System.out.println("Pass stolen by " + opponent
+ ", easy lay up");
add_points(0, 2);
hender_ball();
}
else {
// ball is passed back to you
ball_passed_back();
}
}
else {
System.out.println();
hender_non_jump_shot();
}
}
}
}
else {
System.out.println("Shot is good.");
add_points(1, 2);
opponent_ball();
}
}
// called when the user enters 0, 3, or 4
// lay up, set shot, or defense change
private void hender_non_jump_shot() {
time ++;
if (time == 50) {
halftime();
}
else if (time == 92) {
two_minute_warning();
}
if (shot == 4) {
System.out.println("Set shot.");
}
else if (shot == 3) {
System.out.println("Lay up.");
}
else if (shot == 0) {
change_defense();
}
// simulates different outcomes after a lay up or set shot
if (7/defense*Math.random() > .4) {
if (7/defense*Math.random() > .7) {
if (7/defense*Math.random() > .875) {
if (7/defense*Math.random() > .925) {
System.out.println("Charging foul. Hender loses the ball.\n");
}
else {
System.out.println("Shot blocked. " + opponent + "'s ball.\n");
}
}
else {
foul_shots(1);
}
opponent_ball();
}
else {
System.out.println("Shot is off the rim.");
if (Math.random() > 2/3) {
System.out.println("Hender controls the rebound.");
if (Math.random() > .4) {
System.out.println("Ball passed back to you.\n");
hender_ball();
}
else {
hender_non_jump_shot();
}
}
else {
System.out.println(opponent + " controls the rebound.\n");
opponent_ball();
}
}
}
else {
System.out.println("Shot is good. Two points.");
add_points(1, 2);
opponent_ball();
}
}
// plays out a Hender possession, starting with your choice of shot
private void hender_ball() {
Scanner scanner = new Scanner(System.in);
System.out.print("Your shot? ");
shot = -1;
if (scanner.hasNextInt()) {
shot = scanner.nextInt();
}
else {
System.out.println();
scanner.next();
}
while (!shot_choices.contains(shot)) {
System.out.print("Incorrect answer. Retype it. Your shot?");
if (scanner.hasNextInt()) {
shot = scanner.nextInt();
}
else {
System.out.println();
scanner.next();
}
}
if (time < 100 || Math.random() < .5) {
if (shot == 1 || shot == 2) {
hender_jump_shot();
}
else {
hender_non_jump_shot();
}
}
else {
if (score[0] != score[1]) {
System.out.println("\n ***** End Of Game *****");
System.out.println("Final Score: Hender: " + score[1] + " "
+ opponent + ": " + score[0]);
System.exit(0);
}
else {
System.out.println("\n ***** End Of Second Half *****");
System.out.println("Score at end of regulation time:");
System.out.println(" Hender: " + score[1] + " " +
opponent + ": " + score[0]);
System.out.println("Begin two minute overtime period");
time = 93;
start_of_period();
}
}
}
// simulates the opponents jumpshot
private void opponent_jumpshot() {
System.out.println("Jump Shot.");
if (8/defense*Math.random() > .35) {
if (8/defense*Math.random() > .75) {
if (8/defense*Math.random() > .9) {
System.out.println("Offensive foul. Hender's ball.\n");
}
else {
foul_shots(0);
}
hender_ball();
}
else {
System.out.println("Shot is off the rim.");
if (defense/6*Math.random() > .5) {
System.out.println(opponent + " controls the rebound.");
if (defense == 6) {
if (Math.random() > .75) {
System.out.println("Ball stolen. Easy lay up for Dartmouth.");
add_points(1, 2);
opponent_ball();
}
else {
if (Math.random() > .5) {
System.out.println();
opponent_non_jumpshot();
}
else {
System.out.println("Pass back to " + opponent +
" guard.\n");
opponent_ball();
}
}
}
else {
if (Math.random() > .5) {
opponent_non_jumpshot();
}
else {
System.out.println("Pass back to " + opponent +
" guard.\n");
opponent_ball();
}
}
}
else {
System.out.println("Hender controls the rebound.\n");
hender_ball();
}
}
}
else {
System.out.println("Shot is good.");
add_points(0, 2);
hender_ball();
}
}
// simulates opponents lay up or set shot
private void opponent_non_jumpshot() {
if (opponent_chance > 3) {
System.out.println("Set shot.");
}
else {
System.out.println("Lay up");
}
if (7/defense*Math.random() > .413) {
System.out.println("Shot is missed.");
if (defense/6*Math.random() > .5) {
System.out.println(opponent + " controls the rebound.");
if (defense == 6) {
if (Math.random() > .75) {
System.out.println("Ball stolen. Easy lay up for Hender.");
add_points(1, 2);
opponent_ball();
}
else {
if (Math.random() > .5) {
System.out.println();
opponent_non_jumpshot();
}
else {
System.out.println("Pass back to " + opponent +
" guard.\n");
opponent_ball();
}
}
}
else {
if (Math.random() > .5) {
System.out.println();
opponent_non_jumpshot();
}
else {
System.out.println("Pass back to " + opponent + " guard\n");
opponent_ball();
}
}
}
else {
System.out.println("Hender controls the rebound.\n");
hender_ball();
}
}
else {
System.out.println("Shot is good.");
add_points(0, 2);
hender_ball();
}
}
// simulates an opponents' possession
// #randomly picks jump shot or lay up / set shot.
private void opponent_ball() {
time ++;
if (time == 50) {
halftime();
}
opponent_chance = 10/4*Math.random()+1;
if (opponent_chance > 2) {
opponent_non_jumpshot();
}
else {
opponent_jumpshot();
}
}
public static void main(String[] args) {
Basketball new_game = new Basketball();
}
}