Results 1 to 7 of 7
Thread: Help Using Arrays
- 12-21-2012, 04:05 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
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
- 12-21-2012, 04:12 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 12-21-2012, 04:20 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
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?
- 12-21-2012, 04:25 PM #4
Member
- Join Date
- Dec 2012
- Posts
- 3
- Rep Power
- 0
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.
- 12-21-2012, 04:31 PM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Help Using Arrays
That seems to be a good approach to me!
I like likes!.gif)
- 12-22-2012, 06:56 AM #6
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
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.
Here's a link to a tutorial on using ArrayList 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(); } }
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
Here's a link to an object-oriented programming (OOP) tutorial: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"); } }
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.
- 12-22-2012, 07:59 AM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Copying Single Arrays to 2-D Arrays
By jmscarlet9 in forum New To JavaReplies: 7Last Post: 04-02-2012, 11:17 PM -
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 12:49 PM -
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks