I started working on my first java project, it's some kind of a very basic roleplaying game (no gui, of course). Now I'm working on the spells. I created "Spell" class (includes name, effect and cost). I created SpellsList class, that should include all spells. I have "Effects" class that describes what's the spell's effect (it's just for testing right now). When a spell-caster casts a spell then I want to get the relevant spell effect, so the relevant effect will take care of the spell's effect (I'll work on the effects later, now it's just for testing). I have a dice roller, but it doesn't matter for now.
I have a few questions:
- What's wrong with the code ?
- why " public static void main(String[] args) {" is crucial ?
Would you implement the spells in a better way ?
public class Spell { private String name; private String effect; private int cost; Spell(String name, String effect, int cost){ this.name = name; this.effect = effect; this.cost = cost; } String getSpellName(){ return name; } String getEffect(){ return effect; } int getCost(){ return cost; }
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SpellsList {
public static void main(String[] args) {
List<Spell> spellsList = new ArrayList<Spell>();
spellsList.add(new Spell("Fireball", "damage", 5));
spellsList.add(new Spell("Ice Storm", "damage", 8));
spellsList.add(new Spell("Heal", "heal", 8));
static String getSpellEffect(String spellName) {
for (Iterator iter = spellsList.iterator(); iter.hasNext(); ) {
if (iter.next().equals(spellName)) {
iter.next().Spell.getEffect();
break;
}
}
}
}
}
public class Effects {
int damage(int n, int dice, int bonus){
int damage = Dice.roll(n,dice,bonus);
System.out.println("You dealt" + damage + "damage to the enemy!");
return damage;
}
int heal(int n, int dice, int bonus){
int heal = Dice.roll(n,dice,bonus);
System.out.println("You healed" + heal + " hit points!");
return heal;
}
}
public class Dice {
public static int roll(int dice){
int sum = 1 + (int)(Math.random() * ((dice - 1) + 1));
return sum;
}
public static int roll(int n, int dice){
int sum = 0;
for (int i = 0; i < n; i++) {
sum += 1 + (int)(Math.random() * ((dice - 1) + 1));
}
return sum;
}
public static int roll(int n, int dice, int bonus){
int sum = 0;
for (int i = 0; i < n; i++) {
sum += 1 + (int)(Math.random() * ((dice - 1) + 1));
}
return (sum + n*bonus);
}
}