Update ShipWarfare.java

This commit is contained in:
Vikramb987
2019-02-21 22:08:11 -07:00
committed by GitHub
parent 0a93de3354
commit 1540323985

View File

@@ -2,16 +2,32 @@ import java.util.Scanner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class ShipWarfare extends Player {
public class ShipWarfare{
private int numOfPeasantShips = 0;
private boolean userAttacks = true;
private Player player;
public ShipWarfare(Player player){
Player playerDummy = new Player(player);
this.player = playerDummy;
}
public void setPlayer(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
public Player getPlayer(){
Player playerDummy = new Player(player);
return playerDummy;
}
public void peasantFleetAttack() throws Exception {
Scanner userResponse = new Scanner(System.in);
setNumOfPeasantShips(numOfShips());
System.out.printf("By Golly! We have $%,d and are being attacked by %d ships\nCurrently our ship status is %d%%\n", getMoney(), getNumOfPeasantShips(),getHP());
System.out.printf("By Golly! We have $%,d and are being attacked by %d ships\nCurrently our ship status is %d%%\n", player.getMoney(), getNumOfPeasantShips(),player.getHP());
System.out.printf("By Golly! We have $%,d and are being attacked by %d ships\nCurrently our ship status is %d%%\n", player.getMoney(), getNumOfPeasantShips(),player.getHP());
fightOrRunMessage();
while (true) {
String response = userResponse.nextLine();
@@ -43,7 +59,8 @@ public class ShipWarfare extends Player {
public void fightOrRunMessage() {
System.out.printf("What do you want to do? Enter \"f\" to fight, and \"r\" to run (we have %d guns)", getGuns());
System.out.printf("What do you want to do? Enter \"f\" to fight, and \"r\" to run (we have %d guns)", player.getGuns());
System.out.printf("What do you want to do? Enter \"f\" to fight, and \"r\" to run (we have %d guns)", player.getGuns());
}
@@ -65,16 +82,16 @@ public class ShipWarfare extends Player {
int numOfShipsAttacking = 0;
Random randomValue = new Random();
if (getMoney() <= 100000) {
if (player.getMoney() <= 100000) {
//Minimum one ship will attack, maximum 20
numOfShipsAttacking = randomValue.nextInt(20) + 1;
} else if (getMoney() <= 200000) {
} else if (player.getMoney() <= 200000) {
//Minimum 30 Ships will attack, maximum 70
numOfShipsAttacking = randomValue.nextInt(40) + 30;
} else if (getMoney() <= 500000) {
} else if (player.getMoney() <= 500000) {
//Minimum 50 ships will attack, maximum 140
numOfShipsAttacking = randomValue.nextInt(90) + 50;
} else if (getMoney() > 1000000) {
} else if (player.getMoney() > 1000000) {
//Minimum 100 ships will attack, maximum 300 ships
numOfShipsAttacking = randomValue.nextInt(3) + 100;
}
@@ -103,7 +120,7 @@ public class ShipWarfare extends Player {
//Player volley
while (exitValue == 0) {
for (int j = 0; j < getGuns(); j++) {
for (int j = 0; j < player.getGuns(); j++) {
if (userAttacks == true) {
int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) {
@@ -136,23 +153,23 @@ public class ShipWarfare extends Player {
delayForASecond();
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && getGuns() > 0) {
setGuns(getGuns() - 1);
if (takeGunChance == 1 && player.getGuns() > 0) {
player.setGuns(player.getGuns() - 1);
System.out.println("Dang it! They destroyed one of our guns");
} else {
setHP(getHP() - (1 + randomValue.nextInt(10)));
player.setHP(player.getHP() - (1 + randomValue.nextInt(10)));
}
if (getHP() <= 0) {
if (player.getHP() <= 0) {
exitValue = 2;
break;
}
System.out.printf("EEK, our current ship status is %d%% \n", getHP());
System.out.printf("EEK, our current ship status is %d%% \n", player.getHP());
delayForASecond();
if (userAttacks == false) {
userAttacks = true;
}
System.out.printf("Shall we continue to fight? Enter \"f\" to fight, and \"r\" to run (We have %d gun(s) left)", getGuns());
System.out.printf("Shall we continue to fight? Enter \"f\" to fight, and \"r\" to run (We have %d gun(s) left)", player.getGuns());
String response = userInput.nextLine();
if (response.equalsIgnoreCase("r")) {
@@ -169,13 +186,13 @@ public class ShipWarfare extends Player {
if (exitValue == 1) {
System.out.printf("\nGot eem\nVictory!\nIt appears we have defeated the enemy fleet and made it out at %d%% ship status\n", getHP());
System.out.printf("\nGot eem\nVictory!\nIt appears we have defeated the enemy fleet and made it out at %d%% ship status\n", player.getHP());
return true;
} else if (exitValue == 2) {
gameOver();
player.gameOver();
return true;
} 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", player.getHP());
return true;
}
return false;
@@ -185,10 +202,4 @@ public class ShipWarfare extends Player {
//Type of ship implied to be Liu Yen fleet
public static void main(String[] args) throws Exception {
ShipWarfare littyObject = new ShipWarfare();
littyObject.peasantFleetAttack();
}
}