Merge pull request #107 from journich/main

Java versions of Slots and Synonym
This commit is contained in:
Jeff Atwood
2021-02-27 17:47:47 -08:00
committed by GitHub
5 changed files with 492 additions and 0 deletions

View File

@@ -0,0 +1,297 @@
import java.util.Arrays;
import java.util.Scanner;
/**
* Game of Slots
* <p>
* Based on the Basic game of Slots here
* https://github.com/coding-horror/basic-computer-games/blob/main/80%20Slots/slots.bas
* <p>
* 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);
}
}

View File

@@ -0,0 +1,6 @@
public class SlotsGame {
public static void main(String[] args) {
Slots slots = new Slots();
slots.play();
}
}

View File

@@ -0,0 +1,137 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
* Game of Synonym
* <p>
* Based on the Basic game of Synonym here
* https://github.com/coding-horror/basic-computer-games/blob/main/85%20Synonym/synonym.bas
* <p>
* 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<SynonymList> 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);
}
}

View File

@@ -0,0 +1,6 @@
public class SynonymGame {
public static void main(String[] args) {
Synonym synonym = new Synonym();
synonym.play();
}
}

View File

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