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

73
src/logic/FileSaving.java Normal file
View File

@@ -0,0 +1,73 @@
package logic;
import java.io.*;
/**
* 2019-03-10 (Edited on 2019-03-19)
* Authors:
* FileSaving Class allows the user to save the current status of the game and to continue from where they left off.
*/
public class FileSaving extends Player implements Serializable {
/**
*loads the file of type player which contains all the player instances
* @return player
*/
public Player loadFile() {
try {
InputStream in = new FileInputStream(new File("src/saves/playerSave.txt"));
ObjectInputStream inObject = new ObjectInputStream(in);
Player player = (Player) inObject.readObject();
in.close();
inObject.close();
return player;
}
catch (Exception e) {
try {
InputStream in = new FileInputStream(new File("saves/playerSave.txt"));
ObjectInputStream inObject = new ObjectInputStream(in);
Player player = (Player) inObject.readObject();
in.close();
inObject.close();
return player;
}
catch(Exception e2){
return null;
}
}
}
/**
* Saves the file of type player which contains all the player instances
* @param player The player object being saved
*/
public boolean saveFile(Player player){
try{
FileOutputStream out = new FileOutputStream(new File("src/saves/playerSave.txt"));
ObjectOutputStream outObject = new ObjectOutputStream(out);
outObject.writeObject(player);
out.close();
outObject.close();
//returns true if program can save file
return true;
}
catch (Exception e) {
try {
FileOutputStream out = new FileOutputStream(new File("saves/playerSave.txt"));
ObjectOutputStream outObject = new ObjectOutputStream(out);
outObject.writeObject(player);
out.close();
outObject.close();
return true;
}
catch(Exception e2){
return false;
}
}
}
}

592
src/logic/Player.java Normal file
View File

