Who uses Java BlueJ here? :)

A place for people with an interest in developing new shmups.
Post Reply
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

Who uses Java BlueJ here? :)

Post by zak »

Has anyone read the book "objects first with Java; A practical introduction using blueJ"?

I am currently stuck on exercise 7.30 in the book. If anyone can help me out, then you will be handsomely rewarded. :P :P :P
User avatar
landshark
Posts: 2156
Joined: Wed Jan 26, 2005 5:27 am
Location: Chicago 'Burbs

Re: Who uses Java BlueJ here? :)

Post by landshark »

zak wrote:Has anyone read the book "objects first with Java; A practical introduction using blueJ"?

I am currently stuck on exercise 7.30 in the book. If anyone can help me out, then you will be handsomely rewarded. :P :P :P
post the problem.
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

Post by zak »

sorry about that :oops:


Exercise 7.31 Implement an extension that allows a player to pick up one single item. This includes implementing two new commands: take and drop.

Exercise 7.31 Extend your implementation to allow the player to carry any numberof items.

Exercise 7.32 Add a restriction that allows the player to carry items only up to aspecified maximum weight. The maximum weight a player can carry is an attribute ofthe player.

Exercise 7.33 Implement an items command that prints out all items currently car-ried and their total weight.

Exercise 7.34 Add a magic cookie item to a room. Add an eat cookie command.If a player finds and eats the magic cookie, it increases the weight that the player cancarry. (You might like to modify this slightly to better fit into your own game scenario.)
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

Post by zak »

here is the project window:

Image
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

GAME CLASS:

Post by zak »

game class:



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

/**
* Create the game and initialise its internal map.
*/
public Game()
{
createRooms();
parser = new Parser();
}

/**
* Create all the rooms and link their exits together.
*/
private void createRooms()
{
Room outside, theatre, pub, lab, office;

// create the rooms
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");

// 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);

currentRoom = outside; // 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 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("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
}
}
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

ROOM CLASS:

Post by zak »

Room class:



public class Room
{
private String description;
private HashMap exits; // stores exits of this room.

/**
* 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)
{
this.description = description;
exits = new HashMap();
}

/**
* 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);
}
}
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

COMMAND CLASS:

Post by zak »

Command class:



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);
}
}
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

PARSER CLASS:

Post by zak »

Parser class:



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();
}
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();
}
}
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

COMMAND WORDS CLASS:

Post by zak »

command words class:




public class CommandWords
{
// a constant array that holds all valid command words
private static final String[] validCommands = {
"go", "quit", "help"
};

/**
* 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.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 + " ");
}
System.out.println();
}
}
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

Post by zak »

anyone? :cry:
kemical
Posts: 580
Joined: Wed Jan 26, 2005 1:14 am
Location: Tokyo

Post by kemical »

I'm not sure what kind of answer you're wanting :)

I don't use java at all, but from these exercises it looks like you need functions and variables that can handle the following:

-being able to store item names or id's in some type of list or array, for what items are current "held" by the player.
-add a weight property to all items, ex: 20.0, 15.5, 5.0, etc.. the player would then have their own property "maxWeight = 100.0" or something.. and each time you try to add a new item to the array/list it checks to see if the total weight is greater than the max weight, if it is, you can't carry it.
-the item list command would just be a way to list objects in an array/list.. using a for loop or something, for i=0; i<%totalItems; i++ etc..
-the magic cookie would just change the maxWeight property, set it to 10000 or something, then have a timer which resets it back after a while.
zak
Posts: 813
Joined: Thu Mar 24, 2005 12:01 pm

Post by zak »

kemical wrote:I'm not sure what kind of answer you're wanting :)

I don't use java at all, but from these exercises it looks like you need functions and variables that can handle the following:

-being able to store item names or id's in some type of list or array, for what items are current "held" by the player.
-add a weight property to all items, ex: 20.0, 15.5, 5.0, etc.. the player would then have their own property "maxWeight = 100.0" or something.. and each time you try to add a new item to the array/list it checks to see if the total weight is greater than the max weight, if it is, you can't carry it.
-the item list command would just be a way to list objects in an array/list.. using a for loop or something, for i=0; i<%totalItems; i++ etc..
-the magic cookie would just change the maxWeight property, set it to 10000 or something, then have a timer which resets it back after a while.
Thanks Kemical! :P
Post Reply