Results 1 to 7 of 7
  1. #1
    shandan97 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Question Help Using Arrays

    Hello, I am creating a text based game and I am having problems grasping this:
    I have a player class and a weapon class, the weapon class has two arrays, one array for the weapon type, and another array for the actual weapon. The arrays are arrays of strings.(I believe I made a bad choice by making them strings). Then, in the player class all I have is the health and what not, but I am trying to figure out how to implement the weapons and add them to the inventory. I know it sounds really confusing but I'm hoping someone will get where I am going. I am not asking for someone to write the code for me, I am just wanting someone to point me in the right direction in terms of implementing player inventory and inventory. I just need someone to tell me what to read up on. Hope you can help!
    Thanks,

    Shandan

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Help Using Arrays

    It would be a lot easier if you post the code you already have... and use the forum code tags to wrap it.
    Think more abstract: What are the properties of a weapon? Damage, weight, mass,... these are usually int, floats or similar types. So that class should have those properties. No need for arrays here, each weapon is on its own. The player may have a weapon... so give him an attribute of the type your weapon is defined as.

    class Player
    {
    Weapon myWeapon;
    }
    I like likes!

  3. #3
    shandan97 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help Using Arrays

    Alright, That's what I thought, so I would essentially make each weapon an Object then? and then add its attributes?

  4. #4
    shandan97 is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help Using Arrays

    I see, sorry for all my confusion, I think I am just looking at this the wrong way. So would I declare each wepon as an object and then declare attributes? such as
    Public int HitPoints;
    Thank you for your help.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Help Using Arrays

    That seems to be a good approach to me!
    I like likes!

  6. #6
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: Help Using Arrays

    It seems like a Player could have a number of weapons, but an array is not ideal. Consider using an ArrayList of Weapon objects.

    Java Code:
    public class Player {
    
        // instance variables.  Note that instance variables should always be prive!
        private ArrayList<Weapon> weapons =  new ArrayList<Weapon>();
    
        // methods
    
        public void add(Weapon weapon) {
            weapons.add();
        }
    
    }
    Here's a link to a tutorial on using ArrayList objects:
    How to use ArrayList in Java

    A Weapon wouldn't need an array or an ArrayList. Instead of having weapon type as a String. It would make sense to make Weapon an abstract class and then subclass it for each weapon type. Things that are common to all Weapon objects would be put in the abstract class: Weapon

    Java Code:
    public abstract class Weapon {
        public abstract void fire();
    }
    
    public class Phasers {
        public void fire() {
            System.out.println("Firing phaser");
        }
    }
    
    public class PhotonTorpedos {
        public void fire() {
            System.out.println("Firing photon torpedos");
        }
    }
    Here's a link to an object-oriented programming (OOP) tutorial:
    Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)

    The Weapon class could have a method that would return a String giving the description of the weapon.

    Feel free to post again if you have more questions.
    Last edited by kaydell2; 12-22-2012 at 07:45 PM.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: Help Using Arrays

    kaydell2: please go through Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-02-2012, 11:17 PM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •