I am recreating a game that I used to play on the Nintendo DSI, 'Dragon Quest IX'. I've made a player class, but have run into a code efficiency issue. When the Player
object is first instantiated, it has a constructor parameter player_class
that determines what class the user has chosen. In the method setAttributes
, it determines the baseline stats for the newly created player. I use multiple if statements to achieve this. Is there any way I can make this more compact/efficient, without having to have big blocky if statements that generically contain all the same code? Any and all help/suggestions are appreciated and considered.
Player.java
/*
* Player Class
*/
public class Player {
/*
* Private Instance Variables
*/
private String name;
private String player_class;
private int experience;
private int level;
private int gold;
private int hp;
private int mp;
private int strength;
private int agility;
private int resilience;
private int deftness;
private int charm;
private int magical_mending;
private int magical_might;
/*
* Bag object instantiation
*/
private Bag b;
/*
* New Player Constructor (no default since name is needed)
*/
public Player(String name, String player_class) {
this.name = name;
this.player_class = player_class;
this.experience = 0;
this.level = 0;
this.gold = 0;
this.b = new Bag();
}
/*
* Determine stats based on class chosen
*/
private void setAttributes() {
/*
* Can be optimized later, if statements will do
*/
if(this.player_class.equals("Warrior")) { /* DONE */
this.hp = 26;
this.mp = 4;
this.strength = 18;
this.agility = 4;
this.resilience = 18;
this.deftness = 5;
this.charm = 4;
this.magical_mending = 0;
this.magical_might = 0;
}
if(this.player_class.equals("Priest")) { /* DONE */
this.hp = 19;
this.mp = 14;
this.strength = 9;
this.agility = 14;
this.resilience = 9;
this.deftness = 9;
this.charm = 7;
this.magical_mending = 18;
this.magical_might = 0;
}
if(this.player_class.equals("Mage")) { /* DONE */
this.hp = 18;
this.mp = 16;
this.strength = 4;
this.agility = 18;
this.resilience = 7;
this.deftness = 14;
this.charm = 7;
this.magical_mending = 0;
this.magical_might = 18;
}
if(this.player_class.equals("Martial Artist")) { /* DONE */
this.hp = 24;
this.mp = 2;
this.strength = 18;
this.agility = 23;
this.resilience = 11;
this.deftness = 11;
this.charm = 5;
this.magical_mending = 0;
this.magical_might = 0;
}
if(this.player_class.equals("Thief")) { /* DONE */
this.hp = 23;
this.mp = 6;
this.strength = 13;
this.agility = 18;
this.resilience = 11;
this.deftness = 18;
this.charm = 3;
this.magical_mending = 4;
this.magical_might = 0;
}
if(this.player_class.equals("Minstrel")) { /* DONE */
this.hp = 20;
this.mp = 6;
this.strength = 9;
this.agility = 8;
this.resilience = 8;
this.deftness = 12;
this.charm = 9;
this.magical_mending = 7;
this.magical_might = 6;
}
if(this.player_class.equals("Gladiator")) { /* DONE */
this.hp = 32;
this.mp = 2;
this.strength = 30;
this.agility = 7;
this.resilience = 19;
this.deftness = 15;
this.charm = 5;
this.magical_mending = 0;
this.magical_might = 0;
}
if(this.player_class.equals("Armamentalist")) { /* DONE */
this.hp = 32;
this.mp = 14;
this.strength = 21;
this.agility = 10;
this.resilience = 15;
this.deftness = 7;
this.charm = 11;
this.magical_mending = 0;
this.magical_might = 17;
}
if(this.player_class.equals("Paladin")) { /* DONE */
this.hp = 36;
this.mp = 11;
this.strength = 21;
this.agility = 4;
this.resilience = 22;
this.deftness = 1;
this.charm = 7;
this.magical_mending = 10;
this.magical_might = 0;
}
if(this.player_class.equals("Ranger")) { /* DONE */
this.hp = 31;
this.mp = 14;
this.strength = 18;
this.agility = 16;
this.resilience = 14;
this.deftness = 30;
this.charm = 4;
this.magical_mending = 12;
this.magical_might = 0;
}
if(this.player_class.equals("Sage")) { /* DONE */
this.hp = 29;
this.mp = 30;
this.strength = 12;
this.agility = 13;
this.resilience = 12;
this.deftness = 3;
this.charm = 14;
this.magical_mending = 12;
this.magical_might = 14;
}
if(this.player_class.equals("Luminary")) { /* DONE */
this.hp = 31;
this.mp = 13;
this.strength = 9;
this.agility = 22;
this.resilience = 12;
this.deftness = 16;
this.charm = 18;
this.magical_mending = 19;
this.magical_might = 5;
}
}
/*
* levelUp method for increasing stats of player
*/
public void levelUp() {
this.hp += this.level + 5;
this.mp += this.level + 2;
this.strength += this.level + 2;
this.agility += this.level + 2;
this.resilience += this.level + 1;
this.deftness += this.level + 2;
this.charm += this.level + 1;
this.magical_mending += this.level * 2;
this.magical_might += this.level * 2;
this.level += 1;
}
/*
* sellItem, sells item for value
*/
public void sellItem(Item[] item) {
this.gold += item.getValue();
for(int i = 0; i <= this.bag.length - 1; i++) {
if(this.bag[i].getName().equals(item.getName())) {
this.bag[i] == null;
break;
}
}
}
/*
* buyItem, used to add item from vendor
*/
public void buyItem(Item[] item) {
if(this.gold >= item.getValue()) {
this.gold -= item.getValue();
for(int i = 0; i <= this.bag.length - 1; i++) {
if(!(this.bag[i] == null)) {
this.bag[i] == item;
break;
}
}
} else {
System.out.println("Cannot Afford Item!");
}
}
/*
* addItem, used for adding items from monster drops
*/
public void addItem(Item[] item) {
for(int i = 0; i <= this.bag.length - 1; i++) {
if(!(this.bag[i] == null)) {
this.bag[i] == item;
break;
}
}
}
/*
* useItem, for items in medical class
*/
public void useItem(Medical item) {
this.hp += item.getHealValue();
for(int i = 0; i <= this.bag.length - 1; i++) {
if(this.bag[i].getName().equals(item.getName())) {
this.bag[i] == null;
break;
}
}
}
/*
* isDead method, returns a boolean if current health is less than/equal to 0
*/
public boolean isDead() {
return this.hp <= 0;
}
/*
* Getters for player class below
*/
public int getLevel() {
return this.level;
}
public int getGold() {
return this.gold;
}
}
tester.java
/*
* Tester class for Enemy/Player
*/
public class tester {
/*
* Main Method
*/
public static void main(String[] args) {
Player player = new Player("Ben", "Warrior");
}
}
sellItem(Item[] item)
a typo? \$\endgroup\$Item[] item
should be replaced withItem item
, thank you for noticing! \$\endgroup\$Player
and make all player classes implement it? \$\endgroup\$levelUp()
and apply both templates. Which, in turn, means that your ranger/thief class is completely separate from both the ranger and the thief classes - changes toRanger
don't carry-over. \$\endgroup\$