mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-02 08:10:45 -08:00
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,4 +0,0 @@
|
||||
/**/obj/
|
||||
/**/.vs/
|
||||
/**/bin/
|
||||
/**/*.user
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
package aceyducey;
|
||||
|
||||
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 this 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 {
|
||||
|
||||
@@ -32,10 +29,10 @@ public class AceyDucey {
|
||||
private boolean gameOver = false;
|
||||
|
||||
// Used for keyboard input
|
||||
private Scanner kbScanner;
|
||||
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() {
|
||||
@@ -58,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!");
|
||||
@@ -71,9 +68,8 @@ public class AceyDucey {
|
||||
public void play() {
|
||||
|
||||
// Keep playing hands until player runs out of cash
|
||||
// which sets gameover to true
|
||||
do {
|
||||
if(firstTimePlaying) {
|
||||
if (firstTimePlaying) {
|
||||
intro();
|
||||
firstTimePlaying = false;
|
||||
}
|
||||
@@ -81,17 +77,17 @@ public class AceyDucey {
|
||||
drawCards();
|
||||
displayCards();
|
||||
int betAmount = getBet();
|
||||
this.playersCard = randomCard();
|
||||
playersCard = randomCard();
|
||||
displayPlayerCard();
|
||||
if(playerWon()) {
|
||||
if (playerWon()) {
|
||||
System.out.println("YOU WIN!!");
|
||||
this.playerAmount += betAmount;
|
||||
playerAmount += betAmount;
|
||||
} else {
|
||||
System.out.println("SORRY, YOU LOSE");
|
||||
this.playerAmount -= betAmount;
|
||||
playerAmount -= betAmount;
|
||||
// Player run out of money?
|
||||
if(this.playerAmount <=0) {
|
||||
this.gameOver = true;
|
||||
if (playerAmount <= 0) {
|
||||
gameOver = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,18 +98,14 @@ public class AceyDucey {
|
||||
// to win a players card has to be in the range of the first and second dealer
|
||||
// drawn cards inclusive of first and second cards.
|
||||
private boolean playerWon() {
|
||||
if((this.playersCard.getValue() >= this.firstCard.getValue())
|
||||
&& this.playersCard.getValue() <= this.secondCard.getValue()) {
|
||||
// winner
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
// winner
|
||||
return (playersCard.getValue() >= firstCard.getValue())
|
||||
&& playersCard.getValue() <= secondCard.getValue();
|
||||
|
||||
}
|
||||
|
||||
private void displayPlayerCard() {
|
||||
System.out.println(this.playersCard.getName());
|
||||
System.out.println(playersCard.getName());
|
||||
}
|
||||
|
||||
// Get the players bet, and return the amount
|
||||
@@ -121,16 +113,16 @@ public class AceyDucey {
|
||||
// method will loop until a valid bet is entered.
|
||||
private int getBet() {
|
||||
boolean validBet = false;
|
||||
int amount = 0;
|
||||
int amount;
|
||||
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 > this.playerAmount) {
|
||||
} else if (amount > playerAmount) {
|
||||
System.out.println("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.");
|
||||
System.out.println("YOU HAVE ONLY " + this.playerAmount + " DOLLARS TO BET.");
|
||||
System.out.println("YOU HAVE ONLY " + playerAmount + " DOLLARS TO BET.");
|
||||
} else {
|
||||
validBet = true;
|
||||
}
|
||||
@@ -140,13 +132,13 @@ public class AceyDucey {
|
||||
}
|
||||
|
||||
private void displayBalance() {
|
||||
System.out.println("YOU NOW HAVE " + this.playerAmount + " DOLLARS.");
|
||||
System.out.println("YOU NOW HAVE " + playerAmount + " DOLLARS.");
|
||||
}
|
||||
|
||||
private void displayCards() {
|
||||
System.out.println("HERE ARE YOUR NEXT TWO CARDS: ");
|
||||
System.out.println(this.firstCard.getName());
|
||||
System.out.println(this.secondCard.getName());
|
||||
System.out.println(firstCard.getName());
|
||||
System.out.println(secondCard.getName());
|
||||
}
|
||||
|
||||
// Draw two dealer cards, and save them for later use.
|
||||
@@ -154,9 +146,9 @@ public class AceyDucey {
|
||||
private void drawCards() {
|
||||
|
||||
do {
|
||||
this.firstCard = randomCard();
|
||||
this.secondCard = randomCard();
|
||||
} while(this.firstCard.getValue() >= this.secondCard.getValue());
|
||||
firstCard = randomCard();
|
||||
secondCard = randomCard();
|
||||
} while (firstCard.getValue() >= secondCard.getValue());
|
||||
}
|
||||
|
||||
// Creates a random card
|
||||
@@ -1,20 +1,16 @@
|
||||
package aceyducey;
|
||||
|
||||
/**
|
||||
* This class is used to invoke the game.
|
||||
*
|
||||
*/
|
||||
public class Game {
|
||||
|
||||
private static AceyDucey game;
|
||||
public class AceyDuceyGame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
boolean keepPlaying = true;
|
||||
boolean keepPlaying;
|
||||
AceyDucey game = new AceyDucey();
|
||||
|
||||
// Keep playing game until infinity or the player loses
|
||||
do {
|
||||
game = new AceyDucey();
|
||||
game.play();
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
@@ -1,5 +1,3 @@
|
||||
package aceyducey;
|
||||
|
||||
/**
|
||||
* A card from a deck - the value is between 2-14 to cover
|
||||
* cards with a face value of 2-9 and then a Jack, Queen, King, and Ace
|
||||
@@ -14,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) {
|
||||
@@ -3,12 +3,12 @@ import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Bombardment
|
||||
*
|
||||
* <p>
|
||||
* Based on the Basic game of Bombardment here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/11%20Bombardment/bombardment.bas
|
||||
*
|
||||
* Note: The idea was to create a version of this 1970's Basic game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
* <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 Bombardment {
|
||||
|
||||
@@ -28,11 +28,11 @@ public class Bombardment {
|
||||
|
||||
private GAME_STATE gameState;
|
||||
|
||||
public static final String[] PLAYER_HIT_MESSAGES = { "ONE DOWN, THREE TO GO.",
|
||||
"TWO DOWN, TWO TO GO.", "THREE DOWN, ONE TO GO." };
|
||||
public static final String[] PLAYER_HIT_MESSAGES = {"ONE DOWN, THREE TO GO.",
|
||||
"TWO DOWN, TWO TO GO.", "THREE DOWN, ONE TO GO."};
|
||||
|
||||
public static final String[] COMPUTER_HIT_MESSAGES = {"YOU HAVE ONLY THREE OUTPOSTS LEFT.",
|
||||
"YOU HAVE ONLY TWO OUTPOSTS LEFT.", "YOU HAVE ONLY ONE OUTPOST LEFT." };
|
||||
"YOU HAVE ONLY TWO OUTPOSTS LEFT.", "YOU HAVE ONLY ONE OUTPOST LEFT."};
|
||||
|
||||
private HashSet<Integer> computersPlatoons;
|
||||
private HashSet<Integer> playersPlatoons;
|
||||
@@ -44,7 +44,7 @@ public class Bombardment {
|
||||
|
||||
public Bombardment() {
|
||||
|
||||
this.gameState = GAME_STATE.STARTING;
|
||||
gameState = GAME_STATE.STARTING;
|
||||
|
||||
// Initialise kb scanner
|
||||
kbScanner = new Scanner(System.in);
|
||||
@@ -56,19 +56,19 @@ public class Bombardment {
|
||||
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.DRAW_BATTLEFIELD;
|
||||
gameState = GAME_STATE.DRAW_BATTLEFIELD;
|
||||
break;
|
||||
|
||||
// Enter the players name
|
||||
case DRAW_BATTLEFIELD:
|
||||
drawBattlefield();
|
||||
this.gameState = GAME_STATE.GET_PLAYER_CHOICES;
|
||||
gameState = GAME_STATE.GET_PLAYER_CHOICES;
|
||||
break;
|
||||
|
||||
// Get the players 4 locations for their platoons
|
||||
@@ -76,35 +76,35 @@ public class Bombardment {
|
||||
String playerChoices = displayTextAndGetInput("WHAT ARE YOUR FOUR POSITIONS? ");
|
||||
|
||||
// Store the 4 player choices that were entered separated with commas
|
||||
for(int i=0; i<PLATOONS; i++) {
|
||||
playersPlatoons.add(getDelimitedValue(playerChoices,i));
|
||||
for (int i = 0; i < PLATOONS; i++) {
|
||||
playersPlatoons.add(getDelimitedValue(playerChoices, i));
|
||||
}
|
||||
|
||||
this.gameState = GAME_STATE.PLAYERS_TURN;
|
||||
gameState = GAME_STATE.PLAYERS_TURN;
|
||||
break;
|
||||
|
||||
// Players turn to pick a location
|
||||
case PLAYERS_TURN:
|
||||
|
||||
int firePosition = getDelimitedValue(
|
||||
displayTextAndGetInput("WHERE DO YOU WISH TO FIRE YOUR MISSILE? "),0);
|
||||
displayTextAndGetInput("WHERE DO YOU WISH TO FIRE YOUR MISSILE? "), 0);
|
||||
|
||||
if(didPlayerHitComputerPlatoon(firePosition)) {
|
||||
if (didPlayerHitComputerPlatoon(firePosition)) {
|
||||
// Player hit a player platoon
|
||||
int hits = updatePlayerHits(firePosition);
|
||||
// How many hits has the player made?
|
||||
if(hits != PLATOONS) {
|
||||
if (hits != PLATOONS) {
|
||||
showPlayerProgress(hits);
|
||||
this.gameState = GAME_STATE.COMPUTER_TURN;
|
||||
gameState = GAME_STATE.COMPUTER_TURN;
|
||||
} else {
|
||||
// Player has obtained 4 hits, they win
|
||||
this.gameState = GAME_STATE.PLAYER_WON;
|
||||
gameState = GAME_STATE.PLAYER_WON;
|
||||
}
|
||||
} else {
|
||||
// Player missed
|
||||
System.out.println("HA, HA YOU MISSED. MY TURN NOW:");
|
||||
System.out.println();
|
||||
this.gameState = GAME_STATE.COMPUTER_TURN;
|
||||
gameState = GAME_STATE.COMPUTER_TURN;
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -114,25 +114,25 @@ public class Bombardment {
|
||||
|
||||
// Computer takes a guess of a location
|
||||
int computerFirePosition = uniqueComputerGuess();
|
||||
if(didComputerHitPlayerPlatoon(computerFirePosition)) {
|
||||
if (didComputerHitPlayerPlatoon(computerFirePosition)) {
|
||||
// Computer hit a player platoon
|
||||
int hits = updateComputerHits(computerFirePosition);
|
||||
// How many hits has the computer made?
|
||||
if(hits != PLATOONS) {
|
||||
if (hits != PLATOONS) {
|
||||
showComputerProgress(hits, computerFirePosition);
|
||||
this.gameState = GAME_STATE.PLAYERS_TURN;
|
||||
gameState = GAME_STATE.PLAYERS_TURN;
|
||||
} else {
|
||||
// Computer has obtained 4 hits, they win
|
||||
System.out.println("YOU'RE DEAD. YOUR LAST OUTPOST WAS AT " + computerFirePosition
|
||||
+ ". HA, HA, HA.");
|
||||
this.gameState = GAME_STATE.PLAYER_LOST;
|
||||
gameState = GAME_STATE.PLAYER_LOST;
|
||||
}
|
||||
} else {
|
||||
// Computer missed
|
||||
System.out.println("I MISSED YOU, YOU DIRTY RAT. I PICKED " + computerFirePosition
|
||||
+ ". YOUR TURN:");
|
||||
System.out.println();
|
||||
this.gameState = GAME_STATE.PLAYERS_TURN;
|
||||
gameState = GAME_STATE.PLAYERS_TURN;
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -141,37 +141,36 @@ public class Bombardment {
|
||||
case PLAYER_WON:
|
||||
System.out.println("YOU GOT ME, I'M GOING FAST. BUT I'LL GET YOU WHEN");
|
||||
System.out.println("MY TRANSISTO&S RECUP%RA*E!");
|
||||
this.gameState = GAME_STATE.GAME_OVER;
|
||||
gameState = GAME_STATE.GAME_OVER;
|
||||
break;
|
||||
|
||||
case PLAYER_LOST:
|
||||
System.out.println("BETTER LUCK NEXT TIME.");
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate computer guess. Make that the computer does not guess the same
|
||||
* location twice
|
||||
*
|
||||
* @return location of the computers guess that has not been guessed previously
|
||||
* @return location of the computers guess that has not been guessed previously
|
||||
*/
|
||||
private int uniqueComputerGuess() {
|
||||
|
||||
boolean validGuess = false;
|
||||
int computerGuess;
|
||||
do
|
||||
{
|
||||
do {
|
||||
computerGuess = randomNumber();
|
||||
|
||||
if(!computersGuesses.contains(computerGuess)) {
|
||||
if (!computersGuesses.contains(computerGuess)) {
|
||||
validGuess = true;
|
||||
}
|
||||
} while(!validGuess);
|
||||
} while (!validGuess);
|
||||
|
||||
computersGuesses.add(computerGuess);
|
||||
|
||||
@@ -185,7 +184,6 @@ public class Bombardment {
|
||||
* until all four are in the hashset
|
||||
*
|
||||
* @return 4 locations of computers platoons
|
||||
*
|
||||
*/
|
||||
private HashSet<Integer> computersChosenPlatoons() {
|
||||
|
||||
@@ -198,19 +196,20 @@ public class Bombardment {
|
||||
tempPlatoons.add(randomNumber());
|
||||
|
||||
// All four created?
|
||||
if(tempPlatoons.size() == PLATOONS) {
|
||||
if (tempPlatoons.size() == PLATOONS) {
|
||||
// Exit when we have created four
|
||||
allPlatoonsAdded = true;
|
||||
}
|
||||
|
||||
} while(!allPlatoonsAdded);
|
||||
} while (!allPlatoonsAdded);
|
||||
|
||||
return tempPlatoons;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows a different message for each number of hits
|
||||
*
|
||||
* @param hits total number of hits by player on computer
|
||||
* @param hits total number of hits by player on computer
|
||||
*/
|
||||
private void showPlayerProgress(int hits) {
|
||||
|
||||
@@ -221,7 +220,7 @@ public class Bombardment {
|
||||
/**
|
||||
* Shows a different message for each number of hits
|
||||
*
|
||||
* @param hits total number of hits by computer on player
|
||||
* @param hits total number of hits by computer on player
|
||||
*/
|
||||
private void showComputerProgress(int hits, int lastGuess) {
|
||||
|
||||
@@ -231,42 +230,42 @@ public class Bombardment {
|
||||
|
||||
/**
|
||||
* Prints a message from the passed array based on the value of hits
|
||||
|
||||
* @param hits - number of hits the player or computer has made
|
||||
*
|
||||
* @param hits - number of hits the player or computer has made
|
||||
* @param messages - an array of string with messages
|
||||
*/
|
||||
private void showProgress(int hits, String[] messages) {
|
||||
System.out.println(messages[hits-1]);
|
||||
System.out.println(messages[hits - 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Update a player hit - adds a hit the player made on the computers platoon.
|
||||
*
|
||||
* @param fireLocation - computer location that got hit
|
||||
* @return number of hits the player has inflicted on the computer in total
|
||||
*/
|
||||
private int updatePlayerHits(int fireLocation) {
|
||||
|
||||
// N.B. only removes if present, so its redundant to check if it exists first
|
||||
this.computersPlatoons.remove(fireLocation);
|
||||
computersPlatoons.remove(fireLocation);
|
||||
|
||||
// return number of hits in total
|
||||
return PLATOONS - this.computersPlatoons.size();
|
||||
return PLATOONS - computersPlatoons.size();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Update a computer hit - adds a hit the computer made on the players platoon.
|
||||
*
|
||||
* @param fireLocation - player location that got hit
|
||||
* @return number of hits the player has inflicted on the computer in total
|
||||
*/
|
||||
private int updateComputerHits(int fireLocation) {
|
||||
|
||||
// N.B. only removes if present, so its redundant to check if it exists first
|
||||
this.playersPlatoons.remove(fireLocation);
|
||||
playersPlatoons.remove(fireLocation);
|
||||
|
||||
// return number of hits in total
|
||||
return PLATOONS - this.playersPlatoons.size();
|
||||
return PLATOONS - playersPlatoons.size();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,7 +275,7 @@ public class Bombardment {
|
||||
* @return true if a computer platoon was at that position
|
||||
*/
|
||||
private boolean didPlayerHitComputerPlatoon(int fireLocation) {
|
||||
return this.computersPlatoons.contains(fireLocation);
|
||||
return computersPlatoons.contains(fireLocation);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -286,22 +285,20 @@ public class Bombardment {
|
||||
* @return true if a players platoon was at that position
|
||||
*/
|
||||
private boolean didComputerHitPlayerPlatoon(int fireLocation) {
|
||||
return this.playersPlatoons.contains(fireLocation);
|
||||
return playersPlatoons.contains(fireLocation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the battlefield grid
|
||||
*
|
||||
*/
|
||||
private void drawBattlefield() {
|
||||
for(int i=1; i<MAX_GRID_SIZE+1; i+= 5) {
|
||||
System.out.printf("%-2s %-2s %-2s %-2s %-2s %n", i, i+1, i+2, i+3, i+4);
|
||||
for (int i = 1; i < MAX_GRID_SIZE + 1; i += 5) {
|
||||
System.out.printf("%-2s %-2s %-2s %-2s %-2s %n", i, i + 1, i + 2, i + 3, i + 4);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic information about the game
|
||||
*
|
||||
*/
|
||||
private void intro() {
|
||||
System.out.println("BOMBARDMENT");
|
||||
@@ -327,12 +324,12 @@ public class Bombardment {
|
||||
private void init() {
|
||||
|
||||
// Create four locations for the computers platoons.
|
||||
this.computersPlatoons = computersChosenPlatoons();
|
||||
computersPlatoons = computersChosenPlatoons();
|
||||
|
||||
// Players platoons.
|
||||
this.playersPlatoons = new HashSet<>();
|
||||
playersPlatoons = new HashSet<>();
|
||||
|
||||
this.computersGuesses = new HashSet<>();
|
||||
computersGuesses = new HashSet<>();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Bullseye
|
||||
* <p>
|
||||
* Based on the Basic game of Bullseye here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/18%20Bullseye/bullseye.bas
|
||||
* <p>
|
||||
* 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.
|
||||
*/
|
||||
public class Bullseye {
|
||||
|
||||
// Used for formatting output
|
||||
public static final int FIRST_IDENT = 10;
|
||||
public static final int SECOND_IDENT = 30;
|
||||
public static final int THIRD_INDENT = 30;
|
||||
|
||||
public static final double[] SHOT_ONE = new double[] { .65, .55, .5, .5};
|
||||
public static final double[] SHOT_TWO = new double[] { .99, .77, .43,.01};
|
||||
public static final double[] SHOT_THREE = new double[] { .95, .75, .45, .05 };
|
||||
// Used to decide throw result
|
||||
public static final double[] SHOT_ONE = new double[]{.65, .55, .5, .5};
|
||||
public static final double[] SHOT_TWO = new double[]{.99, .77, .43, .01};
|
||||
public static final double[] SHOT_THREE = new double[]{.95, .75, .45, .05};
|
||||
|
||||
private enum GAME_STATE {
|
||||
STARTING,
|
||||
@@ -20,14 +31,12 @@ public class Bullseye {
|
||||
|
||||
private GAME_STATE gameState;
|
||||
|
||||
private ArrayList<Player> players;
|
||||
private final ArrayList<Player> players;
|
||||
|
||||
private Shot[] shots;
|
||||
private final Shot[] shots;
|
||||
|
||||
// Used for keyboard input
|
||||
private Scanner kbScanner;
|
||||
|
||||
private int numberOfPlayers;
|
||||
private final Scanner kbScanner;
|
||||
|
||||
private int round;
|
||||
|
||||
@@ -49,7 +58,6 @@ public class Bullseye {
|
||||
|
||||
/**
|
||||
* Main game loop
|
||||
*
|
||||
*/
|
||||
public void play() {
|
||||
|
||||
@@ -65,10 +73,10 @@ public class Bullseye {
|
||||
// Start the game, set the number of players, names and round
|
||||
case START_GAME:
|
||||
|
||||
this.numberOfPlayers = chooseNumberOfPlayers();
|
||||
int numberOfPlayers = chooseNumberOfPlayers();
|
||||
|
||||
for(int i=0; i<this.numberOfPlayers; i++) {
|
||||
String name = displayTextAndGetInput("NAME OF PLAYER #" + (i+1) + "? ");
|
||||
for (int i = 0; i < numberOfPlayers; i++) {
|
||||
String name = displayTextAndGetInput("NAME OF PLAYER #" + (i + 1) + "? ");
|
||||
Player player = new Player(name);
|
||||
this.players.add(player);
|
||||
}
|
||||
@@ -85,8 +93,7 @@ public class Bullseye {
|
||||
System.out.println("=======");
|
||||
|
||||
// Each player takes their turn
|
||||
for(int i=0; i<players.size(); i++) {
|
||||
Player player = players.get(i);
|
||||
for (Player player : players) {
|
||||
int playerThrow = getPlayersThrow(player);
|
||||
int points = calculatePlayerPoints(playerThrow);
|
||||
player.addScore(points);
|
||||
@@ -96,11 +103,10 @@ public class Bullseye {
|
||||
boolean foundWinner = false;
|
||||
|
||||
// Check if any player won
|
||||
for(int i=0; i<players.size(); i++) {
|
||||
Player thePlayer = players.get(i);
|
||||
for (Player thePlayer : players) {
|
||||
int score = thePlayer.getScore();
|
||||
if(score >=200) {
|
||||
if(!foundWinner) {
|
||||
if (score >= 200) {
|
||||
if (!foundWinner) {
|
||||
System.out.println("WE HAVE A WINNER!!");
|
||||
System.out.println();
|
||||
foundWinner = true;
|
||||
@@ -110,7 +116,7 @@ public class Bullseye {
|
||||
}
|
||||
}
|
||||
|
||||
if(foundWinner) {
|
||||
if (foundWinner) {
|
||||
System.out.println("THANKS FOR THE GAME.");
|
||||
gameState = GAME_STATE.GAME_OVER;
|
||||
} else {
|
||||
@@ -120,12 +126,11 @@ public class Bullseye {
|
||||
|
||||
break;
|
||||
}
|
||||
} while(gameState != GAME_STATE.GAME_OVER);
|
||||
} while (gameState != GAME_STATE.GAME_OVER);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display info about the game
|
||||
*
|
||||
*/
|
||||
private void intro() {
|
||||
System.out.println("BULLSEYE");
|
||||
@@ -136,7 +141,7 @@ public class Bullseye {
|
||||
System.out.println("TO GET 200 POINTS.");
|
||||
System.out.println();
|
||||
System.out.println(paddedString("THROW", "DESCRIPTION", "PROBABLE SCORE"));
|
||||
System.out.println(paddedString("1", "FAST OVERARM","BULLSEYE OR COMPLETE MISS"));
|
||||
System.out.println(paddedString("1", "FAST OVERARM", "BULLSEYE OR COMPLETE MISS"));
|
||||
System.out.println(paddedString("2", "CONTROLLED OVERARM", "10, 20 OR 30 POINTS"));
|
||||
System.out.println(paddedString("3", "UNDERARM", "ANYTHING"));
|
||||
}
|
||||
@@ -151,16 +156,16 @@ public class Bullseye {
|
||||
private int calculatePlayerPoints(int playerThrow) {
|
||||
|
||||
// -1 is because of 0 base Java array
|
||||
double p1 = this.shots[playerThrow-1].getShot(0);
|
||||
double p2 = this.shots[playerThrow-1].getShot(1);
|
||||
double p3 = this.shots[playerThrow-1].getShot(2);
|
||||
double p4 = this.shots[playerThrow-1].getShot(3);
|
||||
double p1 = this.shots[playerThrow - 1].getShot(0);
|
||||
double p2 = this.shots[playerThrow - 1].getShot(1);
|
||||
double p3 = this.shots[playerThrow - 1].getShot(2);
|
||||
double p4 = this.shots[playerThrow - 1].getShot(3);
|
||||
|
||||
double random = Math.random();
|
||||
|
||||
int points;
|
||||
|
||||
if(random >= p1) {
|
||||
if (random >= p1) {
|
||||
System.out.println("BULLSEYE!! 40 POINTS!");
|
||||
points = 40;
|
||||
// If the throw was 1 (bullseye or missed, then make it missed
|
||||
@@ -170,13 +175,13 @@ public class Bullseye {
|
||||
} else if (playerThrow == 1) {
|
||||
System.out.println("MISSED THE TARGET! TOO BAD.");
|
||||
points = 0;
|
||||
} else if(random >= p2) {
|
||||
} else if (random >= p2) {
|
||||
System.out.println("30-POINT ZONE!");
|
||||
points = 30;
|
||||
} else if(random >= p3) {
|
||||
} else if (random >= p3) {
|
||||
System.out.println("20-POINT ZONE");
|
||||
points = 20;
|
||||
} else if(random >= p4) {
|
||||
} else if (random >= p4) {
|
||||
System.out.println("WHEW! 10 POINTS.");
|
||||
points = 10;
|
||||
} else {
|
||||
@@ -190,23 +195,23 @@ public class Bullseye {
|
||||
/**
|
||||
* Get players shot 1,2, or 3 - ask again if invalid input
|
||||
*
|
||||
* @param player
|
||||
* @return 1,2, or 3 indicating the players shot
|
||||
* @param player the player we are calculating the throw on
|
||||
* @return 1, 2, or 3 indicating the players shot
|
||||
*/
|
||||
private int getPlayersThrow(Player player) {
|
||||
boolean inputCorrect = false;
|
||||
String theThrow;
|
||||
do {
|
||||
theThrow = displayTextAndGetInput(player.getName() + "'S THROW ");
|
||||
if(theThrow.equals("1") || theThrow.equals("2") || theThrow.equals("3")) {
|
||||
if (theThrow.equals("1") || theThrow.equals("2") || theThrow.equals("3")) {
|
||||
inputCorrect = true;
|
||||
} else {
|
||||
System.out.println("INPUT 1, 2, OR 3!");
|
||||
}
|
||||
|
||||
} while(!inputCorrect);
|
||||
} while (!inputCorrect);
|
||||
|
||||
return Integer.valueOf(theThrow);
|
||||
return Integer.parseInt(theThrow);
|
||||
}
|
||||
|
||||
|
||||
@@ -217,8 +222,9 @@ public class Bullseye {
|
||||
*/
|
||||
private int chooseNumberOfPlayers() {
|
||||
|
||||
return Integer.valueOf((displayTextAndGetInput("HOW MANY PLAYERS? ")));
|
||||
return Integer.parseInt((displayTextAndGetInput("HOW MANY PLAYERS? ")));
|
||||
}
|
||||
|
||||
/*
|
||||
* Print a message on the screen, then accept input from Keyboard.
|
||||
*
|
||||
@@ -234,9 +240,9 @@ public class Bullseye {
|
||||
* Format three strings to a given number of spaces
|
||||
* Replacing the original basic code which used tabs
|
||||
*
|
||||
* @param first String to print in pos 1
|
||||
* @param first String to print in pos 1
|
||||
* @param second String to print in pos 2
|
||||
* @param third String to print in pos 3
|
||||
* @param third String to print in pos 3
|
||||
* @return formatted string
|
||||
*/
|
||||
private String paddedString(String first, String second, String third) {
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
*/
|
||||
public class Player {
|
||||
|
||||
private String name;
|
||||
private final String name;
|
||||
|
||||
private int score;
|
||||
|
||||
Player(String name) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Chief
|
||||
* <p>
|
||||
* 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 the 1970's Basic game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*/
|
||||
public class Chief {
|
||||
|
||||
private enum GAME_STATE {
|
||||
@@ -14,6 +23,7 @@ public class Chief {
|
||||
|
||||
private GAME_STATE gameState;
|
||||
|
||||
// The number the computer determines to be the players starting number
|
||||
private double calculatedNumber;
|
||||
|
||||
// Used for keyboard input
|
||||
@@ -43,7 +53,7 @@ public class Chief {
|
||||
|
||||
// show an message to start
|
||||
case READY_TO_START:
|
||||
if(!yesEntered(displayTextAndGetInput("ARE YOU READY TO TAKE THE TEST YOU CALLED ME OUT FOR? "))) {
|
||||
if (!yesEntered(displayTextAndGetInput("ARE YOU READY TO TAKE THE TEST YOU CALLED ME OUT FOR? "))) {
|
||||
System.out.println("SHUT UP, PALE FACE WITH WISE TONGUE.");
|
||||
}
|
||||
|
||||
@@ -57,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
|
||||
+ ". AM I RIGHT? "))) {
|
||||
this.gameState = GAME_STATE.END_GAME;
|
||||
if (yesEntered(
|
||||
displayTextAndGetInput("I BET YOUR NUMBER WAS " + calculatedNumber
|
||||
+ ". AM I RIGHT? "))) {
|
||||
gameState = GAME_STATE.END_GAME;
|
||||
|
||||
} else {
|
||||
// Player did not agree, so show the breakdown
|
||||
@@ -76,27 +86,27 @@ public class Chief {
|
||||
double f = number + 3;
|
||||
double g = f / 5;
|
||||
double h = g * 8;
|
||||
double i = h/5 + 5;
|
||||
double j = i -1;
|
||||
double i = h / 5 + 5;
|
||||
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;
|
||||
if (yesEntered(displayTextAndGetInput("NOW DO YOU BELIEVE ME? "))) {
|
||||
gameState = GAME_STATE.END_GAME;
|
||||
} else {
|
||||
// Time for a lightning bolt.
|
||||
System.out.println("YOU HAVE MADE ME MAD!!!");
|
||||
System.out.println("THERE MUST BE A GREAT LIGHTNING BOLT!");
|
||||
System.out.println();
|
||||
for(int x=30; x>=22; x--) {
|
||||
for (int x = 30; x >= 22; x--) {
|
||||
System.out.println(tabbedSpaces(x) + "X X");
|
||||
}
|
||||
System.out.println(tabbedSpaces(21) + "X XXX");
|
||||
System.out.println(tabbedSpaces(20) + "X X");
|
||||
System.out.println(tabbedSpaces(19) + "XX X");
|
||||
for(int y=20; y>=13; y--) {
|
||||
for (int y = 20; y >= 13; y--) {
|
||||
System.out.println(tabbedSpaces(y) + "X X");
|
||||
}
|
||||
System.out.println(tabbedSpaces(12) + "XX");
|
||||
@@ -106,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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -115,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
|
||||
@@ -134,14 +144,14 @@ public class Chief {
|
||||
Arrays.fill(repeat, ' ');
|
||||
return new String(repeat);
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic information about the game
|
||||
*
|
||||
*/
|
||||
private void intro() {
|
||||
System.out.println("CHIEF");
|
||||
@@ -165,15 +175,15 @@ public class Chief {
|
||||
* Returns true if a given string contains at least one of the varargs (2nd parameter).
|
||||
* Note: Case insensitive comparison.
|
||||
*
|
||||
* @param text string to search
|
||||
* @param text string to search
|
||||
* @param values varargs of type string containing values to compare
|
||||
* @return true if one of the varargs arguments was found in text
|
||||
*/
|
||||
private boolean stringIsAnyValue(String text, String... values) {
|
||||
|
||||
// Cycle through the variable number of values and test each
|
||||
for(String val:values) {
|
||||
if(text.equalsIgnoreCase(val)) {
|
||||
for (String val : values) {
|
||||
if (text.equalsIgnoreCase(val)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 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 HiLo {
|
||||
@@ -24,7 +24,7 @@ public class HiLo {
|
||||
}
|
||||
|
||||
// Used for keyboard input
|
||||
private Scanner kbScanner;
|
||||
private final Scanner kbScanner;
|
||||
|
||||
// Current game state
|
||||
private GAME_STATE gameState;
|
||||
@@ -40,8 +40,8 @@ public class HiLo {
|
||||
|
||||
public HiLo() {
|
||||
|
||||
this.gameState = GAME_STATE.STARTING;
|
||||
this.playerAmountWon = 0;
|
||||
gameState = GAME_STATE.STARTING;
|
||||
playerAmountWon = 0;
|
||||
|
||||
// Initialise kb scanner
|
||||
kbScanner = new Scanner(System.in);
|
||||
@@ -66,7 +66,7 @@ public class HiLo {
|
||||
case START_GAME:
|
||||
init();
|
||||
System.out.println("O.K. I HAVE A NUMBER IN MIND.");
|
||||
this.gameState = GAME_STATE.GUESSING;
|
||||
gameState = GAME_STATE.GUESSING;
|
||||
break;
|
||||
|
||||
// Player guesses the number until they get it or run out of guesses
|
||||
@@ -75,21 +75,21 @@ public class HiLo {
|
||||
|
||||
// Check if the player guessed the number
|
||||
if(validateGuess(guess)) {
|
||||
System.out.println("GOT IT!!!!!!!!!! YOU WIN " + this.computersNumber
|
||||
System.out.println("GOT IT!!!!!!!!!! YOU WIN " + computersNumber
|
||||
+ " DOLLARS.");
|
||||
this.playerAmountWon += this.computersNumber;
|
||||
playerAmountWon += computersNumber;
|
||||
System.out.println("YOUR TOTAL WINNINGS ARE NOW "
|
||||
+ this.playerAmountWon + " DOLLARS.");
|
||||
this.gameState = GAME_STATE.PLAY_AGAIN;
|
||||
+ playerAmountWon + " DOLLARS.");
|
||||
gameState = GAME_STATE.PLAY_AGAIN;
|
||||
} else {
|
||||
// incorrect guess
|
||||
this.playersGuesses++;
|
||||
playersGuesses++;
|
||||
// Ran out of guesses?
|
||||
if (this.playersGuesses == MAX_GUESSES) {
|
||||
if (playersGuesses == MAX_GUESSES) {
|
||||
System.out.println("YOU BLEW IT...TOO BAD...THE NUMBER WAS "
|
||||
+ this.computersNumber);
|
||||
this.playerAmountWon = 0;
|
||||
this.gameState = GAME_STATE.PLAY_AGAIN;
|
||||
+ computersNumber);
|
||||
playerAmountWon = 0;
|
||||
gameState = GAME_STATE.PLAY_AGAIN;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -98,11 +98,11 @@ public class HiLo {
|
||||
case PLAY_AGAIN:
|
||||
System.out.println();
|
||||
if(yesEntered(displayTextAndGetInput("PLAY AGAIN (YES OR NO) "))) {
|
||||
this.gameState = GAME_STATE.START_GAME;
|
||||
gameState = GAME_STATE.START_GAME;
|
||||
} else {
|
||||
// Chose not to play again
|
||||
System.out.println("SO LONG. HOPE YOU ENJOYED YOURSELF!!!");
|
||||
this.gameState = GAME_STATE.GAME_OVER;
|
||||
gameState = GAME_STATE.GAME_OVER;
|
||||
}
|
||||
}
|
||||
} while (gameState != GAME_STATE.GAME_OVER);
|
||||
@@ -111,17 +111,17 @@ public class HiLo {
|
||||
/**
|
||||
* Checks the players guess against the computers randomly generated number
|
||||
*
|
||||
* @param theGuess
|
||||
* @param theGuess the players guess
|
||||
* @return true if the player guessed correctly, false otherwise
|
||||
*/
|
||||
private boolean validateGuess(int theGuess) {
|
||||
|
||||
// Correct guess?
|
||||
if(theGuess == this.computersNumber) {
|
||||
if(theGuess == computersNumber) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(theGuess > this.computersNumber) {
|
||||
if(theGuess > computersNumber) {
|
||||
System.out.println("YOUR GUESS IS TOO HIGH.");
|
||||
} else {
|
||||
System.out.println("YOUR GUESS IS TOO LOW.");
|
||||
@@ -131,8 +131,8 @@ public class HiLo {
|
||||
}
|
||||
|
||||
private void init() {
|
||||
this.playersGuesses = 0;
|
||||
this.computersNumber = randomNumber();
|
||||
playersGuesses = 0;
|
||||
computersNumber = randomNumber();
|
||||
}
|
||||
|
||||
public void intro() {
|
||||
@@ -140,7 +140,7 @@ public class HiLo {
|
||||
System.out.println("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY");
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
System.out.println("THIS IS THE GAME OF HI LO.");
|
||||
System.out.println("IS THE GAME OF HI LO.");
|
||||
System.out.println();
|
||||
System.out.println("YOU WILL HAVE 6 TRIES TO GUESS THE AMOUNT OF MONEY IN THE");
|
||||
System.out.println("HI LO JACKPOT, WHICH IS BETWEEN 1 AND 100 DOLLARS. IF YOU");
|
||||
@@ -155,7 +155,7 @@ public class HiLo {
|
||||
* @return players guess as an int
|
||||
*/
|
||||
private int playerGuess() {
|
||||
return Integer.valueOf((displayTextAndGetInput("YOUR GUESS? ")));
|
||||
return Integer.parseInt((displayTextAndGetInput("YOUR GUESS? ")));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -165,7 +165,7 @@ public class HiLo {
|
||||
* @return true of Y or YES was entered, otherwise false
|
||||
*/
|
||||
private boolean yesEntered(String text) {
|
||||
return stringIsAnyValue(text, new String[] {"Y", "YES"});
|
||||
return stringIsAnyValue(text, "Y", "YES");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Hurkle
|
||||
* <p>
|
||||
* 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 the 1970's Basic game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*/
|
||||
public class Hurkle {
|
||||
|
||||
public static final int GRID_SIZE = 10;
|
||||
@@ -17,7 +25,7 @@ public class Hurkle {
|
||||
private GAME_STATE gameState;
|
||||
|
||||
// Used for keyboard input
|
||||
private Scanner kbScanner;
|
||||
private final Scanner kbScanner;
|
||||
|
||||
private int guesses;
|
||||
|
||||
@@ -54,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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,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
|
||||
@@ -98,31 +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();
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
public class HurkleGame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Hurkle hurkle = new Hurkle();
|
||||
hurkle.play();
|
||||
Hurkle hurkle = new Hurkle();
|
||||
hurkle.play();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Pizza
|
||||
* <p>
|
||||
* 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 the 1970's Basic game in Java, without introducing
|
||||
* new features - no additional text, error checking, etc has been added.
|
||||
*/
|
||||
public class Pizza {
|
||||
|
||||
private final int MAX_DELIVERIES = 5;
|
||||
@@ -17,11 +26,11 @@ public class Pizza {
|
||||
}
|
||||
|
||||
// houses that can order pizza
|
||||
private final char[] houses = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
|
||||
private final char[] houses = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
|
||||
'J', 'K', 'L', 'M', 'N', 'O', 'P'};
|
||||
|
||||
// size of grid
|
||||
private final int[] gridPos = new int[] { 1, 2, 3, 4};
|
||||
private final int[] gridPos = new int[]{1, 2, 3, 4};
|
||||
|
||||
private GAME_STATE gameState;
|
||||
|
||||
@@ -38,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);
|
||||
@@ -50,40 +59,40 @@ 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) ?
|
||||
case MORE_DIRECTIONS:
|
||||
extendedIntro();
|
||||
String moreInfo = displayTextAndGetInput("DO YOU NEED MORE DIRECTIONS? ");
|
||||
if(!yesOrNoEntered(moreInfo)) {
|
||||
if (!yesOrNoEntered(moreInfo)) {
|
||||
System.out.println("'YES' OR 'NO' PLEASE, NOW THEN,");
|
||||
} else {
|
||||
// More instructions selected
|
||||
if(yesEntered(moreInfo)) {
|
||||
if (yesEntered(moreInfo)) {
|
||||
displayMoreDirections();
|
||||
// Player understand now?
|
||||
if (yesEntered(displayTextAndGetInput("UNDERSTAND? "))) {
|
||||
@@ -91,86 +100,86 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
break;
|
||||
|
||||
// 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
|
||||
int x = getDelimitedValue(answer, 0);
|
||||
int y = getDelimitedValue(answer, 1);
|
||||
int calculatedPos = (x + (y -1) * 4) -1;
|
||||
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;
|
||||
|
||||
// Sign off message for cases where the Chief is not upset
|
||||
case END_GAME:
|
||||
if(yesEntered(displayTextAndGetInput("DO YOU WANT TO DELIVER MORE PIZZAS? "))) {
|
||||
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() {
|
||||
@@ -179,19 +188,19 @@ public class Pizza {
|
||||
System.out.println();
|
||||
System.out.println(" -----1-----2-----3-----4-----");
|
||||
int k = 3;
|
||||
for(int i=1; i<5; i++) {
|
||||
for (int i = 1; i < 5; i++) {
|
||||
System.out.println("-");
|
||||
System.out.println("-");
|
||||
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("-");
|
||||
@@ -199,10 +208,10 @@ public class Pizza {
|
||||
System.out.println("-");
|
||||
System.out.println("-");
|
||||
System.out.println(" -----1-----2-----3-----4-----");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic information about the game
|
||||
*
|
||||
*/
|
||||
private void intro() {
|
||||
System.out.println("PIZZA");
|
||||
@@ -229,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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,19 +282,20 @@ public class Pizza {
|
||||
private boolean yesOrNoEntered(String text) {
|
||||
return stringIsAnyValue(text, "Y", "YES", "N", "NO");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if a given string contains at least one of the varargs (2nd parameter).
|
||||
* Note: Case insensitive comparison.
|
||||
*
|
||||
* @param text string to search
|
||||
* @param text string to search
|
||||
* @param values varargs of type string containing values to compare
|
||||
* @return true if one of the varargs arguments was found in text
|
||||
*/
|
||||
private boolean stringIsAnyValue(String text, String... values) {
|
||||
|
||||
// Cycle through the variable number of values and test each
|
||||
for(String val:values) {
|
||||
if(text.equalsIgnoreCase(val)) {
|
||||
for (String val : values) {
|
||||
if (text.equalsIgnoreCase(val)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ public class PizzaGame {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Pizza pizza = new Pizza();
|
||||
pizza.play();
|
||||
Pizza pizza = new Pizza();
|
||||
pizza.play();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
@@ -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() {
|
||||
|
||||
@@ -3,12 +3,12 @@ import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Game of Trap
|
||||
*
|
||||
* <p>
|
||||
* Based on the Basic game of Trap here
|
||||
* https://github.com/coding-horror/basic-computer-games/blob/main/92%20Trap/trap.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.
|
||||
* <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 Trap {
|
||||
|
||||
@@ -45,7 +45,6 @@ public class Trap {
|
||||
|
||||
/**
|
||||
* Main game loop
|
||||
*
|
||||
*/
|
||||
public void play() {
|
||||
|
||||
@@ -55,7 +54,7 @@ public class Trap {
|
||||
// Show an introduction and optional instructions the first time the game is played.
|
||||
case STARTING:
|
||||
intro();
|
||||
if(yesEntered(displayTextAndGetInput("INSTRUCTIONS? "))) {
|
||||
if (yesEntered(displayTextAndGetInput("INSTRUCTIONS? "))) {
|
||||
instructions();
|
||||
}
|
||||
gameState = GAME_STATE.START_GAME;
|
||||
@@ -72,11 +71,11 @@ public class Trap {
|
||||
case GUESSING:
|
||||
System.out.println();
|
||||
String playerRangeGuess = displayTextAndGetInput("GUESS # " + currentPlayersGuess + "? ");
|
||||
int startRange = getDelimitedValue(playerRangeGuess,0);
|
||||
int endRange = getDelimitedValue(playerRangeGuess,1);
|
||||
int startRange = getDelimitedValue(playerRangeGuess, 0);
|
||||
int endRange = getDelimitedValue(playerRangeGuess, 1);
|
||||
|
||||
// Has the player won?
|
||||
if(startRange == computersNumber && endRange == computersNumber) {
|
||||
if (startRange == computersNumber && endRange == computersNumber) {
|
||||
System.out.println("YOU GOT IT!!!");
|
||||
System.out.println();
|
||||
gameState = GAME_STATE.PLAY_AGAIN;
|
||||
@@ -84,7 +83,7 @@ public class Trap {
|
||||
// show where the guess is at
|
||||
System.out.println(showGuessResult(startRange, endRange));
|
||||
currentPlayersGuess++;
|
||||
if(currentPlayersGuess > MAX_GUESSES) {
|
||||
if (currentPlayersGuess > MAX_GUESSES) {
|
||||
System.out.println("SORRY, THAT'S " + MAX_GUESSES + " GUESSES. THE NUMBER WAS "
|
||||
+ computersNumber);
|
||||
gameState = GAME_STATE.PLAY_AGAIN;
|
||||
@@ -104,15 +103,15 @@ public class Trap {
|
||||
* Show the players guess result
|
||||
*
|
||||
* @param start start range entered by player
|
||||
* @param end end range
|
||||
* @param end end range
|
||||
* @return text to indicate their progress.
|
||||
*/
|
||||
private String showGuessResult(int start, int end) {
|
||||
|
||||
String status;
|
||||
if(start <= computersNumber && computersNumber <= end) {
|
||||
if (start <= computersNumber && computersNumber <= end) {
|
||||
status = "YOU HAVE TRAPPED MY NUMBER.";
|
||||
} else if(computersNumber < start) {
|
||||
} else if (computersNumber < start) {
|
||||
status = "MY NUMBER IS SMALLER THAN YOUR TRAP NUMBERS.";
|
||||
} else {
|
||||
status = "MY NUMBER IS LARGER THAN YOUR TRAP NUMBERS.";
|
||||
@@ -157,7 +156,7 @@ public class Trap {
|
||||
/**
|
||||
* Checks whether player entered Y or YES to a question.
|
||||
*
|
||||
* @param text player string from kb
|
||||
* @param text player string from kb
|
||||
* @return true of Y or YES was entered, otherwise false
|
||||
*/
|
||||
private boolean yesEntered(String text) {
|
||||
@@ -169,7 +168,7 @@ public class Trap {
|
||||
* Useful to check for Y or YES for example
|
||||
* Comparison is case insensitive.
|
||||
*
|
||||
* @param text source string
|
||||
* @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
|
||||
*/
|
||||
Reference in New Issue
Block a user