@@ -0,0 +1,592 @@
package logic;
import java.io.Serializable;
/**
* 2019-03-10
* Authors: Harkamal, Vikram, Haris, Siddhant, Nathan
* Player class, stores all the information about the player such as inventory, health, etc
*
*/
public class Player implements Serializable {
private String name = "Taipan";
private int bank = 0;
private int money = 0;
private int opiumHeld = 0;
private int silkHeld = 0;
private int generalHeld = 0;
private int armsHeld = 0;
private int location = 1;
private int guns = 5;
private int HP = 100;
private int debt = 0;
private int wOpium = 0;
private int wSilk = 0;
private int wGeneral = 0;
private int wArms = 0;
private boolean retire = false;
private int cargoSpace = 60;
private int opiumPrice = 16000;
private int silkPrice = 1600;
private int armsPrice = 160;
private int generalPrice = 8;
private int isPriceChanged = 0;
private boolean attackingShips = true;
/**
* default constructor for player
*/
public Player(){
}
/**
* Copy constructor
*
* @param player object of the class Player
*/
public Player(Player player) {
setPlayer(player);
}
/**
* getter for this player object
*
* @return a copy of this player object
*/
public Player getPlayer(){
return new Player(this);
}
/**
* copies the provided player's properties into the instance variable of this object
*
* @param player
*/
public void setPlayer(Player player){
this.name = player.name;
this.bank = player.bank;
this.money = player.money;
this.opiumHeld = player.opiumHeld;
this.silkHeld = player.silkHeld;
this.generalHeld = player.generalHeld;
this.armsHeld = player.armsHeld;
this.location = player.location;
this.guns = player.guns;
this.HP = player.HP;
this.debt = player.debt;
this.wOpium = player.wOpium;
this.wSilk = player.wSilk;
this.wGeneral = player.wGeneral;
this.wArms = player.wArms;
this.retire = player.retire;
this.cargoSpace = player.cargoSpace;
this.opiumPrice = player.opiumPrice;
this.silkPrice = player.silkPrice;
this.armsPrice = player.armsPrice;
this.generalPrice = player.generalPrice;
this.isPriceChanged = player.isPriceChanged;
this.attackingShips = player.attackingShips;
}
/**
* getter method for the instance variable cargoSpace.
*
* @return returns the instance variable cargoSpace
*/
public int getCargoSpace() {
return cargoSpace;
}
/**
* setter method for the instance variable cargoSpace.
*
* @param cargoSpace takes an int that is greater than 0 as an argument
*/
public void setCargoSpace(int cargoSpace) {
if (cargoSpace > 0) {
this.cargoSpace = cargoSpace;
}
}
/**
* getter method for the instance variable attackingShips.
*
* @return returns the instance variable attackingShips
*/
public boolean getAttackingShips() {
return attackingShips;
}
/**
* setter method for the instance variable attackingShips.
*
* @param attackingShips takes an int that is greater than 0 as an argument
*/
public void setAttackingShips(boolean attackingShips) {
this.attackingShips = attackingShips;
}
/**
* getter method for the instance variable retire.
*
* @return returns the instance variable retire
*/
public boolean getRetire() {
return retire;
}
/**
* setter method for the instance variable retire.
*
* @param retire takes a boolean as an argument
*/
public void setRetire(boolean retire) {
if (retire) {
this.retire = retire;
}
}
/**
* getter method for the instance variable name.
*
* @return returns the instance variable name
*/
public String getName() {
return name;
}
/**
* setter method for the instance variable name.
*
* @param name takes a string as an argument
*/
public void setName(String name) {
this.name = name;
}
/**
* getter method for the instance variable HP.
*
* @return returns the instance variable HP
*/
public int getHP() {
return HP;
}
/**
* setter method for the instance variable HP.
*
* @param HP takes an int as an argument
*/
public void setHP(int HP) {
this.HP = HP;
}
/**
* getter method for the instance variable bank.
*
* @return returns the instance variable bank
*/
public int getBank() {
return bank;
}
/**
* setter method for the instance variable bank.
*
* @param bank takes an int that is greater than or equal to 0 as an argument
*/
public void setBank(int bank) {
if (bank >= 0) {
this.bank = bank;
}
}
/**
* getter method for the instance variable money.
*
* @return returns the instance variable money
*/
public int getMoney() {
return money;
}
/**
* setter method for the instance variable money.
*
* @param money takes an int that is greater than or equal to 0 as an argument
*/
public void setMoney(int money) {
if (money >= 0) {
this.money = money;
}
}
/**
* getter method for the instance variable opiumHeld.
*
* @return returns the instance variable opiumHeld
*/
public int getOpiumHeld() {
return opiumHeld;
}
/**
* setter method for the instance variable opiumHeld.
*
* @param opiumHeld takes an int that is greater than or equal to 0 as an argument
*/
public void setOpiumHeld(int opiumHeld) {
if (opiumHeld >= 0) {
this.opiumHeld = opiumHeld;
}
}
/**
* getter method for the instance variable silkHeld.
*
* @return returns the instance variable silkHeld
*/
public int getSilkHeld() {
return silkHeld;
}
/**
* setter method for the instance variable silkHeld.
*
* @param silkHeld takes an int that is greater than or equal to 0 as an argument
*/
public void setSilkHeld(int silkHeld) {
if (silkHeld >= 0) {
this.silkHeld = silkHeld;
}
}
/**
* getter method for the instance variable generalHeld.
*
* @return returns the instance variable generalHeld
*/
public int getGeneralHeld() {
return generalHeld;
}
/**
* setter method for the instance variable generalHeld.
*
* @param generalHeld takes an int that is greater than or equal to 0 as an argument
*/
public void setGeneralHeld(int generalHeld) {
if (generalHeld >= 0) {
this.generalHeld = generalHeld;
}
}
/**
* getter method for the instance variable armsHeld.
*
* @return returns the instance variable armsHeld
*/
public int getArmsHeld() {
return armsHeld;
}
/**
* setter method for the instance variable armsHeld.
*
* @param armsHeld takes an int that is greater than or equal to 0 as an argument
*/
public void setArmsHeld(int armsHeld) {
if (armsHeld >= 0) {
this.armsHeld = armsHeld;
}
}
/**
* getter method for the instance variable location.
*
* @return returns the instance variable location
*/
public int getLocation() {
return location;
}
/**
* setter method for the instance variable location.
*
* @param location takes an int that is greater than or equal to 0 as an argument
*/
public void setLocation(int location) {
if (location >= 0) {
this.location = location;
}
}
/**
* getter method for the instance variable guns.
*
* @return returns the instance variable guns
*/
public int getGuns() {
return guns;
}
/**
* setter method for the instance variable guns.
*
* @param guns takes an int that is greater than or equal to 0 as an argument
*/
public void setGuns(int guns) {
if (guns >= 0) {
this.guns = guns;
}
}
/**
* getter method for the instance variable debt.
*
* @return returns the instance variable debt
*/
public int getDebt() {
return debt;
}
/**
* setter method for the instance variable debt.
*
* @param debt takes an int that is greater than or equal to 0 as an argument
*/
public void setDebt(int debt) {
if (debt >= 0) {
this.debt = debt;
}
}
/**
* getter method for the instance variable wOpium.
*
* @return returns the instance variable wOpium
*/
public int getwOpium() {
return wOpium;
}
/**
* setter method for the instance variable wOpium.
*
* @param wOpium takes an int that is greater than or equal to 0 as an argument
*/
public void setwOpium(int wOpium) {
if (wOpium >= 0) {
this.wOpium = wOpium;
}
}
/**
* getter method for the instance variable wSilk.
*
* @return returns the instance variable wSilk
*/
public int getwSilk() {
return wSilk;
}
/**
* setter method for the instance variable wSilk.
*
* @param wSilk takes an int that is greater than or equal to 0 as an argument
*/
public void setwSilk(int wSilk) {
if (wSilk >= 0) {
this.wSilk = wSilk;
}
}
/**
* getter method for the instance variable wGeneral.
*
* @return returns the instance variable wGeneral
*/
public int getwGeneral() {
return wGeneral;
}
/**
* setter method for the instance variable wGeneral.
*
* @param wGeneral takes an int that is greater than or equal to 0 as an argument
*/
public void setwGeneral(int wGeneral) {
if (wGeneral >= 0) {
this.wGeneral = wGeneral;
}
}
/**
* getter method for the instance variable wArms.
*
* @return returns the instance variable wArms
*/
public int getwArms() {
return wArms;
}
/**
* setter method for the instance variable wArms.
*
* @param wArms takes an int that is greater than or equal to 0 as an argument
*/
public void setwArms(int wArms) {
if (wArms >= 0) {
this.wArms = wArms;
}
}
/**
* getter for opiumPrice instance variable.
*
* @return opiumPrice -- the price of opium in the shop
*/
public int getOpiumPrice() {
return opiumPrice;
}
/**
* setter for the opiumPrice instance variable. Runs as long as the parameter is greater than 0.
*
* @param opiumPrice -- what the instance variable opiumPrice should be changed to.
*/
public void setOpiumPrice(int opiumPrice) {
if (opiumPrice > 0) {
this.opiumPrice = opiumPrice;
}
}
/**
* getter for silkPrice instance variable.
*
* @return silkPrice -- the price of silk in the shop.
*/
public int getSilkPrice() {
return silkPrice;
}
/**
* setter for the silkPrice instance variable. Runs as long as the parameter is greater than 0.
*
* @param silkPrice -- what the instance variable silkPrice should be changed to.
*/
public void setSilkPrice(int silkPrice) {
if (silkPrice > 0) {
this.silkPrice = silkPrice;
}
}
/**
* getter for armsPrice instance variable.
*
* @return armsPrice -- the price of arms in the shop.
*/
public int getArmsPrice() {
return armsPrice;
}
/**
* setter for the armsPrice instance variable. Runs as long as the parameter is greater than 0.
*
* @param armsPrice -- what the instance variable armsPrice should be changed to.
*/
public void setArmsPrice(int armsPrice) {
if (armsPrice > 0) {
this.armsPrice = armsPrice;
}
}
/**
* getter for generalPrice instance variable.
*
* @return generalPrice -- the price of general cargo in the shop.
*/
public int getGeneralPrice() {
return generalPrice;
}
/**
* setter for the generalPrice instance variable. Runs as long as the parameter is greater than 0.
*
* @param generalPrice -- what the instance variable generalPrice should be changed to.
*/
public void setGeneralPrice(int generalPrice) {
if (generalPrice > 0) {
this.generalPrice = generalPrice;
}
}
/**
* getter for isPriceChanged instance variable.
*
* @return isPriceChanged -- Checks if the price has changed since last in shop.
*/
public int getIsPriceChanged() {
return isPriceChanged;
}
/**
* setter for the isPriceChanged instance variable. Runs as long as the parameter is greater than 0.
*
* @param isPriceChanged -- what the instance variable isPriceChanged should be changed to.
*/
public void setIsPriceChanged(int isPriceChanged) {
if(isPriceChanged >= 0) {
this.isPriceChanged = isPriceChanged;
}
}
/**
* Method to indicate that you have lost the game. If the player has lost, console will be cleared and will only
* show the statement "Game Over". After showing the message the game closes.
**/
public void gameOver() {
System.out.flush();
System.out.println("Game over");
System.exit(0);
}
}

