3 Replies - 394 Views - Last Post: 15 February 2013 - 05:25 AM Rate Topic: -----

#1 crmbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 13

Text-Based Video Game ArrayList Problem

Posted 14 February 2013 - 07:47 PM

Hey guys I just have a quick question about a java program I am working on. So I'm working on a text based video game in NetBeans and I have my GUI class, an enemy class, player class, actor class (superclass of enemy and player) and a database class. The problem is that my database class uses an ArrayList to store all of the enemies players can fight. Now when players click the battle button in the GUI class this is what happens code wise:
  private void battleButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
        if(!gamer.isFighting())
        {
            gamer.fighting();
        }
        
        if(gamer.isFighting())
        {
            monster = spawner.getMob(rand.nextInt(3));
            eName.setText(monster.getName());
            eLvl.setText("Lvl " + monster.getLevel());
            eHP.setText("HP: " + monster.getHealth());
            eMana.setText("Mana: " + monster.getMana());
            eAttack.setText("Attack: " + monster.getAttack());
            eArmor.setText("Armor: " + monster.getArmor());
            bLog1.setText("You've encountered a " + monster.getName());
            System.out.println(monster.getHealth());
        }
    }                                     



The problem is once the player clicks the battle button again after beating a monster, the data shown on the GUI shows zero health and zero armor. I think whats happening is the attack method:
 public void attack(Enemy badGuy)
    {
        badDude = badGuy;
        if(badDude.getHealth() > 0)
        {
        if(badDude.getArmor() > 0)
        {
            if(this.getAttack() > badDude.getArmor())
            {
                badDude.setHealth(badDude.getHealth() -(this.getAttack() - badDude.getArmor()));
                badDude.setArmor(0);
            }else{
            badDude.setArmor(badDude.getArmor() - this.getAttack());
            }
        }else{
           badDude.setHealth(badDude.getHealth() - this.getAttack());
        }
        
    }
        if(badDude.getHealth() < 0)
        {
            badDude.setHealth(0);
            
        }
        
    }


is actually changing the value of the enemy in the arraylist. Thanks for your time and I hope you guys can help me out!

So I have an object of type Database. This object has an ArrayList variable in it that holds all the Enemy objects for the game. The problem is that I want the ArrayList to act as a reference for Enemy information. This means I don't want the data changing. The thing is though, when players of this game hit the battle button and defeat an Enemy the actual variables of the Enemy in the ArrayList in the Database class change. As seen above in the first code segment I create another Enemy object that takes the information from the Database object so that the information in the ArrayList doesnt change. I've tried almost everything from reloading the database object when the enemy is defeated to making a reset method in the Enemy class that resets its stats but all of those didnt work. Hopefully you guys can help me. Oh also, here is the getMob() method depicted in the battle button method:
public Enemy getMob(int inx)
    {
        return creatures.get(inx);
    }



Thanks for your time and hopefully you guys can help me since no one else has seemed to be able to thus far.

Is This A Good Question/Topic? 0
  • +

Replies To: Text-Based Video Game ArrayList Problem

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,531
  • Joined: 29-July 11

Re: Text-Based Video Game ArrayList Problem

Posted 14 February 2013 - 08:19 PM

View Postcrmbuddy, on 15 February 2013 - 02:47 AM, said:

The problem is once the player clicks the battle button again after beating a monster, the data shown on the GUI shows zero health and zero armor.


Whose stats, the player or the next monster?

Quote

So I have an object of type Database. This object has an ArrayList variable in it that holds all the Enemy objects for the game. The problem is that I want the ArrayList to act as a reference for Enemy information. This means I don't want the data changing.


You should have a class for each and every type of monster with its data (strength = 8, defense = 5, etc). When you start a new game, simply populate a List with new copies of each enemy class, then just defeat enemies from that List. You can always refill it.

This post has been edited by farrell2k: 14 February 2013 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 crmbuddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 13

Re: Text-Based Video Game ArrayList Problem

Posted 15 February 2013 - 05:05 AM

But say I have like 200 monsters in the game, won't that be inefficient and kinda dumb to have 200 classes lol?
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,531
  • Joined: 29-July 11

Re: Text-Based Video Game ArrayList Problem

Posted 15 February 2013 - 05:25 AM

Having 200 monters would probably be dumb for your game, but a larger game, no. You have an abstract base GameCharacter class, then a Hero class, then individual classes for each specific monster. It's perfectly object oriented. I wouldn't do it any other way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1