I put stuff in a hashmap but cannot retrieve it
<code>
Room values are still null

<>code<>
Here is main:
import java.util.*;
public class testx extends junit.framework.TestCase
{
private Parser parser1;
private Game game1;
private ArrayList<String> cmds;
private Cmd c1;
private Room cr;

public testx(){
}


protected void setUp()
{
parser1 = new Parser();
game1 = new Game();
cmds=new ArrayList<String>();
cr=new Room("test");

}


protected void tearDown()
{
}


public void fA(){
cmds.add(new String("go east"));
cmds.add(new String("go west"));
cmds.add(new String("go north"));
cmds.add(new String("go south"));
cmds.add(new String("go pub"));
cmds.add(new String("go outside"));
cmds.add(new String("go lab"));
cmds.add(new String("go office"));
}

public void testTd(){
System.out.print('\u000C');
fA();

int i=0; boolean finished=false;
game1.createRooms();
while (i<cmds.size()){
String s=cmds.get(i);
Cmd dmc=parser1.getCommand(s);
finished = game1.process(dmc);

i++;
}
}

}

public class Game
{
private Parser parser;
private Room currentRoom;
private Room nextRoom;

public Game()
{

parser=new Parser();
}


public void createRooms()
{
Room outside, theatre, pub, lab, office;
outside = new Room("outside the main entrance of the university");
theatre = new Room("in a lecture theatre");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");

outside.setExits(null, theatre, lab, pub);
theatre.setExits(null, null, null, outside);
pub.setExits(null, outside, null, null);
lab.setExits(outside, office, null, null);
office.setExits(null, null, null, lab);
currentRoom = outside;
System.out.println(currentRoom.getExitString());

}

public void pl()
{

System.out.print("Exits: " + currentRoom.getExitString());
}
public boolean process(Cmd 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);
return wantToQuit;
}
private void printHelp()
{
System.out.println("You are lost. You are alone. You wander");
System.out.println("around at the university.");
System.out.println();
System.out.println("Your command words are:");
System.out.println(" go quit help");
}
public void goRoom(Cmd command)
{

if(!command.hasSecondWord()) {
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
Room nextRoom=currentRoom.getExit(direction);
System.out.println(currentRoom.getExit(direction)) ;
if (nextRoom==null){ System.out.println("There is no door!");
}
else{
System.out.println("Exit: "+ nextRoom);
}
}

private boolean quit(Cmd command)
{
if(command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
}
else {
return true; // signal that we want to quit
}
}
}


import java.util.HashMap;
import java.util.Set;
public class Room
{
private String description;
private HashMap<String, Room> exits;

public Room (String description)
{
this.description=description;
exits=new HashMap<String, Room>();
}
public void setExits(Room north, Room east, Room south, Room west)
{
if (north!=null)
exits.put("north", north);
System.out.println(" " + west);
if(east !=null)
exits.put("east",east);
if(south!=null)
exits.put("south",south);
if(west!=null)
exits.put("west",west);

}

public Room getExit(String direction)
{
return exits.get(direction);

}

public String getDescription()
{
return description;
}
public String getExitString(){
String returnString="Exits: ";
Set<String> keys=exits.keySet();
for (String exit: keys) {
returnString += " " + exit;
}
return returnString;

}

}
public class Parser
{
private CommandWords commands; // holds all valid command words
private Scanner reader; // source of command input

public Parser()
{
commands = new CommandWords();
reader = new Scanner(System.in);
}

public Cmd getCommand(String s)
{
String inputLine; // will hold the full input line
String word1 = null;
String word2 = null;
inputLine = s;
Scanner tokenizer = new Scanner(inputLine);
if(tokenizer.hasNext()) {
word1 = tokenizer.next(); // get first word
if(tokenizer.hasNext()) {
word2 = tokenizer.next();
}
}

if(commands.isCommand(word1)) {
return new Cmd(word1, word2);
}
else {
return new Cmd(null, word2);
}
}

}

public class Cmd
{
private String commandWord;
private String secondWord;

public Cmd(String firstWord, String secondWord)
{
commandWord = firstWord;
this.secondWord = secondWord;
}

public String getCommandWord()
{
return commandWord;
}


public String getSecondWord()
{
return secondWord;
}

public boolean isUnknown()
{
return (commandWord == null);
}

/**
* @return true if the command has a second word.
*/
public boolean hasSecondWord()
{
return (secondWord != null);
}
}
Room@7a5a19
Room@b122a1
null
null
Room@1589e56
Exits: south east west
Room@4c47db
Exit: Room@4c47db
Room@7a5a19
Exit: Room@7a5a19
null
There is no door!
Room@1589e56
Exit: Room@1589e56
null
There is no door!
null
There is no door!

Last edited by uhertz; Yesterday at 03:32 PM.