diff --git a/15 Boxing/java/Basic.java b/15 Boxing/java/Basic.java new file mode 100644 index 00000000..3822e3f3 --- /dev/null +++ b/15 Boxing/java/Basic.java @@ -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); + } + } +} \ No newline at end of file diff --git a/15 Boxing/java/Boxing.java b/15 Boxing/java/Boxing.java new file mode 100644 index 00000000..64b20155 --- /dev/null +++ b/15 Boxing/java/Boxing.java @@ -0,0 +1,227 @@ +/** + * Boxing + * + *
+ * Based on the Basic game of BatNum here + * https://github.com/coding-horror/basic-computer-games/tree/main/15%20Boxing + *
+ */ +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"); + } +} \ No newline at end of file diff --git a/15 Boxing/java/BoxingGame.java b/15 Boxing/java/BoxingGame.java new file mode 100644 index 00000000..5e5f897b --- /dev/null +++ b/15 Boxing/java/BoxingGame.java @@ -0,0 +1,6 @@ +public class BoxingGame { + + public static void main(String[] args) { + new Boxing().play(); + } +} diff --git a/15 Boxing/java/GameSession.java b/15 Boxing/java/GameSession.java new file mode 100644 index 00000000..3a2c77a5 --- /dev/null +++ b/15 Boxing/java/GameSession.java @@ -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; + } +} \ No newline at end of file diff --git a/15 Boxing/java/Player.java b/15 Boxing/java/Player.java new file mode 100644 index 00000000..2462b9d0 --- /dev/null +++ b/15 Boxing/java/Player.java @@ -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; + } +} \ No newline at end of file diff --git a/15 Boxing/java/Punch.java b/15 Boxing/java/Punch.java new file mode 100644 index 00000000..24745b92 --- /dev/null +++ b/15 Boxing/java/Punch.java @@ -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)); + } +} diff --git a/66 Number/java/main.class b/66 Number/java/main.class new file mode 100644 index 00000000..c3df4ebf --- /dev/null +++ b/66 Number/java/main.class @@ -0,0 +1,69 @@ + +import java.time.temporal.ValueRange; +import java.util.Arrays; +import java.util.Random; +import java.util.Scanner; + +public class Number { + + public static int points = 0; + + public static void printempty() { System.out.println(" "); } + + public static void print(String toprint) { System.out.println(toprint); } + + public static void main(String[] args) { + print("YOU HAVE 100 POINTS. BY GUESSING NUMBERS FROM 1 TO 5, YOU"); + print("CAN GAIN OR LOSE POINTS DEPENDING UPON HOW CLOSE YOU GET TO"); + print("A RANDOM NUMBER SELECTED BY THE COMPUTER."); + printempty(); + print("YOU OCCASIONALLY WILL GET A JACKPOT WHICH WILL DOUBLE(!)"); + print("YOUR POINT COUNT. YOU WIN WHEN YOU GET 500 POINTS."); + printempty(); + + try { + while (true) { + print("GUESS A NUMBER FROM 1 TO 5"); + + + Scanner numbersc = new Scanner(System.in); + String numberstring = numbersc.nextLine(); + + int number = Integer.parseInt(numberstring); + + if (!(number < 1| number > 5)) { + + Random rand = new Random(); + + int randomNum = rand.nextInt((5 - 1) + 1) + 1; + + if (randomNum == number) { + print("YOU HIT THE JACKPOT!!!"); + points = points * 2; + } else if(ValueRange.of(randomNum, randomNum + 1).isValidIntValue(number)) { + print("+5"); + points = points + 5; + } else if(ValueRange.of(randomNum - 1, randomNum + 2).isValidIntValue(number)) { + print("+1"); + points = points + 1; + } else if(ValueRange.of(randomNum - 3, randomNum + 1).isValidIntValue(number)) { + print("-1"); + points = points - 1; + } else { + print("-half"); + points = (int) (points * 0.5); + } + + print("YOU HAVE " + points + " POINTS."); + } + + if (points >= 500) { + print("!!!!YOU WIN!!!! WITH " + points + " POINTS."); + return; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } +}