Moved everything into logic classes

This commit is contained in:
2019-04-07 14:44:41 -06:00
parent 9e97877e6e
commit e2ad32c4ee
22 changed files with 456 additions and 242 deletions

185
src/gui/BankGUI.java Normal file
View File

@@ -0,0 +1,185 @@
package gui;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import logic.Player;
public class BankGUI extends Player {
/**
* 2019-03-10
* Authors: Siddhant Dewani
* BankGUI allows the user to store cash and gain interest off of the cash
*/
/**
* Class Constructor that takes in a type player as a parameter
*
* @param player object of the class Player
*/
public BankGUI(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* Initializes the GUI for the Bank in our game.
*
* @param primaryStage
* @return
*/
public Stage initializeBank(Stage primaryStage) {
primaryStage.setTitle("Bank");
/**
* Creating all the layouts, labels, buttons, and a textfield.
*
*/
BorderPane brdr1 = new BorderPane();
HBox hbx1 = new HBox(30);
HBox hbx2 = new HBox(30);
VBox vbx1 = new VBox(30);
Label l1 = new Label("Player: " + getName());
Label l2 = new Label("Balance: " + getBank());
Label l3 = new Label("Enter Amount: ");
Label l4 = new Label("Cash: " + getMoney());
Label l5 = new Label(" ");
Button b1 = new Button("Withdraw");
Button b2 = new Button("Deposit");
Button b3 = new Button("Go back");
TextField txtField1 = new TextField();
/**
* Adds the buttons so that they are at the bottom of the screen.
*
*/
hbx1.setAlignment(Pos.CENTER);
hbx1.getChildren().add(b1);
hbx1.getChildren().add(b2);
hbx1.getChildren().add(b3);
hbx1.setPadding(new Insets(0, 0, 20, 0));
brdr1.setBottom(hbx1);
/**
* Adds the text field to the center of the screen.
*
*/
hbx2.setAlignment(Pos.CENTER);
hbx2.getChildren().add(l3);
hbx2.getChildren().add(txtField1);
brdr1.setCenter(hbx2);
/**
* Adds the labels to the top of the screen.
*
*/
vbx1.setAlignment(Pos.CENTER);
vbx1.getChildren().add(l1);
vbx1.getChildren().add(l2);
vbx1.getChildren().add(l4);
vbx1.getChildren().add(l5);
vbx1.setPadding(new Insets(20, 0, 0, 0));
brdr1.setTop(vbx1);
/**
* Adds function to button 1 which, when clicked, withdraws money from your bank to your person but, will not let you overdraw.
*
*/
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
int withdraw = Integer.parseInt(txtField1.getText());
if (withdraw < 0) {
l5.setText("Come on " + getName() + ", are you trying to fool me?\nNo negative numbers please!");
} else if (withdraw <= getBank()) {
setMoney(withdraw + getMoney());
setBank(getBank() - withdraw);
} else {
l5.setText("Sorry, you can not withdraw that much.");
}
l2.setText("Balance: " + getBank());
l4.setText("Cash: " + getMoney());
} catch (Exception e) {
l5.setText("Please enter a valid response.");
}
}
}
);
/**
* Adds function to button 2 which, when clicked, deposits money into your bank but, will not let you overdraw.
*
*/
// Set the event handler when the withdraw button is clicked
b2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
int deposit = Integer.parseInt(txtField1.getText());
if (deposit < 0) {
l5.setText("Nice try! You can not enter negative numbers.");
} else if (deposit <= getMoney()) {
setBank(deposit + getBank());
setMoney(getMoney() - deposit);
} else {
l5.setText("Sorry, you can not deposit that much.");
}
l2.setText("Balance: " + getBank());
l4.setText("Cash: " + getMoney());
} catch (Exception e) {
l5.setText("Please enter a valid response.");
}
}
}
);
/**
* Adds function to button 3 which, when clicked, brings you back to the Shop GUI.
*
*/
b3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
TaipanShopGUI shopGUI = new TaipanShopGUI(getPlayer());
shopGUI.initializeShop(primaryStage);
primaryStage.show();
}
}
);
/**
* Sets the window size to a width of 600 and height of 480 and displays the screen.
*
*/
Scene scene = new Scene(brdr1, 600, 480);
scene.getStylesheets().add("styleguide.css");
primaryStage.setScene(scene);
return primaryStage;
}
/**
* sets scene and runs stage
*
* @param primaryStage the stage in which the scene may be run and switched to
*/
public void start(Stage primaryStage) {
BankGUI bank = new BankGUI(getPlayer());
bank.initializeBank(primaryStage);
primaryStage.show();
}
}

123
src/gui/GameEndGUI.java Normal file
View File

@@ -0,0 +1,123 @@
package gui;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import logic.Player;
/**
* 2019-03-10
* Authors: Harkamal, Vikram, Haris, Siddhant, Nathan
* GameEndGUI class, Initializes and displays the graphical interface for when you lose
*
*/
public class GameEndGUI extends Player {
private Label title;
private VBox vBox;
private Label firmName;
private Label gunsHeld;
private Label netWorth;
private BorderPane borderPane;
public GameEndGUI(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* Sets up the graphical part of GameEndGUI and includes all logic for the class
*
* @param stage sets the stage to which we will execute the scene of the GameEndGUI class
* @return stage so that another class can switch to the stage
*/
public Stage initializeGameEndGUI(Stage stage) {
//Creating all the nodes
title = new Label();
vBox = new VBox();
firmName = new Label();
gunsHeld = new Label();
netWorth = new Label();
borderPane = new BorderPane();
int netWorthInt = 0;
borderPane.setPrefHeight(480.0);
borderPane.setPrefWidth(600.0);
//Setting positions and names of all the nodes
BorderPane.setAlignment(title, javafx.geometry.Pos.CENTER);
title.setText("Game Over!");
title.setFont(new Font(43.0));
BorderPane.setMargin(title, new Insets(0.0));
title.setPadding(new Insets(50.0, 0.0, 0.0, 0.0));
borderPane.setTop(title);
BorderPane.setAlignment(vBox, javafx.geometry.Pos.CENTER);
vBox.setAlignment(javafx.geometry.Pos.CENTER);
vBox.setPrefHeight(200.0);
vBox.setPrefWidth(100.0);
firmName.setText("Name:");
firmName.setFont(new Font(22.0));
gunsHeld.setText("Guns Held:");
gunsHeld.setFont(new Font(22.0));
netWorth.setText("Net Worth:");
netWorth.setFont(new Font(22.0));
borderPane.setCenter(vBox);
//Adding the labels to the character's stats to the VBox which will show up on the screen
vBox.getChildren().add(firmName);
vBox.getChildren().add(gunsHeld);
vBox.getChildren().add(netWorth);
/**
* If health is below or equal to 0 then the game will either show the gameOver screen or the win screen
* */
if (getHP() <= 0) {
title.setText("Game Over!");
} else {
title.setText("Congratulations!");
}
/**
* Calculates the networth of the player by the end of the game
* */
netWorthInt = getMoney() + (getOpiumHeld() * 16000) + (getSilkHeld() * 160) + (getArmsHeld() * 160) + (getGeneralHeld() * 8);
netWorthInt += (getwOpium() * 16000) + (getwSilk() * 160) + (getwArms() * 160) + (getwGeneral() * 8);
netWorthInt -= getDebt();
//Updating the endgame stats of the player
firmName.setText("Firm Name: " + getName());
gunsHeld.setText("Guns Held: " + getGuns());
netWorth.setText("Net Worth: " + netWorthInt);
Scene root = new Scene(borderPane, 600, 480);
root.getStylesheets().add("styleguide.css");
stage.setTitle("End Game Stats");
stage.setResizable(false);
stage.setScene(root);
return stage;
}
/**
* sets scene and runs stage
*
* @param primaryStage the stage in which the scene may be run and switched to
*/
public void start(Stage primaryStage) {
GameEndGUI gameEndGUI = new GameEndGUI(getPlayer());
gameEndGUI.initializeGameEndGUI(primaryStage);
primaryStage.show();
}
}

184
src/gui/LoanSharkGUI.java Normal file
View File

@@ -0,0 +1,184 @@
package gui;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import logic.Player;
/**
* 2019-03-10
* Authors: Siddhant Dewani
* LoanShark GUI Class allows the user to get a loan from the loan shark
*/
public class LoanSharkGUI extends Player {
/**
* Class Constructor that takes in a type player as a parameter
*
* @param player object of the class Player
*/
public LoanSharkGUI(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* This methods purpose is to loan the player the funds it wants
* or pay its outstanding debts. The method prompts the user if they
* would like to borrow money or repay. depending on what the player chooses
* the corresponding loop is evoked. The player can only be loaned 2 times the
* money they have minus the debt if their debt exceeds the cash balance, the loan
* cannot be given.
*
* @param primaryStage the stage upon which the GUI will be imposed
*/
public Stage initializeLoanShark(Stage primaryStage) {
primaryStage.setTitle("Loan Shark");
//Declaring each Layout
BorderPane brdr1 = new BorderPane();
HBox hbx1 = new HBox(10);
HBox hbx2 = new HBox(10);
VBox vbx1 = new VBox(10);
//Declaring all Variables
Label l1 = new Label("Player: " + getName());
Label l2 = new Label("Debt " + getDebt());
Label l4 = new Label("Cash: " + getMoney());
Label l3 = new Label("Enter Amount: ");
Label l5 = new Label(" ");
//Declaring All Buttons
Button b1 = new Button("Borrow");
Button b2 = new Button("Repay");
Button b3 = new Button("Go back");
//Declaring All TextFields
TextField txtField1 = new TextField();
//Creating the buttons at the bottom of the screen
hbx1.setAlignment(Pos.CENTER);
hbx1.getChildren().add(b1);
hbx1.getChildren().add(b2);
hbx1.getChildren().add(b3);
hbx1.setPadding(new Insets(0,0,20,0));
brdr1.setBottom(hbx1);
//Creating the TextField at the center of the screen
hbx2.setAlignment(Pos.CENTER);
hbx2.getChildren().add(l3);
hbx2.getChildren().add(txtField1);
brdr1.setCenter(hbx2);
//Creating the Labels at the top of the Screen
vbx1.setAlignment(Pos.CENTER);
vbx1.getChildren().add(l1);
vbx1.getChildren().add(l2);
vbx1.getChildren().add(l4);
vbx1.getChildren().add(l5);
vbx1.setPadding(new Insets(20,0,0,0));
brdr1.setTop(vbx1);
// Set the event handler when the deposit button is clicked
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
int loanAsk = Integer.parseInt(txtField1.getText());
if (loanAsk <= 2 * (getMoney() - getDebt()) && loanAsk >= 0) {
setDebt(getDebt() + loanAsk);
setMoney(getMoney() + loanAsk);
l4.setText("Cash: " + getMoney());
} else if (loanAsk < 0) {
l5.setText("Sorry you cannot enter negative numbers");
}
else{
l5.setText("Sorry you cannot get the loan requested");
}
l2.setText("Debt: " + getDebt());
} catch (Exception e) {
l5.setText("Please enter a valid value");
}
}
}
);
// Set the event handler when the withdraw button is clicked
b2.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
int returnAsk = Integer.parseInt(txtField1.getText());
if (returnAsk > getDebt()) {
l5.setText("You do not need to return that much.");
}
else if (returnAsk <= getDebt() && returnAsk >= 0 && getMoney() >= returnAsk) {
setDebt(getDebt() - returnAsk);
setMoney(getMoney() - returnAsk);
l4.setText("Cash: " + getMoney());
}
else if(getMoney() < returnAsk) {
l5.setText("Look " + getName() + ", you are being cheap!");
}
else {
l5.setText("Sorry, you can not return a negative amount!");
}
l2.setText("Debt: " + getDebt());
}
catch (Exception e) {
l5.setText("Please enter a valid value");
}
}
}
);
b3.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
TaipanShopGUI shopGUI = new TaipanShopGUI(getPlayer());
shopGUI.initializeShop(primaryStage);
primaryStage.show();
}
}
);
//Setting the Scene and displaying it
Scene scene = new Scene(brdr1, 600, 480);
scene.getStylesheets().add("styleguide.css");
primaryStage.setScene(scene);
//primaryStage.show();
return primaryStage;
}
public void start(Stage primaryStage) {
LoanSharkGUI loan = new LoanSharkGUI(getPlayer());
loan.initializeLoanShark(primaryStage);
primaryStage.show();
}
}

