diff --git a/01 Acey Ducey/java/src/AceyDucey.java b/01 Acey Ducey/java/src/AceyDucey.java index 5320c8e0..43fc9950 100644 --- a/01 Acey Ducey/java/src/AceyDucey.java +++ b/01 Acey Ducey/java/src/AceyDucey.java @@ -2,12 +2,11 @@ import java.util.Scanner; /** * Game of AceyDucey - * + *
* 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
diff --git a/01 Acey Ducey/java/src/Card.java b/01 Acey Ducey/java/src/Card.java
index d36f7301..88211002 100644
--- a/01 Acey Ducey/java/src/Card.java
+++ b/01 Acey Ducey/java/src/Card.java
@@ -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) {
diff --git a/18 Bullseye/java/src/Player.java b/18 Bullseye/java/src/Player.java
index b67b9309..1a30935a 100644
--- a/18 Bullseye/java/src/Player.java
+++ b/18 Bullseye/java/src/Player.java
@@ -4,7 +4,8 @@
*/
public class Player {
- private String name;
+ private final String name;
+
private int score;
Player(String name) {
diff --git a/18 Bullseye/java/src/Shot.java b/18 Bullseye/java/src/Shot.java
index 01d8b283..43fa84e4 100644
--- a/18 Bullseye/java/src/Shot.java
+++ b/18 Bullseye/java/src/Shot.java
@@ -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
- * 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;
}
diff --git a/69 Pizza/java/src/Pizza.java b/69 Pizza/java/src/Pizza.java
index fadfccc6..55bf8ceb 100644
--- a/69 Pizza/java/src/Pizza.java
+++ b/69 Pizza/java/src/Pizza.java
@@ -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
*
- * 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;
}
/**
diff --git a/78 Sine Wave/java/src/SineWave.java b/78 Sine Wave/java/src/SineWave.java
index 7d917a75..f3b3c7eb 100644
--- a/78 Sine Wave/java/src/SineWave.java
+++ b/78 Sine Wave/java/src/SineWave.java
@@ -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 {
diff --git a/82 Stars/java/src/Stars.java b/82 Stars/java/src/Stars.java
index f28a0510..75e7cdd6 100644
--- a/82 Stars/java/src/Stars.java
+++ b/82 Stars/java/src/Stars.java
@@ -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() {