From 3a902ffd6444aa4a9b9c77c022861bf5a405f0e0 Mon Sep 17 00:00:00 2001 From: journich <70119791+journich@users.noreply.github.com> Date: Sun, 28 Feb 2021 08:14:17 +1030 Subject: [PATCH 1/7] Java version of Slots --- .gitignore | 5 + 80 Slots/java/src/Slots.java | 297 +++++++++++++++++++++++++++++++ 80 Slots/java/src/SlotsGame.java | 6 + 3 files changed, 308 insertions(+) create mode 100644 80 Slots/java/src/Slots.java create mode 100644 80 Slots/java/src/SlotsGame.java diff --git a/.gitignore b/.gitignore index e69de29b..0bbe112f 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,5 @@ + +.DS_Store +*.iml +.idea +out diff --git a/80 Slots/java/src/Slots.java b/80 Slots/java/src/Slots.java new file mode 100644 index 00000000..9d96d94f --- /dev/null +++ b/80 Slots/java/src/Slots.java @@ -0,0 +1,297 @@ +import java.util.Arrays; +import java.util.Scanner; + +/** + * Game of Slots + *

+ * Based on the Basic game of Slots here + * https://github.com/coding-horror/basic-computer-games/blob/main/80%20Slots/slots.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 Slots { + + public static final String[] SLOT_SYMBOLS = {"BAR", "BELL", "ORANGE", "LEMON", "PLUM", "CHERRY"}; + + public static final int NUMBER_SYMBOLS = SLOT_SYMBOLS.length; + + // Jackpot symbol (BAR) + public static final int BAR_SYMBOL = 0; + + // Indicator that the current spin won nothing + public static final int NO_WINNER = -1; + + // Used for keyboard input + private final Scanner kbScanner; + + private enum GAME_STATE { + START_GAME, + ONE_SPIN, + RESULTS, + GAME_OVER + } + + // Current game state + private GAME_STATE gameState; + + // Different types of spin results + private enum WINNINGS { + JACKPOT(100), + TOP_DOLLAR(10), + DOUBLE_BAR(5), + REGULAR(2), + NO_WIN(0); + + private final int multiplier; + + WINNINGS(int mult) { + multiplier = mult; + } + + // No win returns the negative amount of net + // otherwise calculate winnings based on + // multiplier + public int calculateWinnings(int bet) { + + if (multiplier == 0) { + return -bet; + } else { + // Return original bet plus a multipler + // of the win type + return (multiplier * bet) + bet; + } + } + } + + private int playerBalance; + + public Slots() { + + kbScanner = new Scanner(System.in); + gameState = GAME_STATE.START_GAME; + } + + /** + * Main game loop + */ + public void play() { + + int[] slotReel = new int[3]; + + do { + // Results of a single spin + WINNINGS winnings; + + switch (gameState) { + + case START_GAME: + intro(); + playerBalance = 0; + gameState = GAME_STATE.ONE_SPIN; + break; + + case ONE_SPIN: + + int playerBet = displayTextAndGetNumber("YOUR BET? "); + + slotReel[0] = randomSymbol(); + slotReel[1] = randomSymbol(); + slotReel[2] = randomSymbol(); + + // Store which symbol (if any) matches at least one other reel + int whichSymbolWon = winningSymbol(slotReel[0], slotReel[1], slotReel[2]); + + // Display the three randomly drawn symbols + StringBuilder output = new StringBuilder(); + for (int i = 0; i < 3; i++) { + if (i > 0) { + output.append(" "); + } + output.append(SLOT_SYMBOLS[slotReel[i]]); + } + + System.out.println(output); + + // Calculate results + + if (whichSymbolWon == NO_WINNER) { + // No symbols match = nothing won + winnings = WINNINGS.NO_WIN; + } else if (slotReel[0] == slotReel[1] && slotReel[0] == slotReel[2]) { + // Top dollar, 3 matching symbols + winnings = WINNINGS.TOP_DOLLAR; + if (slotReel[0] == BAR_SYMBOL) { + // All 3 symbols are BAR. Jackpot! + winnings = WINNINGS.JACKPOT; + } + } else { + // At this point the remaining options are a regular win + // or a double, since the rest (including not winning) have already + // been checked above. + // Assume a regular win + winnings = WINNINGS.REGULAR; + + // But if it was the BAR symbol that matched, its a double bar + if (slotReel[0] == BAR_SYMBOL) { + winnings = WINNINGS.DOUBLE_BAR; + } + + } + + // Update the players balance with the amount won or lost on this spin + playerBalance += winnings.calculateWinnings(playerBet); + + System.out.println(); + + // Output what happened on this spin + switch (winnings) { + case NO_WIN: + System.out.println("YOU LOST."); + break; + + case REGULAR: + System.out.println("DOUBLE!!"); + System.out.println("YOU WON!"); + break; + + case DOUBLE_BAR: + System.out.println("*DOUBLE BAR*"); + System.out.println("YOU WON!"); + break; + + case TOP_DOLLAR: + System.out.println(); + System.out.println("**TOP DOLLAR**"); + System.out.println("YOU WON!"); + break; + + case JACKPOT: + System.out.println(); + System.out.println("***JACKPOT***"); + System.out.println("YOU WON!"); + break; + + } + + System.out.println("YOUR STANDINGS ARE $" + playerBalance); + + // If player does not elect to play again, show results of session + if (!yesEntered(displayTextAndGetInput("AGAIN? "))) { + gameState = GAME_STATE.RESULTS; + + } + break; + + case RESULTS: + if (playerBalance == 0) { + System.out.println("HEY, YOU BROKE EVEN."); + } else if (playerBalance > 0) { + System.out.println("COLLECT YOUR WINNINGS FROM THE H&M CASHIER."); + } else { + // Lost + System.out.println("PAY UP! PLEASE LEAVE YOUR MONEY ON THE TERMINAL."); + } + + gameState = GAME_STATE.GAME_OVER; + break; + } + } while (gameState != GAME_STATE.GAME_OVER); + } + + private void intro() { + System.out.println(simulateTabs(30) + "SLOTS"); + System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println(); + System.out.println("YOU ARE IN THE H&M CASINO,IN FRONT OF ONE OF OUR"); + System.out.println("ONE-ARM BANDITS. BET FROM $1 TO $100."); + System.out.println("TO PULL THE ARM, PUNCH THE RETURN KEY AFTER MAKING YOUR BET."); + } + + /* + * 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(); + } + + /** + * Checks whether player entered Y or YES to a question. + * + * @param text player string from kb + * @return true of Y or YES was entered, otherwise false + */ + private boolean yesEntered(String text) { + return stringIsAnyValue(text, "Y", "YES"); + } + + /** + * Check whether a string equals one of a variable number of values + * Useful to check for Y or YES for example + * Comparison is case insensitive. + * + * @param text source string + * @param values a range of values to compare against the source string + * @return true if a comparison was found in one of the variable number of strings passed + */ + private boolean stringIsAnyValue(String text, String... values) { + + return Arrays.stream(values).anyMatch(str -> str.equalsIgnoreCase(text)); + } + + /** + * 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); + } + + /** + * Find the symbol that won this round i.e. the first reel that matched another reel + * + * @param reel1 reel1 spin result + * @param reel2 reel2 spin result + * @param reel3 reel3 spin result + * @return NO_WINNER if no reels match otherwise an int 0-2 to indicate the reel that matches another + */ + private int winningSymbol(int reel1, int reel2, int reel3) { + if (reel1 == reel2) { + return 0; + } else if (reel1 == reel3) { + return 0; + } else if (reel2 == reel3) { + return 1; + } else { + return NO_WINNER; + } + } + + /** + * Random symbol for a slot wheel + * + * @return number between 0-5 + */ + private int randomSymbol() { + return (int) (Math.random() * NUMBER_SYMBOLS); + } +} diff --git a/80 Slots/java/src/SlotsGame.java b/80 Slots/java/src/SlotsGame.java new file mode 100644 index 00000000..05617cf7 --- /dev/null +++ b/80 Slots/java/src/SlotsGame.java @@ -0,0 +1,6 @@ +public class SlotsGame { + public static void main(String[] args) { + Slots slots = new Slots(); + slots.play(); + } +} From cae335093bc124f26ee0ca7c8feec5b7a148e1bc Mon Sep 17 00:00:00 2001 From: Tim Buchalka <70119791+journich@users.noreply.github.com> Date: Sun, 28 Feb 2021 08:15:55 +1030 Subject: [PATCH 2/7] Delete .gitignore --- .gitignore | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 0bbe112f..00000000 --- a/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ - -.DS_Store -*.iml -.idea -out From b77b7438cd73722350265b3973d51e204c4eb803 Mon Sep 17 00:00:00 2001 From: journich <70119791+journich@users.noreply.github.com> Date: Sun, 28 Feb 2021 11:20:00 +1030 Subject: [PATCH 3/7] Java version of Synonym --- 85 Synonym/java/src/Synonym.java | 137 +++++++++++++++++++++++++++ 85 Synonym/java/src/SynonymGame.java | 6 ++ 85 Synonym/java/src/SynonymList.java | 46 +++++++++ 3 files changed, 189 insertions(+) create mode 100644 85 Synonym/java/src/Synonym.java create mode 100644 85 Synonym/java/src/SynonymGame.java create mode 100644 85 Synonym/java/src/SynonymList.java diff --git a/85 Synonym/java/src/Synonym.java b/85 Synonym/java/src/Synonym.java new file mode 100644 index 00000000..2804bb49 --- /dev/null +++ b/85 Synonym/java/src/Synonym.java @@ -0,0 +1,137 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +/** + * Game of Synonym + *

+ * Based on the Basic game of Synonym here + * https://github.com/coding-horror/basic-computer-games/blob/main/85%20Synonym/synonym.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 Synonym { + + public static final String[] RANDOM_ANSWERS = {"RIGHT", "CORRECT", "FINE", "GOOD!", "CHECK"}; + + // Used for keyboard input + private final Scanner kbScanner; + + // List of words and synonyms + private final ArrayList synonyms; + + private enum GAME_STATE { + INIT, + PLAY, + GAME_OVER + } + + // Current game state + private GAME_STATE gameState; + + private int currentQuestion; + + public Synonym() { + + kbScanner = new Scanner(System.in); + synonyms = new ArrayList<>(); + + gameState = GAME_STATE.INIT; + } + + /** + * Main game loop + */ + public void play() { + + do { + switch (gameState) { + + case INIT: + intro(); + currentQuestion = 0; + + // Load data + synonyms.add(new SynonymList("FIRST", new String[]{"START", "BEGINNING", "ONSET", "INITIAL"})); + synonyms.add(new SynonymList("SIMILAR", new String[]{"SAME", "LIKE", "RESEMBLING"})); + synonyms.add(new SynonymList("MODEL", new String[]{"PATTERN", "PROTOTYPE", "STANDARD", "CRITERION"})); + synonyms.add(new SynonymList("SMALL", new String[]{"INSIGNIFICANT", "LITTLE", "TINY", "MINUTE"})); + synonyms.add(new SynonymList("STOP", new String[]{"HALT", "STAY", "ARREST", "CHECK", "STANDSTILL"})); + synonyms.add(new SynonymList("HOUSE", new String[]{"DWELLING", "RESIDENCE", "DOMICILE", "LODGING", "HABITATION"})); + synonyms.add(new SynonymList("PIT", new String[]{"HOLE", "HOLLOW", "WELL", "GULF", "CHASM", "ABYSS"})); + synonyms.add(new SynonymList("PUSH", new String[]{"SHOVE", "THRUST", "PROD", "POKE", "BUTT", "PRESS"})); + synonyms.add(new SynonymList("RED", new String[]{"ROUGE", "SCARLET", "CRIMSON", "FLAME", "RUBY"})); + synonyms.add(new SynonymList("PAIN", new String[]{"SUFFERING", "HURT", "MISERY", "DISTRESS", "ACHE", "DISCOMFORT"})); + + gameState = GAME_STATE.PLAY; + break; + + case PLAY: + + // Get the word and synonyms to ask a question about + SynonymList synonym = synonyms.get(currentQuestion); + String getAnswer = displayTextAndGetInput(" WHAT IS A SYNONYM OF " + synonym.getWord() + " ? "); + + // HELP is used to give a random synonym for the current word + if (getAnswer.equals("HELP")) { + int randomSynonym = (int) (Math.random() * synonym.size()); + System.out.println("**** A SYNONYM OF " + synonym.getWord() + " IS " + synonym.getSynonyms()[randomSynonym] + "."); + } else { + // Check if the entered word is in the synonym list + if (synonym.exists(getAnswer)) { + // If it is, give a random "correct" response + System.out.println(RANDOM_ANSWERS[(int) (Math.random() * RANDOM_ANSWERS.length)]); + currentQuestion++; + // Have we reached the final word/synonyms on file? + if (currentQuestion == synonyms.size()) { + // We have so end game. + System.out.println("SYNONYM DRILL COMPLETED."); + gameState = GAME_STATE.GAME_OVER; + } + } else { + // Word does not exist in the synonym list + System.out.println("TRY AGAIN."); + } + } + } + } while (gameState != GAME_STATE.GAME_OVER); + } + + private void intro() { + System.out.println(simulateTabs(33) + "SYNONYM"); + System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println(); + System.out.println("A SYNONYM OF A WORD MEANS ANOTHER WORD IN THE ENGLISH"); + System.out.println("LANGUAGE WHICH HAS THE SAME OR VERY NEARLY THE SAME"); + System.out.println(" MEANING."); + System.out.println("I CHOOSE A WORD -- YOU TYPE A SYNONYM."); + System.out.println("IF YOU CAN'T THINK OF A SYNONYM, TYPE THE WORD 'HELP'"); + System.out.println("AND I WILL TELL YOU A SYNONYM."); + System.out.println(); + } + + /* + * Print a message on the screen, then accept input from Keyboard. + * Converts input to uppercase. + * + * @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().toUpperCase(); + } + + /** + * 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/85 Synonym/java/src/SynonymGame.java b/85 Synonym/java/src/SynonymGame.java new file mode 100644 index 00000000..8ad52f4b --- /dev/null +++ b/85 Synonym/java/src/SynonymGame.java @@ -0,0 +1,6 @@ +public class SynonymGame { + public static void main(String[] args) { + Synonym synonym = new Synonym(); + synonym.play(); + } +} diff --git a/85 Synonym/java/src/SynonymList.java b/85 Synonym/java/src/SynonymList.java new file mode 100644 index 00000000..d692d48a --- /dev/null +++ b/85 Synonym/java/src/SynonymList.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Stores a word and a list of synonyms for that word + */ +public class SynonymList { + + private final String word; + + private final ArrayList synonyms; + + public SynonymList(String word, String[] synonyms) { + this.word = word; + this.synonyms = new ArrayList<>(Arrays.asList(synonyms)); + } + + /** + * Check if the word passed to this method exists in the list of synonyms + * N.B. Case insensitive + * + * @param word word to search for + * @return true if found, otherwise false + */ + public boolean exists(String word) { + return synonyms.stream().anyMatch(str -> str.equalsIgnoreCase(word)); + } + + public String getWord() { + return word; + } + + public int size() { + return synonyms.size(); + } + + /** + * Returns all synonyms for this word in string array format + * + * @return + */ + public String[] getSynonyms() { + // Parameter to toArray method determines type of the resultant array + return synonyms.toArray(new String[0]); + } +} From 843ee97e9fa6a92520c81c4acc1aa8cedc45747f Mon Sep 17 00:00:00 2001 From: journich <70119791+journich@users.noreply.github.com> Date: Sun, 28 Feb 2021 11:24:14 +1030 Subject: [PATCH 4/7] Revert "Merge branch 'main' of https://github.com/journich/basic-computer-games into main" This reverts commit 913bc1b91ffa84bfed62457470ee3c933f73fd21, reversing changes made to b77b7438cd73722350265b3973d51e204c4eb803. --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..0bbe112f --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ + +.DS_Store +*.iml +.idea +out From 56f2770d740578198c0ced822ceca534d10b0c19 Mon Sep 17 00:00:00 2001 From: journich <70119791+journich@users.noreply.github.com> Date: Sun, 28 Feb 2021 11:55:35 +1030 Subject: [PATCH 5/7] Revert "Java version of Slots" This reverts commit 3a902ffd6444aa4a9b9c77c022861bf5a405f0e0. --- .gitignore | 5 - 80 Slots/java/src/Slots.java | 297 ------------------------------- 80 Slots/java/src/SlotsGame.java | 6 - 3 files changed, 308 deletions(-) delete mode 100644 80 Slots/java/src/Slots.java delete mode 100644 80 Slots/java/src/SlotsGame.java diff --git a/.gitignore b/.gitignore index 0bbe112f..e69de29b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +0,0 @@ - -.DS_Store -*.iml -.idea -out diff --git a/80 Slots/java/src/Slots.java b/80 Slots/java/src/Slots.java deleted file mode 100644 index 9d96d94f..00000000 --- a/80 Slots/java/src/Slots.java +++ /dev/null @@ -1,297 +0,0 @@ -import java.util.Arrays; -import java.util.Scanner; - -/** - * Game of Slots - *

- * Based on the Basic game of Slots here - * https://github.com/coding-horror/basic-computer-games/blob/main/80%20Slots/slots.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 Slots { - - public static final String[] SLOT_SYMBOLS = {"BAR", "BELL", "ORANGE", "LEMON", "PLUM", "CHERRY"}; - - public static final int NUMBER_SYMBOLS = SLOT_SYMBOLS.length; - - // Jackpot symbol (BAR) - public static final int BAR_SYMBOL = 0; - - // Indicator that the current spin won nothing - public static final int NO_WINNER = -1; - - // Used for keyboard input - private final Scanner kbScanner; - - private enum GAME_STATE { - START_GAME, - ONE_SPIN, - RESULTS, - GAME_OVER - } - - // Current game state - private GAME_STATE gameState; - - // Different types of spin results - private enum WINNINGS { - JACKPOT(100), - TOP_DOLLAR(10), - DOUBLE_BAR(5), - REGULAR(2), - NO_WIN(0); - - private final int multiplier; - - WINNINGS(int mult) { - multiplier = mult; - } - - // No win returns the negative amount of net - // otherwise calculate winnings based on - // multiplier - public int calculateWinnings(int bet) { - - if (multiplier == 0) { - return -bet; - } else { - // Return original bet plus a multipler - // of the win type - return (multiplier * bet) + bet; - } - } - } - - private int playerBalance; - - public Slots() { - - kbScanner = new Scanner(System.in); - gameState = GAME_STATE.START_GAME; - } - - /** - * Main game loop - */ - public void play() { - - int[] slotReel = new int[3]; - - do { - // Results of a single spin - WINNINGS winnings; - - switch (gameState) { - - case START_GAME: - intro(); - playerBalance = 0; - gameState = GAME_STATE.ONE_SPIN; - break; - - case ONE_SPIN: - - int playerBet = displayTextAndGetNumber("YOUR BET? "); - - slotReel[0] = randomSymbol(); - slotReel[1] = randomSymbol(); - slotReel[2] = randomSymbol(); - - // Store which symbol (if any) matches at least one other reel - int whichSymbolWon = winningSymbol(slotReel[0], slotReel[1], slotReel[2]); - - // Display the three randomly drawn symbols - StringBuilder output = new StringBuilder(); - for (int i = 0; i < 3; i++) { - if (i > 0) { - output.append(" "); - } - output.append(SLOT_SYMBOLS[slotReel[i]]); - } - - System.out.println(output); - - // Calculate results - - if (whichSymbolWon == NO_WINNER) { - // No symbols match = nothing won - winnings = WINNINGS.NO_WIN; - } else if (slotReel[0] == slotReel[1] && slotReel[0] == slotReel[2]) { - // Top dollar, 3 matching symbols - winnings = WINNINGS.TOP_DOLLAR; - if (slotReel[0] == BAR_SYMBOL) { - // All 3 symbols are BAR. Jackpot! - winnings = WINNINGS.JACKPOT; - } - } else { - // At this point the remaining options are a regular win - // or a double, since the rest (including not winning) have already - // been checked above. - // Assume a regular win - winnings = WINNINGS.REGULAR; - - // But if it was the BAR symbol that matched, its a double bar - if (slotReel[0] == BAR_SYMBOL) { - winnings = WINNINGS.DOUBLE_BAR; - } - - } - - // Update the players balance with the amount won or lost on this spin - playerBalance += winnings.calculateWinnings(playerBet); - - System.out.println(); - - // Output what happened on this spin - switch (winnings) { - case NO_WIN: - System.out.println("YOU LOST."); - break; - - case REGULAR: - System.out.println("DOUBLE!!"); - System.out.println("YOU WON!"); - break; - - case DOUBLE_BAR: - System.out.println("*DOUBLE BAR*"); - System.out.println("YOU WON!"); - break; - - case TOP_DOLLAR: - System.out.println(); - System.out.println("**TOP DOLLAR**"); - System.out.println("YOU WON!"); - break; - - case JACKPOT: - System.out.println(); - System.out.println("***JACKPOT***"); - System.out.println("YOU WON!"); - break; - - } - - System.out.println("YOUR STANDINGS ARE $" + playerBalance); - - // If player does not elect to play again, show results of session - if (!yesEntered(displayTextAndGetInput("AGAIN? "))) { - gameState = GAME_STATE.RESULTS; - - } - break; - - case RESULTS: - if (playerBalance == 0) { - System.out.println("HEY, YOU BROKE EVEN."); - } else if (playerBalance > 0) { - System.out.println("COLLECT YOUR WINNINGS FROM THE H&M CASHIER."); - } else { - // Lost - System.out.println("PAY UP! PLEASE LEAVE YOUR MONEY ON THE TERMINAL."); - } - - gameState = GAME_STATE.GAME_OVER; - break; - } - } while (gameState != GAME_STATE.GAME_OVER); - } - - private void intro() { - System.out.println(simulateTabs(30) + "SLOTS"); - System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); - System.out.println(); - System.out.println("YOU ARE IN THE H&M CASINO,IN FRONT OF ONE OF OUR"); - System.out.println("ONE-ARM BANDITS. BET FROM $1 TO $100."); - System.out.println("TO PULL THE ARM, PUNCH THE RETURN KEY AFTER MAKING YOUR BET."); - } - - /* - * 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(); - } - - /** - * Checks whether player entered Y or YES to a question. - * - * @param text player string from kb - * @return true of Y or YES was entered, otherwise false - */ - private boolean yesEntered(String text) { - return stringIsAnyValue(text, "Y", "YES"); - } - - /** - * Check whether a string equals one of a variable number of values - * Useful to check for Y or YES for example - * Comparison is case insensitive. - * - * @param text source string - * @param values a range of values to compare against the source string - * @return true if a comparison was found in one of the variable number of strings passed - */ - private boolean stringIsAnyValue(String text, String... values) { - - return Arrays.stream(values).anyMatch(str -> str.equalsIgnoreCase(text)); - } - - /** - * 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); - } - - /** - * Find the symbol that won this round i.e. the first reel that matched another reel - * - * @param reel1 reel1 spin result - * @param reel2 reel2 spin result - * @param reel3 reel3 spin result - * @return NO_WINNER if no reels match otherwise an int 0-2 to indicate the reel that matches another - */ - private int winningSymbol(int reel1, int reel2, int reel3) { - if (reel1 == reel2) { - return 0; - } else if (reel1 == reel3) { - return 0; - } else if (reel2 == reel3) { - return 1; - } else { - return NO_WINNER; - } - } - - /** - * Random symbol for a slot wheel - * - * @return number between 0-5 - */ - private int randomSymbol() { - return (int) (Math.random() * NUMBER_SYMBOLS); - } -} diff --git a/80 Slots/java/src/SlotsGame.java b/80 Slots/java/src/SlotsGame.java deleted file mode 100644 index 05617cf7..00000000 --- a/80 Slots/java/src/SlotsGame.java +++ /dev/null @@ -1,6 +0,0 @@ -public class SlotsGame { - public static void main(String[] args) { - Slots slots = new Slots(); - slots.play(); - } -} From 165b0f5cffc637a4aeb90628bcf352e96284c410 Mon Sep 17 00:00:00 2001 From: journich <70119791+journich@users.noreply.github.com> Date: Sun, 28 Feb 2021 12:04:20 +1030 Subject: [PATCH 6/7] Java versions of Slots and Synonym --- 80 Slots/java/src/Slots.java | 297 +++++++++++++++++++++++++++ 80 Slots/java/src/SlotsGame.java | 6 + 85 Synonym/java/src/Synonym.java | 137 ------------ 85 Synonym/java/src/SynonymGame.java | 6 - 85 Synonym/java/src/SynonymList.java | 46 ----- 5 files changed, 303 insertions(+), 189 deletions(-) create mode 100644 80 Slots/java/src/Slots.java create mode 100644 80 Slots/java/src/SlotsGame.java delete mode 100644 85 Synonym/java/src/Synonym.java delete mode 100644 85 Synonym/java/src/SynonymGame.java delete mode 100644 85 Synonym/java/src/SynonymList.java diff --git a/80 Slots/java/src/Slots.java b/80 Slots/java/src/Slots.java new file mode 100644 index 00000000..9d96d94f --- /dev/null +++ b/80 Slots/java/src/Slots.java @@ -0,0 +1,297 @@ +import java.util.Arrays; +import java.util.Scanner; + +/** + * Game of Slots + *

+ * Based on the Basic game of Slots here + * https://github.com/coding-horror/basic-computer-games/blob/main/80%20Slots/slots.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 Slots { + + public static final String[] SLOT_SYMBOLS = {"BAR", "BELL", "ORANGE", "LEMON", "PLUM", "CHERRY"}; + + public static final int NUMBER_SYMBOLS = SLOT_SYMBOLS.length; + + // Jackpot symbol (BAR) + public static final int BAR_SYMBOL = 0; + + // Indicator that the current spin won nothing + public static final int NO_WINNER = -1; + + // Used for keyboard input + private final Scanner kbScanner; + + private enum GAME_STATE { + START_GAME, + ONE_SPIN, + RESULTS, + GAME_OVER + } + + // Current game state + private GAME_STATE gameState; + + // Different types of spin results + private enum WINNINGS { + JACKPOT(100), + TOP_DOLLAR(10), + DOUBLE_BAR(5), + REGULAR(2), + NO_WIN(0); + + private final int multiplier; + + WINNINGS(int mult) { + multiplier = mult; + } + + // No win returns the negative amount of net + // otherwise calculate winnings based on + // multiplier + public int calculateWinnings(int bet) { + + if (multiplier == 0) { + return -bet; + } else { + // Return original bet plus a multipler + // of the win type + return (multiplier * bet) + bet; + } + } + } + + private int playerBalance; + + public Slots() { + + kbScanner = new Scanner(System.in); + gameState = GAME_STATE.START_GAME; + } + + /** + * Main game loop + */ + public void play() { + + int[] slotReel = new int[3]; + + do { + // Results of a single spin + WINNINGS winnings; + + switch (gameState) { + + case START_GAME: + intro(); + playerBalance = 0; + gameState = GAME_STATE.ONE_SPIN; + break; + + case ONE_SPIN: + + int playerBet = displayTextAndGetNumber("YOUR BET? "); + + slotReel[0] = randomSymbol(); + slotReel[1] = randomSymbol(); + slotReel[2] = randomSymbol(); + + // Store which symbol (if any) matches at least one other reel + int whichSymbolWon = winningSymbol(slotReel[0], slotReel[1], slotReel[2]); + + // Display the three randomly drawn symbols + StringBuilder output = new StringBuilder(); + for (int i = 0; i < 3; i++) { + if (i > 0) { + output.append(" "); + } + output.append(SLOT_SYMBOLS[slotReel[i]]); + } + + System.out.println(output); + + // Calculate results + + if (whichSymbolWon == NO_WINNER) { + // No symbols match = nothing won + winnings = WINNINGS.NO_WIN; + } else if (slotReel[0] == slotReel[1] && slotReel[0] == slotReel[2]) { + // Top dollar, 3 matching symbols + winnings = WINNINGS.TOP_DOLLAR; + if (slotReel[0] == BAR_SYMBOL) { + // All 3 symbols are BAR. Jackpot! + winnings = WINNINGS.JACKPOT; + } + } else { + // At this point the remaining options are a regular win + // or a double, since the rest (including not winning) have already + // been checked above. + // Assume a regular win + winnings = WINNINGS.REGULAR; + + // But if it was the BAR symbol that matched, its a double bar + if (slotReel[0] == BAR_SYMBOL) { + winnings = WINNINGS.DOUBLE_BAR; + } + + } + + // Update the players balance with the amount won or lost on this spin + playerBalance += winnings.calculateWinnings(playerBet); + + System.out.println(); + + // Output what happened on this spin + switch (winnings) { + case NO_WIN: + System.out.println("YOU LOST."); + break; + + case REGULAR: + System.out.println("DOUBLE!!"); + System.out.println("YOU WON!"); + break; + + case DOUBLE_BAR: + System.out.println("*DOUBLE BAR*"); + System.out.println("YOU WON!"); + break; + + case TOP_DOLLAR: + System.out.println(); + System.out.println("**TOP DOLLAR**"); + System.out.println("YOU WON!"); + break; + + case JACKPOT: + System.out.println(); + System.out.println("***JACKPOT***"); + System.out.println("YOU WON!"); + break; + + } + + System.out.println("YOUR STANDINGS ARE $" + playerBalance); + + // If player does not elect to play again, show results of session + if (!yesEntered(displayTextAndGetInput("AGAIN? "))) { + gameState = GAME_STATE.RESULTS; + + } + break; + + case RESULTS: + if (playerBalance == 0) { + System.out.println("HEY, YOU BROKE EVEN."); + } else if (playerBalance > 0) { + System.out.println("COLLECT YOUR WINNINGS FROM THE H&M CASHIER."); + } else { + // Lost + System.out.println("PAY UP! PLEASE LEAVE YOUR MONEY ON THE TERMINAL."); + } + + gameState = GAME_STATE.GAME_OVER; + break; + } + } while (gameState != GAME_STATE.GAME_OVER); + } + + private void intro() { + System.out.println(simulateTabs(30) + "SLOTS"); + System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println(); + System.out.println("YOU ARE IN THE H&M CASINO,IN FRONT OF ONE OF OUR"); + System.out.println("ONE-ARM BANDITS. BET FROM $1 TO $100."); + System.out.println("TO PULL THE ARM, PUNCH THE RETURN KEY AFTER MAKING YOUR BET."); + } + + /* + * 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(); + } + + /** + * Checks whether player entered Y or YES to a question. + * + * @param text player string from kb + * @return true of Y or YES was entered, otherwise false + */ + private boolean yesEntered(String text) { + return stringIsAnyValue(text, "Y", "YES"); + } + + /** + * Check whether a string equals one of a variable number of values + * Useful to check for Y or YES for example + * Comparison is case insensitive. + * + * @param text source string + * @param values a range of values to compare against the source string + * @return true if a comparison was found in one of the variable number of strings passed + */ + private boolean stringIsAnyValue(String text, String... values) { + + return Arrays.stream(values).anyMatch(str -> str.equalsIgnoreCase(text)); + } + + /** + * 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); + } + + /** + * Find the symbol that won this round i.e. the first reel that matched another reel + * + * @param reel1 reel1 spin result + * @param reel2 reel2 spin result + * @param reel3 reel3 spin result + * @return NO_WINNER if no reels match otherwise an int 0-2 to indicate the reel that matches another + */ + private int winningSymbol(int reel1, int reel2, int reel3) { + if (reel1 == reel2) { + return 0; + } else if (reel1 == reel3) { + return 0; + } else if (reel2 == reel3) { + return 1; + } else { + return NO_WINNER; + } + } + + /** + * Random symbol for a slot wheel + * + * @return number between 0-5 + */ + private int randomSymbol() { + return (int) (Math.random() * NUMBER_SYMBOLS); + } +} diff --git a/80 Slots/java/src/SlotsGame.java b/80 Slots/java/src/SlotsGame.java new file mode 100644 index 00000000..05617cf7 --- /dev/null +++ b/80 Slots/java/src/SlotsGame.java @@ -0,0 +1,6 @@ +public class SlotsGame { + public static void main(String[] args) { + Slots slots = new Slots(); + slots.play(); + } +} diff --git a/85 Synonym/java/src/Synonym.java b/85 Synonym/java/src/Synonym.java deleted file mode 100644 index 2804bb49..00000000 --- a/85 Synonym/java/src/Synonym.java +++ /dev/null @@ -1,137 +0,0 @@ -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Scanner; - -/** - * Game of Synonym - *

- * Based on the Basic game of Synonym here - * https://github.com/coding-horror/basic-computer-games/blob/main/85%20Synonym/synonym.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 Synonym { - - public static final String[] RANDOM_ANSWERS = {"RIGHT", "CORRECT", "FINE", "GOOD!", "CHECK"}; - - // Used for keyboard input - private final Scanner kbScanner; - - // List of words and synonyms - private final ArrayList synonyms; - - private enum GAME_STATE { - INIT, - PLAY, - GAME_OVER - } - - // Current game state - private GAME_STATE gameState; - - private int currentQuestion; - - public Synonym() { - - kbScanner = new Scanner(System.in); - synonyms = new ArrayList<>(); - - gameState = GAME_STATE.INIT; - } - - /** - * Main game loop - */ - public void play() { - - do { - switch (gameState) { - - case INIT: - intro(); - currentQuestion = 0; - - // Load data - synonyms.add(new SynonymList("FIRST", new String[]{"START", "BEGINNING", "ONSET", "INITIAL"})); - synonyms.add(new SynonymList("SIMILAR", new String[]{"SAME", "LIKE", "RESEMBLING"})); - synonyms.add(new SynonymList("MODEL", new String[]{"PATTERN", "PROTOTYPE", "STANDARD", "CRITERION"})); - synonyms.add(new SynonymList("SMALL", new String[]{"INSIGNIFICANT", "LITTLE", "TINY", "MINUTE"})); - synonyms.add(new SynonymList("STOP", new String[]{"HALT", "STAY", "ARREST", "CHECK", "STANDSTILL"})); - synonyms.add(new SynonymList("HOUSE", new String[]{"DWELLING", "RESIDENCE", "DOMICILE", "LODGING", "HABITATION"})); - synonyms.add(new SynonymList("PIT", new String[]{"HOLE", "HOLLOW", "WELL", "GULF", "CHASM", "ABYSS"})); - synonyms.add(new SynonymList("PUSH", new String[]{"SHOVE", "THRUST", "PROD", "POKE", "BUTT", "PRESS"})); - synonyms.add(new SynonymList("RED", new String[]{"ROUGE", "SCARLET", "CRIMSON", "FLAME", "RUBY"})); - synonyms.add(new SynonymList("PAIN", new String[]{"SUFFERING", "HURT", "MISERY", "DISTRESS", "ACHE", "DISCOMFORT"})); - - gameState = GAME_STATE.PLAY; - break; - - case PLAY: - - // Get the word and synonyms to ask a question about - SynonymList synonym = synonyms.get(currentQuestion); - String getAnswer = displayTextAndGetInput(" WHAT IS A SYNONYM OF " + synonym.getWord() + " ? "); - - // HELP is used to give a random synonym for the current word - if (getAnswer.equals("HELP")) { - int randomSynonym = (int) (Math.random() * synonym.size()); - System.out.println("**** A SYNONYM OF " + synonym.getWord() + " IS " + synonym.getSynonyms()[randomSynonym] + "."); - } else { - // Check if the entered word is in the synonym list - if (synonym.exists(getAnswer)) { - // If it is, give a random "correct" response - System.out.println(RANDOM_ANSWERS[(int) (Math.random() * RANDOM_ANSWERS.length)]); - currentQuestion++; - // Have we reached the final word/synonyms on file? - if (currentQuestion == synonyms.size()) { - // We have so end game. - System.out.println("SYNONYM DRILL COMPLETED."); - gameState = GAME_STATE.GAME_OVER; - } - } else { - // Word does not exist in the synonym list - System.out.println("TRY AGAIN."); - } - } - } - } while (gameState != GAME_STATE.GAME_OVER); - } - - private void intro() { - System.out.println(simulateTabs(33) + "SYNONYM"); - System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); - System.out.println(); - System.out.println("A SYNONYM OF A WORD MEANS ANOTHER WORD IN THE ENGLISH"); - System.out.println("LANGUAGE WHICH HAS THE SAME OR VERY NEARLY THE SAME"); - System.out.println(" MEANING."); - System.out.println("I CHOOSE A WORD -- YOU TYPE A SYNONYM."); - System.out.println("IF YOU CAN'T THINK OF A SYNONYM, TYPE THE WORD 'HELP'"); - System.out.println("AND I WILL TELL YOU A SYNONYM."); - System.out.println(); - } - - /* - * Print a message on the screen, then accept input from Keyboard. - * Converts input to uppercase. - * - * @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().toUpperCase(); - } - - /** - * 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/85 Synonym/java/src/SynonymGame.java b/85 Synonym/java/src/SynonymGame.java deleted file mode 100644 index 8ad52f4b..00000000 --- a/85 Synonym/java/src/SynonymGame.java +++ /dev/null @@ -1,6 +0,0 @@ -public class SynonymGame { - public static void main(String[] args) { - Synonym synonym = new Synonym(); - synonym.play(); - } -} diff --git a/85 Synonym/java/src/SynonymList.java b/85 Synonym/java/src/SynonymList.java deleted file mode 100644 index d692d48a..00000000 --- a/85 Synonym/java/src/SynonymList.java +++ /dev/null @@ -1,46 +0,0 @@ -import java.util.ArrayList; -import java.util.Arrays; - -/** - * Stores a word and a list of synonyms for that word - */ -public class SynonymList { - - private final String word; - - private final ArrayList synonyms; - - public SynonymList(String word, String[] synonyms) { - this.word = word; - this.synonyms = new ArrayList<>(Arrays.asList(synonyms)); - } - - /** - * Check if the word passed to this method exists in the list of synonyms - * N.B. Case insensitive - * - * @param word word to search for - * @return true if found, otherwise false - */ - public boolean exists(String word) { - return synonyms.stream().anyMatch(str -> str.equalsIgnoreCase(word)); - } - - public String getWord() { - return word; - } - - public int size() { - return synonyms.size(); - } - - /** - * Returns all synonyms for this word in string array format - * - * @return - */ - public String[] getSynonyms() { - // Parameter to toArray method determines type of the resultant array - return synonyms.toArray(new String[0]); - } -} From 70b5a44fbaedbbf359d4fbabda32d86086dc26a8 Mon Sep 17 00:00:00 2001 From: journich <70119791+journich@users.noreply.github.com> Date: Sun, 28 Feb 2021 12:10:10 +1030 Subject: [PATCH 7/7] Java version of Synonym --- 85 Synonym/java/src/Synonym.java | 137 +++++++++++++++++++++++++++ 85 Synonym/java/src/SynonymGame.java | 6 ++ 85 Synonym/java/src/SynonymList.java | 46 +++++++++ 3 files changed, 189 insertions(+) create mode 100644 85 Synonym/java/src/Synonym.java create mode 100644 85 Synonym/java/src/SynonymGame.java create mode 100644 85 Synonym/java/src/SynonymList.java diff --git a/85 Synonym/java/src/Synonym.java b/85 Synonym/java/src/Synonym.java new file mode 100644 index 00000000..2804bb49 --- /dev/null +++ b/85 Synonym/java/src/Synonym.java @@ -0,0 +1,137 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Scanner; + +/** + * Game of Synonym + *

+ * Based on the Basic game of Synonym here + * https://github.com/coding-horror/basic-computer-games/blob/main/85%20Synonym/synonym.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 Synonym { + + public static final String[] RANDOM_ANSWERS = {"RIGHT", "CORRECT", "FINE", "GOOD!", "CHECK"}; + + // Used for keyboard input + private final Scanner kbScanner; + + // List of words and synonyms + private final ArrayList synonyms; + + private enum GAME_STATE { + INIT, + PLAY, + GAME_OVER + } + + // Current game state + private GAME_STATE gameState; + + private int currentQuestion; + + public Synonym() { + + kbScanner = new Scanner(System.in); + synonyms = new ArrayList<>(); + + gameState = GAME_STATE.INIT; + } + + /** + * Main game loop + */ + public void play() { + + do { + switch (gameState) { + + case INIT: + intro(); + currentQuestion = 0; + + // Load data + synonyms.add(new SynonymList("FIRST", new String[]{"START", "BEGINNING", "ONSET", "INITIAL"})); + synonyms.add(new SynonymList("SIMILAR", new String[]{"SAME", "LIKE", "RESEMBLING"})); + synonyms.add(new SynonymList("MODEL", new String[]{"PATTERN", "PROTOTYPE", "STANDARD", "CRITERION"})); + synonyms.add(new SynonymList("SMALL", new String[]{"INSIGNIFICANT", "LITTLE", "TINY", "MINUTE"})); + synonyms.add(new SynonymList("STOP", new String[]{"HALT", "STAY", "ARREST", "CHECK", "STANDSTILL"})); + synonyms.add(new SynonymList("HOUSE", new String[]{"DWELLING", "RESIDENCE", "DOMICILE", "LODGING", "HABITATION"})); + synonyms.add(new SynonymList("PIT", new String[]{"HOLE", "HOLLOW", "WELL", "GULF", "CHASM", "ABYSS"})); + synonyms.add(new SynonymList("PUSH", new String[]{"SHOVE", "THRUST", "PROD", "POKE", "BUTT", "PRESS"})); + synonyms.add(new SynonymList("RED", new String[]{"ROUGE", "SCARLET", "CRIMSON", "FLAME", "RUBY"})); + synonyms.add(new SynonymList("PAIN", new String[]{"SUFFERING", "HURT", "MISERY", "DISTRESS", "ACHE", "DISCOMFORT"})); + + gameState = GAME_STATE.PLAY; + break; + + case PLAY: + + // Get the word and synonyms to ask a question about + SynonymList synonym = synonyms.get(currentQuestion); + String getAnswer = displayTextAndGetInput(" WHAT IS A SYNONYM OF " + synonym.getWord() + " ? "); + + // HELP is used to give a random synonym for the current word + if (getAnswer.equals("HELP")) { + int randomSynonym = (int) (Math.random() * synonym.size()); + System.out.println("**** A SYNONYM OF " + synonym.getWord() + " IS " + synonym.getSynonyms()[randomSynonym] + "."); + } else { + // Check if the entered word is in the synonym list + if (synonym.exists(getAnswer)) { + // If it is, give a random "correct" response + System.out.println(RANDOM_ANSWERS[(int) (Math.random() * RANDOM_ANSWERS.length)]); + currentQuestion++; + // Have we reached the final word/synonyms on file? + if (currentQuestion == synonyms.size()) { + // We have so end game. + System.out.println("SYNONYM DRILL COMPLETED."); + gameState = GAME_STATE.GAME_OVER; + } + } else { + // Word does not exist in the synonym list + System.out.println("TRY AGAIN."); + } + } + } + } while (gameState != GAME_STATE.GAME_OVER); + } + + private void intro() { + System.out.println(simulateTabs(33) + "SYNONYM"); + System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY"); + System.out.println(); + System.out.println("A SYNONYM OF A WORD MEANS ANOTHER WORD IN THE ENGLISH"); + System.out.println("LANGUAGE WHICH HAS THE SAME OR VERY NEARLY THE SAME"); + System.out.println(" MEANING."); + System.out.println("I CHOOSE A WORD -- YOU TYPE A SYNONYM."); + System.out.println("IF YOU CAN'T THINK OF A SYNONYM, TYPE THE WORD 'HELP'"); + System.out.println("AND I WILL TELL YOU A SYNONYM."); + System.out.println(); + } + + /* + * Print a message on the screen, then accept input from Keyboard. + * Converts input to uppercase. + * + * @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().toUpperCase(); + } + + /** + * 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/85 Synonym/java/src/SynonymGame.java b/85 Synonym/java/src/SynonymGame.java new file mode 100644 index 00000000..8ad52f4b --- /dev/null +++ b/85 Synonym/java/src/SynonymGame.java @@ -0,0 +1,6 @@ +public class SynonymGame { + public static void main(String[] args) { + Synonym synonym = new Synonym(); + synonym.play(); + } +} diff --git a/85 Synonym/java/src/SynonymList.java b/85 Synonym/java/src/SynonymList.java new file mode 100644 index 00000000..d692d48a --- /dev/null +++ b/85 Synonym/java/src/SynonymList.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; +import java.util.Arrays; + +/** + * Stores a word and a list of synonyms for that word + */ +public class SynonymList { + + private final String word; + + private final ArrayList synonyms; + + public SynonymList(String word, String[] synonyms) { + this.word = word; + this.synonyms = new ArrayList<>(Arrays.asList(synonyms)); + } + + /** + * Check if the word passed to this method exists in the list of synonyms + * N.B. Case insensitive + * + * @param word word to search for + * @return true if found, otherwise false + */ + public boolean exists(String word) { + return synonyms.stream().anyMatch(str -> str.equalsIgnoreCase(word)); + } + + public String getWord() { + return word; + } + + public int size() { + return synonyms.size(); + } + + /** + * Returns all synonyms for this word in string array format + * + * @return + */ + public String[] getSynonyms() { + // Parameter to toArray method determines type of the resultant array + return synonyms.toArray(new String[0]); + } +}