I'm working on a text-based RPG for one of my classes. There's a random chance for combat to occur, at which time the player must fight a randomly generated monster. I have an ArrayList of monsters, but I'm running into a problem. If the character is level 1, it will always fight one of four level 1 monsters, chosen at random. Say for example the character has to fight a skeleton, and defeats it by depleting the skelton's health to 0. Now say the character once again has to fight, and a skeleton is randomly generated. Combat is skipped, because the skeleton still has 0 health from the previous fight
It appears that my code is referring to the object that resides in the ArrayList, but what I want to happen is to instantiate a new copy of the Monster such that the scope is onlythe method where the Monster is created. I understand what's going wrong, but I don't exactly understand why, and I also don't know how I can tweak my program to fix it. My short-term fix is to reset the monster's health to full after combat, but I'd like to understand my problem and hopefully learn from it.
Here is the method that initiates combat. I have a feeling my problem has to do with the way I'm creating the Monster object in the few lines below:
public void monsterEncounter(Player player)
{
Monster monster = World.retrieveMonster(random.nextInt(4), player.getLevel());
System.out.println("You were ambushed by a " + monster.getName() + "! Prepare to defend yourself!");
The ArrayLists defining the monsters in the game, as well as the method to retrieve them, are defined in another class:
// ArrayList holding all monsters
static ArrayList<ArrayList<Monster>> monsters = new ArrayList<ArrayList<Monster>>();
// ArrayLists holding monsters by level
static ArrayList<Monster> levelOneMonsters = new ArrayList<Monster>(0);
static ArrayList<Monster> levelTwoMonsters = new ArrayList<Monster>(0);
static ArrayList<Monster> levelThreeMonsters = new ArrayList<Monster>(0);
public static void declareMonsters()
{
levelOneMonsters.add(new Monster("Skeleton", 1, 50, 1, 4, 50, 25));
levelOneMonsters.add(new Monster("Zombie", 1, 75, 1, 4, 60, 30));
levelOneMonsters.add(new Monster("Rabid Dog", 1, 35, 2, 3, 40, 20));
levelOneMonsters.add(new Monster("Goblin", 1, 65, 2, 3, 75, 40));
monsters.add(levelOneMonsters);
levelTwoMonsters.add(new Monster("Brown Bear", 2, 100, 2, 6, 100, 40));
levelTwoMonsters.add(new Monster("Goblin Captain", 2, 125, 4, 3, 80, 35));
levelTwoMonsters.add(new Monster("Giant Snake", 2, 110, 1, 3, 95, 75));
levelTwoMonsters.add(new Monster("Lizard Man", 2, 130, 4, 4, 125, 100));
monsters.add(levelTwoMonsters);
levelThreeMonsters.add(new Monster("Gargoyle", 3, 150, 4, 6, 120, 100));
levelThreeMonsters.add(new Monster("Psychotic Farmer", 3, 160, 7, 6, 125, 125));
levelThreeMonsters.add(new Monster("Cultust", 3, 175, 1, 8, 175, 200));
levelThreeMonsters.add(new Monster("Mutated Harpy", 3, 180, 6, 6, 150, 180));
monsters.add(levelThreeMonsters);
}
public static Monster retrieveMonster(int index, int level)
{
return monsters.get(level - 1).get(index);
}
I hope this question makes sense. I sincerely appreciate any insight anybody can provide.