I'm writing a small text-based game in Java to learn the language. I'm concerned that I may be making some poor design decisions. I'll introduce 2 elements: A character and monsters. A singleton character should be able to fight a wide array of monsters.
Here is the monster class:
public abstract class Monster {
protected static Random random = new Random();
protected static int STATS_PER_LEVEL = 3;
public int combatLvl;
public int defense, strength, attack, hp;
protected String name;
protected Monster(int defense, int strength, int attack, int hp) {
this.defense = defense;
this.strength = strength;
this.attack = attack;
this.hp = hp;
this.combatLvl = (int) (defense+strength+attack+Math.round((double)hp/10)-4)/STATS_PER_LEVEL;
}
public int attack() {
//removed the body
}
public String toString(String name) {
return this.name;
}
}
Here's a subclass:
public class Bandit extends Monster {
public static double averageLevelDouble = -1;
public static String averageLevelString = "dummy";
public static int minDefense = 1;
public static int maxDefense = 1;
public static int minStrength = 1;
public static int maxStrength = 3;
public static int minAttack = 1;
public static int maxAttack = 3;
public static int maxHp = 4;
public Bandit() {
super(random.nextInt(maxDefense-minDefense+1) + minDefense,
random.nextInt(maxStrength-minStrength+1) + minStrength,
random.nextInt(maxAttack-minAttack+1) + minAttack,
random.nextInt(maxHp-minHp+1) + minHp);
double min = minDefense+minStrength+minAttack+((double)minHp/10);
double max = maxDefense+maxStrength+maxAttack+((double)maxHp/10);
double avg = ((max+min)/2)-4;
averageLevelDouble = (double)Math.round((avg/STATS_PER_LEVEL)*10)/10;
averageLevelString = (Math.round(min)-4)/STATS_PER_LEVEL + "-" +
(Math.round(max)-4)/STATS_PER_LEVEL;
name = "Bandit";
}
}
My main concern is that I should be able to easily expand the game with arbitrarily many monsters. The way I'd do it now is to copy paste Bandit into a new class: Change the class name to f.i. Skeleton, initiate 'name' variable in the constructor, customize all the static
int
s. Is this a wrong approach?
Also a very obvious thing that comes to mind is that I should be able to display information on a particular monster without having to instantiate it. From a Swing.JComboBox I should be able to choose all monsters, display their information, and at the moment I decide to fight a monster it should be instantiated. I can't find a solution to represent monsters on a JList or JComboBox without having to manually add Bandit.name (pretend it's static) as a line of code in the JList/JComboBox class when I define the Bandit class.
I'm just assuming that I shouldn't be instantiating my subclasses. Again, that is the question: What am I doing wrong and right? I really don't how to ask but; What's wrong with my code?