Multiple Object Declaration
Hey, I'm just going to keep it a bit brief...
I'm working on this project for school, it's a "zombies in the mall" type game the only problem is there are no zombies. What I am trying to do it create zombies as an object, my "rooms" in the game are set via HashMap and I need to (for each room) create a random chance there is a zombie in the room ( I can do this...) and if isZombie = true declare a zombie for that room:
Zombie killBot1 = new Zombie(Room zombieRoom)
The problem is I need to create multiple zombies all with a unique name so that later on I can subtract 'health' from say killBot3 when I need to. Now I know I could just go through and declare them all myself but this will create problems in the future when more levels are added to the game.
I was thinking something like this (SUDO):
for each room
isZombie()
if true:
zombie killbot = new zombie(currentroom)
I cant seem to make this work however... The part I am stumped at is the 'name' of the zombie I tried using a loop so I could do something like:
killbot01
killbot02
killbot03
etc...
but it wont allow.
I know there is probably some stupid trick to do this sort of thing but I either 1) don't remember
or
2) wasnt paying attention
anyways, the help is much appreciated
[1384 byte] By [
sharokua] at [2007-11-27 4:29:06]

You probably want an array or a Collection of Zombie objects.
Why do Zombies need names?
I was thinking about trying an array but like I said, how would I make that work with the declaration of a new zombie?
The need a name because if say you enter a room and there is a zombie that you choose to fight, I wouldn't be able to access that 1 individual zombie to subtract health from it (amongst other methods of course). Unless any of you know of a way that I can declare multiple zombies something like this:
new zombie(Room zombieRoom)
and then be able to change the values of each zombie individually.
Would this work:
ArrayList zomLIST = new ArrayList();
for(loop to add zombies to arraylist)
{ Code Omitted }
for(loop through arraylist) {
Zombie zomList.get(x) = new Zombie(i_eat_dogFood)
}
?
> Would this work:...No. I'm still unsure about what you are trying to do and how you can't accomplish it with an array/ArrayList.
Sorry, I'll try to be a little more explanatory... I have a object class "Room":
public class Room
{
private String description;
private HashMap<String, Room> exits;
public static Room currentRoom;
public Room(String description)
{
this.description = description;
exits = new HashMap<String, Room>();
}
In a separate class the Rooms are declared and exits are set (IE):
Room a1, a2, a3, etc...;
a1=new Room("a1");
a1.setExit("East",b1);
I need to add an object class "Zombies" to some of the rooms (at random), so that if I make a new level I do not have to go through and declare each zombie object like so:
Zombie zomBOT01 = new Zombie(a1);
Zombie zomBOT02 = new Zombie(a2);
Zombie zomBOT03 = new Zombie(b1);
ETC...
This is some sudo code of what I want it to do:
for each (loop) room
isZombie(); //returns boolean (true/false) value based on random chance
// if it is a true value...
Zombie zomBot = new Zombie(current_room_of_loop);
// This is the part Im stuck on ^
I need each Zombie object name to be unique IE: zomBot01, zomBot02, etc...
How can I do this?
I know, but why do you need each zombie object to have a unique name?
int numberRooms = 5;
int numberZombies = 10;
Room room = new Room[numberRooms];// an array of 5 Room objects
Zombie zomBot = new Zombie[numberZombies];// an array of 10 Zombie objects
int c = 0;
for (int i = 0; i < numberRooms; i++)
{
room[i] = new Room();
boolean z = room[i].isZombie();
if (z == true)
{
zomBot[c] = new Zombie( room[i] );
c++;
}
} // end for loop
Thank you, I think this will prove helpful... As far as to why I need each zombie to have a unique name, well, I already said. I will need to be able to change values of each individual zombie during game play, such a zombie health. I know not of a way this can be done unless each one has it's own name. If there is away please do tell
Can zombies move from one room to another room? Or once a zombie is in a particular room, must it stay there? Java_Lava
> Thank you, I think this will prove helpful... As far
> as to why I need each zombie to have a unique name,
> well, I already said. I will need to be able to
> change values of each individual zombie during game
> play, such a zombie health. I know not of a way this
> can be done unless each one has it's own name. If
> there is away please do tell
I suppose it depends what you mean by "name". If by name you mean a reference variable, then, no, you don't need a different variable for each zombie.
Here's some pseudo code. And by the way, the term is "pseudo", not "sudo", which means something else.
class Room {
zombies_coll: collection of Zombie objects
boolean hasZombies? {
return zombies_coll.size() > 0
}
Zombie selectRandomZombie {
return zombies_coll.get(random.nextInt(zombies_coll.size())
}
}
class Player {
enter(Room r) {
while (r.hasZombies?) {
zombie = r.selectRandomZombie()
fight(zombie)
}
}
}
When the player enters the room, it will ask the room if it has any zombies, and if so, it will ask the room for a zombie. It will assign that zombie to a local reference variable. The player object need never know any internal variables or fields in the Room class -- it just knows that it has its own (single) reference to a zombie.
Alternatively, I suppose you might want to give the zombies names, such that the player can choose which one to fight. Maybe the program would run like this:
> Go north.
You have entered the north room.
There are three zombies here. Their names are Bob, Steve, and Ben.
Which one do you want to fight?
> Fight Ben
In that case, Room can have a mapping (java.util.Map) from strings representing names to Zombie objects. Room's API would have ways to select zombies by name from that map. Room would still only have a single reference, this time to the map. The zombie names would exist only as String keys in that map.
