diff --git a/11 Bombardment/java/src/Bombardment.java b/11 Bombardment/java/src/Bombardment.java new file mode 100644 index 00000000..06a3043c --- /dev/null +++ b/11 Bombardment/java/src/Bombardment.java @@ -0,0 +1,372 @@ +import java.util.HashSet; +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. + */ +public class Bombardment { + + public static final int MAX_GRID_SIZE = 25; + public static final int PLATOONS = 4; + + private enum GAME_STATE { + STARTING, + DRAW_BATTLEFIELD, + GET_PLAYER_CHOICES, + PLAYERS_TURN, + COMPUTER_TURN, + PLAYER_WON, + PLAYER_LOST, + GAME_OVER + } + + 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[] COMPUTER_HIT_MESSAGES = {"YOU HAVE ONLY THREE OUTPOSTS LEFT.", + "YOU HAVE ONLY TWO OUTPOSTS LEFT.", "YOU HAVE ONLY ONE OUTPOST LEFT." }; + + private HashSet computersPlatoons; + private HashSet playersPlatoons; + + private HashSet computersGuesses; + + // Used for keyboard input + private final Scanner kbScanner; + + public Bombardment() { + + this.gameState = GAME_STATE.STARTING; + + // Initialise kb scanner + kbScanner = new Scanner(System.in); + } + + /** + * Main game loop + */ + public void play() { + + do { + switch (this.gameState) { + + // Show an introduction the first time the game is played. + case STARTING: + init(); + intro(); + this.gameState = GAME_STATE.DRAW_BATTLEFIELD; + break; + + // Enter the players name + case DRAW_BATTLEFIELD: + drawBattlefield(); + this.gameState = GAME_STATE.GET_PLAYER_CHOICES; + break; + + // Get the players 4 locations for their platoons + case GET_PLAYER_CHOICES: + 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() { + + // Initialise contents + HashSet tempPlatoons = new HashSet<>(); + + boolean allPlatoonsAdded = false; + + do { + tempPlatoons.add(randomNumber()); + + // All four created? + if(tempPlatoons.size() == PLATOONS) { + // Exit when we have created four + allPlatoonsAdded = true; + } + + } while(!allPlatoonsAdded); + + return tempPlatoons; + } + /** + * Shows a different message for each number of hits + * + * @param hits total number of hits by player on computer + */ + private void showPlayerProgress(int hits) { + + System.out.println("YOU GOT ONE OF MY OUTPOSTS!"); + showProgress(hits, PLAYER_HIT_MESSAGES); + } + + /** + * Shows a different message for each number of hits + * + * @param hits total number of hits by computer on player + */ + private void showComputerProgress(int hits, int lastGuess) { + + System.out.println("I GOT YOU. IT WON'T BE LONG NOW. POST " + lastGuess + " WAS HIT."); + showProgress(hits, COMPUTER_HIT_MESSAGES); + } + + /** + * 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 messages - an array of string with messages + */ + private void showProgress(int hits, String[] messages) { + 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); + + // return number of hits in total + return PLATOONS - this.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); + + // return number of hits in total + return PLATOONS - this.playersPlatoons.size(); + } + + /** + * Determine if the player hit one of the computers platoons + * + * @param fireLocation the players choice of location to fire on + * @return true if a computer platoon was at that position + */ + private boolean didPlayerHitComputerPlatoon(int fireLocation) { + return this.computersPlatoons.contains(fireLocation); + } + + /** + * Determine if the computer hit one of the players platoons + * + * @param fireLocation the computers choice of location to fire on + * @return true if a players platoon was at that position + */ + private boolean didComputerHitPlayerPlatoon(int fireLocation) { + return this.playersPlatoons.contains(fireLocation); + } + + /** + * Draw the battlefield grid + * + */ + private void drawBattlefield() { + for(int i=1; i(); + + this.computersGuesses = new HashSet<>(); + } + + /** + * Accepts a string delimited by comma's and returns the nth delimited + * value (starting at count 0). + * + * @param text - text with values separated by comma's + * @param pos - which position to return a value for + * @return the int representation of the value + */ + private int getDelimitedValue(String text, int pos) { + String[] tokens = text.split(","); + return Integer.parseInt(tokens[pos]); + } + + + /* + * Print a message on the screen, then accept input from Keyboard. + * + * @param text message to be displayed on screen. + * @return what was typed by the player. + */ + private String displayTextAndGetInput(String text) { + System.out.print(text); + return kbScanner.next(); + } + + /** + * Generate random number + * + * @return random number + */ + private int randomNumber() { + return (int) (Math.random() + * (MAX_GRID_SIZE) + 1); + } +} \ No newline at end of file diff --git a/11 Bombardment/java/src/BombardmentGame.java b/11 Bombardment/java/src/BombardmentGame.java new file mode 100644 index 00000000..31f0edfe --- /dev/null +++ b/11 Bombardment/java/src/BombardmentGame.java @@ -0,0 +1,8 @@ +public class BombardmentGame { + + public static void main(String[] args) { + + Bombardment bombardment = new Bombardment(); + bombardment.play(); + } +}