This commit is contained in:
Tim
2021-02-22 09:37:34 +10:30
parent 809798e54e
commit 87625f0e8e
10 changed files with 109 additions and 111 deletions

View File

@@ -2,12 +2,11 @@ import java.util.Scanner;
/**
* Game of AceyDucey
*
* <p>
* Based on the Basic game of AceyDucey here
* https://github.com/coding-horror/basic-computer-games/blob/main/01%20Acey%20Ducey/aceyducey.bas
* Note: The idea was to create a version of 1970's Basic game in Java, without introducing
* new features - no additional text, error checking, etc has been added.
*
* 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 AceyDucey {
@@ -33,7 +32,7 @@ public class AceyDucey {
private final Scanner kbScanner;
// Constant value for cards from a deck - 2 lowest, 14 (Ace) highest
public static final int LOW_CARD_RANGE =2;
public static final int LOW_CARD_RANGE = 2;
public static final int HIGH_CARD_RANGE = 14;
public AceyDucey() {
@@ -56,7 +55,7 @@ public class AceyDucey {
String playAgain = kbScanner.next().toUpperCase();
System.out.println();
System.out.println();
if(playAgain.equals("YES")) {
if (playAgain.equals("YES")) {
return true;
} else {
System.out.println("O.K., HOPE YOU HAD FUN!");
@@ -70,7 +69,7 @@ public class AceyDucey {
// Keep playing hands until player runs out of cash
do {
if(firstTimePlaying) {
if (firstTimePlaying) {
intro();
firstTimePlaying = false;
}
@@ -80,14 +79,14 @@ public class AceyDucey {
int betAmount = getBet();
playersCard = randomCard();
displayPlayerCard();
if(playerWon()) {
if (playerWon()) {
System.out.println("YOU WIN!!");
playerAmount += betAmount;
} else {
System.out.println("SORRY, YOU LOSE");
playerAmount -= betAmount;
// Player run out of money?
if(playerAmount <=0) {
if (playerAmount <= 0) {
gameOver = true;
}
}
@@ -118,10 +117,10 @@ public class AceyDucey {
do {
System.out.print("WHAT IS YOUR BET ");
amount = kbScanner.nextInt();
if(amount == 0) {
if (amount == 0) {
System.out.println("CHICKEN!!");
validBet = true;
} else if(amount > playerAmount) {
} else if (amount > playerAmount) {
System.out.println("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.");
System.out.println("YOU HAVE ONLY " + playerAmount + " DOLLARS TO BET.");
} else {
@@ -149,7 +148,7 @@ public class AceyDucey {
do {
firstCard = randomCard();
secondCard = randomCard();
} while(firstCard.getValue() >= secondCard.getValue());
} while (firstCard.getValue() >= secondCard.getValue());
}
// Creates a random card

View File

@@ -12,7 +12,7 @@ public class Card {
private void init(int value) {
this.value = value;
if(value <11) {
if (value < 11) {
this.name = String.valueOf(value);
} else {
switch (value) {

View File

@@ -4,7 +4,8 @@
*/
public class Player {
private String name;
private final String name;
private int score;
Player(String name) {

View File

@@ -10,9 +10,7 @@ public class Shot {
// Array of doubles are passed for a specific type of shot
Shot(double[] shots) {
chances = new double[shots.length];
for(int i=0; i<shots.length; i++) {
chances[i] = shots[i];
}
System.arraycopy(shots, 0, chances, 0, shots.length);
}
public double getShot(int index) {

View File

@@ -7,7 +7,7 @@ import java.util.Scanner;
* Based on the Basic game of Hurkle here
* https://github.com/coding-horror/basic-computer-games/blob/main/25%20Chief/chief.bas
* <p>
* Note: The idea was to create a version of 1970's Basic game in Java, without introducing
* 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 Chief {
@@ -67,17 +67,17 @@ public class Chief {
displayTextAndGetInput(" WHAT DO YOU HAVE? "));
// Exact same formula used in the original game to calculate the players original number
this.calculatedNumber = (playerNumber + 1 - 5) * 5 / 8 * 5 - 3;
calculatedNumber = (playerNumber + 1 - 5) * 5 / 8 * 5 - 3;
this.gameState = GAME_STATE.CALCULATE_AND_SHOW;
gameState = GAME_STATE.CALCULATE_AND_SHOW;
break;
// Enter the number to be used to calculate
case CALCULATE_AND_SHOW:
if (yesEntered(
displayTextAndGetInput("I BET YOUR NUMBER WAS " + this.calculatedNumber
displayTextAndGetInput("I BET YOUR NUMBER WAS " + calculatedNumber
+ ". AM I RIGHT? "))) {
this.gameState = GAME_STATE.END_GAME;
gameState = GAME_STATE.END_GAME;
} else {
// Player did not agree, so show the breakdown
@@ -90,11 +90,11 @@ public class Chief {
double j = i - 1;
System.out.println("SO YOU THINK YOU'RE SO SMART, EH?");
System.out.println("NOW WATCH.");
System.out.println(number + " PLUS 3 EQUALS " + f + ". THIS DIVIDED BY 5 EQUALS " + g);
System.out.println("THIS TIMES 8 EQUALS " + h + ". IF WE DIVIDE BY 5 AND ADD 5,");
System.out.println(number + " PLUS 3 EQUALS " + f + ". DIVIDED BY 5 EQUALS " + g);
System.out.println("TIMES 8 EQUALS " + h + ". IF WE DIVIDE BY 5 AND ADD 5,");
System.out.println("WE GET " + i + ", WHICH, MINUS 1, EQUALS " + j + ".");
if (yesEntered(displayTextAndGetInput("NOW DO YOU BELIEVE ME? "))) {
this.gameState = GAME_STATE.END_GAME;
gameState = GAME_STATE.END_GAME;
} else {
// Time for a lightning bolt.
System.out.println("YOU HAVE MADE ME MAD!!!");
@@ -116,7 +116,7 @@ public class Chief {
System.out.println("#########################");
System.out.println();
System.out.println("I HOPE YOU BELIEVE ME NOW, FOR YOUR SAKE!!");
this.gameState = GAME_STATE.GAME_OVER;
gameState = GAME_STATE.GAME_OVER;
}
}
@@ -125,7 +125,7 @@ public class Chief {
// Sign off message for cases where the Chief is not upset
case END_GAME:
System.out.println("BYE!!!");
this.gameState = GAME_STATE.GAME_OVER;
gameState = GAME_STATE.GAME_OVER;
break;
// GAME_OVER State does not specifically have a case
@@ -146,7 +146,7 @@ public class Chief {
}
private void instructions() {
System.out.println(" TAKE A NUMBER AND ADD 3. DIVIDE THIS NUMBER BY 5 AND");
System.out.println(" TAKE A NUMBER AND ADD 3. DIVIDE NUMBER BY 5 AND");
System.out.println("MULTIPLY BY 8. DIVIDE BY 5 AND ADD THE SAME. SUBTRACT 1.");
}

View File

@@ -6,7 +6,7 @@ import java.util.Scanner;
* Based on the Basic game of Hi-Lo here
* https://github.com/coding-horror/basic-computer-games/blob/main/47%20Hi-Lo/hi-lo.bas
*
* Note: The idea was to create a version of 1970's Basic game in Java, without introducing
* 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 HiLo {

View File

@@ -6,7 +6,7 @@ import java.util.Scanner;
* Based on the Basic game of Hurkle here
* https://github.com/coding-horror/basic-computer-games/blob/main/51%20Hurkle/hurkle.bas
* <p>
* Note: The idea was to create a version of 1970's Basic game in Java, without introducing
* 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 Hurkle {
@@ -62,32 +62,32 @@ public class Hurkle {
// Start the game, set the number of players, names and round
case START_GAME:
this.hurkleXPos = randomNumber();
this.hurkleYPos = randomNumber();
System.out.println("HURKLE AT : " + this.hurkleXPos + "," + this.hurkleYPos);
hurkleXPos = randomNumber();
hurkleYPos = randomNumber();
System.out.println("HURKLE AT : " + hurkleXPos + "," + hurkleYPos);
this.guesses = 1;
guesses = 1;
gameState = GAME_STATE.GUESSING;
break;
// Guess an x,y position of the hurkle
case GUESSING:
String guess = displayTextAndGetInput("GUESS #" + this.guesses + "? ");
this.playerGuessXPos = getDelimitedValue(guess, 0);
this.playerGuessYPos = getDelimitedValue(guess, 1);
String guess = displayTextAndGetInput("GUESS #" + guesses + "? ");
playerGuessXPos = getDelimitedValue(guess, 0);
playerGuessYPos = getDelimitedValue(guess, 1);
if (foundHurkle()) {
this.gameState = GAME_STATE.PLAY_AGAIN;
gameState = GAME_STATE.PLAY_AGAIN;
} else {
showDirectionOfHurkle();
this.guesses++;
if (this.guesses > MAX_GUESSES) {
guesses++;
if (guesses > MAX_GUESSES) {
System.out.println("SORRY, THAT'S "
+ MAX_GUESSES + " GUESSES.");
System.out.println("THE HURKLE IS AT "
+ this.hurkleXPos + "," + this.hurkleYPos);
+ hurkleXPos + "," + hurkleYPos);
System.out.println();
this.gameState = GAME_STATE.PLAY_AGAIN;
gameState = GAME_STATE.PLAY_AGAIN;
}
}
@@ -96,7 +96,7 @@ public class Hurkle {
case PLAY_AGAIN:
System.out.println("LET'S PLAY AGAIN, HURKLE IS HIDING.");
System.out.println();
this.gameState = GAME_STATE.START_GAME;
gameState = GAME_STATE.START_GAME;
break;
}
// Effectively an endless loop because the game never quits as per
@@ -106,30 +106,30 @@ public class Hurkle {
private void showDirectionOfHurkle() {
System.out.print("GO ");
if (this.playerGuessYPos == this.hurkleYPos) {
if (playerGuessYPos == hurkleYPos) {
// don't print North or South because the player has chosen the
// same y grid pos as the hurkle
} else if (this.playerGuessYPos < this.hurkleYPos) {
} else if (playerGuessYPos < hurkleYPos) {
System.out.print("NORTH");
} else if (this.playerGuessYPos > this.hurkleYPos) {
} else if (playerGuessYPos > hurkleYPos) {
System.out.print("SOUTH");
}
if (this.playerGuessXPos == this.hurkleXPos) {
if (playerGuessXPos == hurkleXPos) {
// don't print East or West because the player has chosen the
// same x grid pos as the hurkle
} else if (this.playerGuessXPos < this.hurkleXPos) {
} else if (playerGuessXPos < hurkleXPos) {
System.out.print("EAST");
} else if (this.playerGuessXPos > this.hurkleXPos) {
} else if (playerGuessXPos > hurkleXPos) {
System.out.print("WEST");
}
System.out.println();
}
private boolean foundHurkle() {
if ((this.playerGuessXPos - this.hurkleXPos)
- (this.playerGuessYPos - this.hurkleYPos) == 0) {
System.out.println("YOU FOUND HIM IN " + this.guesses + " GUESSES.");
if ((playerGuessXPos - hurkleXPos)
- (playerGuessYPos - hurkleYPos) == 0) {
System.out.println("YOU FOUND HIM IN " + guesses + " GUESSES.");
return true;
}

View File

@@ -6,7 +6,7 @@ import java.util.Scanner;
* Based on the Basic game of Hurkle here
* https://github.com/coding-horror/basic-computer-games/blob/main/69%20Pizza/pizza.bas
* <p>
* Note: The idea was to create a version of 1970's Basic game in Java, without introducing
* 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 Pizza {
@@ -47,7 +47,7 @@ public class Pizza {
public Pizza() {
this.gameState = GAME_STATE.STARTING;
gameState = GAME_STATE.STARTING;
// Initialise kb scanner
kbScanner = new Scanner(System.in);
@@ -59,29 +59,29 @@ public class Pizza {
public void play() {
do {
switch (this.gameState) {
switch (gameState) {
// Show an introduction the first time the game is played.
case STARTING:
init();
intro();
this.gameState = GAME_STATE.ENTER_NAME;
gameState = GAME_STATE.ENTER_NAME;
break;
// Enter the players name
case ENTER_NAME:
this.playerName = displayTextAndGetInput("WHAT IS YOUR FIRST NAME? ");
System.out.println("HI " + this.playerName + ". IN THIS GAME YOU ARE TO TAKE ORDERS");
playerName = displayTextAndGetInput("WHAT IS YOUR FIRST NAME? ");
System.out.println("HI " + playerName + ". IN GAME YOU ARE TO TAKE ORDERS");
System.out.println("FOR PIZZAS. THEN YOU ARE TO TELL A DELIVERY BOY");
System.out.println("WHERE TO DELIVER THE ORDERED PIZZAS.");
System.out.println();
this.gameState = GAME_STATE.DRAW_MAP;
gameState = GAME_STATE.DRAW_MAP;
break;
// Draw the map
case DRAW_MAP:
drawMap();
this.gameState = GAME_STATE.MORE_DIRECTIONS;
gameState = GAME_STATE.MORE_DIRECTIONS;
break;
// need more directions (how to play) ?
@@ -100,14 +100,14 @@ public class Pizza {
System.out.println();
System.out.println("GOOD LUCK!!");
System.out.println();
this.gameState = GAME_STATE.START_DELIVER;
gameState = GAME_STATE.START_DELIVER;
} else {
// Not understood, essentially game over
this.gameState = GAME_STATE.TOO_DIFFICULT;
gameState = GAME_STATE.TOO_DIFFICULT;
}
} else {
// no more directions were needed, start delivering pizza
this.gameState = GAME_STATE.START_DELIVER;
gameState = GAME_STATE.START_DELIVER;
}
}
@@ -115,27 +115,27 @@ public class Pizza {
// Too difficult to understand, game over!
case TOO_DIFFICULT:
System.out.println("THIS JOB IS DEFINITELY TOO DIFFICULT FOR YOU. THANKS ANYWAY");
this.gameState = GAME_STATE.GAME_OVER;
System.out.println("JOB IS DEFINITELY TOO DIFFICULT FOR YOU. THANKS ANYWAY");
gameState = GAME_STATE.GAME_OVER;
break;
// Delivering pizza
case START_DELIVER:
// select a random house and "order" a pizza for them.
this.currentHouseDelivery = (int) (Math.random()
* (this.houses.length) + 1) - 1; // Deduct 1 for 0-based array
currentHouseDelivery = (int) (Math.random()
* (houses.length) + 1) - 1; // Deduct 1 for 0-based array
System.out.println("HELLO " + this.playerName + "'S PIZZA. THIS IS "
+ this.houses[this.currentHouseDelivery] + ".");
System.out.println("HELLO " + playerName + "'S PIZZA. IS "
+ houses[currentHouseDelivery] + ".");
System.out.println(" PLEASE SEND A PIZZA.");
this.gameState = GAME_STATE.DELIVER_PIZZA;
gameState = GAME_STATE.DELIVER_PIZZA;
break;
// Try and deliver the pizza
case DELIVER_PIZZA:
String question = " DRIVER TO " + this.playerName + ": WHERE DOES "
+ this.houses[this.currentHouseDelivery] + " LIVE ? ";
String question = " DRIVER TO " + playerName + ": WHERE DOES "
+ houses[currentHouseDelivery] + " LIVE ? ";
String answer = displayTextAndGetInput(question);
// Convert x,y entered by player to grid position of a house
@@ -144,22 +144,22 @@ public class Pizza {
int calculatedPos = (x + (y - 1) * 4) - 1;
// Did the player select the right house to deliver?
if (calculatedPos == this.currentHouseDelivery) {
System.out.println("HELLO " + this.playerName + ". THIS IS " + this.houses[this.currentHouseDelivery]
if (calculatedPos == currentHouseDelivery) {
System.out.println("HELLO " + playerName + ". IS " + houses[currentHouseDelivery]
+ ", THANKS FOR THE PIZZA.");
this.pizzaDeliveryCount++;
pizzaDeliveryCount++;
// Delivered enough pizza?
if (this.pizzaDeliveryCount > MAX_DELIVERIES) {
this.gameState = GAME_STATE.END_GAME;
if (pizzaDeliveryCount > MAX_DELIVERIES) {
gameState = GAME_STATE.END_GAME;
} else {
this.gameState = GAME_STATE.START_DELIVER;
gameState = GAME_STATE.START_DELIVER;
}
} else {
System.out.println("THIS IS " + houses[calculatedPos] + ". I DID NOT ORDER A PIZZA.");
System.out.println("IS " + houses[calculatedPos] + ". I DID NOT ORDER A PIZZA.");
System.out.println("I LIVE AT " + x + "," + y);
// Don't change gameState so this state is executed again
// Don't change gameState so state is executed again
}
break;
@@ -168,18 +168,18 @@ public class Pizza {
case END_GAME:
if (yesEntered(displayTextAndGetInput("DO YOU WANT TO DELIVER MORE PIZZAS? "))) {
init();
this.gameState = GAME_STATE.START_DELIVER;
gameState = GAME_STATE.START_DELIVER;
} else {
System.out.println();
System.out.println("O.K. " + this.playerName + ", SEE YOU LATER!");
System.out.println("O.K. " + playerName + ", SEE YOU LATER!");
System.out.println();
this.gameState = GAME_STATE.GAME_OVER;
gameState = GAME_STATE.GAME_OVER;
}
break;
// GAME_OVER State does not specifically have a case
}
} while (this.gameState != GAME_STATE.GAME_OVER);
} while (gameState != GAME_STATE.GAME_OVER);
}
private void drawMap() {
@@ -194,13 +194,13 @@ public class Pizza {
System.out.println("-");
System.out.println("-");
System.out.print(this.gridPos[k]);
System.out.print(gridPos[k]);
int pos = 16 - 4 * i;
System.out.print(" " + this.houses[pos]);
System.out.print(" " + this.houses[pos + 1]);
System.out.print(" " + this.houses[pos + 2]);
System.out.print(" " + this.houses[pos + 3]);
System.out.println(" " + this.gridPos[k]);
System.out.print(" " + houses[pos]);
System.out.print(" " + houses[pos + 1]);
System.out.print(" " + houses[pos + 2]);
System.out.print(" " + houses[pos + 3]);
System.out.println(" " + gridPos[k]);
k = k - 1;
}
System.out.println("-");
@@ -238,14 +238,14 @@ public class Pizza {
System.out.println("DELIVERED. THEN A DELIVERY BOY WILL");
System.out.println("ASK YOU FOR THE LOCATION.");
System.out.println(" EXAMPLE:");
System.out.println("THIS IS J. PLEASE SEND A PIZZA.");
System.out.println("DRIVER TO " + this.playerName + ". WHERE DOES J LIVE?");
System.out.println("IS J. PLEASE SEND A PIZZA.");
System.out.println("DRIVER TO " + playerName + ". WHERE DOES J LIVE?");
System.out.println("YOUR ANSWER WOULD BE 2,3");
System.out.println();
}
private void init() {
this.pizzaDeliveryCount = 1;
pizzaDeliveryCount = 1;
}
/**

View File

@@ -6,7 +6,7 @@ import java.util.Arrays;
* Based on the Sine Wave program here
* https://github.com/coding-horror/basic-computer-games/blob/main/78%20Sine%20Wave/sinewave.bas
*
* Note: The idea was to create a version of this 1970's Basic program in Java, without introducing
* Note: The idea was to create a version of the 1970's Basic program in Java, without introducing
* new features - no additional text, error checking, etc has been added.
*/
public class SineWave {

View File

@@ -7,7 +7,7 @@ import java.util.Scanner;
* Based on the Basic game of Stars here
* https://github.com/coding-horror/basic-computer-games/blob/main/82%20Stars/stars.bas
*
* Note: The idea was to create a version of this 1970's Basic game in Java, without introducing
* 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 Stars {
@@ -42,7 +42,7 @@ public class Stars {
public Stars() {
this.gameState = GAME_STATE.STARTING;
gameState = GAME_STATE.STARTING;
// Initialise kb scanner
kbScanner = new Scanner(System.in);
@@ -68,30 +68,30 @@ public class Stars {
if(yesEntered(displayTextAndGetInput("DO YOU WANT INSTRUCTIONS? "))) {
instructions();
}
this.gameState = GAME_STATE.START_GAME;
gameState = GAME_STATE.START_GAME;
break;
// Generate computers number for player to guess, etc.
case START_GAME:
init();
System.out.println("OK, I AM THINKING OF A NUMBER, START GUESSING.");
this.gameState = GAME_STATE.GUESSING;
gameState = GAME_STATE.GUESSING;
break;
// Player guesses the number until they get it or run out of guesses
case GUESSING:
this.playerCurrentGuess = playerGuess();
playerCurrentGuess = playerGuess();
// Check if the player guessed the number
if(this.playerCurrentGuess == this.computersNumber) {
this.gameState = GAME_STATE.WON;
if(playerCurrentGuess == computersNumber) {
gameState = GAME_STATE.WON;
} else {
// incorrect guess
showStars();
this.playerTotalGuesses++;
playerTotalGuesses++;
// Ran out of guesses?
if (this.playerTotalGuesses > MAX_GUESSES) {
this.gameState = GAME_STATE.LOST;
if (playerTotalGuesses > MAX_GUESSES) {
gameState = GAME_STATE.LOST;
}
}
break;
@@ -100,16 +100,16 @@ public class Stars {
case WON:
System.out.println(stars(79));
System.out.println("YOU GOT IT IN " + this.playerTotalGuesses
System.out.println("YOU GOT IT IN " + playerTotalGuesses
+ " GUESSES!!! LET'S PLAY AGAIN...");
this.gameState = GAME_STATE.START_GAME;
gameState = GAME_STATE.START_GAME;
break;
// Lost game by running out of guesses
case LOST:
System.out.println("SORRY, THAT'S " + MAX_GUESSES
+ " GUESSES. THE NUMBER WAS " + this.computersNumber);
this.gameState = GAME_STATE.START_GAME;
+ " GUESSES. THE NUMBER WAS " + computersNumber);
gameState = GAME_STATE.START_GAME;
break;
}
// Endless loop since the original code did not allow the player to exit
@@ -123,7 +123,7 @@ public class Stars {
*
*/
private void showStars() {
int d = Math.abs(this.playerCurrentGuess - this.computersNumber);
int d = Math.abs(playerCurrentGuess - computersNumber);
int starsToShow;
if(d >=64) {
starsToShow = 1;
@@ -159,8 +159,8 @@ public class Stars {
*
*/
private void init() {
this.playerTotalGuesses = 1;
this.computersNumber = randomNumber();
playerTotalGuesses = 1;
computersNumber = randomNumber();
}
public void instructions() {