Completed ShipWarfare

This commit is contained in:
KahootChampion
2019-04-11 17:52:24 -06:00
parent ff4dd2b733
commit c6b34f49b3
3 changed files with 54 additions and 31 deletions

View File

@@ -19,7 +19,6 @@ import javafx.util.Duration;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.util.Random; import java.util.Random;
import java.util.Scanner;
import logic.Player; import logic.Player;
import logic.ShipWarfareLogic; import logic.ShipWarfareLogic;

View File

@@ -2,6 +2,10 @@ package text;
import logic.Player; 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 class MainText extends Player {
public MainText(Player player) { public MainText(Player player) {
Player playerDummy = new Player(player); Player playerDummy = new Player(player);

View File

@@ -1,7 +1,9 @@
package text; package text;
import java.util.Scanner; import java.util.Scanner;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import logic.Player; import logic.Player;
import logic.ShipWarfareLogic; import logic.ShipWarfareLogic;
@@ -12,7 +14,6 @@ import logic.ShipWarfareLogic;
*/ */
public class ShipWarfareText extends Player { public class ShipWarfareText extends Player {
ShipWarfareLogic logic = new ShipWarfareLogic(getPlayer()); 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 * 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
*/ */
@@ -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 * 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
*/ */
@@ -109,25 +152,10 @@ public class ShipWarfareText extends Player {
while (exitValue == 0) { while (exitValue == 0) {
if (getGuns() > 0) { if (getGuns() > 0) {
for (int j = 0; j < getGuns(); j++) { for (int j = 0; j < getGuns(); j++) {
if (userAttacks == true) { attackOrNot(randomValue,exitValue);
int hitOrMiss = randomValue.nextInt(2) + 1; }
if (hitOrMiss == 2) { if (exitValue == 1) {
logic.setNumOfShips(logic.getNumOfShips() - 1); break;
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 { } 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());
@@ -144,16 +172,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()) { shipsRunOrWeEscape();
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);
}
}
} }
} }
@@ -212,6 +231,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