ShipWarfareText Complete Haris Muhammad

This commit is contained in:
KahootChampion
2019-04-07 14:06:25 -06:00
parent fff5456b45
commit f834afb98f
2 changed files with 18 additions and 25 deletions

View File

@@ -46,13 +46,12 @@ public class ShipWarfareLogic extends Player {
*/ */
public boolean runFromShips() { public boolean runFromShips() {
Random randomValue = new Random(); Random randomValue = new Random();
int runSuccessChance = randomValue.nextInt(10) + 1; int runSuccessChance = randomValue.nextInt(5) + 1;
if (runSuccessChance == 2) { if (runSuccessChance == 2) {
return true; return true;
} else if (runSuccessChance == 1) { } else {
return false; return false;
} }
return false;
} }
/** /**
@@ -99,7 +98,4 @@ public class ShipWarfareLogic extends Player {
} }

View File

@@ -9,8 +9,10 @@ public class ShipWarfareText extends Player {
private boolean userAttacks = true; private boolean userAttacks = true;
private int howMuchRun = 0; private int howMuchRun = 0;
/** /**
* 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) {
@@ -21,6 +23,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 * 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 {
@@ -49,22 +52,18 @@ public class ShipWarfareText extends Player {
System.out.println("Couldn't run away!"); System.out.println("Couldn't run away!");
if (destroyPeasantShipsOrEscape()) if (destroyPeasantShipsOrEscape())
break; break;
}else { } else {
System.out.println("Phew! Got away safely"); System.out.println("Phew! Got away safely");
delayForSeconds(2); delayForSeconds(2);
break; break;
} }
}else{ } else {
System.out.println("Invalid response, please try again"); System.out.println("Invalid response, please try again");
} }
}
} }
}
/** /**
@@ -77,9 +76,9 @@ public class ShipWarfareText extends Player {
} }
/** /**
* delays for a specific amount of seconds, takes an integer as an argument * delays for a specific amount of seconds, takes an integer as an argument
*
* @param num the seconds to delay * @param num the seconds to delay
* @throws Exception in case of errors due to the delay * @throws Exception in case of errors due to the delay
*/ */
@@ -88,9 +87,9 @@ public class ShipWarfareText extends Player {
} }
/** /**
* The user faces off against the peasant ships and either prevails, dies, or runs away * 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 * @return true if the user wins, loses, or flees, it returns false otherwise
* @throws Exception in case of errors due to the delay * @throws Exception in case of errors due to the delay
*/ */
@@ -112,7 +111,7 @@ public class ShipWarfareText extends Player {
if (userAttacks == true) { if (userAttacks == true) {
int hitOrMiss = randomValue.nextInt(2) + 1; int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) { if (hitOrMiss == 2) {
logic.setNumOfShips(logic.getNumOfShips()-1); logic.setNumOfShips(logic.getNumOfShips() - 1);
if (logic.getNumOfShips() <= 0) { if (logic.getNumOfShips() <= 0) {
exitValue = 1; exitValue = 1;
break; break;
@@ -129,8 +128,7 @@ public class ShipWarfareText extends Player {
continue; continue;
} }
} }
} } else {
else{
System.out.printf("%s! We don't have any GUNS!!!!\n", getName()); System.out.printf("%s! We don't have any GUNS!!!!\n", getName());
delayForSeconds(1); delayForSeconds(1);
@@ -173,7 +171,7 @@ public class ShipWarfareText extends Player {
if (getHP() > 0) { if (getHP() > 0) {
String userResponse = displayQuery(userInput); String userResponse = displayQuery(userInput);
if (userResponse.equalsIgnoreCase("r")) { if (userResponse.equalsIgnoreCase("r")) {
userAttacks=false; userAttacks = false;
if (logic.runFromShips() == false) { if (logic.runFromShips() == false) {
System.out.println("Couldn't run away"); System.out.println("Couldn't run away");
} else { } else {
@@ -181,9 +179,7 @@ public class ShipWarfareText extends Player {
break; break;
} }
} }
else if(userResponse.equalsIgnoreCase("f")){
}
} else { } else {
exitValue = 2; exitValue = 2;
break; break;
@@ -203,8 +199,7 @@ public class ShipWarfareText extends Player {
} else if (exitValue == 2) { } else if (exitValue == 2) {
gameOver(); gameOver();
return true; return true;
} } else if (exitValue == 3) {
else if (exitValue == 3) {
System.out.printf("We made it out at %d%% ship status!\n", getHP()); System.out.printf("We made it out at %d%% ship status!\n", getHP());
delayForSeconds(2); delayForSeconds(2);
return true; return true;
@@ -216,6 +211,7 @@ public class ShipWarfareText extends Player {
/** /**
* 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
* @return user input which is the users response * @return user input which is the users response
* @throws Exception in case the delay afects this piece of code * @throws Exception in case the delay afects this piece of code
@@ -230,9 +226,10 @@ public class ShipWarfareText extends Player {
System.out.printf("Shall we continue to fight? Enter \"f\" to fight, and \"r\" to run (We have %d gun(s) left)\n", getGuns()); System.out.printf("Shall we continue to fight? Enter \"f\" to fight, and \"r\" to run (We have %d gun(s) left)\n", getGuns());
String response = userInput.nextLine(); String response = userInput.nextLine();
while(!(response.equalsIgnoreCase("f") || response.equalsIgnoreCase("r"))) while (!(response.equalsIgnoreCase("f") || response.equalsIgnoreCase("r"))) {
System.out.println("Invalid response, try again"); System.out.println("Invalid response, try again");
response=userInput.nextLine(); response = userInput.nextLine();
}
return response; return response;
} }