help with game hashmap

i'm making a game at the moment.......

i need help with a way to check all the hashmaps of enimies in a class called room......i need to check if they all == 0...when they all equal zero the game ends......

i know i can check the size of a hash map with size().....but i want to check if all the hashmaps within all the rooms == 0.

i could do this

privateboolean keepTheGameGoing()

{

for(Room r : rooms){

if(player.getCurrentRoom().getSizeOfMagicList() == 0)

returntrue;

}

returnfalse;

but i have a game class which is the controller class and when i put this in it wont work it say there is an error at (Room r: rooms).cannot find symbol(variable rooms)

Message was edited by:

molleman

[1119 byte] By [mollemana] at [2007-11-26 20:31:56]
# 1

my code for my game class

public class Game

{

private Parser parser;

private Player player;

private Scenario scenario;

/**

* Create the game and initialise its internal map.

*/

public Game(String playerName)

{

player = new Player(playerName);

scenario = new Scenario();

createRooms();

parser = new Parser();

}

/**

* Create all the rooms and link their exits together.

*/

private void createRooms()

{

scenario.createRoomsAndItems(player);

// start game outside

}

/**

* Main play routine. Loops until end of play.

*/

public void play()

{

printWelcome();

// Enter the main command loop. Here we repeatedly read commands and

// execute them until the game is over.

boolean finished = false;

while (! finished) {

Command command = parser.getCommand();

finished = processCommand(command);

}

System.out.println("Thank you for playing. Good bye.");

}

/**

* Print out the opening message for the player.

*/

private void printWelcome()

{

System.out.println();

System.out.println("Welcome to the Princess of the World game!");

System.out.println("You awake..........................");

System.out.println("");

System.out.println("The gash on your head was caused by some sort of blunt object");

System.out.println("but you cant remember.");

System.out.println("");

System.out.println("You notice the princess you were guarding is gone. Suspecting");

System.out.println(" that she was kidnapped you search through the mountains and ");

System.out.println(" come across a castle. you are outside at the moment.");

System.out.println(" The gash is still hurting you");

System.out.println("Type 'help' if you need help.");

System.out.println();

System.out.println(player.getCurrentRoom().getLongDescription());

}

/**

* Given a command, process (that is: execute) the command.

* @param command The command to be processed.

* @return true If the command ends the game, false otherwise.

*/

private boolean processCommand(Command command)

{

boolean wantToQuit = false;

if(command.isUnknown()) {

System.out.println("I don't know what you mean...");

return false;

}

String commandWord = command.getCommandWord();

if (commandWord.equals("help")) {

printHelp();

}

else if (commandWord.equals("go")) {

goRoom(command);

}

else if (commandWord.equals("quit")) {

wantToQuit = quit(command);

}

else if (commandWord.equals("look")) {

processLook();

}

else if (commandWord.equals("pick")) {

pickItem(command);

}

else if (commandWord.equals("drop")) {

dropItem(command);

}

else if (commandWord.equals("back")){

backRoom();

}

else if (commandWord.equals("use")){

useItem(command);

}

// else command not recognised.

return wantToQuit;

}

// implementations of user commands:

/**

* Print out some help information.

* Here we print some stupid, cryptic message and a list of the

* command words.

*/

private void printHelp()

{

System.out.println("You are lost. You are alone. You wander");

System.out.println("around at the castle.");

System.out.println();

System.out.println("Your command words are:");

parser.showCommands();

}

/**

* Try to go to one direction. If there is an exit, enter the new

* room, otherwise print an error message.

*/

private void goRoom(Command command)

{

if(!command.hasSecondWord()) {

// if there is no second word, we don't know where to go...

System.out.println("Go where?");

return;

}

String direction = command.getSecondWord();

// Try to leave current room.

Room nextRoom = player.getCurrentRoom().getExit(direction);

Room previousRoom = player.getCurrentRoom();

if (nextRoom == null) {

System.out.println("There is no door!");

}

else {

player.enterRoom(nextRoom);

player.pushRoom(previousRoom);

System.out.println(player.getCurrentRoom().getLongDescription());

}

}

/**

* "Quit" was entered. Check the rest of the command to see

* whether we really quit the game.

* @return true, if this command quits the game, false otherwise.

*/

private boolean quit(Command command)

{

if(command.hasSecondWord()) {

System.out.println("Quit what?");

return false;

}

else {

return true; // signal that we want to quit

}

}

/**

* "look" was entered. look around the room and print the descripton

* of the room

*/

private void processLook()

{

System.out.println(player.getCurrentRoom().getLongDescription());

}

/**

*try to "pick an item". if that item can be picked.

*keep it.

*/

private void pickItem(Command command)

{

if(!command.hasSecondWord()) {

// if there is no second word, we don't know what item you want

System.out.println("what item");

return;

}

String itemName = command.getSecondWord();

// try to pick the item

Item item = player.getCurrentRoom().getItem(itemName);

if( item != null){

player.addItem(item);

System.out.println("You are now carrying this item." + "\n" + item.toString());

player.getCurrentRoom().removeItem(itemName);

}else { System.out.println(" no such item in the room");

}

}

/**

* a method that will remove will alow the player to drop the

* item and this will leave the item within this room.

*/

private void dropItem(Command command)

{

if(!command.hasSecondWord()) {

// if there is no second word, we don't know what item you want

System.out.println("what item");

return;

}

String anItem = command.getSecondWord();

// try to pick the item

Item item = player.getItem(anItem);

if ( item != null){

player.dropItem(item);

player.getCurrentRoom().addItem(anItem, item);

System.out.println("you have droped this item."+ "\n" + item.toString());

}else

{ System.out.println("you dont have this item");

}

}

/**

* move back method

*/

private Room backRoom()

{

if(player.isEmpty() == true){

System.out.println("You didn't go into any previous rooms");

}else{

Room aRoom = player.popRoom();

Room currentRoom = player.getCurrentRoom();

player.enterRoom(aRoom);

processLook();

}return null;

}

/**

* a method to use an item to get rid of a certain magic. only if the your weight

* is high enough

*/

private void useItem(Command command)

{

if(!command.hasSecondWord()) {

//if there is no second word, we dont nknow what you want to do

System.out.println("sorry what");

return;

}

if (!command.hasThirdWord()) {

System.out.println("sorry what");

return;

}

String anItem = command.getSecondWord();

String theMagic = command.getThirdWord();

//try to use the item agianst the magic

Item item = player.getItem(anItem);

if (item != null) {

Magic magic = player.getCurrentRoom().getMagic(theMagic);

if (magic != null) {

if ( item.getItemWeight() >= magic.getMagicPower() )

{

player.getCurrentRoom().removeMagic(theMagic);

System.out.println("the magic is gone");

}else

{

System.out.println("your power is not great enough to rid of the magic");

}

}else{

System.out.println("sorry what");

}

}

}

/**

* a method to win the game

*/

private boolean keepTheGameGoing()

{

for(Room r : rooms){

if(player.getCurrentRoom().getSizeOfMagicList() == 0)

return true;

}

return false;

}

}

mollemana at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 2

> when i put this in it wont work it say there is an error at (Room r: rooms).cannot find symbol(variable rooms)

Cutting and pasting code you find on the forum (*ahem*) isn't going to work. You should first try to understand what is going on. You're unlikely to get a cut-and-paste solution to your problem.

~

yawmarka at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 3
if you need any other code like room or player....just ask?
mollemana at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 4

> Cutting and pasting code you find on the forum

> (*ahem*) isn't going to work. You should first try

> to understand what is going on. You're unlikely to

> get a cut-and-paste solution to your problem.

>

Not to mention posting the same thing over and over is simply going to annoy the people he wants help from.

Aknibbsa at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 5
no no...i under stand that it is looping through all the rooms and cheecking if they all == 0 but what i dont know is where to do this...i think i should do it in my game class but then i actually dont know how to do it....
mollemana at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 6
sorry!!!
mollemana at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 7
>for(Room r : rooms){You haven't initialized nor declared the rooms variable.
Ruly-o_Oa at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 8

emm i just realised my rooms are not in a hashmap.......they are created within a scenerio. and not put into any hashmap...just created on their own..

now i need to be able to look through all the room that have been created to check the monster they hold = 0

this is where i create the rooms

this is my scenerio class

public class Scenario

{

// instance variables - replace the example below with your own

private String name ;

/**

* Constructor for objects of class Scenario

*/

public Scenario()

{

// initialise instance variables

name = "stefs world";

}

/**

* An example of a method - replace this comment with your own

*

* @param ya sample parameter for a method

* @returnthe sum of x and y

*/

public void createRoomsAndItems(Player player)

{

Room outside, garden, fountain, mainHall, theLab, cellar;

Item pint;

Magic devil;

// create the rooms and items within the room

outside = new Room("Outside in the courtyard of the castle");

garden = new Room("the garden, everything seems dead. i wonder why?");

fountain = new Room("refresh with a drink from the fountain");

mainHall = new Room("in this dark hall there is something spooky about it.");

theLab = new Room("some sort of lab with abundance of strange items...be carefull");

cellar = new Room("in the cellar...the princess is in here.");

pint = new Item("pint", "alcholic bvreage", 10);

devil = new Magic("devil", "a demon from the underworld", 5);

// initialise room exits

outside.setExit("east", garden);

outside.setExit("south", mainHall);

outside.setExit("west", fountain);

garden.setExit("west", outside);

garden.setExit("down", theLab);

fountain.setExit("east", outside);

fountain.addItem("pint", pint);

mainHall.setExit("north", outside);

mainHall.setExit("east", theLab);

mainHall.addMagic("devil", devil);

theLab.setExit("west", mainHall);

theLab.setExit("down", cellar);

cellar.setExit("up", theLab);

player.enterRoom(outside); // start game outside

}

}

mollemana at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 9

i treid this just there in my game class.....but it didnt work...i understand why

its just i dont know what to put for outside....like how would i say that outside is a room and mainhall is a room...getSizeOfMagicList() is a method within my room class

/**

* a method to win the game

*/

private void keepTheGameGoing()

{

if(outside.getSizeOfMagicList() && mainhall.getSizeOfMagicList() == 0){

System.out.println("the game is over");

}

}

mollemana at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...
# 10

In the create RoomsAndItems method, you could create a List, then put all the Rooms you create into it, and return it. Something like this:

public List createRoomsAndItems(Player player) {

Room outside, garden, fountain, mainHall, theLab, cellar;

Item pint;

Magic devil;

List theRoomList = new ArrayList();// INITIALIZE A LIST TO HOLD THE ROOMS

// create the rooms and items within the room

outside = new Room("Outside in the courtyard of the castle");

garden = new Room("the garden, everything seems dead. i wonder why?");

fountain = new Room("refresh with a drink from the fountain");

mainHall = new Room("in this dark hall there is something spooky about it.");

theLab = new Room("some sort of lab with abundance of strange items...be carefull");

cellar = new Room("in the cellar...the princess is in here.");

pint = new Item("pint", "alcholic bvreage", 10);

devil = new Magic("devil", "a demon from the underworld", 5);

theRoomList.add(outside);

theRoomList.add(garden);

// etc.... add the rest of the Rooms to the list

// initialise room exits

outside.setExit("east", garden);

outside.setExit("south", mainHall);

outside.setExit("west", fountain);

garden.setExit("west", outside);

garden.setExit("down", theLab);

fountain.setExit("east", outside);

fountain.addItem("pint", pint);

mainHall.setExit("north", outside);

mainHall.setExit("east", theLab);

mainHall.addMagic("devil", devil);

theLab.setExit("west", mainHall);

theLab.setExit("down", cellar);

cellar.setExit("up", theLab);

player.enterRoom(outside); // start game outside

return theRoomList;// RETURN THE LIST OF ROOMS TO THE METHOD CALLER

}

When you call that method, you get a List of all the Rooms back. Store that somewhere, then use it to examine every room and determine if the monster count == 0 for each.

hunter9000a at 2007-7-10 1:21:56 > top of Java-index,Java Essentials,New To Java...