From 784272e36219cea796c27d8fd7661a1cc16f9bbf Mon Sep 17 00:00:00 2001
From: KahootChampion
Date: Mon, 8 Apr 2019 12:46:55 -0600
Subject: [PATCH] Removed some layerings from SHipWarfare related classes
---
src/gui/ShipWarfareGUI.java | 106 ++++++++++++++-------
src/text/ShipWarfareText.java | 170 +++++++++++++++++++++++-----------
2 files changed, 190 insertions(+), 86 deletions(-)
diff --git a/src/gui/ShipWarfareGUI.java b/src/gui/ShipWarfareGUI.java
index cd64007..78a55bb 100644
--- a/src/gui/ShipWarfareGUI.java
+++ b/src/gui/ShipWarfareGUI.java
@@ -125,26 +125,10 @@ public class ShipWarfareGUI extends Player {
if (super.getGuns() > 0) {
for (int j = 0; j < super.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;
- }
- hitCounter++;
-
-
-
- } else {
- missCounter++;
-
- }
-
-
- }
+ PlayerAttacks playerAttacks = new PlayerAttacks(hitCounter, missCounter, randomValue, exitValue).invoke();
+ hitCounter = playerAttacks.getHitCounter();
+ missCounter = playerAttacks.getMissCounter();
+ exitValue = playerAttacks.getExitValue();
}
if (userAttacks == true) {
report.setText(String.format("Report: Ships hit: %d, Shots missed: %d", hitCounter, missCounter));
@@ -165,20 +149,7 @@ public class ShipWarfareGUI 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) {
- 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())));
- }
-
- }
+ shipsRunning();
}
}
@@ -238,6 +209,23 @@ 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
*/
@@ -597,5 +585,55 @@ 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/text/ShipWarfareText.java b/src/text/ShipWarfareText.java
index 552856c..e7b6ac3 100644
--- a/src/text/ShipWarfareText.java
+++ b/src/text/ShipWarfareText.java
@@ -22,7 +22,6 @@ 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) {
@@ -33,7 +32,6 @@ 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 {
@@ -58,10 +56,11 @@ 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 (destroyPeasantShipsOrEscape())
- break;
+ }
+ if (runSuccess == false && destroyPeasantShipsOrEscape()) {
+ break;
} else {
System.out.println("Phew! Got away safely");
delayForSeconds(2);
@@ -107,34 +106,7 @@ public class ShipWarfareText extends Player {
//Player volley
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;
- }
- }
- } else {
- System.out.printf("%s! We don't have any GUNS!!!!\n", getName());
- delayForSeconds(1);
-
- }
+ exitValue = hitEnemy(randomValue, exitValue);
if (logic.getNumOfShips() <= 0) {
@@ -145,16 +117,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);
- }
- }
+ shipsRun();
}
}
@@ -172,15 +135,9 @@ public class ShipWarfareText extends Player {
}
if (getHP() > 0) {
String userResponse = displayQuery(userInput);
- if (userResponse.equalsIgnoreCase("r")) {
- userAttacks = false;
- if (logic.runFromShips() == false) {
- System.out.println("Couldn't run away");
- } else {
- exitValue = 3;
- break;
- }
- }
+ TryRunning tryRunning = new TryRunning(exitValue, userResponse).invoke();
+ if (tryRunning.is()) break;
+ exitValue = tryRunning.getExitValue();
} else {
exitValue = 2;
@@ -211,6 +168,35 @@ 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
@@ -234,4 +220,84 @@ 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