Adding ShipWarfareText

This commit is contained in:
KahootChampion
2019-04-07 12:41:45 -06:00
parent 4db5b6e318
commit 97927dc3f8
3 changed files with 282 additions and 48 deletions

View File

@@ -99,11 +99,11 @@ public class ShipWarfareGUI extends Player {
/** /**
* The user faces off against the litty ships and either prevails, dies, or runs away * 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 * @return true if the user wins, loses, or flees, it returns false otherwise
*/ */
public boolean destroyLittyShipsOrEscape(Stage stage) throws Exception { public boolean destroyShipsOrEscape(Stage stage) throws Exception {
cannon.setLayoutX(beginningX); cannon.setLayoutX(beginningX);
cannon.setLayoutY(beginningY); cannon.setLayoutY(beginningY);
@@ -125,9 +125,9 @@ public class ShipWarfareGUI extends Player {
int hitOrMiss = randomValue.nextInt(2) + 1; int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) { if (hitOrMiss == 2) {
logic.setNumOfLittyShips(logic.getNumOfLittyShips()-1); logic.setNumOfShips(logic.getNumOfShips()-1);
if (logic.getNumOfLittyShips() <= 0) { if (logic.getNumOfShips() <= 0) {
exitValue = 1; exitValue = 1;
} }
hitCounter++; hitCounter++;
@@ -153,7 +153,7 @@ public class ShipWarfareGUI extends Player {
if (logic.getNumOfLittyShips() <= 0) { if (logic.getNumOfShips() <= 0) {
exitValue = 1; exitValue = 1;
} }
@@ -161,10 +161,10 @@ public class ShipWarfareGUI extends Player {
chanceOfEnemyRun = randomValue.nextInt(2) + 1; chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) { if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1; howMuchRun = randomValue.nextInt(15) + 1;
if (howMuchRun != 0 && howMuchRun < logic.getNumOfLittyShips()) { if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
logic.setNumOfLittyShips(logic.getNumOfLittyShips() - howMuchRun); logic.setNumOfShips(logic.getNumOfShips() - howMuchRun);
if (userAttacks == true) { if (userAttacks == true) {
if (howMuchRun > 0) { if (howMuchRun > 0) {
runAwayOrLeft.setText(String.format("Cowards! %d ships ran away %s! ", howMuchRun, super.getName())); runAwayOrLeft.setText(String.format("Cowards! %d ships ran away %s! ", howMuchRun, super.getName()));
@@ -178,14 +178,14 @@ public class ShipWarfareGUI extends Player {
} }
} }
shipsRemaining.setText(String.format("%d ships remaining and they look angry!", logic.getNumOfLittyShips())); shipsRemaining.setText(String.format("%d ships remaining and they look angry!", logic.getNumOfShips()));
//Computer volley //Computer volley
int takeGunChance = randomValue.nextInt(4) + 1; int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && super.getGuns() > 0) { if (takeGunChance == 1 && super.getGuns() > 0) {
super.setGuns(super.getGuns() - 1); super.setGuns(super.getGuns() - 1);
gunFrustration = true; gunFrustration = true;
} else { } else {
if (logic.getNumOfLittyShips() > 0) { if (logic.getNumOfShips() > 0) {
int HPTaken = randomValue.nextInt(10) + 1; int HPTaken = randomValue.nextInt(10) + 1;
super.setHP(super.getHP() - (HPTaken)); super.setHP(super.getHP() - (HPTaken));
@@ -228,16 +228,8 @@ public class ShipWarfareGUI extends Player {
gameEndGUI.initializeGameEndGUI(stage); gameEndGUI.initializeGameEndGUI(stage);
stage.show(); stage.show();
return true; return true;
} else if (exitValue == 3) {
report.setText(String.format("We made it out at %d%% ship status!", super.getHP()));
continueButton.setVisible(true);
completeWipe();
fightButton.setVisible(false);
runButton.setVisible(false);
continueButton.setDefaultButton(true);
return true;
} }
return false; return false;
} }
@@ -309,13 +301,13 @@ public class ShipWarfareGUI extends Player {
*/ */
public void initializeShip(Stage primaryStage) throws Exception { public void initializeShip(Stage primaryStage) throws Exception {
logic.setNumOfLittyShips(logic.numOfShips()); logic.setNumOfShips(logic.numOfShips());
Pane root = new Pane(); Pane root = new Pane();
HBox usAgainstEnemyDivisor; HBox usAgainstEnemyDivisor;
BorderPane centeringUserShipPane = new BorderPane(); BorderPane centeringUserShipPane = new BorderPane();
Circle cannon; Circle cannon;
BorderPane centeringLittyShipPane = new BorderPane(); BorderPane centeringShipPane = new BorderPane();
BorderPane encompassingPane = new BorderPane(); BorderPane encompassingPane = new BorderPane();
usAgainstEnemyDivisor = new HBox(); usAgainstEnemyDivisor = new HBox();
cannon = new Circle(); cannon = new Circle();
@@ -337,7 +329,7 @@ public class ShipWarfareGUI extends Player {
shipsRemaining = new Label(); shipsRemaining = new Label();
report = 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.getNumOfLittyShips())); 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"); fightButton.setText("Fight");
@@ -396,7 +388,7 @@ public class ShipWarfareGUI extends Player {
//Setting the image view //Setting the image view
ImageView userShip = new ImageView(ourShip); ImageView userShip = new ImageView(ourShip);
ImageView littyShip = new ImageView(enemyShip); ImageView Ship = new ImageView(enemyShip);
BorderPane.setAlignment(userShip, javafx.geometry.Pos.CENTER); BorderPane.setAlignment(userShip, javafx.geometry.Pos.CENTER);
userShip.setFitHeight(150.0); userShip.setFitHeight(150.0);
@@ -415,21 +407,21 @@ public class ShipWarfareGUI extends Player {
cannon.setStrokeType(javafx.scene.shape.StrokeType.INSIDE); cannon.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
centeringUserShipPane.setRight(cannon); centeringUserShipPane.setRight(cannon);
centeringLittyShipPane.setPrefHeight(200.0); centeringShipPane.setPrefHeight(200.0);
centeringLittyShipPane.setPrefWidth(200.0); centeringShipPane.setPrefWidth(200.0);
centeringLittyShipPane.setOpaqueInsets(new Insets(0.0)); centeringShipPane.setOpaqueInsets(new Insets(0.0));
HBox.setMargin(centeringLittyShipPane, new Insets(0.0, 0.0, 0.0, 200.0)); HBox.setMargin(centeringShipPane, new Insets(0.0, 0.0, 0.0, 200.0));
encompassingPane.setAlignment(littyShip, javafx.geometry.Pos.CENTER); encompassingPane.setAlignment(Ship, javafx.geometry.Pos.CENTER);
littyShip.setFitHeight(165.0); Ship.setFitHeight(165.0);
littyShip.setFitWidth(180.0); Ship.setFitWidth(180.0);
littyShip.setPickOnBounds(true); Ship.setPickOnBounds(true);
littyShip.setPreserveRatio(true); Ship.setPreserveRatio(true);
encompassingPane.setMargin(littyShip, new Insets(0.0, 0.0, 20.0, 0.0)); encompassingPane.setMargin(Ship, new Insets(0.0, 0.0, 20.0, 0.0));
centeringLittyShipPane.setCenter(littyShip); centeringShipPane.setCenter(Ship);
usAgainstEnemyDivisor.getChildren().add(centeringUserShipPane); usAgainstEnemyDivisor.getChildren().add(centeringUserShipPane);
usAgainstEnemyDivisor.getChildren().add(centeringLittyShipPane); usAgainstEnemyDivisor.getChildren().add(centeringShipPane);
fightRunBox.getChildren().add(fightButton); fightRunBox.getChildren().add(fightButton);
fightRunBox.getChildren().add(continueButton); fightRunBox.getChildren().add(continueButton);
fightRunBox.getChildren().add(runButton); fightRunBox.getChildren().add(runButton);
@@ -498,7 +490,7 @@ public class ShipWarfareGUI extends Player {
if (logic.runFromShips(userAttacks) == false) { if (logic.runFromShips(userAttacks) == false) {
report.setText(("Couldn't run away")); report.setText(("Couldn't run away"));
try { try {
winOrLose = destroyLittyShipsOrEscape(primaryStage); winOrLose = destroyShipsOrEscape(primaryStage);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -535,7 +527,7 @@ public class ShipWarfareGUI extends Player {
runButton.setVisible(false); runButton.setVisible(false);
try { try {
winOrLose = destroyLittyShipsOrEscape(primaryStage); winOrLose = destroyShipsOrEscape(primaryStage);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@@ -1,5 +1,3 @@
import javafx.stage.Stage;
import java.util.Random; import java.util.Random;
/** /**
@@ -13,9 +11,8 @@ public class ShipWarfareLogic extends Player {
private int numOfLittyShips = 0; private int numOfShips = 0;
private int startingLittyShips=0; private int startingShips=0;
private int counter; private int counter;
@@ -28,18 +25,18 @@ public class ShipWarfareLogic extends Player {
/** /**
* setter method that takes in an integer as an argument * setter method that takes in an integer as an argument
* *
* @param numOfLittyShips the number of ships to be used in the peasant fleet attack * @param numOfShips the number of ships to be used in the peasant fleet attack
*/ */
public void setNumOfLittyShips(int numOfLittyShips) { public void setNumOfShips(int numOfShips) {
counter++; counter++;
this.numOfLittyShips = numOfLittyShips; this.numOfShips = numOfShips;
if (counter == 1) { if (counter == 1) {
startingLittyShips = numOfLittyShips; startingShips = numOfShips;
} }
} }
public int getNumOfLittyShips(){ public int getNumOfShips(){
return numOfLittyShips; return numOfShips;
} }
/** /**
@@ -95,7 +92,7 @@ public class ShipWarfareLogic extends Player {
Random randomValue = new Random(); Random randomValue = new Random();
int calculateLoot; int calculateLoot;
calculateLoot = (startingLittyShips * 100) + randomValue.nextInt(startingLittyShips) * 200; calculateLoot = (startingShips * 100) + randomValue.nextInt(startingShips) * 200;
super.setMoney(super.getMoney() + calculateLoot); super.setMoney(super.getMoney() + calculateLoot);
return calculateLoot; return calculateLoot;

245
src/ShipWarfareText.java Normal file
View File

@@ -0,0 +1,245 @@
import java.util.Scanner;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class ShipWarfareText extends Player {
ShipWarfareLogic logic = new ShipWarfareLogic(getPlayer());
private boolean userAttacks = true;
private int howMuchRun = 0;
private Player player;
/**
* Class Constructor that takes in a type player as a parameter
* @param player object of the class Player
*/
public ShipWarfareText(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* setter method for player
* @param player object of the class Player
*/
public void setPlayer(Player player) {
Player playerDummy = new Player(player);
this.player = playerDummy;
}
/**
* getter method for obtaining a player object.
* @return returns player object
*/
public Player getPlayer() {
Player playerDummy = new Player(player);
return playerDummy;
}
/**
* This fleet is easy to defeat as a maximum of 15 ships can run away each volley, they can not tank hits
* @throws Exception in case of errors due to the delay
*/
public void peasantFleetAttack() throws Exception {
Scanner userResponse = new Scanner(System.in);
logic.setNumOfShips(logic.numOfShips());
System.out.printf("By Golly! We have $%,d and are being attacked by %d Merchant ships\nCurrently our ship status is %d%%\n", player.getMoney(), logic.getNumOfShips(), player.getHP());
fightOrRunMessage();
while (true) {
String response = userResponse.nextLine();
if (response.equalsIgnoreCase("f")) {
userAttacks = true;
System.out.println("Ohh, fight ehh?");
delayForSeconds(1);
boolean winOrLose = destroyPeasantShipsOrEscape();
if (winOrLose == true) {
break;
}
} else if (response.equalsIgnoreCase("r")) {
if (logic.runFromShips(userAttacks) == false) {
System.out.println("Couldn't run away!");
if (destroyPeasantShipsOrEscape())
break;
} else {
System.out.println("Phew! Got away safely");
delayForSeconds(2);
break;
}
}
}
}
/**
* Asks user if they would like to fight or run against ships
*/
public void fightOrRunMessage() {
System.out.printf("What do you want to do? Enter \"f\" to fight, and \"r\" to run (we have %d guns)\n", player.getGuns());
}
/**
* delays for a specific amount of seconds, takes an integer as an argument
* @param num the seconds to delay
* @throws Exception in case of errors due to the delay
*/
public void delayForSeconds(int num) throws Exception {
TimeUnit.SECONDS.sleep(num);
}
/**
* The user faces off against the peasant ships and either prevails, dies, or runs away
* @return true if the user wins, loses, or flees, it returns false otherwise
* @throws Exception in case of errors due to the delay
*/
public boolean destroyPeasantShipsOrEscape() throws Exception {
int calculateLoot = 0;
int chanceOfEnemyRun = 0;
Scanner userInput = new Scanner(System.in);
Random randomValue = new Random();
int exitValue = 0;
//Player volley
while (exitValue == 0) {
if (player.getGuns() > 0) {
for (int j = 0; j < player.getGuns(); j++) {
if (userAttacks == true) {
int hitOrMiss = randomValue.nextInt(2) + 1;
if (hitOrMiss == 2) {
logic.setNumOfShips(logic.getNumOfShips()-1);
if (logic.getNumOfShips() <= 0) {
exitValue = 1;
break;
}
System.out.println("Got eem");
delayForSeconds(1);
} else {
System.out.printf("ARRG! We missed %s\n", player.getName());
delayForSeconds(1);
}
} else {
continue;
}
}
}
else{
System.out.printf("%s! We don't have any GUNS!!!!\n", player.getName());
delayForSeconds(1);
}
if (logic.getNumOfShips() <= 0) {
exitValue = 1;
break;
}
if (player.getGuns() > 0) {
chanceOfEnemyRun = randomValue.nextInt(2) + 1;
if (chanceOfEnemyRun == 2) {
howMuchRun = randomValue.nextInt(15) + 1;
if (howMuchRun != 0 && howMuchRun < logic.getNumOfShips()) {
logic.setNumOfShips(logic.getNumOfShips() - howMuchRun);
if (userAttacks == true) {
System.out.printf("Ahhh, %d ships ran away %s!\n", howMuchRun, player.getName());
} else {
System.out.printf("Escaped %d of them!\n", howMuchRun);
}
}
}
}
System.out.printf("%d ships remaining\n", logic.getNumOfShips());
delayForSeconds(1);
System.out.println("Oh no, they are taking the offensive!");
delayForSeconds(1);
//Computer volley
int takeGunChance = randomValue.nextInt(4) + 1;
if (takeGunChance == 1 && player.getGuns() > 0) {
player.setGuns(player.getGuns() - 1);
System.out.println("Dang it! They destroyed one of our guns");
} else {
player.setHP(player.getHP() - (1 + randomValue.nextInt(10)));
}
if (player.getHP() > 0) {
displayQuery(userInput);
String response ="";
if (response.equalsIgnoreCase("r")) {
if (logic.runFromShips(userAttacks) == false) {
System.out.println("Couldn't run away");
} else {
exitValue = 3;
break;
}
}
} else {
exitValue = 2;
break;
}
}
if (exitValue == 1) {
System.out.printf("\nGot eem\nVictory!\nIt appears we have defeated the enemy fleet and made it out at %d%% ship status\n", player.getHP());
delayForSeconds(1);
calculateLoot = logic.calculateLoot();
System.out.printf("We got $%,d!", calculateLoot);
delayForSeconds(2);
return true;
} else if (exitValue == 2) {
player.gameOver();
return true;
}
return false;
}
public void displayQuery(Scanner userInput) throws Exception {
System.out.printf("EEK, our current ship status is %d%% \n", player.getHP());
delayForSeconds(1);
if (userAttacks == false) {
userAttacks = true;
}
System.out.printf("Shall we continue to fight? Enter \"f\" to fight, and \"r\" to run (We have %d gun(s) left)\n", player.getGuns());
String response = userInput.nextLine();
}
public static void main(String[] args) throws Exception {
Player littyBoi = new Player();
littyBoi.setHP(100);
littyBoi.setGuns(5);
littyBoi.setMoney(1000);
littyBoi.setName("Taipan");
ShipWarfareText test = new ShipWarfareText(littyBoi);
test.peasantFleetAttack();
}
}