Shipwarfare fight button GUI almost complete

This commit is contained in:
KahootChampion
2019-03-10 00:39:59 -07:00
parent 5f6abbc817
commit e5807a46af

View File

@@ -29,21 +29,20 @@ import static javafx.application.Application.launch;
public class ShipWarfareGUI extends Application { public class ShipWarfareGUI extends Application {
private Player player = new Player(); private Player player = new Player();
private ShipWarfare shipWarfare = new ShipWarfare(player); private ShipWarfare shipWarfare = new ShipWarfare(player);
private HBox hBox; private HBox hBox;
private Button fightButton; private Button fightButton;
private Button runButton; private Button runButton;
private VBox vBox; private VBox vBox;
private Label title; private Label title;
private Label chooseFightOrRun; private Label chooseFightOrRun;
private Label report; private Label report;
private Label runAwayOrLeft; private Label runAwayOrLeft;
private Label shipsRemaining; private Label shipsRemaining;
private Label HPLeft; private Label HPLeft;
private Label gunsLeftOrTaken; private Label gunsLeftOrTaken;
private Label continueToFight; private Label continueToFight;
public static void main(String args[]) { public static void main(String args[]) {
@@ -62,6 +61,7 @@ public class ShipWarfareGUI extends Application {
/** /**
* setter method for player * setter method for player
*
* @param player object of the class Player * @param player object of the class Player
*/ */
public void setPlayer(Player player) { public void setPlayer(Player player) {
@@ -71,6 +71,7 @@ public class ShipWarfareGUI extends Application {
/** /**
* getter method for obtaining a player object. * getter method for obtaining a player object.
*
* @return returns player object * @return returns player object
*/ */
public Player getPlayer() { public Player getPlayer() {
@@ -80,6 +81,7 @@ public class ShipWarfareGUI extends Application {
/** /**
* 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
*
* @throws Exception in case of errors due to the delay * @throws Exception in case of errors due to the delay
*/ */
public void peasantFleetAttack() throws Exception { public void peasantFleetAttack() throws Exception {
@@ -118,6 +120,7 @@ public class ShipWarfareGUI extends Application {
/** /**
* 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
*
* @throws Exception in case of errors due to the delay * @throws Exception in case of errors due to the delay
*/ */
public void littyFleetAttack() throws Exception { public void littyFleetAttack() throws Exception {
@@ -164,6 +167,7 @@ public class ShipWarfareGUI extends Application {
/** /**
* setter method that takes in an integer as an argument * setter method that takes in an integer as an argument
*
* @param numOfLittyShips the number of ships to be used in the litty fleet attack * @param numOfLittyShips the number of ships to be used in the litty fleet attack
*/ */
public void setNumOfLittyShips(int numOfLittyShips) { public void setNumOfLittyShips(int numOfLittyShips) {
@@ -174,6 +178,7 @@ public class ShipWarfareGUI extends Application {
/** /**
* setter method that takes in an integer as an argument * setter method that takes in an integer as an argument
*
* @param numOfPeasantShips the number of ships to be used in the peasant fleet attack * @param numOfPeasantShips the number of ships to be used in the peasant fleet attack
*/ */
@@ -184,9 +189,9 @@ public class ShipWarfareGUI extends Application {
} }
/** /**
* The number of ships that attack is based on the amount of money one has on hand * The number of ships that attack is based on the amount of money one has on hand
*
* @return the number of ships which will attack * @return the number of ships which will attack
*/ */
public int numOfShips() { public int numOfShips() {
@@ -214,6 +219,7 @@ public class ShipWarfareGUI extends Application {
/** /**
* One in two chance of running away * One in two chance of running away
*
* @return true if the user is allowed to run, false if not, the "default" is false * @return true if the user is allowed to run, false if not, the "default" is false
*/ */
@@ -232,10 +238,10 @@ public class ShipWarfareGUI extends Application {
/** /**
* The user faces off against the litty ships and either prevails, dies, or runs away * The user faces off against the litty ships and either prevails, dies, or runs away
* The loot for defeating a litty fleet is much higher than that of a peasant one * The loot for defeating a litty fleet is much higher than that of a peasant one
*
* @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 * @throws Exception in case of errors due to the
* */
* */
public boolean destroyLittyShipsOrEscape() throws Exception { public boolean destroyLittyShipsOrEscape() throws Exception {
int calculateLoot = 0; int calculateLoot = 0;
int chanceOfEnemyRun = 0; int chanceOfEnemyRun = 0;
@@ -270,7 +276,7 @@ public class ShipWarfareGUI extends Application {
} }
} }
} else { } else {
System.out.printf("%s! We don't have any GUNS!!!!\n",player.getName()); System.out.printf("%s! We don't have any GUNS!!!!\n", player.getName());
} }
@@ -331,7 +337,7 @@ public class ShipWarfareGUI extends Application {
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());
calculateLoot = (randomValue.nextInt(startingLittyShips) + startingLittyShips) * 300; calculateLoot = (randomValue.nextInt(startingLittyShips) + startingLittyShips) * 300;
player.setMoney(player.getMoney() + calculateLoot); player.setMoney(player.getMoney() + calculateLoot);
System.out.printf("We got $%,d!\n", calculateLoot); System.out.printf("We got $%,d!\n", calculateLoot);
@@ -349,13 +355,8 @@ public class ShipWarfareGUI extends Application {
} }
/**
* 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
* @throws Exception in case of errors due to the delay
*/
public void wipe(){ public void wipe() {
title.setVisible(false); title.setVisible(false);
runAwayOrLeft.setVisible(false); runAwayOrLeft.setVisible(false);
shipsRemaining.setVisible(false); shipsRemaining.setVisible(false);
@@ -366,13 +367,21 @@ public class ShipWarfareGUI extends Application {
} }
/**
* 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
* @throws Exception in case of errors due to the delay
*/
public boolean destroyPeasantShipsOrEscape() throws Exception { public boolean destroyPeasantShipsOrEscape() throws Exception {
int calculateLoot = 0; int calculateLoot = 0;
int chanceOfEnemyRun = 0; int chanceOfEnemyRun = 0;
int hitCounter=0; int hitCounter = 0;
int missCounter=0; int missCounter = 0;
boolean gunFrustration= false; boolean gunFrustration = false;
//runAwayOrLeft.setVisible(false);
runAwayOrLeft.setText("No ships ran away");
Scanner userInput = new Scanner(System.in); Scanner userInput = new Scanner(System.in);
Random randomValue = new Random(); Random randomValue = new Random();
@@ -380,100 +389,97 @@ public class ShipWarfareGUI extends Application {
//Player volley //Player volley
//while (exitValue == 0) { //while (exitValue == 0) {
if (player.getGuns() > 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(2) + 1; int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) { if (hitOrMiss == 2) {
numOfPeasantShips--; numOfPeasantShips--;
if (numOfPeasantShips <= 0) { if (numOfPeasantShips <= 0) {
exitValue = 1; exitValue = 1;
//break; //break;
}
hitCounter++;
} else {
missCounter++;
} }
hitCounter++;
} else { } else {
//continue; missCounter++;
} }
}
report.setText(String.format("Report: Ships we hit: %d Shots missed: %d", hitCounter,missCounter));
}
else{
report.setText("We don't have any guns!!!");
}
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) {
runAwayOrLeft.setText(String.format("Ahhh, %d ships ran away %s! ", howMuchRun, player.getName()));
} else {
runAwayOrLeft.setText(String.format("Escaped %d of them! ", howMuchRun));
}
}
}
}
shipsRemaining.setText(String.format("%d ships remaining and they look angry!", numOfPeasantShips));
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && player.getGuns() > 0) {
player.setGuns(player.getGuns() - 1);
gunFrustration=true;
} else {
player.setHP(player.getHP() - (1 + randomValue.nextInt(10)));
}
if (player.getHP() <= 0) {
exitValue = 2;
//break;
}
if(gunFrustration==true){
gunsLeftOrTaken.setText(String.format("Dang it! We only have %d guns left", player.getGuns()));
}
else{
gunsLeftOrTaken.setText(String.format("We still have %d guns left", player.getGuns()));
}
HPLeft.setText(String.format("EEK, our current ship status is %d%% ", player.getHP()));
if (userAttacks == false) {
userAttacks = true;
}
continueToFight.setText(String.format("Shall we continue to fight? (Click the fight button or the run button)", player.getGuns()));
if (runButton.isPressed()) {
if (runFromShips() == false) {
wipe();
chooseFightOrRun.setText("Couldn't run away");
} else { } else {
exitValue = 3; //continue;
//break;
} }
} }
report.setText(String.format("Report: Ships hit: %d, Shots missed: %d", hitCounter, missCounter));
} else {
report.setText("We don't have any guns!!!");
else if(fightButton.isPressed()){ }
title.setText("AYYYYYYYYYYY");
}
else{
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 (howMuchRun > 0) {
runAwayOrLeft.setText(String.format("Cowards! %d ships ran away %s! ", howMuchRun, player.getName()));
//runAwayOrLeft.setVisible(true);
} else {
runAwayOrLeft.setText("None chose to flee!");
}
}
} }
}
shipsRemaining.setText(String.format("%d ships remaining and they look angry!", numOfPeasantShips));
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && player.getGuns() > 0) {
player.setGuns(player.getGuns() - 1);
gunFrustration = true;
} else {
player.setHP(player.getHP() - (1 + randomValue.nextInt(10)));
}
if (player.getHP() <= 0) {
exitValue = 2;
//break;
}
if (gunFrustration == true) {
gunsLeftOrTaken.setText(String.format("Dang it! We only have %d guns left", player.getGuns()));
} else {
gunsLeftOrTaken.setText(String.format("We still have %d guns left", player.getGuns()));
}
HPLeft.setText(String.format("EEK, our current ship status is %d%% ", player.getHP()));
if (userAttacks == false) {
userAttacks = true;
}
continueToFight.setText(String.format("Shall we continue to fight? (Click the fight button or the run button)", player.getGuns()));
if (runButton.isPressed()) {
if (runFromShips() == false) {
wipe();
chooseFightOrRun.setText("Couldn't run away");
} else {
exitValue = 3;
//break;
}
} else if (fightButton.isPressed()) {
title.setText("AYYYYYYYYYYY");
} else {
}
//} //}
@@ -481,11 +487,11 @@ public class ShipWarfareGUI extends Application {
if (exitValue == 1) { if (exitValue == 1) {
wipe(); wipe();
chooseFightOrRun.setText(String.format("Ayy We won! We survived at %d ship status!", player.getHP())); chooseFightOrRun.setText(String.format("Ayy We won! We survived at %d%% ship status!", 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()); 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; calculateLoot = (randomValue.nextInt(startingPeasantShips) + startingPeasantShips) * 100;
player.setMoney(player.getMoney() + calculateLoot); player.setMoney(player.getMoney() + calculateLoot);
report.setText(String.format("We got $%,d! ",calculateLoot)); report.setText(String.format("We got $%,d! ", calculateLoot));
return true; return true;
} else if (exitValue == 2) { } else if (exitValue == 2) {
player.gameOver(); player.gameOver();
@@ -500,14 +506,13 @@ public class ShipWarfareGUI extends Application {
} }
//Main G //Main G
private int counter = 0; private int counter = 0;
/** /**
* 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
*/ */
@@ -521,8 +526,6 @@ public class ShipWarfareGUI extends Application {
} }
public void setLabel1(String sysOut) { public void setLabel1(String sysOut) {
report.setText(sysOut); report.setText(sysOut);
} }
@@ -537,7 +540,6 @@ public class ShipWarfareGUI extends Application {
public void start(Stage stage) throws Exception { public void start(Stage stage) throws Exception {
setNumOfPeasantShips(numOfShips()); setNumOfPeasantShips(numOfShips());
player.setGuns(5);
BorderPane BorderPane = new BorderPane(); BorderPane BorderPane = new BorderPane();
@@ -559,7 +561,6 @@ public class ShipWarfareGUI extends Application {
continueToFight = new Label(); continueToFight = new Label();
BorderPane.setPrefHeight(400.0); BorderPane.setPrefHeight(400.0);
BorderPane.setPrefWidth(600.0); BorderPane.setPrefWidth(600.0);
hBox.setAlignment(javafx.geometry.Pos.CENTER); hBox.setAlignment(javafx.geometry.Pos.CENTER);
@@ -570,7 +571,7 @@ public class ShipWarfareGUI extends Application {
title.setAlignment(javafx.geometry.Pos.TOP_CENTER); title.setAlignment(javafx.geometry.Pos.TOP_CENTER);
title.setContentDisplay(javafx.scene.control.ContentDisplay.CENTER); title.setContentDisplay(javafx.scene.control.ContentDisplay.CENTER);
title.setId("Label1"); title.setId("Label1");
title.setText(String.format("%d ships attacking. Would you like to Fight or Run?",numOfPeasantShips)); title.setText(String.format("%d ships attacking. Would you like to Fight or Run?", numOfPeasantShips));
title.setPadding(new Insets(6.0, 0.0, 0.0, 0.0)); title.setPadding(new Insets(6.0, 0.0, 0.0, 0.0));
fightButton.setAlignment(javafx.geometry.Pos.CENTER); fightButton.setAlignment(javafx.geometry.Pos.CENTER);
@@ -611,22 +612,19 @@ public class ShipWarfareGUI extends Application {
vBox.getChildren().add(continueToFight); vBox.getChildren().add(continueToFight);
//report.setText(String.format("By Golly! We have $%,d and are being attacked by %d Merchant ships\nCurrently our ship status is %d%%\n", player.getMoney(), shipWarfare.getNumOfPeasantShips(), player.getHP())); //report.setText(String.format("By Golly! We have $%,d and are being attacked by %d Merchant ships\nCurrently our ship status is %d%%\n", player.getMoney(), shipWarfare.getNumOfPeasantShips(), player.getHP()));
//Fight //Fight
fightButton.setOnAction(new EventHandler<ActionEvent>() { fightButton.setOnAction(new EventHandler<ActionEvent>() {
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
counter++; counter++;
chooseFightOrRun.setText("Ohh, Fight ehh?"); chooseFightOrRun.setText("Ohh, Fight ehh?");
try { try {
destroyPeasantShipsOrEscape(); destroyPeasantShipsOrEscape();
} } catch (Exception e) {
catch(Exception e){} }
if (counter == 2) { if (counter == 2) {
title.setVisible(false); title.setVisible(false);