Removed some layerings from SHipWarfare related classes
This commit is contained in:
@@ -125,26 +125,10 @@ public class ShipWarfareGUI extends Player {
|
|||||||
if (super.getGuns() > 0) {
|
if (super.getGuns() > 0) {
|
||||||
|
|
||||||
for (int j = 0; j < super.getGuns(); j++) {
|
for (int j = 0; j < super.getGuns(); j++) {
|
||||||
if (userAttacks == true) {
|
PlayerAttacks playerAttacks = new PlayerAttacks(hitCounter, missCounter, randomValue, exitValue).invoke();
|
||||||
|
hitCounter = playerAttacks.getHitCounter();
|
||||||
int hitOrMiss = randomValue.nextInt(2) + 1;
|
missCounter = playerAttacks.getMissCounter();
|
||||||
if (hitOrMiss == 2) {
|
exitValue = playerAttacks.getExitValue();
|
||||||
logic.setNumOfShips(logic.getNumOfShips()-1);
|
|
||||||
|
|
||||||
if (logic.getNumOfShips() <= 0) {
|
|
||||||
exitValue = 1;
|
|
||||||
}
|
|
||||||
hitCounter++;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
|
||||||
missCounter++;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (userAttacks == true) {
|
if (userAttacks == true) {
|
||||||
report.setText(String.format("Report: Ships hit: %d, Shots missed: %d", hitCounter, missCounter));
|
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;
|
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
|
||||||
if (chanceOfEnemyRun == 2) {
|
if (chanceOfEnemyRun == 2) {
|
||||||
howMuchRun = randomValue.nextInt(15) + 1;
|
howMuchRun = randomValue.nextInt(15) + 1;
|
||||||
if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
|
shipsRunning();
|
||||||
|
|
||||||
|
|
||||||
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())));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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
|
* 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ public class ShipWarfareText extends Player {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Constructor that takes in a type player as a parameter
|
* Class Constructor that takes in a type player as a parameter
|
||||||
*
|
|
||||||
* @param player object of the class Player
|
* @param player object of the class Player
|
||||||
*/
|
*/
|
||||||
public ShipWarfareText(Player 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
|
* 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
|
* @throws Exception in case of errors due to the delay
|
||||||
*/
|
*/
|
||||||
public void peasantFleetAttack() throws Exception {
|
public void peasantFleetAttack() throws Exception {
|
||||||
@@ -58,10 +56,11 @@ public class ShipWarfareText extends Player {
|
|||||||
} else if (response.equalsIgnoreCase("r")) {
|
} else if (response.equalsIgnoreCase("r")) {
|
||||||
userAttacks = false;
|
userAttacks = false;
|
||||||
boolean runSuccess = logic.runFromShips();
|
boolean runSuccess = logic.runFromShips();
|
||||||
if (runSuccess == false) {
|
if(runSuccess == false){
|
||||||
System.out.println("Couldn't run away!");
|
System.out.println("Couldn't run away!");
|
||||||
if (destroyPeasantShipsOrEscape())
|
}
|
||||||
break;
|
if (runSuccess == false && destroyPeasantShipsOrEscape()) {
|
||||||
|
break;
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Phew! Got away safely");
|
System.out.println("Phew! Got away safely");
|
||||||
delayForSeconds(2);
|
delayForSeconds(2);
|
||||||
@@ -107,34 +106,7 @@ public class ShipWarfareText extends Player {
|
|||||||
|
|
||||||
//Player volley
|
//Player volley
|
||||||
while (exitValue == 0) {
|
while (exitValue == 0) {
|
||||||
if (getGuns() > 0) {
|
exitValue = hitEnemy(randomValue, exitValue);
|
||||||
|
|
||||||
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) {
|
if (logic.getNumOfShips() <= 0) {
|
||||||
@@ -145,16 +117,7 @@ public class ShipWarfareText extends Player {
|
|||||||
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
|
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
|
||||||
if (chanceOfEnemyRun == 2) {
|
if (chanceOfEnemyRun == 2) {
|
||||||
howMuchRun = randomValue.nextInt(15) + 1;
|
howMuchRun = randomValue.nextInt(15) + 1;
|
||||||
if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
|
shipsRun();
|
||||||
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,15 +135,9 @@ public class ShipWarfareText extends Player {
|
|||||||
}
|
}
|
||||||
if (getHP() > 0) {
|
if (getHP() > 0) {
|
||||||
String userResponse = displayQuery(userInput);
|
String userResponse = displayQuery(userInput);
|
||||||
if (userResponse.equalsIgnoreCase("r")) {
|
TryRunning tryRunning = new TryRunning(exitValue, userResponse).invoke();
|
||||||
userAttacks = false;
|
if (tryRunning.is()) break;
|
||||||
if (logic.runFromShips() == false) {
|
exitValue = tryRunning.getExitValue();
|
||||||
System.out.println("Couldn't run away");
|
|
||||||
} else {
|
|
||||||
exitValue = 3;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
exitValue = 2;
|
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"
|
* Ask the user to input either "f" or "r"
|
||||||
* @param userInput scanner object which is used to ask for user input
|
* @param userInput scanner object which is used to ask for user input
|
||||||
@@ -234,4 +220,84 @@ public class ShipWarfareText extends Player {
|
|||||||
return response;
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user