1

I am in the process of putting together a simple RPG game engine in java. At this point everything works fine while all my classes are in one directory. Basically, I know I am going to end up with a heap of files and wish to organise them into a package structure. I followed the directions at http://www.jarticles.com/package/package_eng.html but can't seem to make the magic happen. The two classes posted are the least dependent of the lot and I figure if I can get these working then the rest shouldn't be a drama. For the record I am using openJDK in Leeenux (remix of Ubuntu netbook Remix)

First class

package adventure.engine;

import java.util.*;

public class Inventory
{
ArrayList itemList = new ArrayList();

public Inventory()
{

}

public void addItem()
{

}

public void removeItem()
{

}
}

And the second:

package adventure.engine;



import adventure.engine.*;



public class PlayerCharacter

{

private String name = "Player";

private String race;

private String plrClass;

private int level;

private int xp;

private int levelXp;

private Inventory inventory = new Inventory();



//---------

//Abilities

//---------



private static final String[] abilitiesList = {"Strength",

                    "Dexterity",

                    "Constitution",

                    "Intelligence",

                    "Wisdom",

                    "Charisma"};



private int[] abilitiesValues = new int[abilitiesList.length];



//------

//Skills

//------

private static final String[] skillsList    = {"Acrobatics"     , "Insight",

                    "Arcana"            , "Intimidate",

                    "Athletics"         , "Nature",

                    "Bluff"         , "Perception",

                    "Diplomacy"     , "Religion",

                    "Dungeoneering"     , "Stealth",

                    "Endurance"     , "Streetwise",

                    "Heal"          , "Thievery",

                    "History"};



private int[] skillsValues = new int[skillsList.length];



//***********

//Constructor

//***********

public PlayerCharacter()

{

    level = 1;

    xp = 0;

    levelXp = 1000;



    setAbility("Strength", 8);

    setAbility("Dexterity", 10);

    setAbility("Constitution", 10);

    setAbility("Intelligence", 10);

    setAbility("Wisdom", 10);

    setAbility("Charisma", 10);

}       //public PlayerSheet()





//*************

//Class Methods

//*************

public void addXp(int val)

{

    xp += val;



    if (xp >= levelXp)

    {

        level++;

        xp -= levelXp;

        //levelXp += ;

    }

}       //public void addXp(int val)





public void updateSkills()

{



}





//Mutators

public void setName(String n)

{

    name = n;

}





public void setLevel(int l)

{

    level = l;

}



public void setRace(String r)

{

    race = r;

}



public void setXP(int x)

{

    xp = x;

}





public void setClass(String c)

{

    plrClass = c;

}



//set ability value by name

public void setAbility(String a, int val)

{

    for(int i = 0; i < abilitiesList.length; i++)

    {

        if(abilitiesList[i].compareTo(a) == 0)

        {

            abilitiesValues[i] = val;

        }

    }

}



//set ability by index

public void setAbility(int index, int val)

{

    abilitiesValues[index] = val;

}



//set skill by name

public void setSkill(String name, int val)

{

    for(int i = 0; i < skillsList.length; i++)

    {

        if(skillsList[i].compareTo(name) == 0)

        {

            skillsValues[i] = val;

        }

    }

}



//set skill by index

public void setSkill(int index, int val)

{

    skillsValues[index] = val;

}



//Accessors

public static String[] getAbilityList()

{

    return abilitiesList;

}



public static String[] getSkillsList()

{

    return skillsList;

}



//retrieve an ability value by name

public int getAbility(String a)

{

    int val = 0;



    for(int i = 0; i < abilitiesList.length; i++)

    {

        if(abilitiesList[i].compareTo(a) == 0)

        {

            val = abilitiesValues[i];

            break;

        }

    }



    return val;

}



//retrieve an ability value by index number

public int getAbility(int i)

{

    return abilitiesValues[i];

}



public int getSkill(String s)

{

    int val = 0;



    for(int i = 0; i < skillsList.length; i++)

    {

        if(skillsList[i].compareTo(s) == 0)

        {

            val = skillsValues[i];

            break;

        }

    }



    return val;

}



public int getSkill(int i)

{

    return skillsValues[i];

}



public String getName()

{

    return name;

}



public String getRace()

{

    return race;

}



public String getPlrClass()

{

    return plrClass;

}



public int getLevel()

{

    return level;

}



public int getXP()

{

    return xp;

}



public int getLevelXP()

{

    return levelXp;

}



}       //public class PlayerCharacter

Classes reside in /home/user/Java/adventure/engine

Output from echo $classpath is /home/james/Java:/.:

when I attempt to compile the second class I get the following error:

PlayerCharacter.java:18: cannot find symbol
symbol  : class Inventory
location: class adventure.engine.PlayerCharacter
    private Inventory inventory = new Inventory();
            ^
PlayerCharacter.java:18: cannot find symbol
symbol  : class Inventory
location: class adventure.engine.PlayerCharacter
private Inventory inventory = new Inventory();

Any feedback on this would be greatly appreciated.How to solve this?

Ami
  • 4,241
  • 6
  • 41
  • 75

1 Answers1

3

Two things.

1) You might not have compiled Inventory
2) PlayerCharacter and Inventory are in same package. So there is no need to import.

You should be compiling them as

javac adventure/engine/Inventory.java

javac adventure/engine/PlayerCharacter.java

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
  • Inventory is compiled. Tried removing the import, still the same error. – seemvanjest Oct 06 '12 at 08:51
  • You can also use wildcards http://stackoverflow.com/questions/4764768/java-how-can-i-compile-an-entire-directory-structure-of-code?lq=1 – zvez Oct 06 '12 at 08:58
  • Sweet as. no errors compiling from a terminal with that setup. Using SCite as my editor so I was just using it's default compile option. Thanks. – seemvanjest Oct 06 '12 at 09:03