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:
@@ -2,18 +2,18 @@ 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 int numOfLittyShips = 0;
|
||||
private boolean userAttacks = true;
|
||||
private int startingPeasantShips = 0;
|
||||
private int startingLittyShips = 0;
|
||||
private int startingLittyShips =0;
|
||||
private int howMuchRun = 0;
|
||||
private String pirateName = "Liu Yen";
|
||||
private Player player;
|
||||
|
||||
public ShipWarfare(Player player) {
|
||||
public ShipWarfare(Player player){
|
||||
Player playerDummy = new Player(player);
|
||||
this.player = playerDummy;
|
||||
}
|
||||
@@ -23,11 +23,11 @@ public class ShipWarfare extends Player {
|
||||
this.player = playerDummy;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
public Player getPlayer(){
|
||||
Player playerDummy = new Player(player);
|
||||
return playerDummy;
|
||||
}
|
||||
|
||||
|
||||
//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 {
|
||||
Scanner userResponse = new Scanner(System.in);
|
||||
@@ -41,7 +41,6 @@ public class ShipWarfare extends Player {
|
||||
if (response.equalsIgnoreCase("f")) {
|
||||
userAttacks = true;
|
||||
System.out.println("Ohh, fight ehh?");
|
||||
delayForSeconds(1);
|
||||
boolean winOrLose = destroyPeasantShipsOrEscape();
|
||||
if (winOrLose == true) {
|
||||
break;
|
||||
@@ -55,7 +54,6 @@ public class ShipWarfare extends Player {
|
||||
break;
|
||||
} else {
|
||||
System.out.println("Phew! Got away safely");
|
||||
delayForSeconds(2);
|
||||
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
|
||||
public void littyFleetAttack() throws Exception {
|
||||
Scanner userResponse = new Scanner(System.in);
|
||||
@@ -86,12 +83,10 @@ public class ShipWarfare extends Player {
|
||||
} else if (response.equalsIgnoreCase("r")) {
|
||||
if (runFromShips() == false) {
|
||||
System.out.println("Couldn't run away!");
|
||||
delayForSeconds(1);
|
||||
if (destroyLittyShipsOrEscape())
|
||||
break;
|
||||
} else {
|
||||
System.out.println("Phew! Got away safely");
|
||||
delayForSeconds(2);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -104,7 +99,7 @@ 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)\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 {
|
||||
TimeUnit.SECONDS.sleep(num);
|
||||
public void delayForASecond() throws Exception {
|
||||
TimeUnit.SECONDS.sleep(1);
|
||||
}
|
||||
|
||||
//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
|
||||
while (exitValue == 0) {
|
||||
if (player.getGuns() > 0) {
|
||||
for (int j = 0; j < player.getGuns(); j++) {
|
||||
if (userAttacks == true) {
|
||||
int hitOrMiss = randomValue.nextInt(3) + 1;
|
||||
if (hitOrMiss == 1) {
|
||||
numOfLittyShips--;
|
||||
if (numOfLittyShips <= 0) {
|
||||
exitValue = 1;
|
||||
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);
|
||||
for (int j = 0; j < player.getGuns(); j++) {
|
||||
if (userAttacks == true) {
|
||||
int hitOrMiss = randomValue.nextInt(3) + 1;
|
||||
if (hitOrMiss == 1) {
|
||||
numOfLittyShips--;
|
||||
if (numOfLittyShips <= 0) {
|
||||
exitValue = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
System.out.println("Got eem");
|
||||
delayForASecond();
|
||||
} else if (hitOrMiss == 2) {
|
||||
System.out.printf("ARRG! We missed %s\n", player.getName());
|
||||
delayForASecond();
|
||||
} 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;
|
||||
break;
|
||||
}
|
||||
if (player.getGuns() > 0) {
|
||||
if (chanceOfEnemyRun == 2) {
|
||||
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
|
||||
howMuchRun = randomValue.nextInt(10) + 1;
|
||||
if (howMuchRun != 0 && howMuchRun < numOfLittyShips) {
|
||||
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
|
||||
if (chanceOfEnemyRun == 2) {
|
||||
howMuchRun = randomValue.nextInt(10) + 1;
|
||||
if (howMuchRun != 0 && howMuchRun < numOfLittyShips) {
|
||||
|
||||
|
||||
setNumOfLittyShips(numOfLittyShips - howMuchRun);
|
||||
if (userAttacks == true) {
|
||||
System.out.printf("Cowards! %d ships ran away %s!\n", howMuchRun, getName());
|
||||
} else {
|
||||
System.out.printf("Escaped %d of them!\n", howMuchRun);
|
||||
}
|
||||
setNumOfLittyShips(numOfLittyShips - howMuchRun);
|
||||
if (userAttacks == true) {
|
||||
System.out.printf("Cowards! %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", numOfLittyShips);
|
||||
System.out.println("Oh no, they are taking the offensive!");
|
||||
delayForSeconds(1);
|
||||
delayForASecond();
|
||||
//Computer volley
|
||||
int takeGunChance = randomValue.nextInt(4) + 1;
|
||||
if (takeGunChance == 1 && player.getGuns() > 0) {
|
||||
@@ -246,132 +234,7 @@ public class ShipWarfare extends Player {
|
||||
break;
|
||||
}
|
||||
System.out.printf("EEK, our current ship status is %d%% \n", player.getHP());
|
||||
delayForSeconds(1);
|
||||
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);
|
||||
delayForASecond();
|
||||
if (userAttacks == false) {
|
||||
userAttacks = true;
|
||||
}
|
||||
@@ -394,18 +257,124 @@ 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", player.getHP());
|
||||
delayForSeconds(1);
|
||||
calculateLoot = (randomValue.nextInt(startingPeasantShips) + startingPeasantShips) * 100;
|
||||
calculateLoot = (randomValue.nextInt(startingLittyShips) + startingLittyShips) * 300;
|
||||
player.setMoney(player.getMoney() + calculateLoot);
|
||||
System.out.printf("We got $%,d!", calculateLoot);
|
||||
delayForSeconds(2);
|
||||
System.out.printf("We got $%,d!\n", calculateLoot);
|
||||
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", 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;
|
||||
} else if (exitValue == 3) {
|
||||
System.out.printf("We made it out at %d%% ship status!\n", player.getHP());
|
||||
delayForSeconds(2);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user