View File

@@ -0,0 +1,108 @@
package logic;
import java.util.Random;
/**
* 2019-03-10 (Edited on 2019-04-07)
* Author: Haris Muhammad
* ShipWarfareLogic class, logic for ships which the user can attack or run from
*/
public class ShipWarfareLogic extends Player {
private int numOfShips = 0;
private int startingShips = 0;
private int counter = 0;
/**
* constructor; only runs when a Player object is provided. The constructor is fully encapsulated.
*
* @param player is a Player object that will be copied and the player instance variable is set to the copy.
*/
public ShipWarfareLogic(Player player) {
Player playerDummy = new Player(player);
this.setPlayer(playerDummy);
}
/**
* setter method that takes in an integer as an argument
*
* @param numOfShips the number of ships to be used in the peasant fleet attack
*/
public void setNumOfShips(int numOfShips) {
counter++;
this.numOfShips = numOfShips;
if (counter == 1) {
startingShips = numOfShips;
}
}
/**
* get the number of ships attacking
*
* @return the number of ships attacking
*/
public int getNumOfShips() {
return numOfShips;
}
/**
* One in two chance of running away
*
* @return true if the user is allowed to run, false if not, the "default" is false
*/
public boolean runFromShips() {
Random randomValue = new Random();
int runSuccessChance = randomValue.nextInt(5) + 1;
if (runSuccessChance == 2) {
return true;
} else {
return false;
}
}
/**
* The number of ships that attack is based on the amount of money one has on hand
*
* @return the number of ships which will attack
*/
public int numOfShips() {
int numOfShipsAttacking = 0;
Random randomValue = new Random();
if (super.getMoney() <= 100000) {
//Minimum one ship will attack, maximum 20
numOfShipsAttacking = randomValue.nextInt(20) + 1;
} else if (super.getMoney() <= 200000) {
//Minimum 30 Ships will attack, maximum 70
numOfShipsAttacking = randomValue.nextInt(40) + 31;
} else if (super.getMoney() <= 500000) {
//Minimum 50 ships will attack, maximum 140
numOfShipsAttacking = randomValue.nextInt(90) + 51;
} else if (super.getMoney() >= 1000000) {
//Minimum 100 ships will attack, maximum 300 ships
numOfShipsAttacking = randomValue.nextInt(200) + 101;
}
return numOfShipsAttacking;
}
/**
* Calculates the loot for defeating a fleet
*
* @return the loot for defeating the fleet
*/
public int calculateLoot() {
Random randomValue = new Random();
int calculateLoot;
calculateLoot = (startingShips * 100) + randomValue.nextInt(startingShips) * 200;
super.setMoney(super.getMoney() + calculateLoot);
return calculateLoot;
}
}

