Reputation: 678
I work on my first java program (so this questions are relatively simple). I develop some kind of a basic roleplaying game, I work on Character's attributes.
My question are:
enum ClassStats {
Fighter(15,14,12,10,9,10), Rogue(12,12,16,14,10,10), Mage(10,10,14,16,14,10), Cleric(12,14,12,13,16,14);
private int strength, constitution, dexterity, intelligence, wisdom, charisma;
ClassStats(int str, int con, int dex, int intel, int wis, int cha){
strength = str;
constitution = con;
dexterity = dex;
intelligence = intel;
wisdom = wis;
charisma = cha;
}
int getStrength(){
return strength;
}
int getConstitution(){
return constitution;
}
int getDexterity(){
return dexterity;
}
int getIntelligence(){
return getIntelligence();
}
int getWisdom(){
return wisdom;
}
int getCharisma(){
return charisma;
}
}
public class Character {
private String Name;
private String Class;
private int Level;
private long XP;
private int HP;
private int currentHp;
/*private int BAB; /*Base attack bonus*/
private int Strength;
private int Constitution;
private int Dexterity;
private int Intelligence;
private int Wisdom;
private int Charisma;
Character(String name, String chracterClass){
Name = name;
Class = chracterClass;
Level = 1;
XP = 0;
HP = CharacterUtil.setHP(chracterClass);
currentHp = HP;
ClassStats cs = null;
Strength = cs.getStrength();
System.out.println("Strength: " + Strength);
Constitution = cs.getConstitution();
Dexterity = cs.getDexterity();
Intelligence = cs.getIntelligence();
Wisdom = cs.getWisdom();
Charisma = cs.getCharisma();
}
}
Upvotes: 1
Views: 1159
Reputation: 1172
A) You could just pass in the enum into the constructor.
B) Or, you can get an enum from a string value via enumClass.valueOf(strValue)
.
C) Or, better yet, you could have a factory class to generate different default characters for you.
Additionally, these two lines don't make sense:
ClassStats cs = null;
Strength = cs.getStrength();
If you are setting it to null, then you can't invoke a method on the object. This is essentially where you could parse the enum from the string representation passed in the constructor, or if you choose option 2, you'll already have a ClassStats variable.
Upvotes: 1
Reputation: 16
I would recommend not using enum, and using less primitive values and create objects for these needs. Each player class can also be a Java class (all of them extending an abstract PlayerClass) Then Character can receive a player class in the constructor and ask it what the default attributes could be.
After this program gets larger, I would investigate the Factory design pattern.
Upvotes: 0
Reputation: 5629
at first:
ClassStats cs = null;
Strength = cs.getStrength();
System.out.println("Strength: " + Strength);
Constitution = cs.getConstitution();
Dexterity = cs.getDexterity();
Intelligence = cs.getIntelligence();
Wisdom = cs.getWisdom();
Charisma = cs.getCharisma();
this cannot work. In the first line you set cs to "null" and than you try to access data of cs via some getters. How should you get a value from "null"?
Accessing enum values in java works like
YourEnum.values()[index];
like in How to get Enum Value from index in Java?
or
YourEnum.YourValue
like in Java: access to the constants in an enumeration (enum)
Further i would think about using a database system like MySQL to store Character-Class informations - this is much more easy to edit if your project becomes larger. The most used here is the Java Persistance API (JPA).
https://dzone.com/articles/jpa-tutorial-setting-jpa-java
Upvotes: 0