30
src/gui/MainGUI.java Normal file
View File

@@ -0,0 +1,30 @@
package gui;
import javafx.application.Application;
import javafx.stage.Stage;
/**
* 2019-03-10
* Authors: Harkamal, Vikram, Haris, Siddhant, Nathan
* MainGUI class, Initializes the entire game and runs the game for user to play
*
*/
public class MainGUI extends Application {
/**
* Updates main class with player data and starts the game.
* The game will only run as long as the player has not retired or has been destroyed.
*
* @param args Just the console for the player to look at.
*/
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
StartGUI start = new StartGUI(new Player());
start.initializeStart(primaryStage);
primaryStage.show();
}
}

232
src/gui/RandomEventGUI.java Normal file
View File

@@ -0,0 +1,232 @@
package gui;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import logic.Player;
import java.util.Random;
/**
* 2019-03-19
* Authors: Harkamal Randhawa
* Random Event GUI class generates random events that occur during travel, such as fixing your ship,
* liu yen asking for money and to purchase a gun.
*/
public class RandomEventGUI extends Player {
private HBox hBox;
private Button yesButton;
private Button noButton;
private VBox vBox;
private Label paymentLabel;
private Label sellingItemLabel;
private Label cannotAffordLabel;
private VBox vBox0;
private Label shipHPLabel;
private Label gunsShipLabel;
private Label moneyPlayerLabel;
private Label moneyBankLabel;
private Label cargoShipLabel;
private Label cargoWarehouseLabel;
private BorderPane borderPane;
private int eventNumber = 0;
private int itemPrice = 10;
/**
* 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 RandomEventGUI(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* Initializes randomEvent on the given stage as a parameter.
*
* @param stage
*/
public Stage initializeRandomEventGUI(Stage stage) {
//Creating the nodes within the event screen
hBox = new HBox();
yesButton = new Button();
noButton = new Button();
vBox = new VBox();
paymentLabel = new Label();
sellingItemLabel = new Label();
cannotAffordLabel = new Label();
vBox0 = new VBox();
shipHPLabel = new Label();
gunsShipLabel = new Label();
moneyPlayerLabel = new Label();
moneyBankLabel = new Label();
cargoShipLabel = new Label();
borderPane = new BorderPane();
//Messing with the alignments of the buttons and their text
borderPane.setAlignment(hBox, javafx.geometry.Pos.CENTER);
hBox.setAlignment(javafx.geometry.Pos.CENTER);
hBox.setPrefHeight(100.0);
hBox.setPrefWidth(200.0);
hBox.setSpacing(10.0);
yesButton.setMnemonicParsing(false);
yesButton.setText("Yes");
yesButton.setDefaultButton(true);
noButton.setMnemonicParsing(false);
noButton.setText("No");
borderPane.setBottom(hBox);
//Making the vbox to put all Labels for the events
borderPane.setAlignment(vBox, javafx.geometry.Pos.CENTER);
vBox.setAlignment(javafx.geometry.Pos.CENTER);
vBox.setPrefHeight(200.0);
vBox.setPrefWidth(100.0);
paymentLabel.setText("Would you like to pay?");
sellingItemLabel.setText("for a Gun?");
cannotAffordLabel.setText("You can't afford that!");
cannotAffordLabel.setFocusTraversable(false);
borderPane.setCenter(vBox);
borderPane.setAlignment(vBox0, javafx.geometry.Pos.CENTER);
vBox0.setAlignment(javafx.geometry.Pos.CENTER);
vBox0.setPrefHeight(200.0);
vBox0.setPrefWidth(100.0);
//Update the labels to fit the player's stats
shipHPLabel.setText("Ship Health: " + getPlayer().getHP());
gunsShipLabel.setText("Number of Guns Remaining: " + getPlayer().getGuns());
moneyPlayerLabel.setText("Money on Player: " + getPlayer().getMoney());
moneyBankLabel.setText("Money in Bank: " + getPlayer().getBank());
cargoShipLabel.setText("Ship Cargo Space: " + getPlayer().getCargoSpace());
//Adding all the buttons and labels to the elements in the scene
hBox.getChildren().add(yesButton);
hBox.getChildren().add(noButton);
vBox.getChildren().add(sellingItemLabel);
vBox.getChildren().add(paymentLabel);
vBox.getChildren().add(cannotAffordLabel);
vBox0.getChildren().add(shipHPLabel);
vBox0.getChildren().add(gunsShipLabel);
vBox0.getChildren().add(moneyPlayerLabel);
vBox0.getChildren().add(moneyBankLabel);
vBox0.getChildren().add(cargoShipLabel);
//Adding all the elements to the scene
borderPane.setTop(vBox0);
borderPane.setTop(vBox0);
borderPane.setCenter(vBox);
borderPane.setBottom(hBox);
//Make it so that the player can't see the can't afford label until after they actually can't understand
cannotAffordLabel.setVisible(false);
/*Pick a random number dictating the events that could happen.
* 1: New gun for player
* 2: Paying Liu Yuen
* 3: Repairing the Ship
*/
Random rand = new Random();
int randGenNum = rand.nextInt(3) + 1;
while(true){
//Buy Guns
if (randGenNum == 1) {
itemPrice = (int) ((getPlayer().getMoney() * 0.1) + 10);
sellingItemLabel.setText("A vender is selling a gun for $" + itemPrice + " for a gun?");
break;
}
//Liu Yuen
if (randGenNum == 2) {
itemPrice = (int) ((getPlayer().getMoney() * 0.1) + 10);
sellingItemLabel.setText("Liu Yuen asks $" + itemPrice + " in donation to the temple of Tin Hau, the Sea Goddess");
setAttackingShips(true);
break;
}
//Ship Repair
if (randGenNum == 3 && getHP() < 100) {
itemPrice = (int) ((100 - getPlayer().getHP()) * 10 + 10);
sellingItemLabel.setText("Mc Henry from the Hong Kong shipyard has arrived,\n would be willing to repair your ship for $" + itemPrice);
break;
}
else {
randGenNum = 2;
}
}
eventNumber = randGenNum;
if((eventNumber == 1 && getCargoSpace() < 10)){
TaipanShopGUI taipanShopGUI = new TaipanShopGUI(getPlayer());
taipanShopGUI.initializeShop(stage);
stage.show();
}
if((eventNumber == 3 && getPlayer().getHP() >= 100)){
TaipanShopGUI taipanShopGUI = new TaipanShopGUI(getPlayer());
taipanShopGUI.initializeShop(stage);
stage.show();
}
//if yes button is clicked, executes the code depending on the type of event
yesButton.setOnAction(event -> {
if(getPlayer().getMoney() > itemPrice) {
//Buy Guns
if (eventNumber == 1 && (getCargoSpace() >= 10)) {
setGuns(getPlayer().getGuns() + 1);
setMoney(getPlayer().getMoney() - itemPrice);
TaipanShopGUI taipanShopGUI = new TaipanShopGUI(getPlayer());
taipanShopGUI.initializeShop(stage);
stage.show();
}
//Liu Yuen
if (eventNumber == 2) {
setAttackingShips(false);
setMoney(getPlayer().getMoney() - itemPrice);
TaipanShopGUI taipanShopGUI = new TaipanShopGUI(getPlayer());
taipanShopGUI.initializeShop(stage);
stage.show();
}
//Ship Repair
if (eventNumber == 3 && getPlayer().getHP() != 100) {
setHP(100);
setMoney(getPlayer().getMoney() - itemPrice);
TaipanShopGUI taipanShopGUI = new TaipanShopGUI(getPlayer());
taipanShopGUI.initializeShop(stage);
stage.show();
}
}
else{
cannotAffordLabel.setVisible(true);
yesButton.setVisible(false);
noButton.setDefaultButton(true);
noButton.setVisible(true);
}
});
//If the no button is clicked then it skips to the location screen you wanted to go to.
noButton.setOnAction(event -> {
TaipanShopGUI taipanShopGUI = new TaipanShopGUI(getPlayer());
taipanShopGUI.initializeShop(stage);
stage.show();
});
//Creates the scene and window
Scene root = new Scene(borderPane, 600, 480);
root.getStylesheets().add("styleguide.css");
stage.setTitle("Travel");
stage.setResizable(false);
stage.setScene(root);
return stage;
}
}

601
src/gui/ShipWarfareGUI.java Normal file
View File

