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] 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]);
+ }
+}