From 6bbd852c8b2c783507f70bb723ad99c143ec9f84 Mon Sep 17 00:00:00 2001
From: KahootChampion
Date: Thu, 4 Apr 2019 22:13:04 -0600
Subject: [PATCH] Failed attempt at separating logic and GUI but saving just in
case I need it later
---
src/ShipWarfareGUI.java | 308 ++++++----------------------------------
1 file changed, 45 insertions(+), 263 deletions(-)
diff --git a/src/ShipWarfareGUI.java b/src/ShipWarfareGUI.java
index 459908d..3e982d0 100644
--- a/src/ShipWarfareGUI.java
+++ b/src/ShipWarfareGUI.java
@@ -29,6 +29,7 @@ import java.util.concurrent.ExecutionException;
public class ShipWarfareGUI extends Player {
private ShipWarfareGUI ship;
+ private ShipWarfareGUILogic logic;
private Circle cannon;
private VBox buttonBox;
private HBox fightRunBox;
@@ -36,6 +37,7 @@ public class ShipWarfareGUI extends Player {
private Button runButton;
private Button continueButton;
private VBox labelBox;
+
private Label title;
private Label HPLeft;
private Label gunsLeftOrTaken;
@@ -43,21 +45,10 @@ public class ShipWarfareGUI extends Player {
private Label shipsRemaining;
private Label report;
- private boolean winOrLose = false;
- private int counter1;
private int timeCounter;
- private int numOfLittyShips = 0;
- private boolean userAttacks = true;
- private int startingLittyShips = 0;
- private int howMuchRun = 0;
- private int counter = 0;
- private String pirateName = "Liu Yen";
-
- private int missCounter;
- private int hitCounter;
private int beginningX = 150;
private int beginningY = 245;
@@ -66,8 +57,6 @@ public class ShipWarfareGUI extends Player {
private int endY = 90;
private TranslateTransition shotsFired = new TranslateTransition();
- private TranslateTransition shotsMissed = new TranslateTransition();
-
private TranslateTransition enemyShots = new TranslateTransition();
/**
@@ -80,64 +69,9 @@ public class ShipWarfareGUI extends Player {
setPlayer(playerDummy);
}
- /**
- * 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
- */
- public int numOfShips() {
- int numOfShipsAttacking = 0;
- Random randomValue = new Random();
- if (getMoney() <= 100000) {
- //Minimum one ship will attack, maximum 20
- numOfShipsAttacking = randomValue.nextInt(20) + 1;
- } else if (getMoney() <= 200000) {
- //Minimum 30 Ships will attack, maximum 70
- numOfShipsAttacking = randomValue.nextInt(40) + 31;
- } else if (getMoney() <= 500000) {
- //Minimum 50 ships will attack, maximum 140
- numOfShipsAttacking = randomValue.nextInt(90) + 51;
- } else if (getMoney() >= 1000000) {
- //Minimum 100 ships will attack, maximum 300 ships
- numOfShipsAttacking = randomValue.nextInt(200) + 101;
- }
- return numOfShipsAttacking;
-
- }
-
- /**
- * setter method that takes in an integer as an argument
- *
- * @param numOfLittyShips the number of ships to be used in the peasant fleet attack
- */
- public void setNumOfLittyShips(int numOfLittyShips) {
- counter1++;
- this.numOfLittyShips = numOfLittyShips;
- if (counter1 == 1) {
- startingLittyShips = numOfLittyShips;
- }
-
- }
-
- /**
- * One in two chance of running away
- *
- * @return true if the user is allowed to run, false if not, the "default" is false
- */
- public boolean runFromShips() {
- userAttacks = false;
- Random randomValue = new Random();
- int runSuccessChance = randomValue.nextInt(10) + 1;
- if (runSuccessChance == 2) {
- return true;
- } else if (runSuccessChance == 1) {
- return false;
- }
- return false;
- }
/**
* Sets most of the labels invisible except for the "fight or run" label
@@ -165,180 +99,27 @@ public class ShipWarfareGUI extends Player {
}
- /**
- * The user faces off against the litty ships and either prevails, dies, or runs away
- *
- * @return true if the user wins, loses, or flees, it returns false otherwise
- */
- public boolean destroyLittyShipsOrEscape(Stage stage) throws Exception {
- cannon.setLayoutX(beginningX);
- cannon.setLayoutY(beginningY);
- int calculateLoot = 0;
- int chanceOfEnemyRun = 0;
- hitCounter = 0;
- missCounter = 0;
- boolean gunFrustration = false;
- runAwayOrLeft.setText("No ships ran away");
- Random randomValue = new Random();
- int exitValue = 0;
- //Player volley
- //while (exitValue == 0) {
- if (getGuns() > 0) {
-
- for (int j = 0; j < getGuns(); j++) {
- if (userAttacks == true) {
-
- int hitOrMiss = randomValue.nextInt(2) + 1;
- if (hitOrMiss == 2) {
- numOfLittyShips--;
- if (numOfLittyShips <= 0) {
- exitValue = 1;
- //break;
- }
- hitCounter++;
-
-
- } else {
- missCounter++;
-
- }
-
-
- } else {
- //continue;
- }
- }
- if (userAttacks == true) {
- report.setText(String.format("Report: Ships hit: %d, Shots missed: %d", hitCounter, missCounter));
- }
- } else {
- report.setText("We don't have any guns!!!");
-
- }
-
-
- if (numOfLittyShips <= 0) {
- exitValue = 1;
- //break;
- }
- if (getGuns() > 0) {
- chanceOfEnemyRun = randomValue.nextInt(2) + 1;
- if (chanceOfEnemyRun == 2) {
- howMuchRun = randomValue.nextInt(15) + 1;
- if (howMuchRun != 0 && howMuchRun < numOfLittyShips) {
-
-
- setNumOfLittyShips(numOfLittyShips - howMuchRun);
- if (userAttacks == true) {
- if (howMuchRun > 0) {
- runAwayOrLeft.setText(String.format("Cowards! %d ships ran away %s! ", howMuchRun, getName()));
-
- //runAwayOrLeft.setVisible(true);
- }
-
- } else {
- report.setText((String.format("Escaped %d of them %s!", howMuchRun, getName())));
- }
-
- }
- }
- }
-
- shipsRemaining.setText(String.format("%d ships remaining and they look angry!", numOfLittyShips));
- //Computer volley
- int takeGunChance = randomValue.nextInt(4) + 1;
- if (takeGunChance == 1 && getGuns() > 0) {
- setGuns(getGuns() - 1);
- gunFrustration = true;
- } else {
- if (numOfLittyShips > 0) {
- int HPTaken = randomValue.nextInt(10);
- setHP(getHP() - (HPTaken));
-
-
- }
- }
- if (getHP() <= 0) {
- exitValue = 2;
- //break;
- }
- if (gunFrustration == true) {
- gunsLeftOrTaken.setText(String.format("Dang it! We only have %d guns left", getGuns()));
-
- } else {
- gunsLeftOrTaken.setText(String.format("We still have %d guns left", getGuns()));
- }
-
- HPLeft.setText(String.format("EEK, our current ship status is %d%% ", getHP()));
- if (userAttacks == false) {
- userAttacks = true;
- }
-
-
- if (exitValue == 1) {
- wipe();
- calculateLoot = (startingLittyShips * 100) + randomValue.nextInt(startingLittyShips) * 200;
- setMoney(getMoney() + calculateLoot);
- report.setText(String.format("Our firm has earned $%,d in loot! ", calculateLoot));
- continueButton.setVisible(true);
- completeWipe();
- fightButton.setVisible(false);
- runButton.setVisible(false);
- continueButton.setDefaultButton(true);
- return true;
- } else if (exitValue == 2) {
- GameEndGUI gameEndGUI = new GameEndGUI(getPlayer());
- gameEndGUI.initializeGameEndGUI(stage);
- stage.show();
- return true;
- } else if (exitValue == 3) {
- report.setText(String.format("We made it out at %d%% ship status!", getHP()));
-
- continueButton.setVisible(true);
- completeWipe();
- fightButton.setVisible(false);
- runButton.setVisible(false);
- continueButton.setDefaultButton(true);
- return true;
- }
- return false;
-
- }
/**
* Player attacks enemy ships in an animation
*/
public void playerShoots(int amountOfShots) {
- userAttacks = true;
shotsFired.setFromX(0);
shotsFired.setFromY(0);
shotsFired.setToX(endX);
shotsFired.setToY(endY);
- shots(amountOfShots, shotsFired);
- }
-
- public void playerMisses(int amountOfShots) {
- userAttacks = true;
- shotsMissed.setFromX(0);
- shotsMissed.setFromY(0);
- shotsMissed.setToX(endX + 90);
- shotsMissed.setToY(endY + 90);
- shots(amountOfShots, shotsMissed);
- }
-
- public void shots(int amountOfShots, TranslateTransition shots) {
- shots.setDuration(Duration.seconds(0.5));
+ shotsFired.setDuration(Duration.seconds(0.5));
if (getGuns() > 0) {
- shots.setCycleCount(amountOfShots);
+ shotsFired.setCycleCount(amountOfShots);
} else {
- shots.setCycleCount(0);
- shots.stop();
+ shotsFired.setCycleCount(0);
+ shotsFired.stop();
cannon.setVisible(false);
}
- shots.setNode(cannon);
- shots.play();
+ shotsFired.setNode(cannon);
+ shotsFired.play();
}
/**
@@ -379,6 +160,37 @@ public class ShipWarfareGUI extends Player {
});
}
+ public boolean winOrLose( Stage stage){
+ if (logic.destroyLittyShipsOrEscape() == 1) {
+ wipe();
+ report.setText(logic.getReportMessage());
+ continueButton.setVisible(true);
+ completeWipe();
+ fightButton.setVisible(false);
+ runButton.setVisible(false);
+ continueButton.setDefaultButton(true);
+ return true;
+
+
+ } else if (logic.destroyLittyShipsOrEscape() == 2) {
+ GameEndGUI gameEndGUI = new GameEndGUI(getPlayer());
+ gameEndGUI.initializeGameEndGUI(stage);
+ stage.show();
+ return true;
+
+ } else if (logic.destroyLittyShipsOrEscape() == 3) {
+ report.setText(String.format("We made it out at %d%% ship status!", getHP()));
+
+ continueButton.setVisible(true);
+ completeWipe();
+ fightButton.setVisible(false);
+ runButton.setVisible(false);
+ continueButton.setDefaultButton(true);
+ return true;
+ }
+ }
+
+
/**
* Generaties ships and deploys logic for the shipwarfare
@@ -387,7 +199,9 @@ public class ShipWarfareGUI extends Player {
* @throws Exception in case of interruptions withing the graphical interface
*/
public void initializeShip(Stage primaryStage) throws Exception {
- setNumOfLittyShips(numOfShips());
+ cannon.setLayoutX(beginningX);
+ cannon.setLayoutY(beginningY);
+ logic.setNumOfLittyShips(logic.numOfShips());
Pane root = new Pane();
HBox usAgainstEnemyDivisor;
@@ -571,7 +385,7 @@ public class ShipWarfareGUI extends Player {
report.setText("Epic");
counter++;
- if (runFromShips() == false) {
+ if (logic.runFromShips() == false) {
report.setText(("Couldn't run away"));
try {
winOrLose = destroyLittyShipsOrEscape(primaryStage);
@@ -630,36 +444,9 @@ public class ShipWarfareGUI extends Player {
}
- if(hitCounter!=0) {
- if(winOrLose==true){
- playerShoots(numOfLittyShips);
-
- }
- else {
- playerShoots(hitCounter);
- }
- }
- else{
- playerMisses(hitCounter);
- }
-
-
+ playerShoots(getGuns());
shotsFired.setOnFinished(new EventHandler() {
- @Override
- public void handle(ActionEvent event) {
- try {
-
- playerMisses(missCounter);
-
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
-
- shotsMissed.setOnFinished(new EventHandler() {
@Override
/**
* When the user is completed their volley this information will be accessed
@@ -710,9 +497,4 @@ public class ShipWarfareGUI extends Player {
});
}
-}
-
-
-
-
-
+}
\ No newline at end of file