mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-28 05:33:48 -08:00
Merge pull request #94 from journich/main
Java versions of Dice and Guess
This commit is contained in:
146
33 Dice/java/src/Dice.java
Normal file
146
33 Dice/java/src/Dice.java
Normal file
@@ -0,0 +1,146 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Dice
|
||||
* <p>
|
||||
* Based on the Basic game of Dice here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/33%20Dice/dice.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 Dice {
|
||||
|
||||
// Used for keyboard input
|
||||
private final Scanner kbScanner;
|
||||
|
||||
private enum GAME_STATE {
|
||||
START_GAME,
|
||||
INPUT_AND_CALCULATE,
|
||||
RESULTS,
|
||||
GAME_OVER
|
||||
}
|
||||
|
||||
// Current game state
|
||||
private GAME_STATE gameState;
|
||||
|
||||
private int[] spots;
|
||||
|
||||
public Dice() {
|
||||
kbScanner = new Scanner(System.in);
|
||||
|
||||
gameState = GAME_STATE.START_GAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main game loop
|
||||
*/
|
||||
public void play() {
|
||||
|
||||
do {
|
||||
switch (gameState) {
|
||||
|
||||
case START_GAME:
|
||||
intro();
|
||||
spots = new int[12];
|
||||
gameState = GAME_STATE.INPUT_AND_CALCULATE;
|
||||
break;
|
||||
|
||||
case INPUT_AND_CALCULATE:
|
||||
|
||||
int howManyRolls = displayTextAndGetNumber("HOW MANY ROLLS? ");
|
||||
for (int i = 0; i < howManyRolls; i++) {
|
||||
int diceRoll = (int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1);
|
||||
// save dice roll in zero based array
|
||||
spots[diceRoll - 1]++;
|
||||
}
|
||||
gameState = GAME_STATE.RESULTS;
|
||||
break;
|
||||
|
||||
case RESULTS:
|
||||
System.out.println("TOTAL SPOTS" + simulateTabs(8) + "NUMBER OF TIMES");
|
||||
for (int i = 1; i < 12; i++) {
|
||||
// show output using zero based array
|
||||
System.out.println(simulateTabs(5) + (i + 1) + simulateTabs(20) + spots[i]);
|
||||
}
|
||||
System.out.println();
|
||||
if (yesEntered(displayTextAndGetInput("TRY AGAIN? "))) {
|
||||
gameState = GAME_STATE.START_GAME;
|
||||
} else {
|
||||
gameState = GAME_STATE.GAME_OVER;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} while (gameState != GAME_STATE.GAME_OVER);
|
||||
}
|
||||
|
||||
private void intro() {
|
||||
System.out.println(simulateTabs(34) + "DICE");
|
||||
System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println("THIS PROGRAM SIMULATES THE ROLLING OF A");
|
||||
System.out.println("PAIR OF DICE.");
|
||||
System.out.println("YOU ENTER THE NUMBER OF TIMES YOU WANT THE COMPUTER TO");
|
||||
System.out.println("'ROLL' THE DICE. WATCH OUT, VERY LARGE NUMBERS TAKE");
|
||||
System.out.println("A LONG TIME. IN PARTICULAR, NUMBERS OVER 5000.");
|
||||
}
|
||||
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
6
33 Dice/java/src/DiceGame.java
Normal file
6
33 Dice/java/src/DiceGame.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class DiceGame {
|
||||
public static void main(String[] args) {
|
||||
Dice dice = new Dice();
|
||||
dice.play();
|
||||
}
|
||||
}
|
||||
161
41 Guess/java/src/Guess.java
Normal file
161
41 Guess/java/src/Guess.java
Normal file
@@ -0,0 +1,161 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Guess
|
||||
* <p>
|
||||
* Based on the Basic game of Guess here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/41%20Guess/guess.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 Guess {
|
||||
|
||||
// Used for keyboard input
|
||||
private final Scanner kbScanner;
|
||||
|
||||
private enum GAME_STATE {
|
||||
STARTUP,
|
||||
INPUT_RANGE,
|
||||
DEFINE_COMPUTERS_NUMBER,
|
||||
GUESS,
|
||||
GAME_OVER
|
||||
}
|
||||
|
||||
// Current game state
|
||||
private GAME_STATE gameState;
|
||||
|
||||
// User supplied maximum number to guess
|
||||
private int limit;
|
||||
|
||||
// Computers calculated number for the player to guess
|
||||
|
||||
private int computersNumber;
|
||||
|
||||
// Number of turns the player has had guessing
|
||||
private int tries;
|
||||
|
||||
// Optimal number of turns it should take to guess
|
||||
private int calculatedTurns;
|
||||
|
||||
public Guess() {
|
||||
kbScanner = new Scanner(System.in);
|
||||
|
||||
gameState = GAME_STATE.STARTUP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main game loop
|
||||
*/
|
||||
public void play() {
|
||||
|
||||
do {
|
||||
switch (gameState) {
|
||||
|
||||
case STARTUP:
|
||||
intro();
|
||||
gameState = GAME_STATE.INPUT_RANGE;
|
||||
break;
|
||||
|
||||
case INPUT_RANGE:
|
||||
|
||||
limit = displayTextAndGetNumber("WHAT LIMIT DO YOU WANT? ");
|
||||
calculatedTurns = (int) (Math.log(limit) / Math.log(2)) + 1;
|
||||
gameState = GAME_STATE.DEFINE_COMPUTERS_NUMBER;
|
||||
break;
|
||||
|
||||
case DEFINE_COMPUTERS_NUMBER:
|
||||
|
||||
tries = 1;
|
||||
System.out.println("I'M THINKING OF A NUMBER BETWEEN 1 AND " + limit);
|
||||
computersNumber = (int) (Math.random() * limit + 1);
|
||||
|
||||
gameState = GAME_STATE.GUESS;
|
||||
break;
|
||||
|
||||
case GUESS:
|
||||
int playersGuess = displayTextAndGetNumber("NOW YOU TRY TO GUESS WHAT IT IS ");
|
||||
|
||||
// Allow player to restart game with entry of 0
|
||||
if (playersGuess == 0) {
|
||||
linePadding();
|
||||
gameState = GAME_STATE.STARTUP;
|
||||
break;
|
||||
}
|
||||
|
||||
if (playersGuess == computersNumber) {
|
||||
System.out.println("THAT'S IT! YOU GOT IT IN " + tries + " TRIES.");
|
||||
if (tries < calculatedTurns) {
|
||||
System.out.println("VERY ");
|
||||
}
|
||||
System.out.println("GOOD.");
|
||||
System.out.println("YOU SHOULD HAVE BEEN ABLE TO GET IT IN ONLY " + calculatedTurns);
|
||||
linePadding();
|
||||
gameState = GAME_STATE.DEFINE_COMPUTERS_NUMBER;
|
||||
break;
|
||||
} else if (playersGuess < computersNumber) {
|
||||
System.out.println("TOO LOW. TRY A BIGGER ANSWER.");
|
||||
} else {
|
||||
System.out.println("TOO HIGH. TRY A SMALLER ANSWER.");
|
||||
}
|
||||
tries++;
|
||||
break;
|
||||
}
|
||||
} while (gameState != GAME_STATE.GAME_OVER);
|
||||
}
|
||||
|
||||
private void intro() {
|
||||
System.out.println(simulateTabs(33) + "GUESS");
|
||||
System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println("THIS IS A NUMBER GUESSING GAME. I'LL THINK");
|
||||
System.out.println("OF A NUMBER BETWEEN 1 AND ANY LIMIT YOU WANT.");
|
||||
System.out.println("THEN YOU HAVE TO GUESS WHAT IT IS.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a predefined number of blank lines
|
||||
*
|
||||
*/
|
||||
private void linePadding() {
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
6
41 Guess/java/src/GuessGame.java
Normal file
6
41 Guess/java/src/GuessGame.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class GuessGame {
|
||||
public static void main(String[] args) {
|
||||
Guess guess = new Guess();
|
||||
guess.play();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user