Failed attempt at separating logic and GUI but saving just in case I need it later

This commit is contained in:
KahootChampion
2019-04-06 15:21:53 -06:00
parent 6bbd852c8b
commit 92cd1db4f5
3 changed files with 348 additions and 276 deletions

View File

@@ -45,6 +45,10 @@ public class ShipWarfareGUI extends Player {
private Label shipsRemaining;
private Label report;
private int counter;
private int checkIfDone;
private int timeCounter;
@@ -179,8 +183,7 @@ public class ShipWarfareGUI extends Player {
return true;
} else if (logic.destroyLittyShipsOrEscape() == 3) {
report.setText(String.format("We made it out at %d%% ship status!", getHP()));
report.setText(logic.getReportMessage());
continueButton.setVisible(true);
completeWipe();
fightButton.setVisible(false);
@@ -188,6 +191,10 @@ public class ShipWarfareGUI extends Player {
continueButton.setDefaultButton(true);
return true;
}
else {
return false;
}
}
@@ -229,7 +236,7 @@ public class ShipWarfareGUI extends Player {
shipsRemaining = new Label();
report = new Label();
title.setText(String.format("%d ships from Liu Yuen's Fleet are attacking, Would you like to fight or run?", numOfLittyShips));
title.setText(String.format("%d ships from Liu Yuen's Fleet are attacking, Would you like to fight or run?", logic.getNumOfLittyShips()));
fightButton.setText("Fight");
@@ -388,11 +395,11 @@ public class ShipWarfareGUI extends Player {
if (logic.runFromShips() == false) {
report.setText(("Couldn't run away"));
try {
winOrLose = destroyLittyShipsOrEscape(primaryStage);
checkIfDone = logic.destroyLittyShipsOrEscape();
} catch (Exception e) {
e.printStackTrace();
}
if (winOrLose == true) {
if (checkIfDone != 4 ) {
report.setVisible(true);
title.setVisible(true);
shipsRemaining.setVisible(true);
@@ -428,7 +435,7 @@ public class ShipWarfareGUI extends Player {
runButton.setVisible(false);
try {
winOrLose = destroyLittyShipsOrEscape(primaryStage);
winOrLose(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
@@ -454,7 +461,7 @@ public class ShipWarfareGUI extends Player {
*/
public void handle(ActionEvent event) {
shotsFired.stop();
if (!winOrLose) {
if (checkIfDone==4) {
shipsRetaliate();
} else {
report.setVisible(true);
@@ -481,7 +488,7 @@ public class ShipWarfareGUI extends Player {
HPLeft.setVisible(true);
gunsLeftOrTaken.setVisible(true);
if (winOrLose == true) {
if (!winOrLose(primaryStage)) {
usAgainstEnemyDivisor.setVisible(false);
}
@@ -497,4 +504,5 @@ public class ShipWarfareGUI extends Player {
});
}
}
}

View File

@@ -0,0 +1,331 @@
import javafx.stage.Stage;
import java.util.Random;
/**
* 2019-03-10 (Edited on 2019-04-1)
* Author: Haris Muhammad
* ShipWarfareGUILogic class, logic for ships which the user can attack or run from
*/
public class ShipWarfareGUILogic extends Player {
private String titleMessage;
private String HPLeftMessage;
private String gunsLeftOrTakenMessage;
private String runAwayOrLeftMessage;
private String shipsRemainingMessage;
private String reportMessage;
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 boolean winOrLose = false;
private int counter1;
private int avenue;
public ShipWarfareGUILogic(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* 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;
}
}
public int getNumOfLittyShips(){
return 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;
}
public String getTitleMessage() {
return titleMessage;
}
public void setTitleMessage(String titleMessage) {
this.titleMessage = titleMessage;
}
public String getHPLeftMessage() {
return HPLeftMessage;
}
public void setHPLeftMessage(String HPLeftMessage) {
this.HPLeftMessage = HPLeftMessage;
}
public String getGunsLeftOrTakenMessage() {
return gunsLeftOrTakenMessage;
}
public void setGunsLeftOrTakenMessage(String gunsLeftOrTakenMessage) {
this.gunsLeftOrTakenMessage = gunsLeftOrTakenMessage;
}
public String getRunAwayOrLeftMessage() {
return runAwayOrLeftMessage;
}
public void setRunAwayOrLeftMessage(String runAwayOrLeftMessage) {
this.runAwayOrLeftMessage = runAwayOrLeftMessage;
}
public String getShipsRemainingMessage() {
return shipsRemainingMessage;
}
public void setShipsRemainingMessage(String shipsRemainingMessage) {
this.shipsRemainingMessage = shipsRemainingMessage;
}
public String getReportMessage() {
return reportMessage;
}
public void setReportMessage(String reportMessage) {
this.reportMessage = reportMessage;
}
public int getAvenue() {
return avenue;
}
public void setAvenue(int avenue) {
this.avenue = avenue;
}
/**
* 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;
}
/**
* 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 int destroyLittyShipsOrEscape() {
int calculateLoot = 0;
int chanceOfEnemyRun = 0;
int hitCounter = 0;
int missCounter = 0;
boolean gunFrustration = false;
setRunAwayOrLeftMessage("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) {
setReportMessage(String.format("Report: Ships hit: %d, Shots missed: %d", hitCounter, missCounter));
}
} else {
setReportMessage(("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) {
setRunAwayOrLeftMessage(String.format("Cowards! %d ships ran away %s! ", howMuchRun, getName()));
}
} else {
setReportMessage((String.format("Escaped %d of them %s!", howMuchRun, getName())));
}
}
}
}
setShipsRemainingMessage(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) {
setGunsLeftOrTakenMessage(String.format("Dang it! We only have %d guns left", getGuns()));
//playerShoots(getGuns() + 1);
} else {
setGunsLeftOrTakenMessage(String.format("We still have %d guns left", getGuns()));
}
setHPLeftMessage(String.format("EEK, our current ship status is %d%% ", getHP()));
if (userAttacks == false) {
userAttacks = true;
}
if (exitValue == 1) {
setAvenue(1);
calculateLoot = (startingLittyShips * 100) + randomValue.nextInt(startingLittyShips) * 200;
setMoney(getMoney() + calculateLoot);
setReportMessage(String.format("Our firm has earned $%,d in loot! ", calculateLoot));
return 1;
} else if (exitValue == 2) {
setAvenue(2);
return 2;
} else if (exitValue == 3) {
setAvenue(3);
setReportMessage(String.format("We made it out at %d%% ship status!", getHP()));
return 3;
}
else{
return 4;
}
}
/*
if (destroyLittyShipsOrEscape() == 1) {
wipe();
calculateLoot = (startingLittyShips * 100) + randomValue.nextInt(startingLittyShips) * 200;
setMoney(getMoney() + calculateLoot);
reportMessage = 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 (destroyLittyShipsOrEscape() == 2) {
GameEndGUI gameEndGUI = new GameEndGUI(getPlayer());
gameEndGUI.initializeGameEndGUI(stage);
stage.show();
return true;
} else if (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;
}
*/
}

View File

@@ -1,267 +0,0 @@
/**
* TaipanShopText deals with the text based version of the shop.
*
* Author: Vikram Bawa
*/
import java.util.Scanner;
public class TaipanShopText extends Player{
/**
* This method is evoked if the user is eligible to win, and chooses to end the game (by winning).
*/
public void retire(){
setRetire(true);
System.out.println("You win!");
System.exit(0);
}
/**
* constructor; only runs when a Player object is provided. The constructor is fully encapsulated.
*
* @param player is a Player object that will be copied and the player instance variable is set to the copy.
*/
public TaipanShopText(Player player){
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* this method is evoked if the user has decided to travel elsewhere.
*/
public void travel(){
TravelText travel = new TravelText(getPlayer());
travel.travelTo();
setPlayer(travel.getPlayer());
}
/**
* this method is evoked if the user wants to use the warehouse to store items or take items out.
*/
public void warehouse(){
WarehouseText warehouse = new WarehouseText(getPlayer());
warehouse.changeWarehouse();
setPlayer(warehouse.getPlayer());
}
/**
* this method is evoked if the user wants to use the bank to deposit or withdraw money.
*/
public void bank(){
BankText bank = new BankText(getPlayer());
bank.bank();
setPlayer(bank.getPlayer());
}
/**
* this method is evoked if the user wants to use get a loan or pay a loan off.
*/
public void loan(){
loanSharkText loan = new loanSharkText(getPlayer());
loan.loanMoney();
setPlayer(loan.getPlayer());
}
/**
* this method prints the shop UI and the player's inventory and status.
*/
public void printShop(){
int currentCargo = getOpiumHeld()+getGuns()*10+getSilkHeld()+getArmsHeld()+getGeneralHeld();
if(getCargoSpace() - currentCargo < 0){
System.out.println("Hold: Overloaded" + " Guns: " + getGuns() + " HP: " + getHP() +"%");
}else{
System.out.println("Hold: " + (getCargoSpace()-currentCargo) + " Guns: " + getGuns() + " HP: " + getHP() +"%");
}
System.out.println("-------------------------------------------------------------");
System.out.println(" Opium: " + getOpiumHeld() + " Silk: " + getSilkHeld());
System.out.println(" Arms: " + getArmsHeld() + " General: " + getGeneralHeld());
System.out.println("-------------------------------------------------------------");
System.out.println("Cash: " + getMoney() + " Bank: " + getBank()+ " Debt: " + getDebt()+"\n");
System.out.println(getName() + ", present prices per unit here are:");
System.out.println(" Opium: " + getOpiumPrice() + " Silk: " + getSilkPrice());
System.out.println(" Arms: " + getArmsPrice() + " General: " + getGeneralPrice());
}
/**
* this is the shop method. Activates the shopping screen.
*/
public void shop(){
TaipanShopLogic logic = new TaipanShopLogic(getPlayer());
System.out.println(logic.updatePrices());
setPlayer(logic.getPlayer());
boolean notDone = true;
int caseNum;
String optionText;
// first case is triggered if the user is at location one, and has less than $1 million net worth
if (getLocation() == 1 && getBank()+getMoney()-getDebt() < 1000000) {
caseNum = 1;
optionText = " Visit Bank, Transfer Cargo, Get Loans,";
} // the second case is triggered if the user is at a location other than location one.
else if(getLocation() != 1) {
caseNum = 2;
optionText = "";
} // the last case is triggered when the other conditions are not met; it is triggered when the user has a net
// worth that is greater than or equal to $1 million and is at location one.
else{
caseNum = 3;
optionText = " Visit Bank, Transfer Cargo, Get Loans, Retire,";
}
Scanner input = new Scanner(System.in);
// as long as the user does not enter a valid input, the code will run in a loop forever.
while(notDone){
printShop();
System.out.printf("\nShall I Buy, Sell,%s or Quit Trading?\n", optionText);
String response = input.next();
if (response.equalsIgnoreCase("B")) {
boolean notDone2 = true;
System.out.println("What do you wish me to buy, " + getName() + "?");
// when buying an item, the user must have the right amount of money, and buy non-negative amounts.
while (notDone2) {
response = input.nextLine();
if (response.equalsIgnoreCase("O")) {
System.out.println("\nHow much Opium shall I buy, " + getName() + "? (You can afford " + getMoney() / getOpiumPrice() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= getMoney() /getOpiumPrice() && num >= 0) {
setOpiumHeld(getOpiumHeld()+num);
setMoney(getMoney()-num *getOpiumPrice());
notDone2 = false;
} else if (num >= 0) {
System.out.println(getName() + ", you can't afford that!");
} else {
System.out.println(getName() + ", how am I supposed to buy " + "'" + num + "'" + " Opium?");
}
}
} else if (response.equalsIgnoreCase("S")) {
System.out.println("\nHow much Silk shall I buy, " + getName() + "? (You can afford " + getMoney() /getSilkPrice() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= getMoney() /getSilkPrice() && num >= 0) {
setSilkHeld(getSilkHeld()+num);
setMoney(getMoney()-num *getSilkPrice());
notDone2 = false;
} else if (num >= 0) {
System.out.println(getName() + ", you can't afford that!");
} else {
System.out.println(getName() + ", how am I supposed to buy " + "'" + num + "'" + " Silk?");
}
}
} else if (response.equalsIgnoreCase("A")) {
System.out.println("\nHow many Arms shall I buy, " + getName() + "? (You can afford " + getMoney() /getArmsPrice() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= getMoney() /getArmsPrice() && num >= 0) {
setArmsHeld(getArmsHeld()+num);
setMoney(getMoney() - num*getArmsPrice());
notDone2 = false;
} else if (num >= 0) {
System.out.println(getName() + ", you can't afford that!");
} else {
System.out.println(getName() + ", how am I supposed to buy " + "'" + num + "'" + " Arms?");
}
}
} else if (response.equalsIgnoreCase("G")) {
System.out.println("\nHow much General Cargo shall I buy, " + getName() + "? (You can afford " + getMoney() /getGeneralPrice() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= getMoney() /getGeneralPrice() && num >= 0) {
setGeneralHeld(getGeneralHeld()+num);
setMoney(getMoney() - num*getGeneralPrice());
notDone2 = false;
} else if (num >= 0) {
System.out.println(getName() + ", you can't afford that!");
} else {
System.out.println(getName() + ", how am I supposed to buy " + "'" + num + "'" + " General Cargo?");
}
}
}
}
} // when selling, the user must enter a non-negative amount of items, and not more than what they have.
else if (response.equalsIgnoreCase("S")) {
boolean notDone2 = true;
System.out.println("What do you wish me to sell, " + getName() + "?");
while (notDone2) {
response = input.nextLine();
if (response.equalsIgnoreCase("O")) {
System.out.println("\nHow much Opium shall I sell, " + getName() + "? (You have " + getOpiumHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= getOpiumHeld() && num >= 0) {
setOpiumHeld(getOpiumHeld()-num);
setMoney(getMoney() + num*getOpiumPrice());
notDone2 = false;
} else if (num >= 0) {
System.out.println(getName() + ", you don't have that many to sell!");
} else {
System.out.println(getName() + ", how am I supposed to sell " + "'" + num + "'" + " Opium?");
}
}
} else if (response.equalsIgnoreCase("S")) {
System.out.println("\nHow much Silk shall I sell, " + getName() + "? (You have " + getSilkHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= getSilkHeld() && num >= 0) {
setSilkHeld(getSilkHeld()-num);
setMoney(getMoney() + num*getSilkPrice());
notDone2 = false;
} else if (num >= 0) {
System.out.println(getName() + ", you don't have that many to sell!");
} else {
System.out.println(getName() + ", how am I supposed to sell " + "'" + num + "'" + " Silk?");
}
}
} else if (response.equalsIgnoreCase("A")) {
System.out.println("\nHow many Arms shall I sell, " + getName() + "? (You have " + getArmsHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= getArmsHeld() && num >= 0) {
setArmsHeld(getArmsHeld()-num);
setMoney(getMoney() + num*getArmsPrice());
notDone2 = false;
} else if (num >= 0) {
System.out.println(getName() + ", you don't have that many to sell!");
} else {
System.out.println(getName() + ", how am I supposed to sell " + "'" + num + "'" + " Arms?");
}
}
} else if (response.equalsIgnoreCase("G")) {
System.out.println("\nHow much General Cargo shall I sell, " + getName() + "? (You have " + getGeneralHeld() + ")");
while (notDone2) {
int num = input.nextInt();
if (num <= getGeneralHeld() && num >= 0) {
setGeneralHeld(getGeneralHeld()-num);
setMoney(getMoney() + num*getGeneralPrice());
notDone2 = false;
} else if (num >= 0) {
System.out.println(getName() + ", you don't have that many to sell!");
} else {
System.out.println(getName() + ", how am I supposed to sell " + "'" + num + "'" + " General Cargo?");
}
}
}
}
} else if (response.equalsIgnoreCase("V") && (caseNum == 1 || caseNum == 3)) {
bank();
} else if (response.equalsIgnoreCase("T") && (caseNum == 1 || caseNum == 3)) {
warehouse();
} else if ((response.equalsIgnoreCase("G")||response.equalsIgnoreCase("L")) && (caseNum == 1 || caseNum == 3)) {
loan();
} // if the user wishes to quit trading, they may do so. Doing this breaks them out of the loop.
else if (response.equalsIgnoreCase("Q") && (caseNum == 1 || caseNum == 3)) {
travel();
notDone = false;
} // if the user wishes to retire and win the game, they may do so. Doing this breaks them out of the loop.
else if (response.equalsIgnoreCase("R") && caseNum == 3) {
retire();
notDone = false;
}
}
}
}