From 4ec469de7df0125598dfd3a9344ce4c224198a76 Mon Sep 17 00:00:00 2001
From: KahootChampion
Date: Thu, 11 Apr 2019 13:48:56 -0600
Subject: [PATCH] Added Javadocs for start in MainGUI and separated ShipWarfare
into more methods.
---
src/gui/MainGUI.java | 7 +-
src/gui/ShipWarfareGUI.java | 142 ++++++++++++-------------
src/logic/ShipWarfareLogic.java | 1 -
src/text/ShipWarfareText.java | 177 ++++++++++----------------------
4 files changed, 125 insertions(+), 202 deletions(-)
diff --git a/src/gui/MainGUI.java b/src/gui/MainGUI.java
index 5e4db43..a4757a5 100644
--- a/src/gui/MainGUI.java
+++ b/src/gui/MainGUI.java
@@ -16,14 +16,17 @@ public class MainGUI extends Application {
/**
* Updates main class with player data and starts the game.
* The game will only run as long as the player has not retired or has been destroyed.
- *
* @param args Just the console for the player to look at.
*/
public static void main(String[] args) {
launch(args);
}
- public void start(Stage primaryStage) throws Exception {
+ /**
+ * Starts the game as a whole, combines all of the other necessary classes
+ * @param primaryStage Creates new stage for scene to be utilized
+ */
+ public void start(Stage primaryStage){
StartGUI start = new StartGUI(new Player());
start.initializeStart(primaryStage);
primaryStage.show();
diff --git a/src/gui/ShipWarfareGUI.java b/src/gui/ShipWarfareGUI.java
index 78a55bb..31d3c81 100644
--- a/src/gui/ShipWarfareGUI.java
+++ b/src/gui/ShipWarfareGUI.java
@@ -16,10 +16,11 @@ import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
+
import java.io.FileInputStream;
import java.util.Random;
+import java.util.Scanner;
-//Importing the logic classes required for this class
import logic.Player;
import logic.ShipWarfareLogic;
@@ -69,6 +70,7 @@ public class ShipWarfareGUI extends Player {
/**
* constructor; only runs when a Player object is provided. The constructor is fully encapsulated.
+ *
* @param player is a Player object that will be copied and the player instance variable is set to the copy.
*/
public ShipWarfareGUI(Player player) {
@@ -86,6 +88,11 @@ public class ShipWarfareGUI extends Player {
}
+ /**
+ * Wipe many of the labels including title
+ *
+ * @param title for GUI
+ */
public void wipeWithTitle(Label title) {
title.setVisible(false);
runAwayOrLeft.setVisible(false);
@@ -102,9 +109,58 @@ public class ShipWarfareGUI extends Player {
report.setVisible(false);
}
+ /**
+ * Attacks the enemy fleet and sees how much shots miss and how much shots hit
+ *
+ * @param exitValue to distingusih if the player won, lost, or ran
+ * @param hitCounter to see how much of the shots hit the ships
+ * @param missCounter to see how much of the shots missed the ships
+ * @param random to utilize the random object
+ * @return an array of values
+ */
+ public int[] successfulHitOrNot(int exitValue, int hitCounter, int missCounter, Random random) {
+ int hitOrMiss = random.nextInt(2) + 1;
+
+ if (userAttacks && hitOrMiss == 2) {
+ if (logic.getNumOfShips() <= 0) {
+ exitValue = 1;
+ }
+ hitCounter++;
+ logic.setNumOfShips(logic.getNumOfShips() - 1);
+
+
+ } else {
+ missCounter++;
+ }
+
+ return new int[]{exitValue, hitCounter, missCounter};
+
+ }
+
+ /**
+ * Reports how many ships escape or run
+ */
+ public void howMuchEscapeOrRun() {
+ if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
+
+ logic.setNumOfShips(logic.getNumOfShips() - howMuchRun);
+ if (userAttacks == true) {
+ if (howMuchRun > 0) {
+ runAwayOrLeft.setText(String.format("Cowards! %d ships ran away %s! ", howMuchRun, super.getName()));
+ }
+
+ } else {
+ report.setText((String.format("Escaped %d of them %s!", howMuchRun, super.getName())));
+ }
+
+ }
+
+ }
+
/**
* The user faces off against the ships and either prevails, dies, or runs away
+ *
* @return true if the user wins, loses, or flees, it returns false otherwise
*/
public boolean destroyShipsOrEscape(Stage stage) throws Exception {
@@ -125,10 +181,10 @@ public class ShipWarfareGUI extends Player {
if (super.getGuns() > 0) {
for (int j = 0; j < super.getGuns(); j++) {
- PlayerAttacks playerAttacks = new PlayerAttacks(hitCounter, missCounter, randomValue, exitValue).invoke();
- hitCounter = playerAttacks.getHitCounter();
- missCounter = playerAttacks.getMissCounter();
- exitValue = playerAttacks.getExitValue();
+ int array[] = successfulHitOrNot(exitValue, hitCounter, missCounter, randomValue);
+ exitValue = array[0];
+ hitCounter = array[1];
+ missCounter = array[2];
}
if (userAttacks == true) {
report.setText(String.format("Report: Ships hit: %d, Shots missed: %d", hitCounter, missCounter));
@@ -139,8 +195,6 @@ public class ShipWarfareGUI extends Player {
}
-
-
if (logic.getNumOfShips() <= 0) {
exitValue = 1;
}
@@ -149,7 +203,8 @@ public class ShipWarfareGUI extends Player {
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1;
- shipsRunning();
+ howMuchEscapeOrRun();
+
}
}
@@ -209,23 +264,6 @@ public class ShipWarfareGUI extends Player {
}
- public void shipsRunning() {
- if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
-
-
- logic.setNumOfShips(logic.getNumOfShips() - howMuchRun);
- if (userAttacks == true) {
- if (howMuchRun > 0) {
- runAwayOrLeft.setText(String.format("Cowards! %d ships ran away %s! ", howMuchRun, super.getName()));
- }
-
- } else {
- report.setText((String.format("Escaped %d of them %s!", howMuchRun, super.getName())));
- }
-
- }
- }
-
/**
* Player attacks enemy ships in an animation
*/
@@ -263,6 +301,7 @@ public class ShipWarfareGUI extends Player {
/**
* Sets most buttons to being invisble and switches to TaipanShop scene
+ *
* @param stage stage the user incorporates when they utilize the GUI
*/
public void setVisibilitiesAndTransition(Stage stage) {
@@ -285,6 +324,7 @@ public class ShipWarfareGUI extends Player {
/**
* Generaties ships and deploys logic for the shipwarfare
+ *
* @param primaryStage sets up the stage to whcih the GUI may be based around
* @throws Exception in case of interruptions withing the graphical interface
*/
@@ -580,60 +620,8 @@ public class ShipWarfareGUI extends Player {
});
-
}
});
}
-
- public class PlayerAttacks {
- private int hitCounter;
- private int missCounter;
- private Random randomValue;
- private int exitValue;
-
- public PlayerAttacks(int hitCounter, int missCounter, Random randomValue, int exitValue) {
- this.hitCounter = hitCounter;
- this.missCounter = missCounter;
- this.randomValue = randomValue;
- this.exitValue = exitValue;
- }
-
- public int getHitCounter() {
- return hitCounter;
- }
-
- public int getMissCounter() {
- return missCounter;
- }
-
- public int getExitValue() {
- return exitValue;
- }
-
- public PlayerAttacks invoke() {
- if (userAttacks == true) {
-
- int hitOrMiss = randomValue.nextInt(2) + 1;
- if (hitOrMiss == 2) {
- logic.setNumOfShips(logic.getNumOfShips()-1);
-
- if (logic.getNumOfShips() <= 0) {
- exitValue = 1;
- }
- hitCounter++;
-
-
-
- } else {
- missCounter++;
-
- }
-
-
- }
- return this;
- }
- }
}
-
diff --git a/src/logic/ShipWarfareLogic.java b/src/logic/ShipWarfareLogic.java
index 14b0bc5..f253e53 100644
--- a/src/logic/ShipWarfareLogic.java
+++ b/src/logic/ShipWarfareLogic.java
@@ -1,5 +1,4 @@
package logic;
-
import java.util.Random;
/**
diff --git a/src/text/ShipWarfareText.java b/src/text/ShipWarfareText.java
index e7b6ac3..54eac84 100644
--- a/src/text/ShipWarfareText.java
+++ b/src/text/ShipWarfareText.java
@@ -1,11 +1,9 @@
package text;
-
-import logic.Player;
-import logic.ShipWarfareLogic;
-
import java.util.Scanner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
+import logic.Player;
+import logic.ShipWarfareLogic;
/**
* 2019-04-07
@@ -13,6 +11,8 @@ import java.util.concurrent.TimeUnit;
* ShipWarfareText class, Based on logic class for ShipWarfare, text-based version which allwos for ship warfare
*/
+
+
public class ShipWarfareText extends Player {
ShipWarfareLogic logic = new ShipWarfareLogic(getPlayer());
@@ -22,6 +22,7 @@ public class ShipWarfareText extends Player {
/**
* Class Constructor that takes in a type player as a parameter
+ *
* @param player object of the class Player
*/
public ShipWarfareText(Player player) {
@@ -32,6 +33,7 @@ public class ShipWarfareText extends Player {
/**
* This fleet is easy to defeat as a maximum of 15 ships can run away each volley, they can not tank hits
+ *
* @throws Exception in case of errors due to the delay
*/
public void peasantFleetAttack() throws Exception {
@@ -56,11 +58,10 @@ public class ShipWarfareText extends Player {
} else if (response.equalsIgnoreCase("r")) {
userAttacks = false;
boolean runSuccess = logic.runFromShips();
- if(runSuccess == false){
+ if (runSuccess == false) {
System.out.println("Couldn't run away!");
- }
- if (runSuccess == false && destroyPeasantShipsOrEscape()) {
- break;
+ if (destroyPeasantShipsOrEscape())
+ break;
} else {
System.out.println("Phew! Got away safely");
delayForSeconds(2);
@@ -106,7 +107,33 @@ public class ShipWarfareText extends Player {
//Player volley
while (exitValue == 0) {
- exitValue = hitEnemy(randomValue, exitValue);
+ if (getGuns() > 0) {
+ for (int j = 0; j < getGuns(); j++) {
+ if (userAttacks == true) {
+ int hitOrMiss = randomValue.nextInt(2) + 1;
+ if (hitOrMiss == 2) {
+ logic.setNumOfShips(logic.getNumOfShips() - 1);
+ if (logic.getNumOfShips() <= 0) {
+ exitValue = 1;
+ break;
+ }
+ System.out.println("Got eem");
+ delayForSeconds(1);
+ } else {
+ System.out.printf("ARRG! We missed %s\n", getName());
+ delayForSeconds(1);
+ }
+
+
+ } else {
+ continue;
+ }
+ }
+ } else {
+ System.out.printf("%s! We don't have any GUNS!!!!\n", getName());
+ delayForSeconds(1);
+
+ }
if (logic.getNumOfShips() <= 0) {
@@ -117,7 +144,16 @@ public class ShipWarfareText extends Player {
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1;
- shipsRun();
+ if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
+
+
+ logic.setNumOfShips(logic.getNumOfShips() - howMuchRun);
+ if (userAttacks == true) {
+ System.out.printf("Ahhh, %d ships ran away %s!\n", howMuchRun, getName());
+ } else {
+ System.out.printf("Escaped %d of them!\n", howMuchRun);
+ }
+ }
}
}
@@ -135,9 +171,15 @@ public class ShipWarfareText extends Player {
}
if (getHP() > 0) {
String userResponse = displayQuery(userInput);
- TryRunning tryRunning = new TryRunning(exitValue, userResponse).invoke();
- if (tryRunning.is()) break;
- exitValue = tryRunning.getExitValue();
+ if (userResponse.equalsIgnoreCase("r")) {
+ userAttacks = false;
+ if (logic.runFromShips() == false) {
+ System.out.println("Couldn't run away");
+ } else {
+ exitValue = 3;
+ break;
+ }
+ }
} else {
exitValue = 2;
@@ -168,35 +210,6 @@ public class ShipWarfareText extends Player {
}
- public int hitEnemy(Random randomValue, int exitValue) throws Exception {
- if (getGuns() > 0) {
-
- for (int j = 0; j < getGuns(); j++) {
- UserKills userKills = new UserKills(randomValue).invoke();
- if (userKills.is()) break;
- exitValue = userKills.getExitValue();
- }
- } else {
- System.out.printf("%s! We don't have any GUNS!!!!\n", getName());
- delayForSeconds(1);
-
- }
- return exitValue;
- }
-
- public void shipsRun() {
- if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
-
-
- logic.setNumOfShips(logic.getNumOfShips() - howMuchRun);
- if (userAttacks == true) {
- System.out.printf("Ahhh, %d ships ran away %s!\n", howMuchRun, getName());
- } else {
- System.out.printf("Escaped %d of them!\n", howMuchRun);
- }
- }
- }
-
/**
* Ask the user to input either "f" or "r"
* @param userInput scanner object which is used to ask for user input
@@ -220,84 +233,4 @@ public class ShipWarfareText extends Player {
return response;
}
- public class UserKills {
- private boolean myResult;
- private Random randomValue;
- private int exitValue;
-
- public UserKills(Random randomValue) {
- this.randomValue = randomValue;
- }
-
- boolean is() {
- return myResult;
- }
-
- public int getExitValue() {
- return exitValue;
- }
-
- public UserKills invoke() throws Exception {
- if (userAttacks == true) {
- int hitOrMiss = randomValue.nextInt(2) + 1;
- if (hitOrMiss == 2) {
- logic.setNumOfShips(logic.getNumOfShips() - 1);
- if (logic.getNumOfShips() <= 0) {
- exitValue = 1;
- myResult = true;
- return this;
- }
- System.out.println("Got eem");
- delayForSeconds(1);
- } else {
- System.out.printf("ARRG! We missed %s\n", getName());
- delayForSeconds(1);
- }
-
-
- }
- myResult = false;
- return this;
- }
- }
-
- private class TryRunning {
- private boolean myResult;
- private int exitValue;
- private String userResponse;
-
- public TryRunning(int exitValue, String userResponse) {
- this.exitValue = exitValue;
- this.userResponse = userResponse;
- }
-
- boolean is() {
- return myResult;
- }
-
- public int getExitValue() {
- return exitValue;
- }
-
- public TryRunning invoke() {
- if (userResponse.equalsIgnoreCase("r")) {
- userAttacks = false;
- if (logic.runFromShips() == false) {
- System.out.println("Couldn't run away");
- } else {
- exitValue = 3;
- myResult = true;
- return this;
- }
- }
- myResult = false;
- return this;
- }
- }
-
- public static void main(String[] args) throws Exception {
- ShipWarfareText test = new ShipWarfareText(new Player());
- test.peasantFleetAttack();
-
- }
}
\ No newline at end of file