View File

@@ -0,0 +1,156 @@
package logic; /**
* TaipanShopLogic deals with the computations necessary for the shop such as randomizing prices.
*
* Author: Vikram Bawa
*/
import java.util.Random;
public class TaipanShopLogic extends Player {
/**
* 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 TaipanShopLogic(Player player) {
Player playerDummy = new Player(player);
setPlayer(playerDummy);
}
/**
* this method is when the shop is accessed, randomizing the prices of all the items.
*/
public String updatePrices() {
String s = "\t" + getName() + ", the price of ";
String a = "";
double value = 0 * Math.random();
Random rand = new Random();
setOpiumPrice((rand.nextInt(201) + 60) * 100);
setSilkPrice((rand.nextInt(201) + 60) * 10);
setArmsPrice((rand.nextInt(21) + 6) * 10);
setGeneralPrice((rand.nextInt(17) + 4));
// there is a 10% chance that the price of an item is increased/decreased beyond its regular range.
if (value < 8) {
if (value < 2) {
if (value < 1) {
setOpiumPrice(getOpiumPrice() / 5);
a = s + "Opium has dropped to " + getOpiumPrice() + "!!!\n";
} else {
setOpiumPrice(getOpiumPrice() * 5);
a = s + "Opium has risen to " + getOpiumPrice() + "!!!\n" ;
}
} else if (value < 4) {
if (value < 3) {
setSilkPrice(getSilkPrice() / 5);
a = s + "Silk has dropped to " + getSilkPrice() + "!!!\n";
} else {
setSilkPrice(getSilkPrice() * 5);
a = s + "Silk has risen to " + getSilkPrice() + "!!!\n";
}
} else if (value < 6) {
if (value < 3) {
setArmsPrice(getArmsPrice() / 5);
a = s + "Arms has dropped to " + getArmsPrice() + "!!!\n";
} else {
setArmsPrice(getArmsPrice() * 5);
a = s + "Arms has risen to " + getArmsPrice() + "!!!\n";
}
} else {
if (value < 7) {
setGeneralPrice(1);
a = s + "General Cargo has dropped to 1!!!\n";
} else {
setGeneralPrice(getGeneralPrice() * 5);
a = s + "General Cargo has risen to " + getGeneralPrice() + "!!!\n";
}
}
}
return a;
}
/**
* 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;
}
/**
* 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;
}
}