@@ -0,0 +1,601 @@
package gui;
import javafx.animation.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.FileInputStream;
import java.util.Random;
//Importing the logic classes required for this class
import logic.Player;
import logic.ShipWarfareLogic;
/**
* 2019-03-10 (Edited on 2019-03-23)
* Author: Haris Muhammad
* ShipWarfareGUI class, Generates and utilizes ships which the user can attack or run from
*/
public class ShipWarfareGUI extends Player {
ShipWarfareLogic logic = new ShipWarfareLogic(getPlayer());
private ShipWarfareGUI ship;
private Circle cannon;
private VBox buttonBox;
private HBox fightRunBox;
private Button fightButton;
private Button runButton;
private Button continueButton;
private VBox labelBox;
private Label title;
private Label HPLeft;
private Label gunsLeftOrTaken;
private Label runAwayOrLeft;
private Label shipsRemaining;
private Label report;
private boolean winOrLose = false;
private boolean userAttacks = true;
private int howMuchRun = 0;
private int counter = 0;
private String pirateName = "Liu Yen";
private int beginningX = 150;
private int beginningY = 245;
private int endX = 350;
private int endY = 90;
private TranslateTransition shotsFired = new TranslateTransition();
private TranslateTransition enemyShots = new TranslateTransition();
/**
* 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 ShipWarfareGUI(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* Sets most of the labels invisible
*/
public void wipe() {
wipeWithTitle(title);
}
public void wipeWithTitle(Label title) {
title.setVisible(false);
runAwayOrLeft.setVisible(false);
shipsRemaining.setVisible(false);
HPLeft.setVisible(false);
gunsLeftOrTaken.setVisible(false);
}
/**
* Sets most of the labels invisible including the fight or run label
*/
public void completeWipe() {
wipe();
report.setVisible(false);
}
/**
* The user faces off against the ships and either prevails, dies, or runs away
* @return true if the user wins, loses, or flees, it returns false otherwise
*/
public boolean destroyShipsOrEscape(Stage stage) throws Exception {
cannon.setLayoutX(beginningX);
cannon.setLayoutY(beginningY);
int calculateLoot = 0;
int chanceOfEnemyRun = 0;
int hitCounter = 0;
int missCounter = 0;
boolean gunFrustration = false;
runAwayOrLeft.setText("No ships ran away");
Random randomValue = new Random();
int exitValue = 0;
//Player volley
if (super.getGuns() > 0) {
for (int j = 0; j < super.getGuns(); j++) {
if (userAttacks == true) {
int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) {
logic.setNumOfShips(logic.getNumOfShips()-1);
if (logic.getNumOfShips() <= 0) {
exitValue = 1;
}
hitCounter++;
} else {
missCounter++;
}
}
}
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 (logic.getNumOfShips() <= 0) {
exitValue = 1;
}
if (super.getGuns() > 0) {
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1;
if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
logic.setNumOfShips(logic.getNumOfShips() - howMuchRun);
if (userAttacks == true) {
if (howMuchRun > 0) {
runAwayOrLeft.setText(String.format("Cowards! %d ships ran away %s! ", howMuchRun, super.getName()));
}
} else {
report.setText((String.format("Escaped %d of them %s!", howMuchRun, super.getName())));
}
}
}
}
shipsRemaining.setText(String.format("%d ships remaining and they look angry!", logic.getNumOfShips()));
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && super.getGuns() > 0) {
super.setGuns(super.getGuns() - 1);
gunFrustration = true;
} else {
if (logic.getNumOfShips() > 0) {
int HPTaken = randomValue.nextInt(10) + 1;
super.setHP(super.getHP() - (HPTaken));
}
}
if (super.getHP() <= 0) {
exitValue = 2;
//break;
}
if (gunFrustration == true) {
gunsLeftOrTaken.setText(String.format("Dang it! We only have %d guns left", super.getGuns()));
playerShoots(getGuns() + 1);
} else {
gunsLeftOrTaken.setText(String.format("We still have %d guns left", super.getGuns()));
}
HPLeft.setText(String.format("EEK, our current ship status is %d%% ", super.getHP()));
if (userAttacks == false) {
userAttacks = true;
}
//The user defeats the enemy fleet
if (exitValue == 1) {
wipe();
calculateLoot = logic.calculateLoot();
super.setMoney(logic.getMoney());
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;
}
return false;
}
/**
* Player attacks enemy ships in an animation
*/
public void playerShoots(int amountOfShots) {
shotsFired.setFromX(0);
shotsFired.setFromY(0);
shotsFired.setToX(endX);
shotsFired.setToY(endY);
shotsFired.setDuration(Duration.seconds(0.5));
if (super.getGuns() > 0) {
shotsFired.setCycleCount(amountOfShots);
} else {
shotsFired.setCycleCount(0);
shotsFired.stop();
cannon.setVisible(false);
}
shotsFired.setNode(cannon);
shotsFired.play();
}
/**
* Ships attack player ship back in an animation
*/
public void shipsRetaliate() {
cannon.setVisible(true);
enemyShots.setFromX(270);
enemyShots.setFromY(0);
enemyShots.setToX(-30);
enemyShots.setToY(90);
enemyShots.setDuration(Duration.seconds(0.5));
enemyShots.setCycleCount(1);
enemyShots.setNode(cannon);
enemyShots.play();
}
/**
* Sets most buttons to being invisble and switches to TaipanShop scene
* @param stage stage the user incorporates when they utilize the GUI
*/
public void setVisibilitiesAndTransition(Stage stage) {
completeWipe();
continueButton.setVisible(true);
continueButton.setDefaultButton(true);
fightButton.setVisible(false);
runButton.setVisible(false);
/**
* Switches to Taipan Shop scene
* @param event, once button is clicked, executes graphical information
*/
continueButton.setOnAction(event -> {
TaipanShopGUI shop = new TaipanShopGUI(getPlayer());
shop.initializeShop(stage);
stage.show();
});
}
/**
* Generaties ships and deploys logic for the shipwarfare
* @param primaryStage sets up the stage to whcih the GUI may be based around
* @throws Exception in case of interruptions withing the graphical interface
*/
public void initializeShip(Stage primaryStage) throws Exception {
logic.setNumOfShips(logic.numOfShips());
Pane root = new Pane();
HBox usAgainstEnemyDivisor;
BorderPane centeringUserShipPane = new BorderPane();
Circle cannon;
BorderPane centeringShipPane = new BorderPane();
BorderPane encompassingPane = new BorderPane();
usAgainstEnemyDivisor = new HBox();
cannon = new Circle();
this.cannon = cannon;
cannon.setVisible(false);
buttonBox = new VBox();
fightRunBox = new HBox();
fightButton = new Button();
runButton = new Button();
continueButton = new Button();
labelBox = new VBox();
title = new Label();
HPLeft = new Label();
gunsLeftOrTaken = new Label();
runAwayOrLeft = new Label();
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?", logic.getNumOfShips()));
fightButton.setText("Fight");
runButton.setText("Run");
continueButton.setText("Continue");
fightButton.setVisible(true);
runButton.setVisible(true);
continueButton.setVisible(false);
encompassingPane.setAlignment(labelBox, javafx.geometry.Pos.CENTER);
labelBox.setAlignment(javafx.geometry.Pos.CENTER);
labelBox.setPrefHeight(41.0);
labelBox.setPrefWidth(600.0);
labelBox.setSpacing(20.0);
labelBox.setPadding(new Insets(10.0, 0.0, 0.0, 0.0));
encompassingPane.setAlignment(buttonBox, javafx.geometry.Pos.CENTER);
buttonBox.setAlignment(javafx.geometry.Pos.TOP_CENTER);
fightRunBox.setAlignment(javafx.geometry.Pos.CENTER);
fightRunBox.setPrefHeight(100.0);
fightRunBox.setPrefWidth(200.0);
fightRunBox.setSpacing(10.0);
VBox.setMargin(continueButton, new Insets(0.0, 0.0, 20.0, 0.0));
root.getChildren().add(cannon);
encompassingPane.setPrefHeight(480);
encompassingPane.setPrefWidth(600);
usAgainstEnemyDivisor.setPrefHeight(0.0);
usAgainstEnemyDivisor.setPrefWidth(600.0);
centeringUserShipPane.setPrefHeight(200.0);
centeringUserShipPane.setPrefWidth(200.0);
Image ourShip;
Image enemyShip;
try {
ourShip = new Image(new FileInputStream("src/images/ourShip.png"));
enemyShip = new Image(new FileInputStream("src/images/enemyShip.png"));
} catch (Exception e) {
ourShip = new Image(new FileInputStream("images/ourShip.png"));
enemyShip = new Image(new FileInputStream("images/enemyShip.png"));
}
//Setting the image view
ImageView userShip = new ImageView(ourShip);
ImageView Ship = new ImageView(enemyShip);
BorderPane.setAlignment(userShip, javafx.geometry.Pos.CENTER);
userShip.setFitHeight(150.0);
userShip.setFitWidth(248.0);
userShip.setPickOnBounds(true);
userShip.setPreserveRatio(true);
centeringUserShipPane.setCenter(userShip);
BorderPane.setAlignment(buttonBox, javafx.geometry.Pos.CENTER);
buttonBox.setAlignment(javafx.geometry.Pos.TOP_CENTER);
usAgainstEnemyDivisor.setTranslateY(-20.0);
cannon.setRadius(10.0);
cannon.setStroke(javafx.scene.paint.Color.BLACK);
cannon.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
centeringUserShipPane.setRight(cannon);
centeringShipPane.setPrefHeight(200.0);
centeringShipPane.setPrefWidth(200.0);
centeringShipPane.setOpaqueInsets(new Insets(0.0));
HBox.setMargin(centeringShipPane, new Insets(0.0, 0.0, 0.0, 200.0));
encompassingPane.setAlignment(Ship, javafx.geometry.Pos.CENTER);
Ship.setFitHeight(165.0);
Ship.setFitWidth(180.0);
Ship.setPickOnBounds(true);
Ship.setPreserveRatio(true);
encompassingPane.setMargin(Ship, new Insets(0.0, 0.0, 20.0, 0.0));
centeringShipPane.setCenter(Ship);
usAgainstEnemyDivisor.getChildren().add(centeringUserShipPane);
usAgainstEnemyDivisor.getChildren().add(centeringShipPane);
fightRunBox.getChildren().add(fightButton);
fightRunBox.getChildren().add(continueButton);
fightRunBox.getChildren().add(runButton);
buttonBox.getChildren().add(fightRunBox);
labelBox.getChildren().add(title);
labelBox.getChildren().add(HPLeft);
labelBox.getChildren().add(gunsLeftOrTaken);
labelBox.getChildren().add(runAwayOrLeft);
labelBox.getChildren().add(shipsRemaining);
labelBox.getChildren().add(report);
encompassingPane.setTop(labelBox);
encompassingPane.setCenter(usAgainstEnemyDivisor);
encompassingPane.setBottom(buttonBox);
root.getChildren().addAll(encompassingPane, cannon);
Scene scene = new Scene(root, 600, 480);
root.getStylesheets().add("styleguide.css");
primaryStage.setResizable(false);
primaryStage.setTitle("Ship Warfare");
primaryStage.setScene(scene);
primaryStage.show();
continueButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
/**
* Continue Button, engages in run logic and graphical interface
* @param event, once button is clicked, executes graphical information
*/
public void handle(ActionEvent event) {
shotsFired.stop();
/**
* Switches to Taipan Shop scene
*/
TaipanShopGUI shop = new TaipanShopGUI(getPlayer());
shop.initializeShop(primaryStage);
primaryStage.show();
}
});
//Flee
runButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
/**
* Run Button, engages in run logic and graphical interface
* @param event, once button is clicked, executes graphical information
*/
public void handle(ActionEvent event) {
userAttacks = false;
report.setVisible(true);
title.setVisible(true);
shipsRemaining.setVisible(true);
gunsLeftOrTaken.setVisible(true);
title.setText("Ayy captain we will try to run!");
counter++;
if (logic.runFromShips() == false) {
report.setText(("Couldn't run away"));
try {
winOrLose = destroyShipsOrEscape(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
if (winOrLose == true) {
report.setVisible(false);
title.setVisible(false);
shipsRemaining.setVisible(false);
gunsLeftOrTaken.setVisible(false);
}
} else {
report.setText("Phew! Got away safely");
setVisibilitiesAndTransition(primaryStage);
}
}
});
//Fight
fightButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
/**
* Fight Button, engages in fight logic and graphical interface
* @param event, once button is clicked, executes graphical information
*/
public void handle(ActionEvent event) {
userAttacks = true;
wipeWithTitle(report);
fightButton.setVisible(false);
runButton.setVisible(false);
try {
winOrLose = destroyShipsOrEscape(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
counter++;
cannon.setVisible(true);
cannon.setLayoutX(beginningX);
cannon.setLayoutY(beginningY);
if (counter >= 1) {
title.setVisible(false);
}
playerShoots(getGuns());
shotsFired.setOnFinished(new EventHandler<ActionEvent>() {
@Override
/**
* When the user is completed their volley this information will be accessed
* @param event, once the shots fired transition is completed, execute graphical information
*/
public void handle(ActionEvent event) {
shotsFired.stop();
if (!winOrLose) {
shipsRetaliate();
} else {
report.setVisible(true);
continueButton.setVisible(true);
usAgainstEnemyDivisor.setVisible(false);
cannon.setVisible(false);
shotsFired.stop();
}
enemyShots.setOnFinished(new EventHandler<ActionEvent>() {
@Override
/**
* When the user is completed their volley, this information will be accessed
* @param event, once the enemy shots transition is completed, execute graphical information
*/
public void handle(ActionEvent event) {
fightButton.setVisible(true);
runButton.setVisible(true);
report.setVisible(true);
cannon.setVisible(false);
runAwayOrLeft.setVisible(true);
gunsLeftOrTaken.setVisible(true);
shipsRemaining.setVisible(true);
HPLeft.setVisible(true);
gunsLeftOrTaken.setVisible(true);
if (winOrLose == true) {
usAgainstEnemyDivisor.setVisible(false);
}
}
});
}
});
}
});
}
}

