i thought it would be easier that i post all the code below and you run it
you will see the problem like this:
"Cannot resolve symbol - consturctor Room (java.jang.String)"
i guess it would be reasonably easy to fix but as i am a super
beginner, i tired but i wasnot be able to fix the problem
can sometbody please just fix the problem for me
thank you very much P.S. the development tool is BlueJ
------------------------------------------------------------
Classes: Game, Parser, CommandWords, Command, Room, Item, Player
------------------------------------------------------------
public class Game
{
private Parser parser;
private Room currentRoom;
private Player currentPlayer;
/**
* Create the game and initialise its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
currentPlayer = new Player("Mark",currentRoom);
}
/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, theatre, pub, lab, office, cellar;
// create the rooms
outside = new Room("outside the main entrance of the Polytechnic");
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");
cellar = new Room("in the cellar");
// initialise room exits
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
theatre.setExit("west", outside);
pub.setExit("east", outside);
lab.setExit("north", outside);
lab.setExit("east", office);
office.setExit("west", lab);
office.setExit("down", cellar);
cellar.setExit("up", office);
currentRoom = outside; // start game outside
// add items
Item textbook = new Item("this is a Java textbook",5);
office.addItem("Textbook",textbook);
}
/**
* Main play routine. Loops until end of play.
*/
public void start()
{
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 World of Zuul!");
System.out.println("World of Zuul is a new, incredibly boring
adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println();
System.out.println(currentRoom.getLongDescription());
}
/**
* Given a command, process (that is: execute) the command.
* If this command ends the game, true is returned, otherwise false is
* returned.
*/
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("look"))
look(command);
else if (commandWord.equals("take"))
take(command);
else if (commandWord.equals("drop"))
drop(command);
else if (commandWord.equals("quit")){
wantToQuit = quit(command);
}
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 university.");
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 = currentRoom.getExit(direction);
if (nextRoom == null)
System.out.println("There is no door!");
else {
currentRoom = nextRoom;
System.out.println(currentRoom.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 if there is any item in a room
*/
private void look(Command command)
{
System.out.println(currentRoom.getMyItemsDescription());
}
/**
* "Take" was entered. take a item in a room, if there is any
*/
private void take(Command command)
{
String theItem = command.getSecondWord();
Item theItems = currentRoom.getItem(theItem);
if (!command.hasSecondWord()){
System.out.println("Take what?");
return;
}
if(currentPlayer.getMyCarriedWeight() +
theItems.getWeight() > currentPlayer.getMaxWeight()){
System.out.println("This Item is too heavy for you to carry!");
return;
}
if(theItems.isMoveable()){
System.out.println("You can't move the item");
return;
}
currentPlayer.addItem(theItems);
System.out.println("The item has been taken");
currentRoom.removeItem(theItems);
}
private void drop(Command command)
{
String theItem = command.getSecondWord();
Item theItems = currentPlayer.getItem(theItem);
if (!command.hasSecondWord()){
System.out.println("Drop what?");
return;
}
currentPlayer.removeItem(theItems);
System.out.println("you have dropped the item");
currentRoom.addItem(theItems);
}
/**
* "ask" was entered. to get more information
*/
private void ask()
{
System.out.println("No one hear you");
}
}
------------------------------------------------------------
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Parser
{
private CommandWords commands; // holds all valid command words
public Parser()
{
commands = new CommandWords();
}
public Command getCommand()
{
String inputLine = ""; // will hold the full input line
String word1;
String word2;
System.out.print("> "); // print prompt
BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
try {
inputLine = reader.readLine().trim().toLowerCase();
}
catch(java.io.IOException exc) {
System.out.println ("There was an error during reading: "
+ exc.getMessage());
}
StringTokenizer tokenizer = new StringTokenizer(inputLine);
if(tokenizer.hasMoreTokens())
word1 = tokenizer.nextToken(); // get first word
else
word1 = null;
if(tokenizer.hasMoreTokens())
word2 = tokenizer.nextToken(); // get second word
else
word2 = null;
// note: we just ignore the rest of the input line.
// Now check whether this word is known. If so, create a command
// with it. If not, create a "null" command (for unknown command).
if(commands.isCommand(word1))
return new Command(word1, word2);
else
return new Command(null, word2);
}
/**
* Print out a list of valid command words.
*/
public void showCommands()
{
commands.showAll();
}
}
----------------------------------------------------------------
public class CommandWords
{
// a constant array that holds all valid command words
private static final String[] validCommands = {
"go", "quit", "help", "look", "take", "drop"
};
/**
* Constructor - initialise the command words.
*/
public CommandWords()
{
// nothing to do at the moment...
}
/**
* Check whether a given String is a valid command word.
* Return true if it is, false if it isn't.
*/
public boolean isCommand(String aString)
{
for(int i = 0; i < validCommands.length; i++) {
if(validCommands[i].equals(aString))
return true;
}
// if we get here, the string was not found in the commands
return false;
}
/*
* Print all valid commands to System.out.
*/
public void showAll()
{
for(int i = 0; i < validCommands.length; i++) {
System.out.print(validCommands[i] + " ");
}
System.out.println();
}
}
--------------------------------------------------------------
public class Command
{
private String commandWord;
private String secondWord;
/**
* Create a command object. First and second word must be supplied, but
* either one (or both) can be null. The command word should be null to
* indicate that this was a command that is not recognised by this game.
*/
public Command(String firstWord, String secondWord)
{
commandWord = firstWord;
this.secondWord = secondWord;
}
/**
* Return the command word (the first word) of this command. If the
* command was not understood, the result is null.
*/
public String getCommandWord()
{
return commandWord;
}
/**
* Return the second word of this command. Returns null if there was no
* second word.
*/
public String getSecondWord()
{
return secondWord;
}
/**
* Return true if this command was not understood.
*/
public boolean isUnknown()
{
return (commandWord == null);
}
/**
* Return true if the command has a second word.
*/
public boolean hasSecondWord()
{
return (secondWord != null);
}
}
-------------------------------------------------------
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashSet;
public class Room
{
private String description;
private HashMap exits; // stores exits of this room.
private String name;
private HashSet myItems;
/**
* Create a room described "description". Initially, it has no exits.
* "description" is something like "in a kitchen" or "in an open court
* yard".
*/
public Room(String description, String name)
{
this.description = description;
this.name = name;
exits = new HashMap();
myItems = new HashSet();
}
/**
* Define an exit from this room.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
/**
* Return the description of the room (the one that was defined in the
* constructor).
*/
public String getShortDescription()
{
return description;
}
/**
* Return a long description of this room, in the form:
* You are in the kitchen.
* Exits: north west
*/
public String getLongDescription()
{
return "You are " + description + ".\n" + getExitString();
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
*/
private String getExitString()
{
String returnString = "Exits:";
Set keys = exits.keySet();
for(Iterator iter = keys.iterator(); iter.hasNext(); )
returnString += " " + iter.next();
return returnString;
}
/**
* Return the room that is reached if we go from this room in direction
* "direction". If there is no room in that direction, return null.
*/
public Room getExit(String direction)
{
return (Room)exits.get(direction);
}
/**
* Add items to the rooms
*/
public void addItem(Item myItem)
{
myItems.add(myItem);
}
public void removeItem(Item myItem)
{
myItems.remove(myItem);
}
public Item getItem(String theItem)
{
Iterator iter = myItems.iterator();
while(iter.hasNext())
{
Item anItem = (Item) iter.next();
if(anItem.getName().equals(theItem))
return anItem;
}
return null;
}
public String getMyItemsDescription()
{
String returnString = "This room has items:";
for(Iterator iter = myItems.iterator(); iter.hasNext();)
returnString += " "+ ((Item)iter.next()).getDescription();
return returnString;
}
public String getName()
{
return name;
}
}
-------------------------------------------------------------------
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;
public class Item
{
private String description;
private String name;
private int weight;
private boolean moveable;
/**
* Constructor for objects of class Item.
*/
public Item(String myDescription, String myName, int myWeight,
boolean itemMoveable)
{
this.description = myDescription;
this.name = myName;
this.weight = myWeight;
this.moveable = itemMoveable;
}
/**
* Return the description of the item (the one that was defined
* in the constructor).
*/
public String getDescription()
{
return description;
}
/**
* Return the name of the item (the one that was defined
* in the constructor).
*/
public String getName()
{
return name;
}
public boolean isMoveable()
{
return (moveable == false);
}
/**
* Return the weight of the item
*/
public int getWeight()
{
return weight;
}
}
-------------------------------------------------------------------------
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
public class Player
{
private String name;
//private int weightCarried;
private static int maxWeight = 300;
private Room currentRoom;
private HashSet myItems;
/**
* Constructor for objects of class Player.
*/
public Player(String myName, Room myCurrentRoom)
{
name = myName;
currentRoom = myCurrentRoom;
myItems = new HashSet();
}
/**
* Return the name of the player (the one that was defined
* in the constructor).
*/
public String getName()
{
return name;
}
public HashSet getMyItems()
{
return myItems;
}
/**
* Return the amount of weight the player is carrying.
*/
public int getMyCarriedWeight()
{
Iterator iter = myItems.iterator();
int total = 0;
while(iter.hasNext()){
total += ((Item) iter.next()).getWeight();
}
return total;
}
public static int getMaxWeight()
{
return maxWeight;
}
/*
* Add an item to the player's collection of items. Items have a
description, name and weight.
*/
public void addItem(Item myItem)
{
myItems.add(myItem);
}
public Item getItem(String theItem)
{
Iterator iter = myItems.iterator();
while(iter.hasNext()){
Item anItem = (Item) iter.next();
if(anItem.getName().equals(theItem))
return anItem;
}
return null;
}
public String getMyItemsDescription()
{
String returnString = "The items in my collection are: ";
for(Iterator iter = myItems.iterator(); iter.hasNext();)
returnString += " " + ((Item)iter.next()).getDescription();
return returnString;
}
public void removeItem(Item myItem)
{
myItems.remove(myItem);
}
} |