From dbb36f25ad5bc71787c3aaf697dda77ef378b6de Mon Sep 17 00:00:00 2001 From: journich <70119791+journich@users.noreply.github.com> Date: Thu, 25 Feb 2021 13:38:20 +1030 Subject: [PATCH 1/2] Java version of Change --- 22 Change/java/src/Change.java | 187 +++++++++++++++++++++++++++++ 22 Change/java/src/ChangeGame.java | 6 + 2 files changed, 193 insertions(+) create mode 100644 22 Change/java/src/Change.java create mode 100644 22 Change/java/src/ChangeGame.java diff --git a/22 Change/java/src/Change.java b/22 Change/java/src/Change.java new file mode 100644 index 00000000..7498be8a --- /dev/null +++ b/22 Change/java/src/Change.java @@ -0,0 +1,187 @@ +import java.util.Arrays; +import java.util.Scanner; + +/** + * Game of Change + *

+ * Based on the Basic game of Change here + * https://github.com/coding-horror/basic-computer-games/blob/main/22%20Change/change.bas + *

+ * Note: The idea was to create a version of the 1970's Basic game in Java, without introducing + * new features - no additional text, error checking, etc has been added. + */ +public class Change { + + // Used for keyboard input + private final Scanner kbScanner; + + private enum GAME_STATE { + START_GAME, + INPUT, + CALCULATE, + END_GAME, + GAME_OVER + } + + // Current game state + private GAME_STATE gameState; + + // Amount of change needed to be given + private double change; + + public Change() { + kbScanner = new Scanner(System.in); + + gameState = GAME_STATE.START_GAME; + } + + /** + * Main game loop + */ + public void play() { + + do { + switch (gameState) { + case START_GAME: + intro(); + gameState = GAME_STATE.INPUT; + break; + + case INPUT: + + double costOfItem = displayTextAndGetNumber("COST OF ITEM "); + double amountPaid = displayTextAndGetNumber("AMOUNT OF PAYMENT "); + change = amountPaid - costOfItem; + if (change == 0) { + // No change needed + System.out.println("CORRECT AMOUNT, THANK YOU."); + gameState = GAME_STATE.END_GAME; + } else if (change < 0) { + System.out.println("YOU HAVE SHORT-CHANGES ME $" + (costOfItem - amountPaid)); + // Don't change game state so it will loop back and try again + } else { + // Change needed. + gameState = GAME_STATE.CALCULATE; + } + break; + + case CALCULATE: + System.out.println("YOUR CHANGE, $" + change); + calculateChange(); + gameState = GAME_STATE.END_GAME; + break; + + case END_GAME: + System.out.println("THANK YOU, COME AGAIN"); + System.out.println(); + gameState = GAME_STATE.INPUT; + } + } while (gameState != GAME_STATE.GAME_OVER); + } + + /** + * Calculate and output the change required for the purchase based on + * what money was paid. + */ + private void calculateChange() { + + double originalChange = change; + + int tenDollarBills = (int) change / 10; + if (tenDollarBills > 0) { + System.out.println(tenDollarBills + " TEN DOLLAR BILL(S)"); + } + change = originalChange - (tenDollarBills * 10); + + int fiveDollarBills = (int) change / 5; + if (fiveDollarBills > 0) { + System.out.println(fiveDollarBills + " FIVE DOLLAR BILL(S)"); + } + change = originalChange - (tenDollarBills * 10 + fiveDollarBills * 5); + + int oneDollarBills = (int) change; + if (oneDollarBills > 0) { + System.out.println(oneDollarBills + " ONE DOLLAR BILL(S)"); + } + change = originalChange - (tenDollarBills * 10 + fiveDollarBills * 5 + oneDollarBills); + + change = change * 100; + double cents = change; + + int halfDollars = (int) change / 50; + if (halfDollars > 0) { + System.out.println(halfDollars + " ONE HALF DOLLAR(S)"); + } + change = cents - (halfDollars * 50); + + int quarters = (int) change / 25; + if (quarters > 0) { + System.out.println(quarters + " QUARTER(S)"); + } + + change = cents - (halfDollars * 50 + quarters * 25); + + int dimes = (int) change / 10; + if (dimes > 0) { + System.out.println(dimes + " DIME(S)"); + } + + change = cents - (halfDollars * 50 + quarters * 25 + dimes * 10); + + int nickels = (int) change / 5; + if (nickels > 0) { + System.out.println(nickels + " NICKEL(S)"); + } + + change = cents - (halfDollars * 50 + quarters * 25 + dimes * 10 + nickels * 5); + + int pennies = (int) (change + .5); + if (pennies > 0) { + System.out.println(pennies + " PENNY(S)"); + } + + } + + private void intro() { + System.out.println(simulateTabs(33) + "CHANGE"); + System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println(); + System.out.println("I, YOUR FRIENDLY MICROCOMPUTER, WILL DETERMINE"); + System.out.println("THE CORRECT CHANGE FOR ITEMS COSTING UP TO $100."); + System.out.println(); + } + + /* + * Print a message on the screen, then accept input from Keyboard. + * Converts input to Integer + * + * @param text message to be displayed on screen. + * @return what was typed by the player. + */ + private double displayTextAndGetNumber(String text) { + return Double.parseDouble(displayTextAndGetInput(text)); + } + + /* + * Print a message on the screen, then accept input from Keyboard. + * + * @param text message to be displayed on screen. + * @return what was typed by the player. + */ + private String displayTextAndGetInput(String text) { + System.out.print(text); + return kbScanner.next(); + } + + /** + * Simulate the old basic tab(xx) command which indented text by xx spaces. + * + * @param spaces number of spaces required + * @return String with number of spaces + */ + private String simulateTabs(int spaces) { + char[] spacesTemp = new char[spaces]; + Arrays.fill(spacesTemp, ' '); + return new String(spacesTemp); + } +} diff --git a/22 Change/java/src/ChangeGame.java b/22 Change/java/src/ChangeGame.java new file mode 100644 index 00000000..c2863914 --- /dev/null +++ b/22 Change/java/src/ChangeGame.java @@ -0,0 +1,6 @@ +public class ChangeGame { + public static void main(String[] args) { + Change change = new Change(); + change.play(); + } +} From a352035027bb0d1790b69ca8db64f407ae0dec3c Mon Sep 17 00:00:00 2001 From: journich <70119791+journich@users.noreply.github.com> Date: Thu, 25 Feb 2021 15:17:11 +1030 Subject: [PATCH 2/2] Java version of Chemist and documentation fix for method in Change --- 22 Change/java/src/Change.java | 2 +- 24 Chemist/java/src/Chemist.java | 143 +++++++++++++++++++++++++++ 24 Chemist/java/src/ChemistGame.java | 6 ++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 24 Chemist/java/src/Chemist.java create mode 100644 24 Chemist/java/src/ChemistGame.java diff --git a/22 Change/java/src/Change.java b/22 Change/java/src/Change.java index 7498be8a..38177245 100644 --- a/22 Change/java/src/Change.java +++ b/22 Change/java/src/Change.java @@ -153,7 +153,7 @@ public class Change { /* * Print a message on the screen, then accept input from Keyboard. - * Converts input to Integer + * Converts input to a Double * * @param text message to be displayed on screen. * @return what was typed by the player. diff --git a/24 Chemist/java/src/Chemist.java b/24 Chemist/java/src/Chemist.java new file mode 100644 index 00000000..137b3ce5 --- /dev/null +++ b/24 Chemist/java/src/Chemist.java @@ -0,0 +1,143 @@ +import java.util.Arrays; +import java.util.Scanner; + +/** + * Game of Chemist + *

+ * Based on the Basic game of Chemist here + * https://github.com/coding-horror/basic-computer-games/blob/main/24%20Chemist/chemist.bas + *

+ * Note: The idea was to create a version of the 1970's Basic game in Java, without introducing + * new features - no additional text, error checking, etc has been added. + */ +public class Chemist { + + public static final int MAX_LIVES = 9; + + // Used for keyboard input + private final Scanner kbScanner; + + private enum GAME_STATE { + START_GAME, + INPUT, + BLOWN_UP, + SURVIVED, + GAME_OVER + } + + // Current game state + private GAME_STATE gameState; + + private int timesBlownUp; + + public Chemist() { + kbScanner = new Scanner(System.in); + + gameState = GAME_STATE.START_GAME; + } + + /** + * Main game loop + */ + public void play() { + + do { + switch (gameState) { + + case START_GAME: + intro(); + timesBlownUp = 0; + gameState = GAME_STATE.INPUT; + break; + + case INPUT: + + int amountOfAcid = (int) (Math.random() * 50); + int correctAmountOfWater = (7 * amountOfAcid) / 3; + int water = displayTextAndGetNumber(amountOfAcid + " LITERS OF KRYPTOCYANIC ACID. HOW MUCH WATER? "); + + // Calculate if the player mixed enough water + int result = Math.abs(correctAmountOfWater - water); + + // Ratio of water wrong? + if (result > (correctAmountOfWater / 20)) { + gameState = GAME_STATE.BLOWN_UP; + } else { + // Got the ratio correct + gameState = GAME_STATE.SURVIVED; + } + break; + + case BLOWN_UP: + System.out.println(" SIZZLE! YOU HAVE JUST BEEN DESALINATED INTO A BLOB"); + System.out.println(" OF QUIVERING PROTOPLASM!"); + + timesBlownUp++; + + if (timesBlownUp < MAX_LIVES) { + System.out.println(" HOWEVER, YOU MAY TRY AGAIN WITH ANOTHER LIFE."); + gameState = GAME_STATE.INPUT; + } else { + System.out.println(" YOUR " + MAX_LIVES + " LIVES ARE USED, BUT YOU WILL BE LONG REMEMBERED FOR"); + System.out.println(" YOUR CONTRIBUTIONS TO THE FIELD OF COMIC BOOK CHEMISTRY."); + gameState = GAME_STATE.GAME_OVER; + } + + break; + + case SURVIVED: + System.out.println(" GOOD JOB! YOU MAY BREATHE NOW, BUT DON'T INHALE THE FUMES!"); + System.out.println(); + gameState = GAME_STATE.INPUT; + break; + + } + } while (gameState != GAME_STATE.GAME_OVER); + } + + private void intro() { + System.out.println(simulateTabs(33) + "CHEMIST"); + System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println(); + System.out.println("THE FICTITIOUS CHEMICAL KRYPTOCYANIC ACID CAN ONLY BE"); + System.out.println("DILUTED BY THE RATIO OF 7 PARTS WATER TO 3 PARTS ACID."); + System.out.println("IF ANY OTHER RATIO IS ATTEMPTED, THE ACID BECOMES UNSTABLE"); + System.out.println("AND SOON EXPLODES. GIVEN THE AMOUNT OF ACID, YOU MUST"); + System.out.println("DECIDE WHO MUCH WATER TO ADD FOR DILUTION. IF YOU MISS"); + System.out.println("YOU FACE THE CONSEQUENCES."); + } + + /* + * Print a message on the screen, then accept input from Keyboard. + * Converts input to an Integer + * + * @param text message to be displayed on screen. + * @return what was typed by the player. + */ + private int displayTextAndGetNumber(String text) { + return Integer.parseInt(displayTextAndGetInput(text)); + } + + /* + * Print a message on the screen, then accept input from Keyboard. + * + * @param text message to be displayed on screen. + * @return what was typed by the player. + */ + private String displayTextAndGetInput(String text) { + System.out.print(text); + return kbScanner.next(); + } + + /** + * Simulate the old basic tab(xx) command which indented text by xx spaces. + * + * @param spaces number of spaces required + * @return String with number of spaces + */ + private String simulateTabs(int spaces) { + char[] spacesTemp = new char[spaces]; + Arrays.fill(spacesTemp, ' '); + return new String(spacesTemp); + } +} diff --git a/24 Chemist/java/src/ChemistGame.java b/24 Chemist/java/src/ChemistGame.java new file mode 100644 index 00000000..494959a3 --- /dev/null +++ b/24 Chemist/java/src/ChemistGame.java @@ -0,0 +1,6 @@ +public class ChemistGame { + public static void main(String[] args) { + Chemist chemist = new Chemist(); + chemist.play(); + } +}