-7

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:

  1. What's wrong with the code ?
  2. why " public static void main(String[] args) {" is crucial ?
  3. 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);
}

}

Niminim
  • 668
  • 1
  • 7
  • 16
  • 1
    what is the problem ? and "public static void main(String[] args) {" is the entry point of the progam, you have to start somewhere. – Tahar Bakir Oct 22 '15 at 18:36
  • What is the error\issue with code right now? What happens when you run it? – Maksim Khaitovich Oct 22 '15 at 18:36
  • Your code doesn't have balanced number of `{` and `}`. Try to indent it properly (use IDE or any other editor which can indent code for you). – Pshemo Oct 22 '15 at 18:36
  • Maybe here some { got lost, in my code it's fine. The problem is with getSpellEffect, I get on the declaration line illegal start of expression, and in this method spellsList and Spell are shown in red – Niminim Oct 22 '15 at 18:52

1 Answers1

0

You are having a method named getSpellEffect() inside the main() method. You cannot have methods inside methods. Check Methods inside methods

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

    public class SpellsList {

        static List<Spell> spellsList = new ArrayList<Spell>();

        static {

            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) {

            String spellEffect = "";

            for (Iterator<Spell> iter = spellsList.iterator(); iter.hasNext();) {
                Spell spell = iter.next();
                if (spellName.equals(spell.getSpellName())) {
                    spellEffect = spell.getEffect();
                    break;
                }
            }
            return spellEffect;

        }
    }

Main.java

public class Main {

    public static void main(String[] args) {

        System.out.println(SpellsList.getSpellEffect("Fireball"));

    }

}
Community
  • 1
  • 1
kensplanet
  • 493
  • 8
  • 15