From 9660676b7acef74a3fd6765201ae38c5585691ba Mon Sep 17 00:00:00 2001 From: Tim <70119791+journich@users.noreply.github.com> Date: Thu, 18 Feb 2021 16:53:09 +1030 Subject: [PATCH] Java version of Bullseye --- 18 Bullseye/java/src/Bullseye.java | 248 +++++++++++++++++++++++++ 18 Bullseye/java/src/BullseyeGame.java | 8 + 18 Bullseye/java/src/Player.java | 26 +++ 18 Bullseye/java/src/Shot.java | 21 +++ 4 files changed, 303 insertions(+) create mode 100644 18 Bullseye/java/src/Bullseye.java create mode 100644 18 Bullseye/java/src/BullseyeGame.java create mode 100644 18 Bullseye/java/src/Player.java create mode 100644 18 Bullseye/java/src/Shot.java diff --git a/18 Bullseye/java/src/Bullseye.java b/18 Bullseye/java/src/Bullseye.java new file mode 100644 index 00000000..06090351 --- /dev/null +++ b/18 Bullseye/java/src/Bullseye.java @@ -0,0 +1,248 @@ +import java.util.ArrayList; +import java.util.Scanner; + +public class Bullseye { + + public static final int FIRST_IDENT = 10; + public static final int SECOND_IDENT = 30; + public static final int THIRD_INDENT = 30; + + public static final double[] SHOT_ONE = new double[] { .65, .55, .5, .5}; + public static final double[] SHOT_TWO = new double[] { .99, .77, .43,.01}; + public static final double[] SHOT_THREE = new double[] { .95, .75, .45, .05 }; + + private enum GAME_STATE { + STARTING, + START_GAME, + PLAYING, + GAME_OVER + } + + private GAME_STATE gameState; + + private ArrayList players; + + private Shot[] shots; + + // Used for keyboard input + private Scanner kbScanner; + + private int numberOfPlayers; + + private int round; + + public Bullseye() { + + gameState = GAME_STATE.STARTING; + players = new ArrayList<>(); + + // Save the random chances of points based on shot type + + shots = new Shot[3]; + shots[0] = new Shot(SHOT_ONE); + shots[1] = new Shot(SHOT_TWO); + shots[2] = new Shot(SHOT_THREE); + + // Initialise kb scanner + kbScanner = new Scanner(System.in); + } + + /** + * Main game loop + * + */ + public void play() { + + do { + switch (gameState) { + + // Show an introduction the first time the game is played. + case STARTING: + intro(); + gameState = GAME_STATE.START_GAME; + break; + + // Start the game, set the number of players, names and round + case START_GAME: + + this.numberOfPlayers = chooseNumberOfPlayers(); + + for(int i=0; i= p1) { + System.out.println("BULLSEYE!! 40 POINTS!"); + points = 40; + // If the throw was 1 (bullseye or missed, then make it missed + // N.B. This is a fix for the basic code which for shot type 1 + // allowed a bullseye but did not make the score zero if a bullseye + // was not made (which it should have done). + } else if (playerThrow == 1) { + System.out.println("MISSED THE TARGET! TOO BAD."); + points = 0; + } else if(random >= p2) { + System.out.println("30-POINT ZONE!"); + points = 30; + } else if(random >= p3) { + System.out.println("20-POINT ZONE"); + points = 20; + } else if(random >= p4) { + System.out.println("WHEW! 10 POINTS."); + points = 10; + } else { + System.out.println("MISSED THE TARGET! TOO BAD."); + points = 0; + } + + return points; + } + + /** + * Get players shot 1,2, or 3 - ask again if invalid input + * + * @param player + * @return 1,2, or 3 indicating the players shot + */ + private int getPlayersThrow(Player player) { + boolean inputCorrect = false; + String theThrow; + do { + theThrow = displayTextAndGetInput(player.getName() + "'S THROW "); + if(theThrow.equals("1") || theThrow.equals("2") || theThrow.equals("3")) { + inputCorrect = true; + } else { + System.out.println("INPUT 1, 2, OR 3!"); + } + + } while(!inputCorrect); + + return Integer.valueOf(theThrow); + } + + + /** + * Get players guess from kb + * + * @return players guess as an int + */ + private int chooseNumberOfPlayers() { + + return Integer.valueOf((displayTextAndGetInput("HOW MANY PLAYERS? "))); + } + /* + * 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(); + } + + /** + * Format three strings to a given number of spaces + * Replacing the original basic code which used tabs + * + * @param first String to print in pos 1 + * @param second String to print in pos 2 + * @param third String to print in pos 3 + * @return formatted string + */ + private String paddedString(String first, String second, String third) { + String output = String.format("%1$" + FIRST_IDENT + "s", first); + output += String.format("%1$" + SECOND_IDENT + "s", second); + output += String.format("%1$" + THIRD_INDENT + "s", third); + return output; + } +} diff --git a/18 Bullseye/java/src/BullseyeGame.java b/18 Bullseye/java/src/BullseyeGame.java new file mode 100644 index 00000000..19be8fec --- /dev/null +++ b/18 Bullseye/java/src/BullseyeGame.java @@ -0,0 +1,8 @@ +public class BullseyeGame { + + public static void main(String[] args) { + + Bullseye bullseye = new Bullseye(); + bullseye.play(); + } +} diff --git a/18 Bullseye/java/src/Player.java b/18 Bullseye/java/src/Player.java new file mode 100644 index 00000000..b67b9309 --- /dev/null +++ b/18 Bullseye/java/src/Player.java @@ -0,0 +1,26 @@ +/** + * A Player in the game - consists of name and score + * + */ +public class Player { + + private String name; + private int score; + + Player(String name) { + this.name = name; + this.score = 0; + } + + public void addScore(int score) { + this.score += score; + } + + public String getName() { + return name; + } + + public int getScore() { + return score; + } +} diff --git a/18 Bullseye/java/src/Shot.java b/18 Bullseye/java/src/Shot.java new file mode 100644 index 00000000..01d8b283 --- /dev/null +++ b/18 Bullseye/java/src/Shot.java @@ -0,0 +1,21 @@ +/** + * This class records the percentage chance of a given type of shot + * scoring specific points + * see Bullseye class points calculation method where its used + */ +public class Shot { + + double[] chances; + + // Array of doubles are passed for a specific type of shot + Shot(double[] shots) { + chances = new double[shots.length]; + for(int i=0; i