From c6b34f49b37a6724eba2c698b0ad719b480a6aee Mon Sep 17 00:00:00 2001
From: KahootChampion
Date: Thu, 11 Apr 2019 17:52:24 -0600
Subject: [PATCH] Completed ShipWarfare
---
src/gui/ShipWarfareGUI.java | 1 -
src/text/MainText.java | 4 ++
src/text/ShipWarfareText.java | 80 ++++++++++++++++++++++-------------
3 files changed, 54 insertions(+), 31 deletions(-)
diff --git a/src/gui/ShipWarfareGUI.java b/src/gui/ShipWarfareGUI.java
index 31d3c81..fa46b0b 100644
--- a/src/gui/ShipWarfareGUI.java
+++ b/src/gui/ShipWarfareGUI.java
@@ -19,7 +19,6 @@ import javafx.util.Duration;
import java.io.FileInputStream;
import java.util.Random;
-import java.util.Scanner;
import logic.Player;
import logic.ShipWarfareLogic;
diff --git a/src/text/MainText.java b/src/text/MainText.java
index 140de8d..2c80173 100644
--- a/src/text/MainText.java
+++ b/src/text/MainText.java
@@ -2,6 +2,10 @@ package text;
import logic.Player;
+/**
+ * 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.
+ */
public class MainText extends Player {
public MainText(Player player) {
Player playerDummy = new Player(player);
diff --git a/src/text/ShipWarfareText.java b/src/text/ShipWarfareText.java
index 54eac84..01b25cc 100644
--- a/src/text/ShipWarfareText.java
+++ b/src/text/ShipWarfareText.java
@@ -1,7 +1,9 @@
package text;
+
import java.util.Scanner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
+
import logic.Player;
import logic.ShipWarfareLogic;
@@ -12,7 +14,6 @@ import logic.ShipWarfareLogic;
*/
-
public class ShipWarfareText extends Player {
ShipWarfareLogic logic = new ShipWarfareLogic(getPlayer());
@@ -83,6 +84,7 @@ public class ShipWarfareText extends Player {
/**
* delays for a specific amount of seconds, takes an integer as an argument
+ *
* @param num the seconds to delay
* @throws Exception in case of errors due to the delay
*/
@@ -91,8 +93,49 @@ public class ShipWarfareText extends Player {
}
+ /**
+ * Ships either land a hit or do not
+ * @param random random object is used
+ * @param exitValue exit in order to know if user wins, loses, or flees
+ * @throws Exception in case error is caused due to the delay
+ */
+ public void attackOrNot(Random random, int exitValue) throws Exception {
+ if (userAttacks == true) {
+ int hitOrMiss = random.nextInt(2) + 1;
+ if (hitOrMiss == 2) {
+ logic.setNumOfShips(logic.getNumOfShips() - 1);
+ if (logic.getNumOfShips() <= 0) {
+ exitValue = 1;
+ }
+ System.out.println("Got eem");
+ delayForSeconds(1);
+ } else {
+ System.out.printf("ARRG! We missed %s\n", getName());
+ delayForSeconds(1);
+ }
+
+
+ }
+ }
+
+ /**
+ * Ships either run away or we escape
+ */
+ public void shipsRunOrWeEscape(){
+ 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);
+ }
+ }
+ }
+
+
/**
* The user faces off against the peasant ships and either prevails, dies, or runs away
+ *
* @return true if the user wins, loses, or flees, it returns false otherwise
* @throws Exception in case of errors due to the delay
*/
@@ -109,25 +152,10 @@ public class ShipWarfareText extends Player {
while (exitValue == 0) {
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;
- }
+ attackOrNot(randomValue,exitValue);
+ }
+ if (exitValue == 1) {
+ break;
}
} else {
System.out.printf("%s! We don't have any GUNS!!!!\n", getName());
@@ -144,16 +172,7 @@ public class ShipWarfareText extends Player {
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1;
- 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);
- }
- }
+ shipsRunOrWeEscape();
}
}
@@ -212,6 +231,7 @@ public class ShipWarfareText extends Player {
/**
* Ask the user to input either "f" or "r"
+ *
* @param userInput scanner object which is used to ask for user input
* @return user input which is the users response
* @throws Exception in case the delay afects this piece of code