247
src/gui/StartGUI.java Normal file
View File

@@ -0,0 +1,247 @@
package gui;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import logic.Player;
import logic.FileSaving;
/**
* 2019-03-10
* Authors: Harkamal, Vikram, Haris, Siddhant, Nathan
* StartGUI class, Initializes and displays the start menu for Taipan
*
*/
public class StartGUI extends Player {
private BorderPane borderPane = new BorderPane();
private HBox hBox = new HBox();
private TextField nameField = new TextField();
private Button startButton = new Button();
private Button continueButton = new Button();
private VBox vBox = new VBox();
private Label choiceLabel = new Label();
private RadioButton gunChoice = new RadioButton();
private ToggleGroup Start = new ToggleGroup();
private RadioButton cashChoice = new RadioButton();
private VBox vBox0 = new VBox();
private Label title = new Label();
private Label authors = new Label();
/**
* Copy constructor.
* @param player object of the class Player
*/
public StartGUI(Player player) {
Player playerTemp = new Player(player);
setPlayer(playerTemp);
}
/**
* Asks the user to input the name that they would like to be called in the game
*
* @param name the name that you would like to be called in the game
*/
public void setFirm(String name) {
if (name.length() <= 22) {
super.setName(name);
} else {
super.setName("Taipan");
}
}
/**
* Initializes the Start GUI the game.
*
* @param stage object of type Stage
* @return returns the stage of GUI
*/
public Stage initializeStart(Stage stage) {
/**
* Creates an HBox at the center of the borderpane with a width of 200, height of 100 and spacing of 10.
*
*/
BorderPane.setAlignment(hBox, javafx.geometry.Pos.CENTER);
hBox.setAlignment(javafx.geometry.Pos.CENTER);
hBox.setPrefHeight(100.0);
hBox.setPrefWidth(200.0);
hBox.setSpacing(10.0);
/**
* Creates a borderpane window of width 600 and height 480.
*
*/
borderPane.setPrefHeight(480.0);
borderPane.setPrefWidth(600.0);
/**
* Creates a prompt text field that asks for your firm name and has a default text set to "Taipan".
*
*/
nameField.setPromptText("Enter Firm Name.");
nameField.setText("Taipan");
/**
* Creates a button with text "Start"
*
*/
startButton.setMnemonicParsing(false);
startButton.setText("New");
/**
* Creates a button with text "Continue"
*
*/
continueButton.setMnemonicParsing(false);
continueButton.setText("Load");
/**
* Creates a VBox at the left of center of the borderpane.
*
*/
vBox.setAlignment(javafx.geometry.Pos.CENTER_LEFT);
/**
* Creates a label with text "Do you want to start with..." to indicate the user has to choose between 2 given scenarios.
*
*/
choiceLabel.setText("Do you want to start with...");
/**
* Label for scenario one which is you start with five guns and no cash or debt.
*
*/
gunChoice.setMnemonicParsing(false);
gunChoice.setSelected(true);
gunChoice.setText("Five guns and no cash (But no debt!)?");
gunChoice.setToggleGroup(Start);
/**
* Label for scenario 2 which is you start with cash but also a debt.
*
*/
cashChoice.setAlignment(javafx.geometry.Pos.TOP_LEFT);
cashChoice.setMnemonicParsing(false);
cashChoice.setText("Cash (and a debt)");
cashChoice.setToggleGroup(Start);
borderPane.setBottom(hBox);
/**
* Creates a VBox at the center of the borderpane with a width of 100 and height of 200.
*
*/
BorderPane.setAlignment(vBox0, javafx.geometry.Pos.CENTER);
vBox0.setAlignment(javafx.geometry.Pos.CENTER);
vBox0.setPrefHeight(200.0);
vBox0.setPrefWidth(100.0);
/**
* Creates a label with text "Taipan" in size 66 font and default font style.
*
*/
title.setText("Taipan");
title.setFont(new Font(66.0));
/**
* Creates a label with our names as text
*
*/
authors.setPrefHeight(80.0);
authors.setPrefWidth(480.0);
authors.setText("By Vikram Bawa, Haris Muhammad, Siddhant Dewani, Nathan Lum \nand Harkamal Randhawa");
/**
* Puts Vbox0 in the center of the borderpane.
*
*/
authors.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
authors.setAlignment(Pos.CENTER);
vBox0.setAlignment(Pos.CENTER);
borderPane.setCenter(vBox0);
/**
* Adds all the buttons and labels to their respective boxes.
*
*/
hBox.getChildren().add(nameField);
hBox.getChildren().add(startButton);
hBox.getChildren().add(continueButton);
vBox.getChildren().add(choiceLabel);
vBox.getChildren().add(gunChoice);
vBox.getChildren().add(cashChoice);
hBox.getChildren().add(vBox);
vBox0.getChildren().add(title);
vBox0.getChildren().add(authors);
startButton.setDefaultButton(true);
/**
* Adds function to the "Start" button, scenario 1 gives the player $400 and a $5000 debt at the start of the game;
* scenario 2 gives the player 5 guns.
*
*/
startButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (Start.getSelectedToggle() == cashChoice) {
setMoney(400);
setDebt(5000);
setGuns(0);
}
if (Start.getSelectedToggle() == gunChoice) {
setGuns(5);
}
String response = nameField.getText();
// purely for testing purposes.
if (response.equalsIgnoreCase("Vikram")) {
setMoney(999999999);
setBank(999999999);
setGuns(999);
setHP(99999999);
setCargoSpace(Integer.MAX_VALUE);
}
setFirm(response);
TaipanShopGUI shop = new TaipanShopGUI(getPlayer());
shop.initializeShop(stage);
stage.show();
}
});
continueButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
FileSaving saving = new FileSaving();
if(saving.loadFile() != null){
TaipanShopGUI shop = new TaipanShopGUI(saving.loadFile());
shop.initializeShop(stage);
stage.show();
}
else{
authors.setText("There are no previous saves!");
}
}
});
Scene root = new Scene(borderPane, 600, 480);
root.getStylesheets().add("styleguide.css");
stage.setTitle("Start");
stage.setResizable(false);
stage.setScene(root);
stage.setHeight(510);
stage.setWidth(600);
return stage;
}
}

747
src/gui/TaipanShopGUI.java Normal file
View File

