mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 07:10:42 -08:00
Removed spaces from top-level directory names.
Spaces tend to cause annoyances in a Unix-style shell environment. This change fixes that.
This commit is contained in:
45
15_Boxing/java/Basic.java
Normal file
45
15_Boxing/java/Basic.java
Normal file
@@ -0,0 +1,45 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* It provide some kind of BASIC language behaviour simulations.
|
||||
*/
|
||||
final class Basic {
|
||||
|
||||
public static int randomOf(int base) {
|
||||
return (int)Math.round(Math.floor(base* Math.random() + 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* The Console "simulate" the message error when input does not match with the expected type.
|
||||
* Specifically for this game if you enter an String when and int was expected.
|
||||
*/
|
||||
public static class Console {
|
||||
private final Scanner input = new Scanner(System.in);
|
||||
|
||||
public String readLine() {
|
||||
return input.nextLine();
|
||||
}
|
||||
|
||||
public int readInt() {
|
||||
int ret = -1;
|
||||
boolean failedInput = true;
|
||||
do {
|
||||
boolean b = input.hasNextInt();
|
||||
if (b) {
|
||||
ret = input.nextInt();
|
||||
failedInput = false;
|
||||
} else {
|
||||
input.next(); // discard read
|
||||
System.out.print("!NUMBER EXPECTED - RETRY INPUT LINE\n? ");
|
||||
}
|
||||
|
||||
} while (failedInput);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void print(String message, Object... args) {
|
||||
System.out.printf(message, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
227
15_Boxing/java/Boxing.java
Normal file
227
15_Boxing/java/Boxing.java
Normal file
@@ -0,0 +1,227 @@
|
||||
/**
|
||||
* Boxing
|
||||
*
|
||||
* <p>
|
||||
* Based on the Basic game of BatNum here
|
||||
* https://github.com/coding-horror/basic-computer-games/tree/main/15%20Boxing
|
||||
* <p>
|
||||
*/
|
||||
public class Boxing {
|
||||
|
||||
private static final Basic.Console console = new Basic.Console();
|
||||
|
||||
private GameSession session;
|
||||
|
||||
public void play() {
|
||||
showIntro();
|
||||
|
||||
loadPlayers();
|
||||
|
||||
console.print("%s'S ADVANTAGE IS %d AND VULNERABILITY IS SECRET.\n", session.getOpponent().getName(), session.getOpponent().getBestPunch().getCode());
|
||||
|
||||
|
||||
for (int roundNro = 1; roundNro <= 3; roundNro++) {
|
||||
if (session.isOver())
|
||||
break;
|
||||
|
||||
session.resetPoints();
|
||||
console.print("\nROUND %d BEGINS...%n", roundNro);
|
||||
|
||||
for (int majorPunches = 1; majorPunches <= 7; majorPunches++) {
|
||||
long i = Basic.randomOf(10);
|
||||
|
||||
if (i > 5) {
|
||||
boolean stopPunches = opponentPunch();
|
||||
if (stopPunches ) break;
|
||||
} else {
|
||||
playerPunch();
|
||||
}
|
||||
}
|
||||
showRoundWinner(roundNro);
|
||||
}
|
||||
showWinner();
|
||||
}
|
||||
|
||||
private boolean opponentPunch() {
|
||||
final Punch punch = Punch.random();
|
||||
|
||||
if (punch == session.getOpponent().getBestPunch()) session.addOpponentPoints(2);
|
||||
|
||||
if (punch == Punch.FULL_SWING) {
|
||||
console.print("%s TAKES A FULL SWING AND", session.getOpponent().getName());
|
||||
long r6 = Basic.randomOf(60);
|
||||
|
||||
if (session.getPlayer().hitVulnerability(Punch.FULL_SWING) || r6 < 30) {
|
||||
console.print(" POW!!!!! HE HITS HIM RIGHT IN THE FACE!\n");
|
||||
if (session.getPoints(session.getOpponent()) > 35) {
|
||||
|
||||
session.setKnocked();
|
||||
return true;
|
||||
}
|
||||
session.addOpponentPoints(15);
|
||||
} else {
|
||||
console.print(" IT'S BLOCKED!\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (punch == Punch.HOOK || punch == Punch.UPPERCUT) {
|
||||
if (punch == Punch.HOOK) {
|
||||
console.print("%s GETS %s IN THE JAW (OUCH!)\n", session.getOpponent().getName(), session.getPlayer().getName());
|
||||
|
||||
session.addOpponentPoints(7);
|
||||
console.print("....AND AGAIN!\n");
|
||||
|
||||
session.addOpponentPoints(5);
|
||||
if (session.getPoints(session.getOpponent()) > 35) {
|
||||
session.setKnocked();
|
||||
return true;
|
||||
}
|
||||
console.print("\n");
|
||||
|
||||
}
|
||||
console.print("%s IS ATTACKED BY AN UPPERCUT (OH,OH)...\n", session.getPlayer().getName());
|
||||
long q4 = Basic.randomOf(200);
|
||||
if (session.getPlayer().hitVulnerability(Punch.UPPERCUT) || q4 <= 75) {
|
||||
console.print("AND %s CONNECTS...\n", session.getOpponent().getName());
|
||||
|
||||
session.addOpponentPoints(8);
|
||||
} else {
|
||||
console.print(" BLOCKS AND HITS %s WITH A HOOK.\n", session.getOpponent().getName());
|
||||
|
||||
session.addPlayerPoints(5);
|
||||
}
|
||||
}
|
||||
else {
|
||||
console.print("%s JABS AND ", session.getOpponent().getName());
|
||||
long z4 = Basic.randomOf(7);
|
||||
if (session.getPlayer().hitVulnerability(Punch.JAB))
|
||||
|
||||
session.addOpponentPoints(5);
|
||||
else if (z4 > 4) {
|
||||
console.print(" BLOOD SPILLS !!!\n");
|
||||
|
||||
session.addOpponentPoints(5);
|
||||
} else {
|
||||
console.print("IT'S BLOCKED!\n");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void playerPunch() {
|
||||
console.print("%s'S PUNCH? ", session.getPlayer().getName());
|
||||
final Punch punch = Punch.fromCode(console.readInt());
|
||||
|
||||
if (punch == session.getPlayer().getBestPunch()) session.addPlayerPoints(2);
|
||||
|
||||
switch (punch) {
|
||||
case FULL_SWING -> {
|
||||
console.print("%s SWINGS AND ", session.getPlayer().getName());
|
||||
if (session.getOpponent().getBestPunch() == Punch.JAB) {
|
||||
console.print("HE CONNECTS!\n");
|
||||
if (session.getPoints(session.getPlayer()) <= 35) session.addPlayerPoints(15);
|
||||
} else {
|
||||
long x3 = Basic.randomOf(30);
|
||||
if (x3 < 10) {
|
||||
console.print("HE CONNECTS!\n");
|
||||
if (session.getPoints(session.getPlayer()) <= 35) session.addPlayerPoints(15);
|
||||
} else {
|
||||
console.print("HE MISSES \n");
|
||||
if (session.getPoints(session.getPlayer()) != 1) console.print("\n\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
case HOOK -> {
|
||||
console.print("\n%s GIVES THE HOOK... ", session.getPlayer().getName());
|
||||
long h1 = Basic.randomOf(2);
|
||||
if (session.getOpponent().getBestPunch() == Punch.HOOK) {
|
||||
|
||||
session.addPlayerPoints(7);
|
||||
} else if (h1 == 1) {
|
||||
console.print("BUT IT'S BLOCKED!!!!!!!!!!!!!\n");
|
||||
} else {
|
||||
console.print("CONNECTS...\n");
|
||||
|
||||
session.addPlayerPoints(7);
|
||||
}
|
||||
}
|
||||
case UPPERCUT -> {
|
||||
console.print("\n%s TRIES AN UPPERCUT ", session.getPlayer().getName());
|
||||
long d5 = Basic.randomOf(100);
|
||||
if (session.getOpponent().getBestPunch() == Punch.UPPERCUT || d5 < 51) {
|
||||
console.print("AND HE CONNECTS!\n");
|
||||
|
||||
session.addPlayerPoints(4);
|
||||
} else {
|
||||
console.print("AND IT'S BLOCKED (LUCKY BLOCK!)\n");
|
||||
}
|
||||
}
|
||||
default -> {
|
||||
console.print("%s JABS AT %s'S HEAD \n", session.getPlayer().getName(), session.getOpponent().getName());
|
||||
if (session.getOpponent().getBestPunch() == Punch.JAB) {
|
||||
|
||||
session.addPlayerPoints(3);
|
||||
} else {
|
||||
long c = Basic.randomOf(8);
|
||||
if (c < 4) {
|
||||
console.print("IT'S BLOCKED.\n");
|
||||
} else {
|
||||
|
||||
session.addPlayerPoints(3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void showRoundWinner(int roundNro) {
|
||||
if (session.isRoundWinner(session.getPlayer())) {
|
||||
console.print("\n %s WINS ROUND %d\n", session.getPlayer().getName(), roundNro);
|
||||
session.addRoundWind(session.getPlayer());
|
||||
} else {
|
||||
console.print("\n %s WINS ROUND %d\n", session.getOpponent().getName(), roundNro);
|
||||
session.addRoundWind(session.getOpponent());
|
||||
}
|
||||
}
|
||||
|
||||
private void showWinner() {
|
||||
if (session.isGameWinner(session.getOpponent())) {
|
||||
console.print("%s WINS (NICE GOING, " + session.getOpponent().getName() + ").", session.getOpponent().getName());
|
||||
} else if (session.isGameWinner(session.getPlayer())) {
|
||||
console.print("%s AMAZINGLY WINS!!", session.getPlayer().getName());
|
||||
} else if (session.isPlayerKnocked()) {
|
||||
console.print("%s IS KNOCKED COLD AND %s IS THE WINNER AND CHAMP!", session.getPlayer().getName(), session.getOpponent().getName());
|
||||
} else {
|
||||
console.print("%s IS KNOCKED COLD AND %s IS THE WINNER AND CHAMP!", session.getOpponent().getName(), session.getPlayer().getName());
|
||||
}
|
||||
|
||||
console.print("\n\nAND NOW GOODBYE FROM THE OLYMPIC ARENA.\n");
|
||||
}
|
||||
|
||||
private void loadPlayers() {
|
||||
console.print("WHAT IS YOUR OPPONENT'S NAME? ");
|
||||
final String opponentName = console.readLine();
|
||||
|
||||
console.print("INPUT YOUR MAN'S NAME? ");
|
||||
final String playerName = console.readLine();
|
||||
|
||||
console.print("DIFFERENT PUNCHES ARE: (1) FULL SWING; (2) HOOK; (3) UPPERCUT; (4) JAB.\n");
|
||||
console.print("WHAT IS YOUR MANS BEST? ");
|
||||
|
||||
final int b = console.readInt();
|
||||
|
||||
console.print("WHAT IS HIS VULNERABILITY? ");
|
||||
final int d = console.readInt();
|
||||
|
||||
final Player player = new Player(playerName, Punch.fromCode(b), Punch.fromCode(d));
|
||||
final Player opponent = new Player(opponentName);
|
||||
|
||||
session = new GameSession(player, opponent);
|
||||
}
|
||||
|
||||
private void showIntro () {
|
||||
console.print(" BOXING\n");
|
||||
console.print(" CREATIVE COMPUTING MORRISTOWN, NEW JERSEY\n\n\n");
|
||||
console.print("BOXING OLYMPIC STYLE (3 ROUNDS -- 2 OUT OF 3 WINS)\n\n");
|
||||
}
|
||||
}
|
||||
6
15_Boxing/java/BoxingGame.java
Normal file
6
15_Boxing/java/BoxingGame.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class BoxingGame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Boxing().play();
|
||||
}
|
||||
}
|
||||
67
15_Boxing/java/GameSession.java
Normal file
67
15_Boxing/java/GameSession.java
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Game Session
|
||||
* The session store the state of the game
|
||||
*/
|
||||
public class GameSession {
|
||||
private final Player player;
|
||||
private final Player opponent;
|
||||
private int opponentRoundWins = 0;
|
||||
private int playerRoundWins = 0;
|
||||
|
||||
int playerPoints = 0;
|
||||
int opponentPoints = 0;
|
||||
boolean knocked = false;
|
||||
|
||||
GameSession(Player player, Player opponent) {
|
||||
this.player = player;
|
||||
this.opponent = opponent;
|
||||
}
|
||||
|
||||
public Player getPlayer() { return player;}
|
||||
public Player getOpponent() { return opponent;}
|
||||
|
||||
public void setKnocked() {
|
||||
knocked = true;
|
||||
}
|
||||
|
||||
public void resetPoints() {
|
||||
playerPoints = 0;
|
||||
opponentPoints = 0;
|
||||
}
|
||||
|
||||
public void addPlayerPoints(int ptos) { playerPoints+=ptos;}
|
||||
public void addOpponentPoints(int ptos) { opponentPoints+=ptos;}
|
||||
|
||||
public int getPoints(Player player) {
|
||||
if(player.isPlayer())
|
||||
return playerPoints;
|
||||
else
|
||||
return opponentPoints;
|
||||
}
|
||||
|
||||
public void addRoundWind(Player player) {
|
||||
if(player.isPlayer()) playerRoundWins++; else opponentRoundWins++;
|
||||
}
|
||||
|
||||
public boolean isOver() {
|
||||
return (opponentRoundWins >= 2 || playerRoundWins >= 2);
|
||||
}
|
||||
|
||||
public boolean isRoundWinner(Player player) {
|
||||
if (player.isPlayer())
|
||||
return playerPoints > opponentPoints;
|
||||
else
|
||||
return opponentPoints > playerPoints;
|
||||
}
|
||||
|
||||
public boolean isGameWinner(Player player) {
|
||||
if (player.isPlayer())
|
||||
return playerRoundWins > 2;
|
||||
else
|
||||
return opponentRoundWins > 2;
|
||||
}
|
||||
|
||||
public boolean isPlayerKnocked() {
|
||||
return knocked;
|
||||
}
|
||||
}
|
||||
42
15_Boxing/java/Player.java
Normal file
42
15_Boxing/java/Player.java
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* The Player class model the user and compuer player
|
||||
*/
|
||||
public class Player {
|
||||
private final String name;
|
||||
private final Punch bestPunch;
|
||||
private final Punch vulnerability;
|
||||
private boolean isPlayer = false;
|
||||
|
||||
public Player(String name, Punch bestPunch, Punch vulnerability) {
|
||||
this.name = name;
|
||||
this.bestPunch = bestPunch;
|
||||
this.vulnerability = vulnerability;
|
||||
this.isPlayer = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Player with random Best Punch and Vulnerability
|
||||
*/
|
||||
public Player(String name) {
|
||||
this.name = name;
|
||||
|
||||
int b1;
|
||||
int d1;
|
||||
|
||||
do {
|
||||
b1 = Basic.randomOf(4);
|
||||
d1 = Basic.randomOf(4);
|
||||
} while (b1 == d1);
|
||||
|
||||
this.bestPunch = Punch.fromCode(b1);
|
||||
this.vulnerability = Punch.fromCode(d1);
|
||||
}
|
||||
|
||||
public boolean isPlayer() { return isPlayer; }
|
||||
public String getName() { return name; }
|
||||
public Punch getBestPunch() { return bestPunch; }
|
||||
|
||||
public boolean hitVulnerability(Punch punch) {
|
||||
return vulnerability == punch;
|
||||
}
|
||||
}
|
||||
27
15_Boxing/java/Punch.java
Normal file
27
15_Boxing/java/Punch.java
Normal file
@@ -0,0 +1,27 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Types of Punches
|
||||
*/
|
||||
public enum Punch {
|
||||
FULL_SWING(1),
|
||||
HOOK(2),
|
||||
UPPERCUT(3),
|
||||
JAB(4);
|
||||
|
||||
private final int code;
|
||||
|
||||
Punch(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
int getCode() { return code;}
|
||||
|
||||
public static Punch fromCode(int code) {
|
||||
return Arrays.stream(Punch.values()).filter(p->p.code == code).findAny().orElse(null);
|
||||
}
|
||||
|
||||
public static Punch random() {
|
||||
return Punch.fromCode(Basic.randomOf(4));
|
||||
}
|
||||
}
|
||||
3
15_Boxing/java/README.md
Normal file
3
15_Boxing/java/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html)
|
||||
|
||||
Conversion to [Oracle Java](https://openjdk.java.net/)
|
||||
Reference in New Issue
Block a user