mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 23:00:43 -08:00
MAINT: Apply pre-commit
Remove byte-order-marker pre-commit check as there would be many adjustments necessary
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import java.lang.Math;
|
||||
import java.util.*;
|
||||
import java.util.*;
|
||||
import java.util.Scanner;
|
||||
|
||||
/* The basketball class is a computer game that allows you to play as
|
||||
@@ -15,7 +15,7 @@ public class Basketball {
|
||||
int shot = -1;
|
||||
List<Integer> shot_choices = Arrays.asList(0, 1, 2, 3, 4);
|
||||
double opponent_chance = 0;
|
||||
String opponent = null;
|
||||
String opponent = null;
|
||||
|
||||
public Basketball() {
|
||||
|
||||
@@ -41,7 +41,7 @@ public class Basketball {
|
||||
else {
|
||||
scanner.next();
|
||||
}
|
||||
|
||||
|
||||
// makes sure that input is legal
|
||||
while (!defense_choices.contains(defense)) {
|
||||
System.out.print("Your new defensive allignment is? ");
|
||||
@@ -57,7 +57,7 @@ public class Basketball {
|
||||
// takes input for opponent's name
|
||||
System.out.print("\nChoose your opponent? ");
|
||||
|
||||
opponent = scanner.next();
|
||||
opponent = scanner.next();
|
||||
start_of_period();
|
||||
}
|
||||
|
||||
@@ -67,13 +67,13 @@ public class Basketball {
|
||||
score[team] += points;
|
||||
print_score();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void ball_passed_back() {
|
||||
System.out.print("Ball passed back to you. ");
|
||||
dartmouth_ball();
|
||||
}
|
||||
|
||||
|
||||
// change defense, called when the user enters 0 for their shot
|
||||
private void change_defense() {
|
||||
defense = -1;
|
||||
@@ -82,13 +82,13 @@ public class Basketball {
|
||||
while (!defense_choices.contains(defense)) {
|
||||
System.out.println("Your new defensive allignment is? ");
|
||||
if (scanner.hasNextDouble()) {
|
||||
defense = (double)(scanner.nextDouble());
|
||||
defense = (double)(scanner.nextDouble());
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dartmouth_ball();
|
||||
}
|
||||
|
||||
@@ -102,15 +102,15 @@ public class Basketball {
|
||||
}
|
||||
else {
|
||||
System.out.println("Shooter makes one shot and misses one.");
|
||||
score[team] += 1;
|
||||
}
|
||||
score[team] += 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Shooter makes both shots.");
|
||||
score[team] += 2;
|
||||
}
|
||||
|
||||
print_score();
|
||||
|
||||
print_score();
|
||||
}
|
||||
|
||||
// called when time = 50, starts a new period
|
||||
@@ -127,13 +127,13 @@ public class Basketball {
|
||||
|
||||
// simulates a center jump for posession at the beginning of a period
|
||||
private void start_of_period() {
|
||||
System.out.println("Center jump");
|
||||
System.out.println("Center jump");
|
||||
if (Math.random() > .6) {
|
||||
System.out.println("Dartmouth controls the tap.\n");
|
||||
System.out.println("Dartmouth controls the tap.\n");
|
||||
dartmouth_ball();
|
||||
}
|
||||
else {
|
||||
System.out.println(opponent + " controls the tap.\n");
|
||||
System.out.println(opponent + " controls the tap.\n");
|
||||
opponent_ball();
|
||||
}
|
||||
}
|
||||
@@ -142,10 +142,10 @@ public class Basketball {
|
||||
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 dartmouth_jump_shot() {
|
||||
time ++;
|
||||
time ++;
|
||||
if (time == 50) {
|
||||
halftime();
|
||||
}
|
||||
@@ -171,135 +171,135 @@ public class Basketball {
|
||||
else {
|
||||
if (Math.random() > .5) {
|
||||
System.out.println("Shot is blocked. Ball controlled by " +
|
||||
opponent + ".\n");
|
||||
opponent_ball();
|
||||
}
|
||||
opponent + ".\n");
|
||||
opponent_ball();
|
||||
}
|
||||
else {
|
||||
System.out.println("Shot is blocked. Ball controlled by Dartmouth.");
|
||||
dartmouth_ball();
|
||||
}
|
||||
System.out.println("Shot is blocked. Ball controlled by Dartmouth.");
|
||||
dartmouth_ball();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Shot is off target.");
|
||||
System.out.println("Shot is off target.");
|
||||
if (defense / 6 * Math.random() > .45) {
|
||||
System.out.println("Rebound to " + opponent + "\n");
|
||||
opponent_ball();
|
||||
}
|
||||
System.out.println("Rebound to " + opponent + "\n");
|
||||
opponent_ball();
|
||||
}
|
||||
else {
|
||||
System.out.println("Dartmouth controls the rebound.");
|
||||
System.out.println("Dartmouth 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);
|
||||
dartmouth_ball();
|
||||
}
|
||||
+ ", easy lay up");
|
||||
add_points(0, 2);
|
||||
dartmouth_ball();
|
||||
}
|
||||
else {
|
||||
// ball is passed back to you
|
||||
ball_passed_back();
|
||||
}
|
||||
ball_passed_back();
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("");
|
||||
dartmouth_non_jump_shot();
|
||||
System.out.println("");
|
||||
dartmouth_non_jump_shot();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Shot is good.");
|
||||
add_points(1, 2);
|
||||
opponent_ball();
|
||||
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 dartmouth_non_jump_shot() {
|
||||
time ++;
|
||||
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.");
|
||||
System.out.println("Lay up.");
|
||||
}
|
||||
else if (shot == 0) {
|
||||
change_defense();
|
||||
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. Dartmouth loses the ball.\n");
|
||||
opponent_ball();
|
||||
System.out.println("Charging foul. Dartmouth loses the ball.\n");
|
||||
opponent_ball();
|
||||
}
|
||||
else {
|
||||
System.out.println("Shot blocked. " + opponent + "'s ball.\n");
|
||||
opponent_ball();
|
||||
System.out.println("Shot blocked. " + opponent + "'s ball.\n");
|
||||
opponent_ball();
|
||||
}
|
||||
}
|
||||
else {
|
||||
foul_shots(1);
|
||||
opponent_ball();
|
||||
foul_shots(1);
|
||||
opponent_ball();
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Shot is off the rim.");
|
||||
System.out.println("Shot is off the rim.");
|
||||
if (Math.random() > 2/3) {
|
||||
System.out.println("Dartmouth controls the rebound.");
|
||||
System.out.println("Dartmouth controls the rebound.");
|
||||
if (Math.random() > .4) {
|
||||
System.out.println("Ball passed back to you.\n");
|
||||
dartmouth_ball();
|
||||
System.out.println("Ball passed back to you.\n");
|
||||
dartmouth_ball();
|
||||
}
|
||||
else {
|
||||
dartmouth_non_jump_shot();
|
||||
dartmouth_non_jump_shot();
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println(opponent + " controls the rebound.\n");
|
||||
opponent_ball();
|
||||
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();
|
||||
System.out.println("Shot is good. Two points.");
|
||||
add_points(1, 2);
|
||||
opponent_ball();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// plays out a Dartmouth posession, starting with your choice of shot
|
||||
private void dartmouth_ball() {
|
||||
Scanner scanner = new Scanner(System.in); // creates a scanner
|
||||
System.out.print("Your shot? ");
|
||||
shot = -1;
|
||||
System.out.print("Your shot? ");
|
||||
shot = -1;
|
||||
if (scanner.hasNextInt()) {
|
||||
shot = scanner.nextInt();
|
||||
shot = scanner.nextInt();
|
||||
}
|
||||
else {
|
||||
System.out.println("");
|
||||
scanner.next();
|
||||
scanner.next();
|
||||
}
|
||||
|
||||
|
||||
while (!shot_choices.contains(shot)) {
|
||||
System.out.print("Incorrect answer. Retype it. Your shot?");
|
||||
if (scanner.hasNextInt()) {
|
||||
shot = scanner.nextInt();
|
||||
shot = scanner.nextInt();
|
||||
}
|
||||
else {
|
||||
System.out.println("");
|
||||
scanner.next();
|
||||
scanner.next();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,154 +308,154 @@ public class Basketball {
|
||||
dartmouth_jump_shot();
|
||||
}
|
||||
else {
|
||||
dartmouth_non_jump_shot();
|
||||
dartmouth_non_jump_shot();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (score[0] != score[1]) {
|
||||
System.out.println("\n ***** End Of Game *****");
|
||||
System.out.println("\n ***** End Of Game *****");
|
||||
System.out.println("Final Score: Dartmouth: " + score[1] + " "
|
||||
+ opponent + ": " + score[0]);
|
||||
+ 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("\n ***** End Of Second Half *****");
|
||||
System.out.println("Score at end of regulation time:");
|
||||
System.out.println(" Dartmouth: " + score[1] + " " +
|
||||
opponent + ": " + score[0]);
|
||||
System.out.println("Begin two minute overtime period");
|
||||
time = 93;
|
||||
start_of_period();
|
||||
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.");
|
||||
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. Dartmouth's ball.\n");
|
||||
dartmouth_ball();
|
||||
System.out.println("Offensive foul. Dartmouth's ball.\n");
|
||||
dartmouth_ball();
|
||||
}
|
||||
else {
|
||||
foul_shots(0);
|
||||
dartmouth_ball();
|
||||
foul_shots(0);
|
||||
dartmouth_ball();
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Shot is off the rim.");
|
||||
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();
|
||||
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();
|
||||
System.out.println("");
|
||||
opponent_non_jumpshot();
|
||||
}
|
||||
else {
|
||||
System.out.println("Pass back to " + opponent +
|
||||
" guard.\n");
|
||||
opponent_ball();
|
||||
" guard.\n");
|
||||
opponent_ball();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (Math.random() > .5) {
|
||||
opponent_non_jumpshot();
|
||||
opponent_non_jumpshot();
|
||||
}
|
||||
else {
|
||||
System.out.println("Pass back to " + opponent +
|
||||
" guard.\n");
|
||||
opponent_ball();
|
||||
" guard.\n");
|
||||
opponent_ball();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Dartmouth controls the rebound.\n");
|
||||
dartmouth_ball();
|
||||
System.out.println("Dartmouth controls the rebound.\n");
|
||||
dartmouth_ball();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Shot is good.");
|
||||
add_points(0, 2);
|
||||
dartmouth_ball();
|
||||
System.out.println("Shot is good.");
|
||||
add_points(0, 2);
|
||||
dartmouth_ball();
|
||||
}
|
||||
}
|
||||
|
||||
// simulates opponents lay up or set shot
|
||||
private void opponent_non_jumpshot() {
|
||||
if (opponent_chance > 3) {
|
||||
System.out.println("Set shot.");
|
||||
System.out.println("Set shot.");
|
||||
}
|
||||
else {
|
||||
System.out.println("Lay up");
|
||||
System.out.println("Lay up");
|
||||
}
|
||||
if (7/defense*Math.random() > .413) {
|
||||
System.out.println("Shot is missed.");
|
||||
System.out.println("Shot is missed.");
|
||||
if (defense/6*Math.random() > .5) {
|
||||
System.out.println(opponent + " controls the rebound.");
|
||||
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();
|
||||
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();
|
||||
System.out.println("");
|
||||
opponent_non_jumpshot();
|
||||
}
|
||||
else {
|
||||
System.out.println("Pass back to " + opponent +
|
||||
" guard.\n");
|
||||
opponent_ball();
|
||||
" guard.\n");
|
||||
opponent_ball();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (Math.random() > .5) {
|
||||
System.out.println("");
|
||||
opponent_non_jumpshot();
|
||||
System.out.println("");
|
||||
opponent_non_jumpshot();
|
||||
}
|
||||
else {
|
||||
System.out.println("Pass back to " + opponent + " guard\n");
|
||||
opponent_ball();
|
||||
System.out.println("Pass back to " + opponent + " guard\n");
|
||||
opponent_ball();
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Dartmouth controls the rebound.\n");
|
||||
dartmouth_ball();
|
||||
System.out.println("Dartmouth controls the rebound.\n");
|
||||
dartmouth_ball();
|
||||
}
|
||||
}
|
||||
else {
|
||||
System.out.println("Shot is good.");
|
||||
add_points(0, 2);
|
||||
dartmouth_ball();
|
||||
System.out.println("Shot is good.");
|
||||
add_points(0, 2);
|
||||
dartmouth_ball();
|
||||
}
|
||||
}
|
||||
|
||||
// simulates an opponents possesion
|
||||
// #randomly picks jump shot or lay up / set shot.
|
||||
private void opponent_ball() {
|
||||
time ++;
|
||||
time ++;
|
||||
if (time == 50) {
|
||||
halftime();
|
||||
}
|
||||
opponent_chance = 10/4*Math.random()+1;
|
||||
opponent_chance = 10/4*Math.random()+1;
|
||||
if (opponent_chance > 2) {
|
||||
opponent_non_jumpshot();
|
||||
opponent_non_jumpshot();
|
||||
}
|
||||
else {
|
||||
opponent_jumpshot();
|
||||
opponent_jumpshot();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -463,7 +463,3 @@ public class Basketball {
|
||||
Basketball new_game = new Basketball();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user