From 749cd0e683030e494e81f230d24bd95b46cc3cb8 Mon Sep 17 00:00:00 2001 From: Tim <70119791+journich@users.noreply.github.com> Date: Mon, 22 Feb 2021 09:40:47 +1030 Subject: [PATCH] cleanup --- 11 Bombardment/java/src/Bombardment.java | 109 +++++++++++------------ 1 file changed, 53 insertions(+), 56 deletions(-) diff --git a/11 Bombardment/java/src/Bombardment.java b/11 Bombardment/java/src/Bombardment.java index 06a3043c..1dede841 100644 --- a/11 Bombardment/java/src/Bombardment.java +++ b/11 Bombardment/java/src/Bombardment.java @@ -3,12 +3,12 @@ import java.util.Scanner; /** * Game of Bombardment - * + *

* 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. + *

+ * 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 computersPlatoons; private HashSet 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 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(); + playersPlatoons = new HashSet<>(); - this.computersGuesses = new HashSet<>(); + computersGuesses = new HashSet<>(); } /**