Fixed Instance variables being used directly inside ship warfare and replaced them with set methods, also asked for userInput as to whether to fight or retreat.

This commit is contained in:
KahootChampion
2019-02-17 16:46:03 -07:00
parent 66a97330b6
commit 525fce5fbc

View File

@@ -1,23 +1,44 @@
//AYYYYYYYYYY
package TeamProject; package TeamProject;
import java.util.InputMismatchException;
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;
public class ShipWarfare { public class ShipWarfare {
private int money = 0; private int money = 100000000;
private int guns = 3; private int guns = 3;
private int hp = 100; private int hp = 100;
private int numOfPeasantShips= 0; private int numOfPeasantShips= 0;
public void peasantFleetAttack() throws Exception{ public void peasantFleetAttack() throws Exception{
Scanner userResponse = new Scanner(System.in); Scanner userResponse = new Scanner(System.in);
numOfPeasantShips=numOfShips(); setNumOfPeasantShips(numOfShips());
System.out.printf("By Golly! You have $%,d \nYou are being attacked by %d ships\n", getMoney(), getNumOfPeasantShips()); System.out.printf("By Golly! We have $%,d \nwe are being attacked by %d ships\n", getMoney(), getNumOfPeasantShips());
System.out.println("What do you want to do?"); System.out.println("What do you want to do? Press \"f\" to fight, and \"r\" to run ");
System.out.println("Ohh, fight ehh?"); while(true) {
fightShips(getNumOfPeasantShips()); try {
String response = userResponse.nextLine();
if (response.equalsIgnoreCase("f")) {
System.out.println("Ohh, fight ehh?");
fightShips(getNumOfPeasantShips());
}
else if(response.equalsIgnoreCase("r")){
}
} catch (InputMismatchException e) {
String response;
System.out.println("Sorry, that is not an acceptable input please try again");
response = userResponse.nextLine();
if (response.equalsIgnoreCase("f") || response.equalsIgnoreCase("r"))
break;
}
}
} }
@@ -37,6 +58,22 @@ public class ShipWarfare {
return numOfPeasantShips; return numOfPeasantShips;
} }
public void setHp(int hp) {
this.hp = hp;
}
public void setMoney(int money) {
this.money = money;
}
public void setGuns(int guns) {
this.guns = guns;
}
public void setNumOfPeasantShips(int numOfPeasantShips) {
this.numOfPeasantShips = numOfPeasantShips;
}
public void delayForASecond() throws Exception { public void delayForASecond() throws Exception {
TimeUnit.SECONDS.sleep(1); TimeUnit.SECONDS.sleep(1);
} }
@@ -75,6 +112,9 @@ public class ShipWarfare {
} }
public void fightShips(int typeOfShip) throws Exception { public void fightShips(int typeOfShip) throws Exception {
Random randomValue = new Random(); Random randomValue = new Random();
@@ -112,12 +152,12 @@ public class ShipWarfare {
System.out.println("Oh no, they are taking the offensive!"); System.out.println("Oh no, they are taking the offensive!");
delayForASecond(); delayForASecond();
//Computer volley //Computer volley
hp -= randomValue.nextInt(10); setHp(getHp() - randomValue.nextInt(10));
if(getHp()<=0){ if(getHp()<=0){
exitValue=2; exitValue=2;
break; break;
} }
System.out.printf("EEk, you have %d health left\n", getHp()); System.out.printf("EEK, we have %d health left\n", getHp());
delayForASecond(); delayForASecond();
} }