Update ShipWarfare.java

the comment somehow made it so that I couldn't compile (using the command line i couldn't compile, i could compile file using intellij) Also, you the file extended player, which is not necessary. You also were calling gameOver() and getName() directly, you needed "player." before all those methods.
This commit is contained in:
Vikramb987
2019-02-25 07:41:58 -07:00
committed by GitHub
parent 600fe404d5
commit 1bad8198a1

View File

@@ -2,18 +2,18 @@ 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 extends Player { public class ShipWarfare {
private int numOfPeasantShips = 0; private int numOfPeasantShips = 0;
private int numOfLittyShips = 0; private int numOfLittyShips = 0;
private boolean userAttacks = true; private boolean userAttacks = true;
private int startingPeasantShips = 0; private int startingPeasantShips = 0;
private int startingLittyShips = 0; private int startingLittyShips =0;
private int howMuchRun = 0; private int howMuchRun = 0;
private String pirateName = "Liu Yen"; private String pirateName = "Liu Yen";
private Player player; private Player player;
public ShipWarfare(Player player) { public ShipWarfare(Player player){
Player playerDummy = new Player(player); Player playerDummy = new Player(player);
this.player = playerDummy; this.player = playerDummy;
} }
@@ -23,11 +23,11 @@ public class ShipWarfare extends Player {
this.player = playerDummy; this.player = playerDummy;
} }
public Player getPlayer() { public Player getPlayer(){
Player playerDummy = new Player(player); Player playerDummy = new Player(player);
return playerDummy; return playerDummy;
} }
//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
public void peasantFleetAttack() throws Exception { public void peasantFleetAttack() throws Exception {
Scanner userResponse = new Scanner(System.in); Scanner userResponse = new Scanner(System.in);
@@ -41,7 +41,6 @@ public class ShipWarfare extends Player {
if (response.equalsIgnoreCase("f")) { if (response.equalsIgnoreCase("f")) {
userAttacks = true; userAttacks = true;
System.out.println("Ohh, fight ehh?"); System.out.println("Ohh, fight ehh?");
delayForSeconds(1);
boolean winOrLose = destroyPeasantShipsOrEscape(); boolean winOrLose = destroyPeasantShipsOrEscape();
if (winOrLose == true) { if (winOrLose == true) {
break; break;
@@ -55,7 +54,6 @@ public class ShipWarfare extends Player {
break; break;
} else { } else {
System.out.println("Phew! Got away safely"); System.out.println("Phew! Got away safely");
delayForSeconds(2);
break; break;
} }
@@ -65,7 +63,6 @@ public class ShipWarfare extends Player {
} }
//This fleet is difficult to defeat as a maximum of 10 ships can run away each volley, they can tank hits //This fleet is difficult to defeat as a maximum of 10 ships can run away each volley, they can tank hits
public void littyFleetAttack() throws Exception { public void littyFleetAttack() throws Exception {
Scanner userResponse = new Scanner(System.in); Scanner userResponse = new Scanner(System.in);
@@ -86,12 +83,10 @@ public class ShipWarfare extends Player {
} else if (response.equalsIgnoreCase("r")) { } else if (response.equalsIgnoreCase("r")) {
if (runFromShips() == false) { if (runFromShips() == false) {
System.out.println("Couldn't run away!"); System.out.println("Couldn't run away!");
delayForSeconds(1);
if (destroyLittyShipsOrEscape()) if (destroyLittyShipsOrEscape())
break; break;
} else { } else {
System.out.println("Phew! Got away safely"); System.out.println("Phew! Got away safely");
delayForSeconds(2);
break; break;
} }
@@ -104,7 +99,7 @@ public class ShipWarfare extends Player {
public void fightOrRunMessage() { public void fightOrRunMessage() {
System.out.printf("What do you want to do? Enter \"f\" to fight, and \"r\" to run (we have %d guns)\n", 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());
} }
@@ -125,8 +120,8 @@ public class ShipWarfare extends Player {
} }
public void delayForSeconds(int num) throws Exception { public void delayForASecond() throws Exception {
TimeUnit.SECONDS.sleep(num); TimeUnit.SECONDS.sleep(1);
} }
//The number of ships which attack is based on the amount of money one has on hand //The number of ships which attack is based on the amount of money one has on hand
@@ -178,34 +173,29 @@ public class ShipWarfare extends Player {
//Player volley //Player volley
while (exitValue == 0) { while (exitValue == 0) {
if (player.getGuns() > 0) { for (int j = 0; j < player.getGuns(); j++) {
for (int j = 0; j < player.getGuns(); j++) { if (userAttacks == true) {
if (userAttacks == true) { int hitOrMiss = randomValue.nextInt(3) + 1;
int hitOrMiss = randomValue.nextInt(3) + 1; if (hitOrMiss == 1) {
if (hitOrMiss == 1) { numOfLittyShips--;
numOfLittyShips--; if (numOfLittyShips <= 0) {
if (numOfLittyShips <= 0) { exitValue = 1;
exitValue = 1; break;
break;
}
System.out.println("Got eem");
delayForSeconds(1);
} else if (hitOrMiss == 2) {
System.out.printf("ARRG! We missed %s\n", getName());
delayForSeconds(1);
} else {
System.out.println("Darn! Their fleet tanked our attack");
delayForSeconds(1);
} }
System.out.println("Got eem");
delayForASecond();
} else if (hitOrMiss == 2) {
System.out.printf("ARRG! We missed %s\n", player.getName());
delayForASecond();
} else { } else {
continue; System.out.println("Darn! Their fleet tanked our attack");
delayForASecond();
} }
} else {
continue;
} }
} else {
System.out.printf("%s! We don't have any GUNS!!!!\n",player.getName());
delayForSeconds(1);
} }
@@ -213,26 +203,24 @@ public class ShipWarfare extends Player {
exitValue = 1; exitValue = 1;
break; break;
} }
if (player.getGuns() > 0) { chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) { if (chanceOfEnemyRun == 2) {
chanceOfEnemyRun = randomValue.nextInt(2) + 1; howMuchRun = randomValue.nextInt(10) + 1;
howMuchRun = randomValue.nextInt(10) + 1; if (howMuchRun != 0 && howMuchRun < numOfLittyShips) {
if (howMuchRun != 0 && howMuchRun < numOfLittyShips) {
setNumOfLittyShips(numOfLittyShips - howMuchRun); setNumOfLittyShips(numOfLittyShips - howMuchRun);
if (userAttacks == true) { if (userAttacks == true) {
System.out.printf("Cowards! %d ships ran away %s!\n", howMuchRun, getName()); System.out.printf("Cowards! %d ships ran away %s!\n", howMuchRun, player.getName());
} else { } else {
System.out.printf("Escaped %d of them!\n", howMuchRun); System.out.printf("Escaped %d of them!\n", howMuchRun);
}
} }
} }
} }
System.out.printf("%d ships remaining\n", numOfLittyShips); System.out.printf("%d ships remaining\n", numOfLittyShips);
System.out.println("Oh no, they are taking the offensive!"); System.out.println("Oh no, they are taking the offensive!");
delayForSeconds(1); delayForASecond();
//Computer volley //Computer volley
int takeGunChance = randomValue.nextInt(4) + 1; int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && player.getGuns() > 0) { if (takeGunChance == 1 && player.getGuns() > 0) {
@@ -246,132 +234,7 @@ public class ShipWarfare extends Player {
break; break;
} }
System.out.printf("EEK, our current ship status is %d%% \n", player.getHP()); System.out.printf("EEK, our current ship status is %d%% \n", player.getHP());
delayForSeconds(1); 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)\n", player.getGuns());
String response = userInput.nextLine();
if (response.equalsIgnoreCase("r")) {
if (runFromShips() == false) {
System.out.println("Couldn't run away");
delayForSeconds(1);
} else {
System.out.println("Phew! Got away safely");
delayForSeconds(2);
break;
}
}
}
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", player.getHP());
delayForSeconds(1);
calculateLoot = (randomValue.nextInt(startingLittyShips) + startingLittyShips) * 300;
player.setMoney(player.getMoney() + calculateLoot);
System.out.printf("We got $%,d!\n", calculateLoot);
delayForSeconds(2);
return true;
} else if (exitValue == 2) {
gameOver();
return true;
} else if (exitValue == 3) {
System.out.printf("We made it out at %d%% ship status!\n", player.getHP());
delayForSeconds(2);
return true;
}
return false;
}
public boolean destroyPeasantShipsOrEscape() throws Exception {
int calculateLoot = 0;
int chanceOfEnemyRun = 0;
Scanner userInput = new Scanner(System.in);
Random randomValue = new Random();
int exitValue = 0;
//Player volley
while (exitValue == 0) {
if (player.getGuns() > 0) {
for (int j = 0; j < player.getGuns(); j++) {
if (userAttacks == true) {
int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) {
numOfPeasantShips--;
if (numOfPeasantShips <= 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", player.getName());
delayForSeconds(1);
}
if (numOfPeasantShips <= 0) {
exitValue = 1;
break;
}
if (player.getGuns() > 0) {
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1;
if (howMuchRun != 0 && howMuchRun < numOfPeasantShips) {
setNumOfPeasantShips(numOfPeasantShips - 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);
}
}
}
}
System.out.printf("%d ships remaining\n", numOfPeasantShips);
delayForSeconds(1);
System.out.println("Oh no, they are taking the offensive!");
delayForSeconds(1);
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && player.getGuns() > 0) {
player.setGuns(player.getGuns() - 1);
System.out.println("Dang it! They destroyed one of our guns");
} else {
player.setHP(player.getHP() - (1 + randomValue.nextInt(10)));
}
if (player.getHP() <= 0) {
exitValue = 2;
break;
}
System.out.printf("EEK, our current ship status is %d%% \n", player.getHP());
delayForSeconds(1);
if (userAttacks == false) { if (userAttacks == false) {
userAttacks = true; userAttacks = true;
} }
@@ -394,18 +257,124 @@ public class ShipWarfare extends Player {
if (exitValue == 1) { 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", player.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());
delayForSeconds(1); calculateLoot = (randomValue.nextInt(startingLittyShips) + startingLittyShips) * 300;
calculateLoot = (randomValue.nextInt(startingPeasantShips) + startingPeasantShips) * 100;
player.setMoney(player.getMoney() + calculateLoot); player.setMoney(player.getMoney() + calculateLoot);
System.out.printf("We got $%,d!", calculateLoot); System.out.printf("We got $%,d!\n", calculateLoot);
delayForSeconds(2);
return true; return true;
} else if (exitValue == 2) { } 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", player.getHP());
return true;
}
return false;
}
public boolean destroyPeasantShipsOrEscape() throws Exception {
int calculateLoot = 0;
int chanceOfEnemyRun = 0;
Scanner userInput = new Scanner(System.in);
Random randomValue = new Random();
int exitValue = 0;
//Player volley
while (exitValue == 0) {
for (int j = 0; j < player.getGuns(); j++) {
if (userAttacks == true) {
int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) {
numOfPeasantShips--;
if (numOfPeasantShips <= 0) {
exitValue = 1;
break;
}
System.out.println("Got eem");
delayForASecond();
} else {
System.out.printf("ARRG! We missed %s\n", player.getName());
delayForASecond();
}
} else {
continue;
}
}
if (numOfPeasantShips <= 0) {
exitValue = 1;
break;
}
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1;
if (howMuchRun != 0 && howMuchRun < numOfPeasantShips) {
setNumOfPeasantShips(numOfPeasantShips - howMuchRun);
if (userAttacks == true) {
System.out.printf("Ahhh, %d ships ran away %s!\n", howMuchRun, player.getName());
} else {
System.out.printf("Escaped %d of them!\n", howMuchRun);
}
}
}
System.out.printf("%d ships remaining\n", numOfPeasantShips);
System.out.println("Oh no, they are taking the offensive!");
delayForASecond();
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && player.getGuns() > 0) {
player.setGuns(player.getGuns() - 1);
System.out.println("Dang it! They destroyed one of our guns");
} else {
player.setHP(player.getHP() - (1 + randomValue.nextInt(10)));
}
if (player.getHP() <= 0) {
exitValue = 2;
break;
}
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)\n", player.getGuns());
String response = userInput.nextLine();
if (response.equalsIgnoreCase("r")) {
if (runFromShips() == false) {
System.out.println("Couldn't run away");
} else {
exitValue = 3;
break;
}
}
}
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", player.getHP());
calculateLoot = (randomValue.nextInt(startingPeasantShips) + startingPeasantShips) * 100;
player.setMoney(player.getMoney() + calculateLoot);
System.out.printf("We got $%,d!", calculateLoot);
return true;
} else if (exitValue == 2) {
player.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", player.getHP()); System.out.printf("We made it out at %d%% ship status!\n", player.getHP());
delayForSeconds(2);
return true; return true;
} }
return false; return false;