Refference "nameless" Objects
Hey, how can I access an object that I did not declare with a name. For example if I have a class called Monster, and I create 3 objects from the monster class each with its own int health:
publicclass monster
{
int health;
public monster(int h)
{
health = h;
}
publicvoid makeMonsters()
{
for(int x=0; x<3;x++){
new Monster(100);
}
}
How would a reference individual monsters to subtract health. If this is not possible based off of just this alone then assume each monster has its own "room" associated with it.. Thanks
[1136 byte] By [
sharokua] at [2007-11-27 4:50:27]

This isnt a problem (that has to be) rooted or solved in the object itself.
Rather, you would manage your references to these Objects somehow.
Meaning, you have to "store" these objects somewhere right?
A collection perhaps? There are many solutions like putting them
in a wrapper class that holds a String name and the object, using
a hashmap, or just using the index in the collection.
Of course the right solution is context dependent. Good luck.
Also: have makeMonster a Monster method is a bad idea, unless you think you should ask one monster to create three more!
Perhaps you should arrange it so that you can write:
//elsewhere, not in Monster:
Monster[] mash = new Monster[3];
for(int i=0; i < mash.length; ++i)
mash[i] = new Monster(100);
//later, weaken:
for(Monster m : mash)
m.weaken();
Could you give me an example please for this situation of how I could reference the zombie?
Say that I have a multiple "Room Object" made from a "Room Class" and each Room Object is given a "new Zombie()" as a constructor:
public class Room
{
public Room()
{
new Zombie(); // creates a zombie object inside the room object
}
public void makeROOMS()
{
Room r1, r2, r3;
// creates the rooms
r1=new Room();
r2=new Room();
r3=new Room();
}
}
Let's also assume that there is a player class that keeps a "playerRoom" and handles player actions and movements.
Now, if "playerRoom" was equal Room r1:
First, how could I do a check to see if there was a zombie in that room (assuming that we don't know that there is a zombie in the room). I understand that I could use a boolean value, but what would the if statement consist of to do that check.
Two, how could I access the zombie in that room and subtract from it's health.
You help is appreciated.
>>>//later, weaken:
>>>for(Monster m : mash)
>>>m.weaken();
I'm having a bit of trouble understanding for each loops, it's not that I don't know what it does (because I do) it's just I don't understand the format inside the "for()". I don't want to take up too much of your time, but I don't want to have to read a whole tutorial on it and then still not really understand what's going on. Could you give me just a brief description of what "for(Monster m : mash) is saying. What I am getting from this is that it would "weaken()" all the monsters and not just the one that is in the room with you.
Also, I do not intend to declare monsters within the monster class, I just put that up there for simplicity.
> What I am getting from this is that it would
> "weaken()" all the monsters and not just the one that
> is in the room with you.
Then each room should have a private variable which is a collection of the monsters in that room. The for-each loop is applied to all monsters in "mash", not *all* monsters that have been constructed, the contents of mash is under your control. Each Room could have methods such as addMonster(), clearMonsterList(), setMash(), getMash(), etc.
What everyone has been implying is that you *do* need to maintain a reference to the object. If you have a statement that is simply "new Monster(100);", then you'll never be able to access it again. if you have one of the followingmash.add( new Monster(100) );
mash[i] = new Monster(100);
mash.put( "name"+i, new Monster(100) );
then you will be able to access the monsters later.
I appreciate the help I really do. Using hash maps is still pretty new to me, but I'm getting the hang of it and I think that is what I am going to use.
Here is some code that I whipped up, comments/advice would be great:
public class Room
{
private String description;
private HashMap<String, Room> exits;
public static Room currentRoom;
public static ArrayList<Room> roomList = new ArrayList();
public Random numGen = new Random();
public Room(String description)
{
this.description = description;
exits = new HashMap<String, Room>();
roomList.add(this); //adds each room object to an arraylist
}
public void mapZombies()
{
for(Room r : roomList)
{
int randomNum = numGen.nextInt(2); // 50% chance of adding zombie
if(randomNum==1)
{
Zombie.zMap.put(r, new Zombie()); //appends to hashmap a room and a zombie object
}
}
}
-
public class Zombie
{
public static HashMap<Room, Zombie> zMap = new HashMap();
public int zombieHP;
public Zombie()
{
zombieHP=100;
}
}
There is another class "mazeLevel" that declares all the rooms and sets the 'exits hashmap'. There is no real logic in this class, it just primarly sets up the game's level.
I have been following your posts for a bit now, and it seems to me that you are tackling a big project, one that may be at this time beyond your abilities. This is not bad, because we all have to start somewhere and then progress, and aiming high is often times good, as it gives you something to strive for. But sometimes if the project is too big, if you bite off more than you can chew, you can see the trees and not the forest and eventually become discouraged, or worse yet, learn bad habits.
My unsolicited advice to you is to consider putting off the big project for just a little bit and go through a basic Java text book chapter by chapter doing each of the practice and sample problems. After you have done that, many of the questions you are asking now will be very clear to you and it will all make sense. Please don't take this the wrong way; you probably have a fine future in programming, but I just don't want you to sacrifice your education for the sake of the project.
Good luck,
Pete
Well, it's true that I've taken up a pretty big project and this is my first BIG project, but honestly I think I'm a little more ready then I may appear. This year at school I've been in an AP Comp Science class and we've been going through a book doing the examples like you said, and around the beginning of this semester I took up a Independent Study in GUI programming.
My problem is that I kind of slacked off a bit when it came to learn a lot of the basics and could have paid more attention then I did. I then started paying more attention when the advanced stuff came along and got good at that... (Ironic isn't it). Anyways like I was saying I'm not as far off as one might think, I did program out a pretty nice GUI all in code w/o any help, and a lot of core stuff in the game I got working on my own as well.
I do think your right however and I actually have been thumbing through my book looking at some of the basics lately (when I realized that I didn't know them as well as I should).
As far as the project I'm working on goes, I've just come way to far to give up on it now, I have but 2 things I really need to get working and I'll be set.
Thanks of the advice though Pete, and thanks to everyone that takes the time to answer my questions you really don't know how much it means to me. I feel just a little more inspired now :)
> As far as the project I'm working on goes, I've just
> come way to far to give up on it now, I have but 2
> things I really need to get working and I'll be set.
Well I wish you luck. In all likelihood, you'll succeed w/ the project; I'm not worried about that. Just please don't sacrifice your studies of the basics. In the long run, they are more important than the project, at least at this stage of the game.
Hey Pete, I don't want to bug you or anything, but I was just wondering if maybe you could give me an idea for a program to make that would be a good learning experience of a lot the basics. I tend to be much more motivated to learn and will be much more likely to finish what I start if I have a goal (like you said). so you know, if it's not to much trouble and you can maybe think up a program that you once wrote well learning the basics that would be cool. Of course anyone else is free to shoot me some ideas if the feel like it :)
I don't want to like bug you or anything though, I hate it when I ask too many questions and annoy people, so you don't have to.. g2g
Have a good one
Perhaps a simpler game would be what you need, something like a sudoku grid as a for instance. Before you say this is too simple, realize that this can be as simple or as complex as desired. It could even be a useful exercise in program architecture or even Design patterns. the program could be done with a three-tier architecture if desired by separating out the logic, the data (can even have a repository of old games), and the GUI of the program into three module groups etc...
The advantage this has over your project is that it would be more limited in its scope (I think).
Other projects I've seen is a simple network chat program, a computer representation of a board game such as tic tac toe (too simple probably) or "go", or the game "concentration".
I like your ideas, specially the network chat program that is something that I've been wanting to program for awhile but I would not know how. The book I'm currently reading through (and many that I have looked through) menchen nothing on doing any kind of networking / internet protocol in java, so that kind of leaves me to feel that I would not know where to start. Though I would absolutely love to learn networking with java.
I think you I'll try my hand at sudoku grid once I finish this project (though I have never herd of the game, it is a game right?). Maybe you could send me some links on where I can learn some basic java net stuff, that would be really cool. Everytime I've searched around for stuff on it I always get things that are just over my head or 'assume' that you know something about networking in java. =\
Oh, your right about the project I'm working on, it really is to wide ranged and I was planning on doing too much stuff on it that quite frankly I would just confuse myself. So I just plan to add some working zombies and items to the game and call it finished.
Thanks for your help and sorry if I ask too many question, I tend to be over curious at times.
> I think you I'll try my hand at sudoku grid once I> finish this project (though I have never herd of the> game, it is a game right?). check out [url= http://www.sudoku.com/]Sudoku.com[/url]. As addicting a game if there ever was one.