@@ -0,0 +1,747 @@
package gui; /**
* TaipanShopGUI deals with setting the stage for shop.
*
* Author: Vikram Bawa
*/
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import logic.FileSaving;
import logic.Player;
import logic.TaipanShopLogic;
public class TaipanShopGUI extends Player {
private Label firm = new Label();
private Label wItemsText = new Label();
private Label wItemSpaceText = new Label();
private Label locationText = new Label();
private Label inventoryText = new Label();
private Label inventoryHeldText = new Label();
private Label gunsText = new Label();
private Label shipStatusText = new Label();
private Label cashText = new Label();
private Label bankText = new Label();
private Label textOut = new Label();
private Button quitButton = new Button();
private Button retireButton = new Button();
private Button bankButton = new Button();
private Button sellButton = new Button();
private Button buyButton = new Button();
private Button loanButton = new Button();
private Button cargoButton = new Button();
private Button opiumButton = new Button();
private Button silkButton = new Button();
private Button armsButton = new Button();
private Button generalButton = new Button();
private TextField numberInput = new TextField();
/**
* 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 TaipanShopGUI(logic.Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* This method is evoked if the user is eligible to win, and chooses to end the game (by winning).
*/
public void retire(Stage stage) {
setRetire(true);
GameEndGUI gameEndGUI = new GameEndGUI(getPlayer());
gameEndGUI.initializeGameEndGUI(stage);
stage.show();
}
/**
* Sets the default dialogue of simply stating the prices of the items.
*/
public void defaultTextOut() {
textOut.setText(String.format("\t%s, present prices per unit here are:\n\n\t\tOpium: %d\t\t\tSilk: %d\n\t\tArms: %d\t\t\tGeneral: %d", getName(), getOpiumPrice(), getSilkPrice(), getArmsPrice(), getGeneralPrice()));
}
/**
* Sets up buttons according to which "state" is inputted. When used in "shop", only the item buttons are visible.
* When used in "reset", all the item buttons are invisible; if the user is at location one and is eligible to win,
* then all utilities and the retire button are visible. If the user is not at location one, then the user can only
* buy, sell, or exit. If the user is at location one and is not eligible to win, then all utilities are visible but
* the retire button is not. When used in "input" everything near the bottom is invisible except for the text area.
*
* @param state -- the state determines which buttons are visible and which are not.
*/
public void buttonSetup(String state) {
if (state.equals("shop")) {
buyButton.setVisible(false);
sellButton.setVisible(false);
bankButton.setVisible(false);
numberInput.setVisible(false);
cargoButton.setVisible(false);
loanButton.setVisible(false);
armsButton.setVisible(true);
quitButton.setVisible(false);
opiumButton.setVisible(true);
silkButton.setVisible(true);
generalButton.setVisible(true);
retireButton.setVisible(false);
} else if (state.equals("reset")) {
buyButton.setText("Buy");
sellButton.setText("Sell");
opiumButton.setText("Opium");
silkButton.setText("Silk");
armsButton.setText("Arms");
generalButton.setText("General");
if (getLocation() != 1) {
buyButton.setVisible(true);
sellButton.setVisible(true);
bankButton.setVisible(false);
cargoButton.setVisible(false);
loanButton.setVisible(false);
armsButton.setVisible(false);
quitButton.setVisible(true);
//quitButton.setDefaultButton(true);
opiumButton.setVisible(false);
silkButton.setVisible(false);
numberInput.setVisible(false);
generalButton.setVisible(false);
retireButton.setVisible(false);
}
if (getBank() + getMoney() - getDebt() < 1000000 && getLocation() == 1) {
buyButton.setVisible(true);
sellButton.setVisible(true);
bankButton.setVisible(true);
cargoButton.setVisible(true);
loanButton.setVisible(true);
quitButton.setVisible(true);
//quitButton.setDefaultButton(true);
opiumButton.setVisible(false);
silkButton.setVisible(false);
numberInput.setVisible(false);
generalButton.setVisible(false);
armsButton.setVisible(false);
retireButton.setVisible(false);
} else if (getLocation() == 1) {
buyButton.setVisible(true);
sellButton.setVisible(true);
bankButton.setVisible(true);
cargoButton.setVisible(true);
loanButton.setVisible(true);
numberInput.setVisible(false);
quitButton.setVisible(true);
//quitButton.setDefaultButton(true);
opiumButton.setVisible(false);
silkButton.setVisible(false);
generalButton.setVisible(false);
armsButton.setVisible(false);
retireButton.setVisible(true);
}
} else if (state.equals("input")) {
buyButton.setVisible(false);
sellButton.setVisible(false);
generalButton.setVisible(false);
bankButton.setVisible(false);
loanButton.setVisible(false);
quitButton.setVisible(false);
opiumButton.setVisible(false);
cargoButton.setVisible(false);
silkButton.setVisible(false);
armsButton.setVisible(false);
retireButton.setVisible(false);
numberInput.setVisible(true);
}
}
/**
* this method is responsible for the actual purchasing/selling of items, and the text associated with the act.
*/
public void shop() {
String originalDialogue = textOut.getText();
int num = Integer.parseInt(numberInput.getText().replace(" ", ""));
if (buyButton.getText().contains(".")) {
if (opiumButton.getText().contains(".") && num <= getMoney() / getOpiumPrice() && num >= 0) {
setMoney(getMoney() - num * getOpiumPrice());
setOpiumHeld(getOpiumHeld() + num);
} else if (num >= 0 && opiumButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", you can't afford that!");
} else if (opiumButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", how am I supposed to buy " + "'" + num + "'" + " Opium?");
} else if (silkButton.getText().contains(".") && num <= getMoney() / getSilkPrice() && num >= 0) {
setSilkHeld(getSilkHeld() + num);
setMoney(getMoney() - num * getSilkPrice());
} else if (num >= 0 && silkButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", you can't afford that!");
} else if (silkButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", how am I supposed to buy " + "'" + num + "'" + " Silk?");
} else if (armsButton.getText().contains(".") && num <= getMoney() / getArmsPrice() && num >= 0) {
setArmsHeld(getArmsHeld() + num);
setMoney(getMoney() - num * getArmsPrice());
} else if (num >= 0 && armsButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", you can't afford that!");
} else if (armsButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", how am I supposed to buy " + "'" + num + "'" + " Arms?");
} else if (generalButton.getText().contains(".") && num <= getMoney() / getGeneralPrice() && num >= 0) {
setGeneralHeld(getGeneralHeld()+num);
setMoney(getMoney() - num * getGeneralPrice());
} else if (num >= 0 && generalButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", you can't afford that!");
} else if (generalButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", how am I supposed to buy " + "'" + num + "'" + " General Cargo?");
}
} else if (sellButton.getText().contains(".")) {
if (opiumButton.getText().contains(".") && num <= getOpiumHeld() && num >= 0) {
setOpiumHeld(getOpiumHeld() - num);
setMoney(getMoney() + num * getOpiumPrice());
} else if (num >= 0 && opiumButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", you don't have that many to sell!");
} else if (opiumButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", how am I supposed to sell " + "'" + num + "'" + " Opium?");
} else if (silkButton.getText().contains(".") && num <= getSilkHeld() && num >= 0) {
setSilkHeld(getSilkHeld() - num);
setMoney(getMoney() + num * getSilkPrice());
} else if (num >= 0 && silkButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", you don't have that many to sell!");
} else if (silkButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", how am I supposed to sell " + "'" + num + "'" + " Silk?");
} else if (armsButton.getText().contains(".") && num <= getArmsHeld() && num >= 0) {
setArmsHeld(getArmsHeld() - num);
setMoney(getMoney() + num * getArmsPrice());
} else if (num >= 0 && armsButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", you don't have that many to sell!");
} else if (armsButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", how am I supposed to sell " + "'" + num + "'" + " Arms?");
} else if (generalButton.getText().contains(".") && num <= getGeneralHeld() && num >= 0) {
setGeneralHeld(getGeneralHeld() - num);
setMoney(getMoney() + num * getGeneralPrice());
} else if (num >= 0 && generalButton.getText().contains(".")) {
textOut.setText(originalDialogue + "\n\t" + getName() + ", you don't have that many to sell!");
} else {
textOut.setText(originalDialogue + "\n\t" + getName() + ", how am I supposed to sell " + "'" + num + "'" + " General Cargo?");
}
}
}
/**
* Initializes the shop on the given stage as a parameter.
*
* @param stage
*/
public void initializeShop(Stage stage) {
FileSaving saving = new FileSaving();
saving.saveFile(getPlayer());
FlowPane flowPane = new FlowPane();
buyButton.setMnemonicParsing(false);
buyButton.setPrefHeight(25.0);
buyButton.setPrefWidth(45.0);
buyButton.setText("Buy");
buyButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* if the buy button is clicked, the main utility buttons are set to be invisible and the buying process begins.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
buttonSetup("shop");
buyButton.setText("Buy.");
defaultTextOut();
textOut.setText(textOut.getText() + "\n\tWhich good would you like to purchase?");
}
});
sellButton.setPrefHeight(25.0);
sellButton.setText("Sell");
// if the sell button is clicked, the main utility buttons are set to be invisible and the selling process begins.
sellButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* if the sell button is clicked, the main utility buttons are set to be invisible and the selling process begins.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
buttonSetup("shop");
sellButton.setText("Sell.");
defaultTextOut();
textOut.setText(textOut.getText() + "\n\tWhich good would you like to sell?");
}
});
sellButton.setPrefWidth(45.0);
sellButton.setMnemonicParsing(false);
bankButton.setPrefHeight(25.0);
bankButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* opens the bank if the bank button is clicked.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
BankGUI bank = new BankGUI(getPlayer());
bank.initializeBank(stage);
stage.show();
}
});
bankButton.setMnemonicParsing(false);
bankButton.setPrefWidth(74.0);
bankButton.setText("Bank");
cargoButton.setPrefHeight(25.0);
cargoButton.setText("Transfer");
cargoButton.setMnemonicParsing(false);
cargoButton.setPrefWidth(94.0);
cargoButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* warehouse is entered when the warehouse button is clicked.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
WarehouseGUI warehouseGUI = new WarehouseGUI(getPlayer());
warehouseGUI.initializeWarehouse(stage);
stage.show();
}
});
loanButton.setMnemonicParsing(false);
loanButton.setPrefHeight(25.0);
loanButton.setPrefWidth(73.0);
loanButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* loan office is entered when the loan button is clicked.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
LoanSharkGUI loan = new LoanSharkGUI(getPlayer());
loan.initializeLoanShark(stage);
stage.show();
}
});
loanButton.setText("Loans");
quitButton.setPrefHeight(25.0);
quitButton.setMnemonicParsing(false);
quitButton.setPrefWidth(90.0);
quitButton.setText("Quit");
quitButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* the user is free to travel once the quit button is clicked.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
setIsPriceChanged(1);
TravelGUI travelGUI = new TravelGUI(getPlayer());
travelGUI.initializeTravel(stage);
stage.show();
}
});
retireButton.setPrefHeight(25.0);
retireButton.setPrefWidth(49.0);
retireButton.setText("Retire");
retireButton.setVisible(false);
retireButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* the user wins the game when the retire button is clicked.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
retire(stage);
}
});
retireButton.setMnemonicParsing(false);
opiumButton.setMnemonicParsing(false);
opiumButton.setPrefWidth(86.0);
opiumButton.setPrefHeight(25.0);
opiumButton.setText("Opium");
opiumButton.setVisible(false);
opiumButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* the opium buying/selling process starts as soon as the user clicks the opium button.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
buttonSetup("input");
opiumButton.setText("Opium.");
defaultTextOut();
String extraText;
if (buyButton.getText().contains(".")) {
extraText = String.format(" (You can afford %d)", getMoney() / getOpiumPrice());
} else {
extraText = String.format(" (You have %d)", getOpiumHeld());
}
textOut.setText(textOut.getText() + "\n\tWhat quantity of Opium?" + extraText);
}
});
silkButton.setPrefHeight(25.0);
silkButton.setPrefWidth(86.0);
silkButton.setMnemonicParsing(false);
silkButton.setText("Silk");
silkButton.setVisible(false);
silkButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* the silk buying/selling process starts as soon as the user clicks the silk button.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
buttonSetup("input");
silkButton.setText("Silk.");
defaultTextOut();
String extraText;
if (buyButton.getText().contains(".")) {
extraText = String.format(" (You can afford %d)", getMoney() / getSilkPrice());
} else {
extraText = String.format(" (You have %d)", getSilkHeld());
}
textOut.setText(textOut.getText() + "\n\tWhat quantity of Silk?" + extraText);
}
});
armsButton.setPrefWidth(86.0);
armsButton.setMnemonicParsing(false);
armsButton.setVisible(false);
armsButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* the arms buying/selling process starts as soon as the user clicks the arms button.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
buttonSetup("input");
armsButton.setText("Arms.");
defaultTextOut();
String extraText;
if (buyButton.getText().contains(".")) {
extraText = String.format(" (You can afford %d)", getMoney() / getArmsPrice());
} else {
extraText = String.format(" (You have %d)", getArmsHeld());
}
textOut.setText(textOut.getText() + "\n\tWhat quantity of Arms?" + extraText);
}
});
armsButton.setText("Arms");
armsButton.setPrefHeight(25.0);
generalButton.setMnemonicParsing(false);
generalButton.setPrefHeight(25.0);
generalButton.setPrefWidth(86.0);
generalButton.setText("General");
generalButton.setVisible(false);
generalButton.setOnAction(new EventHandler<ActionEvent>() {
/**
* the general cargo buying/selling process starts as soon as the user clicks the general cargo button.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(ActionEvent event) {
buttonSetup("input");
generalButton.setText("General.");
defaultTextOut();
String extraText;
if (buyButton.getText().contains(".")) {
extraText = String.format(" (You can afford %d)", getMoney() / getGeneralPrice());
} else {
extraText = String.format(" (You have %d)", getGeneralHeld());
}
textOut.setText(textOut.getText() + "\n\tWhat quantity of General Cargo?" + extraText);
}
});
numberInput.setAlignment(javafx.geometry.Pos.CENTER_RIGHT);
numberInput.setText("Enter amount here...");
numberInput.setVisible(false);
numberInput.setOnKeyPressed(new EventHandler<KeyEvent>() {
/**
* after the user inputs a valid input into the text field and presses Z or ENTER, the buying/selling ends
* and the user is returned to the regular shop dialogue.
*
* @param event -- the event corresponding to the button click
*/
@Override
public void handle(KeyEvent event) {
boolean exit = true;
defaultTextOut();
if (event.getCode().equals(KeyCode.ENTER) || event.getCode().equals(KeyCode.Z)) {
while (true) {
if (!textOut.getText().contains("You entered an invalid input!") && !exit) {
textOut.setText(textOut.getText() + "\n\n\tYou entered an invalid input! Please try again.");
break;
}
try {
shop();
} catch (Exception e) {
exit = false;
}
if (exit) {
break;
}
}
updateStage(firm,wItemsText,wItemSpaceText,locationText,gunsText,inventoryText,inventoryHeldText,shipStatusText,cashText,bankText);
buttonSetup("reset");
}
}
});
stage.setTitle("Shop");
stage.setResizable(false);
flowPane.getChildren().addAll(buyButton, sellButton, bankButton, cargoButton, loanButton, quitButton, retireButton, opiumButton, silkButton, armsButton, generalButton, numberInput);
Scene root = new Scene(declareStage(flowPane,firm,wItemsText,wItemSpaceText,locationText,gunsText,inventoryText,inventoryHeldText,shipStatusText,cashText,bankText,textOut), 600, 480);
stage.setScene(root);
root.getStylesheets().add("styleguide.css");
// general updates to the buttons, user stats/inventory, and text.
buttonSetup("reset");
if(getIsPriceChanged() == 0 || getIsPriceChanged() == 2){
TaipanShopLogic logic = new TaipanShopLogic(getPlayer());
String temp = logic.updatePrices();
setPlayer(logic.getPlayer());
defaultTextOut();
textOut.setText(temp + textOut.getText());
}
defaultTextOut();
updateStage(firm,wItemsText,wItemSpaceText,locationText,gunsText,inventoryText,inventoryHeldText,shipStatusText,cashText,bankText);
}
public AnchorPane declareStage(FlowPane flowPane,Label firm, Label wItemsText, Label wItemSpaceText, Label locationText, Label gunsText, Label inventoryText, Label inventoryHeldText, Label shipStatusText, Label cashText, Label bankText, Label textOut) {
//Declaring all the elements required for the information on screen
Rectangle dialogueRectangle = new Rectangle();
Rectangle inventoryRectangle = new Rectangle();
Rectangle warehouseRectangle = new Rectangle();
AnchorPane anchorPane = new AnchorPane();
GridPane gridPane = new GridPane();
ColumnConstraints columnConstraints = new ColumnConstraints();
RowConstraints rowConstraints = new RowConstraints();
RowConstraints rowConstraints0 = new RowConstraints();
RowConstraints rowConstraints1 = new RowConstraints();
RowConstraints rowConstraints2 = new RowConstraints();
RowConstraints rowConstraints3 = new RowConstraints();
RowConstraints rowConstraints4 = new RowConstraints();
HBox hBox = new HBox();
HBox hBox0 = new HBox();
Font size14 = new Font(14.0);
Label warehouseText = new Label();
dialogueRectangle.setFill(Color.WHITE);
dialogueRectangle.setHeight(180.0);
dialogueRectangle.setLayoutX(8.0);
dialogueRectangle.setLayoutY(294.0);
dialogueRectangle.setStroke(Color.BLACK);
dialogueRectangle.setStrokeType(StrokeType.INSIDE);
dialogueRectangle.setWidth(582.0);
inventoryRectangle.setFill(Color.WHITE);
inventoryRectangle.setHeight(108.0);
inventoryRectangle.setLayoutX(8.0);
inventoryRectangle.setLayoutY(147.0);
inventoryRectangle.setStroke(Color.BLACK);
inventoryRectangle.setStrokeType(StrokeType.INSIDE);
inventoryRectangle.setWidth(405.0);
warehouseRectangle.setFill(Color.WHITE);
warehouseRectangle.setHeight(108.0);
warehouseRectangle.setLayoutY(33.0);
warehouseRectangle.setLayoutX(8.0);
warehouseRectangle.setStroke(Color.BLACK);
warehouseRectangle.setStrokeType(StrokeType.INSIDE);
warehouseRectangle.setWidth(405.0);
anchorPane.setPrefHeight(480.0);
anchorPane.setPrefWidth(600.0);
gridPane.setPrefHeight(480.0);
gridPane.setPrefWidth(600.0);
columnConstraints.setMaxWidth(590.0);
columnConstraints.setMinWidth(0.0);
columnConstraints.setPrefWidth(590.0);
rowConstraints.setMinHeight(20.0);
rowConstraints.setPrefHeight(20.0);
rowConstraints0.setMaxHeight(122.0);
rowConstraints0.setMinHeight(10.0);
rowConstraints0.setPrefHeight(117.0);
rowConstraints1.setMaxHeight(163.0);
rowConstraints1.setMinHeight(10.0);
rowConstraints1.setPrefHeight(112.0);
rowConstraints2.setMaxHeight(126.0);
rowConstraints2.setMinHeight(0.0);
rowConstraints2.setPrefHeight(42.0);
rowConstraints3.setMaxHeight(269.0);
rowConstraints3.setMinHeight(10.0);
rowConstraints3.setPrefHeight(118.0);
rowConstraints4.setMaxHeight(179.0);
rowConstraints4.setMinHeight(10.0);
rowConstraints4.setPrefHeight(52.0);
gridPane.setPadding(new Insets(10.0, 10.0, 10.0, 0.0));
GridPane.setRowIndex(hBox, 1);
hBox.setPrefHeight(100.0);
hBox.setPrefWidth(200.0);
GridPane.setRowIndex(hBox0, 2);
hBox0.setPrefHeight(100.0);
hBox0.setPrefWidth(200.0);
GridPane.setRowIndex(flowPane, 5);
flowPane.setAlignment(Pos.CENTER);
flowPane.setHgap(5.0);
flowPane.setPrefHeight(200.0);
flowPane.setPrefWidth(200.0);
firm.setAlignment(Pos.CENTER);
firm.setPrefHeight(27.0);
firm.setPrefWidth(632.0);
firm.setFont(new Font(18.0));
warehouseText.setAlignment(Pos.CENTER);
warehouseText.setPrefHeight(108.0);
warehouseText.setPrefWidth(100.0);
warehouseText.setText(" Warehouse\n\tOpium\n\tSilk\n\tArms\n\tGeneral");
warehouseText.setFont(size14);
wItemsText.setAlignment(Pos.CENTER);
wItemsText.setPrefWidth(100.0);
wItemsText.setPrefHeight(108.0);
wItemsText.setFont(size14);
wItemSpaceText.setPrefHeight(108.0);
wItemSpaceText.setPrefWidth(215.0);
wItemSpaceText.setFont(size14);
locationText.setAlignment(Pos.BOTTOM_CENTER);
locationText.setPrefHeight(106.0);
locationText.setPrefWidth(175.0);
locationText.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
locationText.setFont(size14);
inventoryText.setAlignment(Pos.CENTER);
inventoryText.setFont(size14);
inventoryHeldText.setAlignment(Pos.CENTER);
inventoryHeldText.setPrefHeight(108.0);
inventoryHeldText.setPrefWidth(100.0);
inventoryHeldText.setFont(size14);
gunsText.setPrefHeight(108.0);
gunsText.setPrefWidth(212.0);
gunsText.setAlignment(Pos.CENTER_LEFT);
gunsText.setFont(size14);
shipStatusText.setAlignment(Pos.TOP_CENTER);
shipStatusText.setContentDisplay(javafx.scene.control.ContentDisplay.CENTER);
shipStatusText.setPrefHeight(110.0);
shipStatusText.setPrefWidth(180.0);
shipStatusText.setTextAlignment(javafx.scene.text.TextAlignment.CENTER);
shipStatusText.setFont(size14);
GridPane.setRowIndex(cashText, 3);
cashText.setPrefHeight(17.0);
cashText.setPrefWidth(209.0);
cashText.setFont(size14);
GridPane.setHalignment(bankText, javafx.geometry.HPos.CENTER);
GridPane.setRowIndex(bankText, 3);
bankText.setAlignment(Pos.CENTER);
bankText.setPrefHeight(20.0);
bankText.setPrefWidth(264.0);
bankText.setFont(size14);
GridPane.setRowIndex(textOut, 4);
textOut.setAlignment(Pos.TOP_LEFT);
textOut.setContentDisplay(javafx.scene.control.ContentDisplay.TOP);
textOut.setPrefHeight(163.0);
textOut.setPrefWidth(583.0);
//defaultTextOut();
textOut.setFont(size14);
anchorPane.getChildren().addAll(dialogueRectangle, inventoryRectangle, warehouseRectangle);
hBox.getChildren().addAll(warehouseText, wItemsText, wItemSpaceText, locationText);
hBox0.getChildren().addAll(inventoryText, inventoryHeldText, gunsText, shipStatusText);
gridPane.getColumnConstraints().add(columnConstraints);
gridPane.getRowConstraints().addAll(rowConstraints, rowConstraints0, rowConstraints1, rowConstraints2, rowConstraints3, rowConstraints4);
gridPane.getChildren().addAll(firm, hBox, hBox0, cashText, bankText, textOut, flowPane);
anchorPane.getChildren().add(gridPane);
return anchorPane;
}
/**
* updates the text associated with the user's inventory.
*/
public void updateStage(Label firm, Label wItemsText, Label wItemSpaceText, Label locationText, Label gunsText, Label inventoryText, Label inventoryHeldText, Label shipStatusText, Label cashText, Label bankText) {
TaipanShopLogic logic = new TaipanShopLogic(super.getPlayer());
firm.setText(String.format("Firm: %s, %s", getName(), logic.getStringLocation()));
wItemsText.setText(String.format("\n %d\n %d\n %d\n %d", getwOpium(), getwSilk(), getwArms(), getwGeneral()));
int itemsInWarehouse = getwOpium() + getwGeneral() + getwArms() + getwSilk();
wItemSpaceText.setText(String.format("\n\t\tIn use:\n\t\t %d \n\t\tVacant:\n\t\t %d", itemsInWarehouse, (10000 - itemsInWarehouse)));
locationText.setText(String.format("Location\n%s", logic.getStringLocation()));
int itemsInInventory = getCargoSpace() - getSilkHeld() - getOpiumHeld() - getGeneralHeld() - getArmsHeld() - 10 * getGuns();
if (itemsInInventory < 0) {
inventoryText.setText(" Overloaded\n\t Opium\n\t Silk\n\t Arms\n\t General");
} else {
inventoryText.setText(String.format(" Hold %d\n\t Opium\n\t Silk\n\t Arms\n\t General", itemsInInventory));
}
gunsText.setText(String.format("Guns %d\n\n\n\n ", getGuns()));
inventoryHeldText.setText(String.format("\n %d\n %d\n %d\n %d", getOpiumHeld(), getSilkHeld(), getArmsHeld(), getGeneralHeld()));
shipStatusText.setText(String.format("\tDebt\n\t%d\n\n\tShip status\n\t%s: %d", getDebt(), logic.shipStatusString(), getHP()));
cashText.setText(String.format(" Cash: $%,d", getMoney()));
bankText.setText(String.format("Bank: $%,d", getBank()));
}
}

