mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
cleanup
This commit is contained in:
@@ -3,12 +3,12 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Game of Bombardment
|
* Game of Bombardment
|
||||||
*
|
* <p>
|
||||||
* Based on the Basic game of Bombardment here
|
* Based on the Basic game of Bombardment here
|
||||||
* https://github.com/coding-horror/basic-computer-games/blob/main/11%20Bombardment/bombardment.bas
|
* https://github.com/coding-horror/basic-computer-games/blob/main/11%20Bombardment/bombardment.bas
|
||||||
*
|
* <p>
|
||||||
* 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.
|
* new features - no additional text, error checking, etc has been added.
|
||||||
*/
|
*/
|
||||||
public class Bombardment {
|
public class Bombardment {
|
||||||
|
|
||||||
@@ -28,11 +28,11 @@ public class Bombardment {
|
|||||||
|
|
||||||
private GAME_STATE gameState;
|
private GAME_STATE gameState;
|
||||||
|
|
||||||
public static final String[] PLAYER_HIT_MESSAGES = { "ONE DOWN, THREE TO GO.",
|
public static final String[] PLAYER_HIT_MESSAGES = {"ONE DOWN, THREE TO GO.",
|
||||||
"TWO DOWN, TWO TO GO.", "THREE DOWN, ONE 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.",
|
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> computersPlatoons;
|
||||||
private HashSet<Integer> playersPlatoons;
|
private HashSet<Integer> playersPlatoons;
|
||||||
@@ -44,7 +44,7 @@ public class Bombardment {
|
|||||||
|
|
||||||
public Bombardment() {
|
public Bombardment() {
|
||||||
|
|
||||||
this.gameState = GAME_STATE.STARTING;
|
gameState = GAME_STATE.STARTING;
|
||||||
|
|
||||||
// Initialise kb scanner
|
// Initialise kb scanner
|
||||||
kbScanner = new Scanner(System.in);
|
kbScanner = new Scanner(System.in);
|
||||||
@@ -56,19 +56,19 @@ public class Bombardment {
|
|||||||
public void play() {
|
public void play() {
|
||||||
|
|
||||||
do {
|
do {
|
||||||
switch (this.gameState) {
|
switch (gameState) {
|
||||||
|
|
||||||
// Show an introduction the first time the game is played.
|
// Show an introduction the first time the game is played.
|
||||||
case STARTING:
|
case STARTING:
|
||||||
init();
|
init();
|
||||||
intro();
|
intro();
|
||||||
this.gameState = GAME_STATE.DRAW_BATTLEFIELD;
|
gameState = GAME_STATE.DRAW_BATTLEFIELD;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Enter the players name
|
// Enter the players name
|
||||||
case DRAW_BATTLEFIELD:
|
case DRAW_BATTLEFIELD:
|
||||||
drawBattlefield();
|
drawBattlefield();
|
||||||
this.gameState = GAME_STATE.GET_PLAYER_CHOICES;
|
gameState = GAME_STATE.GET_PLAYER_CHOICES;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Get the players 4 locations for their platoons
|
// Get the players 4 locations for their platoons
|
||||||
@@ -76,35 +76,35 @@ public class Bombardment {
|
|||||||
String playerChoices = displayTextAndGetInput("WHAT ARE YOUR FOUR POSITIONS? ");
|
String playerChoices = displayTextAndGetInput("WHAT ARE YOUR FOUR POSITIONS? ");
|
||||||
|
|
||||||
// Store the 4 player choices that were entered separated with commas
|
// Store the 4 player choices that were entered separated with commas
|
||||||
for(int i=0; i<PLATOONS; i++) {
|
for (int i = 0; i < PLATOONS; i++) {
|
||||||
playersPlatoons.add(getDelimitedValue(playerChoices,i));
|
playersPlatoons.add(getDelimitedValue(playerChoices, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.gameState = GAME_STATE.PLAYERS_TURN;
|
gameState = GAME_STATE.PLAYERS_TURN;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Players turn to pick a location
|
// Players turn to pick a location
|
||||||
case PLAYERS_TURN:
|
case PLAYERS_TURN:
|
||||||
|
|
||||||
int firePosition = getDelimitedValue(
|
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
|
// Player hit a player platoon
|
||||||
int hits = updatePlayerHits(firePosition);
|
int hits = updatePlayerHits(firePosition);
|
||||||
// How many hits has the player made?
|
// How many hits has the player made?
|
||||||
if(hits != PLATOONS) {
|
if (hits != PLATOONS) {
|
||||||
showPlayerProgress(hits);
|
showPlayerProgress(hits);
|
||||||
this.gameState = GAME_STATE.COMPUTER_TURN;
|
gameState = GAME_STATE.COMPUTER_TURN;
|
||||||
} else {
|
} else {
|
||||||
// Player has obtained 4 hits, they win
|
// Player has obtained 4 hits, they win
|
||||||
this.gameState = GAME_STATE.PLAYER_WON;
|
gameState = GAME_STATE.PLAYER_WON;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Player missed
|
// Player missed
|
||||||
System.out.println("HA, HA YOU MISSED. MY TURN NOW:");
|
System.out.println("HA, HA YOU MISSED. MY TURN NOW:");
|
||||||
System.out.println();
|
System.out.println();
|
||||||
this.gameState = GAME_STATE.COMPUTER_TURN;
|
gameState = GAME_STATE.COMPUTER_TURN;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -114,25 +114,25 @@ public class Bombardment {
|
|||||||
|
|
||||||
// Computer takes a guess of a location
|
// Computer takes a guess of a location
|
||||||
int computerFirePosition = uniqueComputerGuess();
|
int computerFirePosition = uniqueComputerGuess();
|
||||||
if(didComputerHitPlayerPlatoon(computerFirePosition)) {
|
if (didComputerHitPlayerPlatoon(computerFirePosition)) {
|
||||||
// Computer hit a player platoon
|
// Computer hit a player platoon
|
||||||
int hits = updateComputerHits(computerFirePosition);
|
int hits = updateComputerHits(computerFirePosition);
|
||||||
// How many hits has the computer made?
|
// How many hits has the computer made?
|
||||||
if(hits != PLATOONS) {
|
if (hits != PLATOONS) {
|
||||||
showComputerProgress(hits, computerFirePosition);
|
showComputerProgress(hits, computerFirePosition);
|
||||||
this.gameState = GAME_STATE.PLAYERS_TURN;
|
gameState = GAME_STATE.PLAYERS_TURN;
|
||||||
} else {
|
} else {
|
||||||
// Computer has obtained 4 hits, they win
|
// Computer has obtained 4 hits, they win
|
||||||
System.out.println("YOU'RE DEAD. YOUR LAST OUTPOST WAS AT " + computerFirePosition
|
System.out.println("YOU'RE DEAD. YOUR LAST OUTPOST WAS AT " + computerFirePosition
|
||||||
+ ". HA, HA, HA.");
|
+ ". HA, HA, HA.");
|
||||||
this.gameState = GAME_STATE.PLAYER_LOST;
|
gameState = GAME_STATE.PLAYER_LOST;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Computer missed
|
// Computer missed
|
||||||
System.out.println("I MISSED YOU, YOU DIRTY RAT. I PICKED " + computerFirePosition
|
System.out.println("I MISSED YOU, YOU DIRTY RAT. I PICKED " + computerFirePosition
|
||||||
+ ". YOUR TURN:");
|
+ ". YOUR TURN:");
|
||||||
System.out.println();
|
System.out.println();
|
||||||
this.gameState = GAME_STATE.PLAYERS_TURN;
|
gameState = GAME_STATE.PLAYERS_TURN;
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -141,37 +141,36 @@ public class Bombardment {
|
|||||||
case PLAYER_WON:
|
case PLAYER_WON:
|
||||||
System.out.println("YOU GOT ME, I'M GOING FAST. BUT I'LL GET YOU WHEN");
|
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!");
|
System.out.println("MY TRANSISTO&S RECUP%RA*E!");
|
||||||
this.gameState = GAME_STATE.GAME_OVER;
|
gameState = GAME_STATE.GAME_OVER;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PLAYER_LOST:
|
case PLAYER_LOST:
|
||||||
System.out.println("BETTER LUCK NEXT TIME.");
|
System.out.println("BETTER LUCK NEXT TIME.");
|
||||||
this.gameState = GAME_STATE.GAME_OVER;
|
gameState = GAME_STATE.GAME_OVER;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// GAME_OVER State does not specifically have a case
|
// 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
|
* Calculate computer guess. Make that the computer does not guess the same
|
||||||
* location twice
|
* 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() {
|
private int uniqueComputerGuess() {
|
||||||
|
|
||||||
boolean validGuess = false;
|
boolean validGuess = false;
|
||||||
int computerGuess;
|
int computerGuess;
|
||||||
do
|
do {
|
||||||
{
|
|
||||||
computerGuess = randomNumber();
|
computerGuess = randomNumber();
|
||||||
|
|
||||||
if(!computersGuesses.contains(computerGuess)) {
|
if (!computersGuesses.contains(computerGuess)) {
|
||||||
validGuess = true;
|
validGuess = true;
|
||||||
}
|
}
|
||||||
} while(!validGuess);
|
} while (!validGuess);
|
||||||
|
|
||||||
computersGuesses.add(computerGuess);
|
computersGuesses.add(computerGuess);
|
||||||
|
|
||||||
@@ -185,7 +184,6 @@ public class Bombardment {
|
|||||||
* until all four are in the hashset
|
* until all four are in the hashset
|
||||||
*
|
*
|
||||||
* @return 4 locations of computers platoons
|
* @return 4 locations of computers platoons
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
private HashSet<Integer> computersChosenPlatoons() {
|
private HashSet<Integer> computersChosenPlatoons() {
|
||||||
|
|
||||||
@@ -198,19 +196,20 @@ public class Bombardment {
|
|||||||
tempPlatoons.add(randomNumber());
|
tempPlatoons.add(randomNumber());
|
||||||
|
|
||||||
// All four created?
|
// All four created?
|
||||||
if(tempPlatoons.size() == PLATOONS) {
|
if (tempPlatoons.size() == PLATOONS) {
|
||||||
// Exit when we have created four
|
// Exit when we have created four
|
||||||
allPlatoonsAdded = true;
|
allPlatoonsAdded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} while(!allPlatoonsAdded);
|
} while (!allPlatoonsAdded);
|
||||||
|
|
||||||
return tempPlatoons;
|
return tempPlatoons;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows a different message for each number of hits
|
* 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) {
|
private void showPlayerProgress(int hits) {
|
||||||
|
|
||||||
@@ -221,7 +220,7 @@ public class Bombardment {
|
|||||||
/**
|
/**
|
||||||
* Shows a different message for each number of hits
|
* 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) {
|
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
|
* 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
|
* @param messages - an array of string with messages
|
||||||
*/
|
*/
|
||||||
private void showProgress(int hits, String[] 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.
|
* Update a player hit - adds a hit the player made on the computers platoon.
|
||||||
|
*
|
||||||
* @param fireLocation - computer location that got hit
|
* @param fireLocation - computer location that got hit
|
||||||
* @return number of hits the player has inflicted on the computer in total
|
* @return number of hits the player has inflicted on the computer in total
|
||||||
*/
|
*/
|
||||||
private int updatePlayerHits(int fireLocation) {
|
private int updatePlayerHits(int fireLocation) {
|
||||||
|
|
||||||
// N.B. only removes if present, so its redundant to check if it exists first
|
// 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 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.
|
* Update a computer hit - adds a hit the computer made on the players platoon.
|
||||||
|
*
|
||||||
* @param fireLocation - player location that got hit
|
* @param fireLocation - player location that got hit
|
||||||
* @return number of hits the player has inflicted on the computer in total
|
* @return number of hits the player has inflicted on the computer in total
|
||||||
*/
|
*/
|
||||||
private int updateComputerHits(int fireLocation) {
|
private int updateComputerHits(int fireLocation) {
|
||||||
|
|
||||||
// N.B. only removes if present, so its redundant to check if it exists first
|
// 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 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
|
* @return true if a computer platoon was at that position
|
||||||
*/
|
*/
|
||||||
private boolean didPlayerHitComputerPlatoon(int fireLocation) {
|
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
|
* @return true if a players platoon was at that position
|
||||||
*/
|
*/
|
||||||
private boolean didComputerHitPlayerPlatoon(int fireLocation) {
|
private boolean didComputerHitPlayerPlatoon(int fireLocation) {
|
||||||
return this.playersPlatoons.contains(fireLocation);
|
return playersPlatoons.contains(fireLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Draw the battlefield grid
|
* Draw the battlefield grid
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
private void drawBattlefield() {
|
private void drawBattlefield() {
|
||||||
for(int i=1; i<MAX_GRID_SIZE+1; i+= 5) {
|
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);
|
System.out.printf("%-2s %-2s %-2s %-2s %-2s %n", i, i + 1, i + 2, i + 3, i + 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic information about the game
|
* Basic information about the game
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
private void intro() {
|
private void intro() {
|
||||||
System.out.println("BOMBARDMENT");
|
System.out.println("BOMBARDMENT");
|
||||||
@@ -327,12 +324,12 @@ public class Bombardment {
|
|||||||
private void init() {
|
private void init() {
|
||||||
|
|
||||||
// Create four locations for the computers platoons.
|
// Create four locations for the computers platoons.
|
||||||
this.computersPlatoons = computersChosenPlatoons();
|
computersPlatoons = computersChosenPlatoons();
|
||||||
|
|
||||||
// Players platoons.
|
// Players platoons.
|
||||||
this.playersPlatoons = new HashSet<>();
|
playersPlatoons = new HashSet<>();
|
||||||
|
|
||||||
this.computersGuesses = new HashSet<>();
|
computersGuesses = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user