mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-28 05:33:48 -08:00
cleanup
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user