This commit is contained in:
Tim
2021-02-22 09:40:47 +10:30
parent 87625f0e8e
commit 749cd0e683

View File

@@ -3,11 +3,11 @@ 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 {
@@ -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
@@ -80,7 +80,7 @@ public class Bombardment {
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
@@ -95,16 +95,16 @@ public class Bombardment {
// 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;
@@ -120,19 +120,19 @@ public class Bombardment {
// 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,17 +141,17 @@ 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);
} }
/** /**
@@ -164,8 +164,7 @@ public class Bombardment {
boolean validGuess = false; boolean validGuess = false;
int computerGuess; int computerGuess;
do do {
{
computerGuess = randomNumber(); computerGuess = randomNumber();
if (!computersGuesses.contains(computerGuess)) { if (!computersGuesses.contains(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() {
@@ -207,6 +205,7 @@ public class Bombardment {
return tempPlatoons; return tempPlatoons;
} }
/** /**
* Shows a different message for each number of hits * Shows a different message for each number of hits
* *
@@ -231,7 +230,7 @@ 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
*/ */
@@ -240,33 +239,33 @@ public class Bombardment {
} }
/** /**
*
* 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,12 +285,11 @@ 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) {
@@ -301,7 +299,6 @@ public class Bombardment {
/** /**
* 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<>();
} }
/** /**