315
src/gui/TravelGUI.java Normal file
View File

@@ -0,0 +1,315 @@
package gui; /**
* TravelGUI is the class in which takes the player from location to location
*
* Author: Harkamal Randhawa
*/
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import logic.Player;
import java.util.Random;
public class TravelGUI extends Player {
private TaipanShopGUI shop;
private Label firm = new Label();
private Label wItemsText = new Label();
private Label wItemSpaceText = new Label();
private Label locationText = new Label();
private Label inventoryText = new Label();
private Label inventoryHeldText = new Label();
private Label gunsText = new Label();
private Label shipStatusText = new Label();
private Label cashText = new Label();
private Label bankText = new Label();
private Label textOut = new Label();
private FlowPane flowPane = new FlowPane();
private Button quitButton = new Button();
private Button continueButton = new Button();
private TextField numberInput = new TextField();
private Boolean peasantShipScene = false;
private Boolean littyShipScene = false;
private Boolean shopScene = false;
private Boolean stormScene = false;
private Boolean sceneContinue = false;
/**
* 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 TravelGUI(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* Sets up the graphical part of TravelGUI and includes all logic for the class
*
* @param stage sets the stage to which we will execute the scene of the TravelGUI class
* @return stage so that another class can switch to the stage
*/
public Stage initializeTravel(Stage stage) {
//Creating the continue and quit buttons
quitButton.setPrefHeight(25.0);
quitButton.setMnemonicParsing(false);
quitButton.setPrefWidth(90.0);
quitButton.setText("Go back");
continueButton.setPrefHeight(25.0);
continueButton.setMnemonicParsing(false);
continueButton.setPrefWidth(90.0);
continueButton.setText("Continue");
continueButton.setVisible(false);
//Goes back to shop
quitButton.setOnAction(event -> {
TaipanShopGUI taipanShopGUI = new TaipanShopGUI(getPlayer());
taipanShopGUI.initializeShop(stage);
stage.show();
});
//Continues on to either shop or shipwarfare
continueButton.setOnAction(event -> {
if(peasantShipScene && getAttackingShips()){
ShipWarfareGUI ship = new ShipWarfareGUI(getPlayer());
try {
ship.initializeShip(stage);
} catch (Exception e) {
e.printStackTrace();
}
stage.show();
}
else if(shopScene){
Random rand = new Random();
int randGenNum = rand.nextInt(3) + 1;
if(randGenNum >= 2) {
TaipanShopGUI shop = new TaipanShopGUI(getPlayer());
shop.initializeShop(stage);
stage.show();
}
else {
RandomEventGUI randomEventGUI = new RandomEventGUI(getPlayer());
randomEventGUI.initializeRandomEventGUI(stage);
stage.show();
}
}
});
//Text input for where the player needs to go inside of the game world
numberInput.setAlignment(javafx.geometry.Pos.CENTER_RIGHT);
//numberInput.setText("Enter preferred location.");
numberInput.setOnKeyPressed(event -> {
if(event.getCode().equals(KeyCode.ENTER)||event.getCode().equals(KeyCode.Z)) {
int response;
try {
response = Integer.parseInt(numberInput.getText().replace(" ", ""));
}
catch (Exception e){
response = 0;
}
boolean hasTraveled = false;
//Only lets the player leave the port if their inventory is greater than or equal to the sum of the items in the inventory.
if(getCargoSpace() >= (getOpiumHeld()+ (getGuns()*10)+getSilkHeld() + getArmsHeld() + getGeneralHeld())){
//Just in case the player types something that was not intended. It will refresh the question and ask it again
try {
//Makes sure you can't travel to your own location.
if (response != getLocation() && response > 0 && 8 > response && event.getCode().equals(KeyCode.ENTER)||event.getCode().equals(KeyCode.Z)){
hasTraveled = seaAtlas(response);
randomEventSea(response,stage);
setBank((int) (getBank() * 1.01));
setDebt((int) (getDebt() * 1.01));
setIsPriceChanged(2);
//shopScene = false;
//stormScene = false;
} else{
if(response == getLocation()){
textOut.setText(" " + "You're already here " + getName() + "\n");
}
else{
textOut.setText(" " + getName() + "; Sorry but could you say that again?");
}
textOut.setText(textOut.getText() + "\n\n 1) Hong Kong, 2) Shanghai, 3) Nagasaki, 4) Saigon,\n 5) Manila, 6) Singapore, or 7) Batavia?");
}
} catch (Exception e) {
textOut.setText(" " + "Sorry, " + getName() + " could you say that again?");
}
if (hasTraveled) {
continueButton.setVisible(true);
continueButton.setDefaultButton(true);
quitButton.setVisible(false);
numberInput.setVisible(false);
shopScene = true;
}
}
}
else if (getCargoSpace() < (getOpiumHeld()+ (getGuns()*10)+getSilkHeld() + getArmsHeld() + getGeneralHeld())){
textOut.setText(" "+getName() + " the cargo is too heavy! We can't set sail!");
}
});
firm.setAlignment(Pos.CENTER);
firm.setPrefHeight(27.0);
firm.setPrefWidth(632.0);
firm.setFont(new Font(18.0));
flowPane.getChildren().addAll(numberInput, quitButton, continueButton);
TaipanShopGUI shop = new TaipanShopGUI(super.getPlayer());
Scene root = new Scene(shop.declareStage(flowPane,firm,wItemsText,wItemSpaceText,locationText,gunsText,inventoryText,inventoryHeldText,shipStatusText,cashText,bankText,textOut), 600, 480);
root.getStylesheets().add("styleguide.css");
//Updates the stage for the first-time you read it
shop.updateStage(firm,wItemsText,wItemSpaceText,locationText,gunsText,inventoryText,inventoryHeldText,shipStatusText,cashText,bankText);
textOut.setText(" Taipan, do you wish to go to:\n\n 1) Hong Kong, 2) Shanghai, 3) Nagasaki, 4) Saigon,\n 5) Manila, 6) Singapore, or 7) Batavia?\n After typing the number you want to go to press 'Enter' or 'Z'");
stage.setTitle("Travel");
stage.setResizable(false);
stage.setScene(root);
return stage;
}
/**
* When provided a location number: the method returns a print statement stating the location's name and call another
* method to change the location of the Player object.
*
* @param locationOfTravel is a Player object that will be copied and the player instance variable is set to the copy.
*/
private Boolean seaAtlas(int locationOfTravel) {
switch (locationOfTravel) {
case 1:
if(!peasantShipScene && !stormScene) textOut.setText( textOut.getText() + "\n " + "Arriving at Hong Kong");
setLocation(1);
return true;
case 2:
if(!peasantShipScene && !stormScene) textOut.setText( textOut.getText() + "\n " + "Arriving at Shanghai");
setLocation(2);
return true;
case 3:
if(!peasantShipScene && !stormScene) textOut.setText( textOut.getText() + "\n " + "Arriving at Nagasaki");
setLocation(3);
return true;
case 4:
if(!peasantShipScene && !stormScene) textOut.setText( textOut.getText() + "\n " + "Arriving at Saigon");
setLocation(4);
return true;
case 5:
if(!peasantShipScene && !stormScene) textOut.setText( textOut.getText() + "\n " + "Arriving at Manila");
setLocation(5);
return true;
case 6:
if(!peasantShipScene && !stormScene) textOut.setText( textOut.getText() + "\n " + "Arriving at Singapore");
setLocation(6);
return true;
case 7:
if(!peasantShipScene && !stormScene) textOut.setText( textOut.getText() + "\n " + "Arriving at Batavia");
setLocation(7);
return true;
default:
textOut.setText(" " + "Sorry but could you say that again " + getName() + "?");
return false;
}
}
/**
* Based on random chance either attacks the player with enemy ships, throws them to a different location or does nothing.
*
* @param locationOfTravel is used to see where the player is going to travel, just in case their location is changed
* by a typhoon.
**/
private void randomEventSea(int locationOfTravel, Stage stage) {
Random rand = new Random();
int randGenNum = rand.nextInt(3) + 1;
if (randGenNum == 1) {
continueButton.setVisible(true);
continueButton.setDefaultButton(true);
quitButton.setVisible(false);
numberInput.setVisible(false);
textOut.setText(" We see a ship on the horizon " + getName() + "; Prepare for combat!");
peasantShipScene = true;
}else if (randGenNum == 2) {
disaster(locationOfTravel);
textOut.setText(textOut.getText() + "\n " + "We made it!");
}
}
/**
* Based on random chance either throws the player character off course, or continues them on their way to their
* destination.
*
* @param locationOfTravel is used to see where the player is going to travel, just in case their location is changed
* by a typhoon.
**/
private void disaster(int locationOfTravel) {
//Tells player that there is a storm approaching.
textOut.setText(" " + "Storm " + getName() + "! ");
Random rand = new Random();
int randGenNum = rand.nextInt(5) + 1;
//If the player lands within this range, nothing happens to them
//Else they randomly get thrown into a location they weren't planning on going to(Anything but location of Travel).
if (randGenNum <= 2) {
textOut.setText(textOut.getText() + "\n " + "We got through the storm " + getName() + "!");
}else {
while (randGenNum == locationOfTravel) {
randGenNum = rand.nextInt(7) + 1;
if (randGenNum != locationOfTravel) {
textOut.setText(textOut.getText() + "\n " + "We've been blown off course!");
seaAtlas(randGenNum);
}
}
}
}
/**
* converts the user's location (an integer) to a String, and returns it.
*
* @return location -- the user's location as a string; the actual name of the location.
*/
public String getStringLocation(){
String location;
switch(getLocation()){
case 1: location = "Hong Kong"; break;
case 2: location = "Shanghai"; break;
case 3: location = "Nagasaki"; break;
case 4: location = "Saigon"; break;
case 5: location = "Manila"; break;
case 6: location = "Singapore"; break;
case 7: location = "Batavia"; break;
default: location = "Error"; break;
}
return location;
}
/**
* returns the user's condition based upon their current HP.
*
* @return shipStatus -- a representation of their ship's health in words.
*/
public String shipStatusString(){
String shipStatus;
switch(getHP()/10){
case 10: shipStatus = "Mint Condition"; break;
case 9: shipStatus = "Near Perfect"; break;
case 8: shipStatus = "Great"; break;
case 7: shipStatus = "Good"; break;
case 6: shipStatus = "Acceptable"; break;
case 5: shipStatus = "Tolerable"; break;
case 4: shipStatus = "Needs Repair"; break;
case 3: shipStatus = "Damaged"; break;
case 2: shipStatus = "Indangered"; break;
case 1: shipStatus = "Near Sinking"; break;
case 0: shipStatus = "Sinking"; break;
default: shipStatus = "Invincible"; break;
}
return shipStatus;
}
}

