mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-29 06:05:36 -08:00
Merge pull request #95 from journich/main
Java versions of Kinema,Letter and Literature Quiz
This commit is contained in:
176
52 Kinema/java/src/Kinema.java
Normal file
176
52 Kinema/java/src/Kinema.java
Normal file
@@ -0,0 +1,176 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Kinema
|
||||
* <p>
|
||||
* Based on the Basic game of Kinema here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/52%20Kinema/kinema.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 Kinema {
|
||||
|
||||
// Used for keyboard input
|
||||
private final Scanner kbScanner;
|
||||
|
||||
private enum GAME_STATE {
|
||||
STARTUP,
|
||||
INIT,
|
||||
HOW_HIGH,
|
||||
SECONDS_TILL_IT_RETURNS,
|
||||
ITS_VELOCITY,
|
||||
RESULTS,
|
||||
GAME_OVER
|
||||
}
|
||||
|
||||
// Current game state
|
||||
private GAME_STATE gameState;
|
||||
|
||||
private int numberAnswersCorrect;
|
||||
|
||||
// How many meters per second a ball is thrown
|
||||
private int velocity;
|
||||
|
||||
public Kinema() {
|
||||
kbScanner = new Scanner(System.in);
|
||||
|
||||
gameState = GAME_STATE.STARTUP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main game loop
|
||||
*/
|
||||
public void play() {
|
||||
|
||||
double playerAnswer;
|
||||
double correctAnswer;
|
||||
do {
|
||||
switch (gameState) {
|
||||
|
||||
case STARTUP:
|
||||
intro();
|
||||
gameState = GAME_STATE.INIT;
|
||||
break;
|
||||
|
||||
case INIT:
|
||||
numberAnswersCorrect = 0;
|
||||
|
||||
// calculate a random velocity for the player to use in the calculations
|
||||
velocity = 5 + (int) (35 * Math.random());
|
||||
System.out.println("A BALL IS THROWN UPWARDS AT " + velocity + " METERS PER SECOND.");
|
||||
gameState = GAME_STATE.HOW_HIGH;
|
||||
break;
|
||||
|
||||
case HOW_HIGH:
|
||||
|
||||
playerAnswer = displayTextAndGetNumber("HOW HIGH WILL IT GO (IN METERS)? ");
|
||||
|
||||
// Calculate the correct answer to how high it will go
|
||||
correctAnswer = 0.05 * Math.pow(velocity, 2);
|
||||
if (calculate(playerAnswer, correctAnswer)) {
|
||||
numberAnswersCorrect++;
|
||||
}
|
||||
gameState = GAME_STATE.ITS_VELOCITY;
|
||||
break;
|
||||
|
||||
case ITS_VELOCITY:
|
||||
|
||||
playerAnswer = displayTextAndGetNumber("HOW LONG UNTIL IT RETURNS (IN SECONDS)? ");
|
||||
|
||||
// Calculate current Answer for how long until it returns to the ground in seconds
|
||||
correctAnswer = (double) velocity / 5;
|
||||
if (calculate(playerAnswer, correctAnswer)) {
|
||||
numberAnswersCorrect++;
|
||||
}
|
||||
gameState = GAME_STATE.SECONDS_TILL_IT_RETURNS;
|
||||
break;
|
||||
|
||||
case SECONDS_TILL_IT_RETURNS:
|
||||
|
||||
// Calculate random number of seconds for 3rd question
|
||||
double seconds = 1 + (Math.random() * (2 * velocity)) / 10;
|
||||
|
||||
// Round to one decimal place.
|
||||
double scale = Math.pow(10, 1);
|
||||
seconds = Math.round(seconds * scale) / scale;
|
||||
|
||||
playerAnswer = displayTextAndGetNumber("WHAT WILL ITS VELOCITY BE AFTER " + seconds + " SECONDS? ");
|
||||
|
||||
// Calculate the velocity after the given number of seconds
|
||||
correctAnswer = velocity - (10 * seconds);
|
||||
if (calculate(playerAnswer, correctAnswer)) {
|
||||
numberAnswersCorrect++;
|
||||
}
|
||||
gameState = GAME_STATE.RESULTS;
|
||||
break;
|
||||
|
||||
case RESULTS:
|
||||
System.out.println(numberAnswersCorrect + " RIGHT OUT OF 3");
|
||||
if (numberAnswersCorrect > 1) {
|
||||
System.out.println(" NOT BAD.");
|
||||
}
|
||||
gameState = GAME_STATE.STARTUP;
|
||||
break;
|
||||
}
|
||||
} while (gameState != GAME_STATE.GAME_OVER);
|
||||
}
|
||||
|
||||
private void intro() {
|
||||
System.out.println(simulateTabs(33) + "KINEMA");
|
||||
System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private boolean calculate(double playerAnswer, double correctAnswer) {
|
||||
|
||||
boolean gotItRight = false;
|
||||
|
||||
if (Math.abs((playerAnswer - correctAnswer) / correctAnswer) < 0.15) {
|
||||
System.out.println("CLOSE ENOUGH");
|
||||
gotItRight = true;
|
||||
} else {
|
||||
System.out.println("NOT EVEN CLOSE");
|
||||
}
|
||||
System.out.println("CORRECT ANSWER IS " + correctAnswer);
|
||||
System.out.println();
|
||||
|
||||
return gotItRight;
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a message on the screen, then accept input from Keyboard.
|
||||
* Converts input to a Double
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
}
|
||||
7
52 Kinema/java/src/KinemaGame.java
Normal file
7
52 Kinema/java/src/KinemaGame.java
Normal file
@@ -0,0 +1,7 @@
|
||||
public class KinemaGame {
|
||||
public static void main(String[] args) {
|
||||
|
||||
Kinema kinema = new Kinema();
|
||||
kinema.play();
|
||||
}
|
||||
}
|
||||
142
54 Letter/java/src/Letter.java
Normal file
142
54 Letter/java/src/Letter.java
Normal file
@@ -0,0 +1,142 @@
|
||||
import java.awt.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Letter
|
||||
* <p>
|
||||
* Based on the Basic game of Letter here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/54%20Letter/letter.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 Letter {
|
||||
|
||||
public static final int OPTIMAL_GUESSES = 5;
|
||||
public static final int ASCII_A = 65;
|
||||
public static final int ALL_LETTERS = 26;
|
||||
|
||||
private enum GAME_STATE {
|
||||
STARTUP,
|
||||
INIT,
|
||||
GUESSING,
|
||||
RESULTS,
|
||||
GAME_OVER
|
||||
}
|
||||
|
||||
// Used for keyboard input
|
||||
private final Scanner kbScanner;
|
||||
|
||||
// Current game state
|
||||
private GAME_STATE gameState;
|
||||
|
||||
// Players guess count;
|
||||
private int playerGuesses;
|
||||
|
||||
// Computers ascii code for a random letter between A..Z
|
||||
private int computersLetter;
|
||||
|
||||
public Letter() {
|
||||
|
||||
gameState = GAME_STATE.STARTUP;
|
||||
|
||||
// Initialise kb scanner
|
||||
kbScanner = new Scanner(System.in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main game loop
|
||||
*/
|
||||
public void play() {
|
||||
|
||||
do {
|
||||
switch (gameState) {
|
||||
|
||||
// Show an introduction the first time the game is played.
|
||||
case STARTUP:
|
||||
intro();
|
||||
gameState = GAME_STATE.INIT;
|
||||
break;
|
||||
|
||||
case INIT:
|
||||
playerGuesses = 0;
|
||||
computersLetter = ASCII_A + (int) (Math.random() * ALL_LETTERS);
|
||||
System.out.println("O.K., I HAVE A LETTER. START GUESSING.");
|
||||
gameState = GAME_STATE.GUESSING;
|
||||
break;
|
||||
|
||||
// Player guesses the number until they get it or run out of guesses
|
||||
case GUESSING:
|
||||
String playerGuess = displayTextAndGetInput("WHAT IS YOUR GUESS? ").toUpperCase();
|
||||
|
||||
// Convert first character of input string to ascii
|
||||
int toAscii = playerGuess.charAt(0);
|
||||
playerGuesses++;
|
||||
if (toAscii == computersLetter) {
|
||||
gameState = GAME_STATE.RESULTS;
|
||||
break;
|
||||
}
|
||||
|
||||
if (toAscii > computersLetter) {
|
||||
System.out.println("TOO HIGH. TRY A LOWER LETTER.");
|
||||
} else {
|
||||
System.out.println("TOO LOW. TRY A HIGHER LETTER.");
|
||||
}
|
||||
break;
|
||||
|
||||
// Play again, or exit game?
|
||||
case RESULTS:
|
||||
System.out.println();
|
||||
System.out.println("YOU GOT IT IN " + playerGuesses + " GUESSES!!");
|
||||
if (playerGuesses <= OPTIMAL_GUESSES) {
|
||||
System.out.println("GOOD JOB !!!!!");
|
||||
// Original game beeped 15 tims if you guessed in the optimal guesses or less
|
||||
// Changed this to do a single beep only
|
||||
Toolkit.getDefaultToolkit().beep();
|
||||
} else {
|
||||
// Took more than optimal number of guesses
|
||||
System.out.println("BUT IT SHOULDN'T TAKE MORE THAN " + OPTIMAL_GUESSES + " GUESSES!");
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("LET'S PLAN AGAIN.....");
|
||||
gameState = GAME_STATE.INIT;
|
||||
break;
|
||||
}
|
||||
} while (gameState != GAME_STATE.GAME_OVER);
|
||||
}
|
||||
|
||||
public void intro() {
|
||||
System.out.println(simulateTabs(33) + "LETTER");
|
||||
System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println("LETTER GUESSING GAME");
|
||||
System.out.println();
|
||||
System.out.println("I'LL THINK OF A LETTER OF THE ALPHABET, A TO Z.");
|
||||
System.out.println("TRY TO GUESS MY LETTER AND I'LL GIVE YOU CLUES");
|
||||
System.out.println("AS TO HOW CLOSE YOU'RE GETTING TO MY LETTER.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
8
54 Letter/java/src/LetterGame.java
Normal file
8
54 Letter/java/src/LetterGame.java
Normal file
@@ -0,0 +1,8 @@
|
||||
public class LetterGame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Letter letter = new Letter();
|
||||
letter.play();
|
||||
}
|
||||
}
|
||||
176
57 Literature Quiz/java/src/LiteratureQuiz.java
Normal file
176
57 Literature Quiz/java/src/LiteratureQuiz.java
Normal file
@@ -0,0 +1,176 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Literature Quiz
|
||||
* <p>
|
||||
* Based on the Basic game of Literature Quiz here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/57%20Literature%20Quiz/litquiz.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 LiteratureQuiz {
|
||||
|
||||
// Used for keyboard input
|
||||
private final Scanner kbScanner;
|
||||
|
||||
private enum GAME_STATE {
|
||||
STARTUP,
|
||||
QUESTIONS,
|
||||
RESULTS,
|
||||
GAME_OVER
|
||||
}
|
||||
|
||||
// Current game state
|
||||
private GAME_STATE gameState;
|
||||
// Players correct answers
|
||||
private int correctAnswers;
|
||||
|
||||
public LiteratureQuiz() {
|
||||
|
||||
gameState = GAME_STATE.STARTUP;
|
||||
|
||||
// Initialise kb scanner
|
||||
kbScanner = new Scanner(System.in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main game loop
|
||||
*/
|
||||
public void play() {
|
||||
|
||||
do {
|
||||
switch (gameState) {
|
||||
|
||||
// Show an introduction the first time the game is played.
|
||||
case STARTUP:
|
||||
intro();
|
||||
correctAnswers = 0;
|
||||
gameState = GAME_STATE.QUESTIONS;
|
||||
break;
|
||||
|
||||
// Ask the player four questions
|
||||
case QUESTIONS:
|
||||
|
||||
// Question 1
|
||||
System.out.println("IN PINOCCHIO, WHAT WAS THE NAME OF THE CAT");
|
||||
int question1Answer = displayTextAndGetNumber("1)TIGGER, 2)CICERO, 3)FIGARO, 4)GUIPETTO ? ");
|
||||
if (question1Answer == 3) {
|
||||
System.out.println("VERY GOOD! HERE'S ANOTHER.");
|
||||
correctAnswers++;
|
||||
} else {
|
||||
System.out.println("SORRY...FIGARO WAS HIS NAME.");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Question 2
|
||||
System.out.println("FROM WHOSE GARDEN DID BUGS BUNNY STEAL THE CARROTS?");
|
||||
int question2Answer = displayTextAndGetNumber("1)MR. NIXON'S, 2)ELMER FUDD'S, 3)CLEM JUDD'S, 4)STROMBOLI'S ? ");
|
||||
if (question2Answer == 2) {
|
||||
System.out.println("PRETTY GOOD!");
|
||||
correctAnswers++;
|
||||
} else {
|
||||
System.out.println("TOO BAD...IT WAS ELMER FUDD'S GARDEN.");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Question 3
|
||||
System.out.println("IN THE WIZARD OF OS, DOROTHY'S DOG WAS NAMED");
|
||||
int question3Answer = displayTextAndGetNumber("1)CICERO, 2)TRIXIA, 3)KING, 4)TOTO ? ");
|
||||
if (question3Answer == 4) {
|
||||
System.out.println("YEA! YOU'RE A REAL LITERATURE GIANT.");
|
||||
correctAnswers++;
|
||||
} else {
|
||||
System.out.println("BACK TO THE BOOKS,...TOTO WAS HIS NAME.");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
// Question 4
|
||||
System.out.println("WHO WAS THE FAIR MAIDEN WHO ATE THE POISON APPLE");
|
||||
int question4Answer = displayTextAndGetNumber("1)SLEEPING BEAUTY, 2)CINDERELLA, 3)SNOW WHITE, 4)WENDY ? ");
|
||||
if (question4Answer == 3) {
|
||||
System.out.println("GOOD MEMORY!");
|
||||
correctAnswers++;
|
||||
} else {
|
||||
System.out.println("OH, COME ON NOW...IT WAS SNOW WHITE.");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
gameState = GAME_STATE.RESULTS;
|
||||
break;
|
||||
|
||||
// How did the player do?
|
||||
case RESULTS:
|
||||
if (correctAnswers == 4) {
|
||||
// All correct
|
||||
System.out.println("WOW! THAT'S SUPER! YOU REALLY KNOW YOUR NURSERY");
|
||||
System.out.println("YOUR NEXT QUIZ WILL BE ON 2ND CENTURY CHINESE");
|
||||
System.out.println("LITERATURE (HA, HA, HA)");
|
||||
// one or none correct
|
||||
} else if (correctAnswers < 2) {
|
||||
System.out.println("UGH. THAT WAS DEFINITELY NOT TOO SWIFT. BACK TO");
|
||||
System.out.println("NURSERY SCHOOL FOR YOU, MY FRIEND.");
|
||||
// two or three correct
|
||||
} else {
|
||||
System.out.println("NOT BAD, BUT YOU MIGHT SPEND A LITTLE MORE TIME");
|
||||
System.out.println("READING THE NURSERY GREATS.");
|
||||
}
|
||||
gameState = GAME_STATE.GAME_OVER;
|
||||
break;
|
||||
}
|
||||
} while (gameState != GAME_STATE.GAME_OVER);
|
||||
}
|
||||
|
||||
public void intro() {
|
||||
System.out.println(simulateTabs(25) + "LITERATURE QUIZ");
|
||||
System.out.println(simulateTabs(15) + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println("LITERATURE QUIZ");
|
||||
System.out.println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println("TEST YOUR KNOWLEDGE OF CHILDREN'S LITERATURE.");
|
||||
System.out.println("THIS IS A MULTIPLE-CHOICE QUIZ.");
|
||||
System.out.println("TYPE A 1, 2, 3, OR 4 AFTER THE QUESTION MARK.");
|
||||
System.out.println();
|
||||
System.out.println("GOOD LUCK!");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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();
|
||||
}
|
||||
}
|
||||
8
57 Literature Quiz/java/src/LiteratureQuizGame.java
Normal file
8
57 Literature Quiz/java/src/LiteratureQuizGame.java
Normal file
@@ -0,0 +1,8 @@
|
||||
public class LiteratureQuizGame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
LiteratureQuiz literatureQuiz = new LiteratureQuiz();
|
||||
literatureQuiz.play();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user