339
src/gui/WarehouseGUI.java Normal file
View File

@@ -0,0 +1,339 @@
package gui;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import logic.Player;
/**
* 2019-03-19
* Authors:Siddhant, Vikram, Harkamal, Haris, Nathan
* WarehouseGUI allows the user to store goods in a warehouse in order to have more hold on the ship
*/
public class WarehouseGUI extends Player {
//Create the labels, buttons and textfields required
private HBox hBox;
private VBox vBox;
private TextField textField;
private RadioButton generalRadio;
private ToggleGroup Goods;
private RadioButton armsRadio;
private RadioButton silkRadio;
private RadioButton opiumRadio;
private Button withdrawButton;
private Button depositButton;
private Button quitButton;
private Label title;
private HBox hBox0;
private VBox vBox0;
private Label playerLabel;
private Label playerGeneral;
private Label playerArms;
private Label playerSilk;
private Label playerOpium;
private VBox vBox1;
private Label houseLabel;
private Label houseGeneral;
private Label houseArms;
private Label houseSilk;
private Label houseOpium;
private BorderPane borderPane;
/**
* constructor that creates an object of type player
* @param player
*/
public WarehouseGUI(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
*
* @param stage
* @return stage after creating buttons and layout
*/
public Stage initializeWarehouse(Stage stage){
//Initializes the instances created above to a new type
hBox = new HBox();
vBox = new VBox();
generalRadio = new RadioButton();
Goods = new ToggleGroup();
armsRadio = new RadioButton();
silkRadio = new RadioButton();
opiumRadio = new RadioButton();
withdrawButton = new Button();
depositButton = new Button();
quitButton = new Button();
title = new Label();
hBox0 = new HBox();
vBox0 = new VBox();
playerLabel = new Label();
playerGeneral = new Label();
playerArms = new Label();
playerSilk = new Label();
playerOpium = new Label();
vBox1 = new VBox();
houseLabel = new Label();
houseGeneral = new Label();
houseArms = new Label();
houseSilk = new Label();
houseOpium = new Label();
borderPane = new BorderPane();
textField = new TextField();
//Creating the box for the warehouse GUI
borderPane.setAlignment(hBox, javafx.geometry.Pos.CENTER);
hBox.setAlignment(javafx.geometry.Pos.CENTER);
hBox.setPrefHeight(100.0);
hBox.setPrefWidth(200.0);
hBox.setSpacing(10.0);
vBox.setAlignment(javafx.geometry.Pos.CENTER_LEFT);
vBox.setPrefHeight(200.0);
vBox.setPrefWidth(100.0);
//Adding general button
generalRadio.setMnemonicParsing(false);
generalRadio.setSelected(true);
generalRadio.setText("General");
generalRadio.setToggleGroup(Goods);
//Adding Arms Button
armsRadio.setMnemonicParsing(false);
armsRadio.setText("Arms");
armsRadio.setToggleGroup(Goods);
//Adding Silk button
silkRadio.setMnemonicParsing(false);
silkRadio.setText("Silk");
silkRadio.setToggleGroup(Goods);
// Adding opium button
opiumRadio.setMnemonicParsing(false);
opiumRadio.setText("Opium");
opiumRadio.setToggleGroup(Goods);
// Remove materials button
withdrawButton.setMnemonicParsing(false);
withdrawButton.setText("Withdraw");
// Add materials button
depositButton.setMnemonicParsing(false);
depositButton.setText("Deposit");
//Go back to the previous screen button
quitButton.setMnemonicParsing(false);
quitButton.setText("Go back");
borderPane.setBottom(hBox);
//Takes you to the HK warehouse
borderPane.setAlignment(title, javafx.geometry.Pos.CENTER);
title.setText("Hong Kong Warehouse");
title.setFont(new Font(24.0));
title.setPadding(new Insets(10.0, 0.0, 0.0, 0.0));
borderPane.setTop(title);
// Making the BOX again
borderPane.setAlignment(hBox0, javafx.geometry.Pos.CENTER);
hBox0.setAlignment(javafx.geometry.Pos.CENTER);
hBox0.setPrefHeight(100.0);
hBox0.setPrefWidth(200.0);
hBox0.setSpacing(10.0);
vBox0.setAlignment(javafx.geometry.Pos.CENTER_LEFT);
vBox0.setSpacing(10.0);
playerLabel.setText("Player:");
playerLabel.setFont(new Font(18.0));
// displays the hold of general
playerGeneral.setText("General:");
// displays the hold of Arms
playerArms.setText("Arms:");
// displays the hold of Silk
playerSilk.setText("Silk:");
// displays the hold of Opium
playerOpium.setText("Opium:");
vBox1.setAlignment(javafx.geometry.Pos.CENTER_LEFT);
vBox1.setSpacing(10.0);
houseLabel.setAlignment(javafx.geometry.Pos.TOP_CENTER);
houseLabel.setText("Warehouse:");
houseLabel.setFont(new Font(18.0));
houseGeneral.setText("General:");
houseArms.setText("Arms:");
houseSilk.setText("Silk:");
houseOpium.setText("Opium:");
//Shows the values of the goods that are in the warehouse
borderPane.setMargin(hBox0, new Insets(0.0));
hBox0.setPadding(new Insets(10.0, 0.0, 0.0, 0.0));
vBox0.setPadding(new Insets(0, 0.0, 0.0, 0.0));
borderPane.setCenter(hBox0);
//Add buttons for the goods
vBox.getChildren().add(generalRadio);
vBox.getChildren().add(armsRadio);
vBox.getChildren().add(silkRadio);
vBox.getChildren().add(opiumRadio);
//Adds the buttons for withdraw, deposit and quitting
hBox.getChildren().add(vBox);
hBox.getChildren().add(textField);
hBox.getChildren().add(withdrawButton);
hBox.getChildren().add(depositButton);
hBox.getChildren().add(quitButton);
//Adds the buttons for the amount of the good the player has
vBox0.getChildren().add(playerLabel);
vBox0.getChildren().add(playerGeneral);
vBox0.getChildren().add(playerArms);
vBox0.getChildren().add(playerSilk);
vBox0.getChildren().add(playerOpium);
//Amount of stock the warehouse has of the goods
vBox1.getChildren().add(houseLabel);
vBox1.getChildren().add(houseGeneral);
vBox1.getChildren().add(houseArms);
vBox1.getChildren().add(houseSilk);
vBox1.getChildren().add(houseOpium);
hBox0.getChildren().add(vBox0);
hBox0.getChildren().add(vBox1);
updateLabels();
//Goes back to shop
quitButton.setOnAction(event -> {
TaipanShopGUI taipanShopGUI = new TaipanShopGUI(getPlayer());
taipanShopGUI.initializeShop(stage);
stage.show();
});
//Runs when the withdraw button is selected. and removes the amount of the selected good
withdrawButton.setOnAction(event -> {
try {
int withdraw = Integer.parseInt(textField.getText());
if(withdraw <= 0){
title.setText("Please enter a valid value");
}
//Transfers general amount
else if(Goods.getSelectedToggle() == generalRadio && withdraw <= getwGeneral()){
setGeneralHeld(getPlayer().getGeneralHeld()+withdraw);
setwGeneral(getPlayer().getwGeneral()-withdraw);
}
//Transfers Arms amount
else if(Goods.getSelectedToggle() == armsRadio && withdraw <= getwArms()){
setArmsHeld(getPlayer().getArmsHeld()+withdraw);
setwArms(getPlayer().getwArms()-withdraw);
}
//Transfers Silk Amount
else if(Goods.getSelectedToggle() == silkRadio && withdraw <= getwSilk()){
setSilkHeld(getPlayer().getSilkHeld()+withdraw);
setwSilk(getPlayer().getwSilk()-withdraw);
}
//Transfers Opium amount
else if(Goods.getSelectedToggle() == opiumRadio && withdraw <= getwOpium()) {
setOpiumHeld(getPlayer().getOpiumHeld() + withdraw);
setwOpium(getPlayer().getwOpium() - withdraw);
}
// Ensures a valid value is entered
else{
title.setText("Please enter a valid value");
}
updateLabels();
}
catch (Exception e) {
title.setText("Please enter a valid value");
}
});
//Button to add a user entered amount to the warehouse
depositButton.setOnAction(event -> {
try {
int deposit = Integer.parseInt(textField.getText());
if(deposit <= 0){
title.setText("Please enter a valid value");
}
//Transfers general amount
else if(Goods.getSelectedToggle() == generalRadio && deposit <= getGeneralHeld()){
setGeneralHeld(getPlayer().getGeneralHeld()-deposit);
setwGeneral(getPlayer().getwGeneral()+deposit);
}
//Transfers Arms amount
else if(Goods.getSelectedToggle() == armsRadio && deposit <= getArmsHeld()){
setArmsHeld(getPlayer().getArmsHeld()-deposit);
setwArms(getPlayer().getwArms()+deposit);
}
//Transfers Silk amount
else if(Goods.getSelectedToggle() == silkRadio && deposit <= getSilkHeld()){
setSilkHeld(getPlayer().getSilkHeld()-deposit);
setwSilk(getPlayer().getwSilk()+deposit);
}
//Transfers Opium amount
else if(Goods.getSelectedToggle() == opiumRadio && deposit <= getOpiumHeld()){
setOpiumHeld(getPlayer().getOpiumHeld()-deposit);
setwOpium(getPlayer().getwOpium()+deposit);
}
//Checks if the correct value is entered
else{
title.setText("Please enter a valid value");
}
updateLabels();
}
catch (Exception e) {
title.setText("Please enter a valid value");
}
});
//Create the sceene and box of the desired size
Scene root = new Scene(borderPane, 600, 480);
root.getStylesheets().add("styleguide.css");
stage.setTitle("Warehouse");
stage.setResizable(false);
stage.setScene(root);
return stage;
}
/**
* As soon as the transfer is made the table of the amount of goods is updated using this method
*/
public void updateLabels(){
playerLabel.setText("Player: " + (getPlayer().getCargoSpace()-((getPlayer().getGuns()*10)+ getPlayer().getGeneralHeld() + getPlayer().getArmsHeld() + getPlayer().getSilkHeld() + getPlayer().getOpiumHeld())));
houseLabel.setText("Warehouse: " + (10000 -(getPlayer().getwGeneral() + getPlayer().getwArms() + getPlayer().getwSilk() + getPlayer().getwOpium())));
playerGeneral.setText("General: " + getPlayer().getGeneralHeld());
playerArms.setText("Arms: " + getPlayer().getArmsHeld());
playerSilk.setText("Silk: " + getPlayer().getSilkHeld() );
playerOpium.setText("Opium: " + getPlayer().getOpiumHeld());
houseGeneral.setText("General: " + getPlayer().getwGeneral());
houseArms.setText("Arms: " + getPlayer().getwArms());
houseSilk.setText("Silk: " + getPlayer().getwSilk());
houseOpium.setText("Opium: " + getPlayer().getwOpium());
}
}