Create a simple adventure game


Assignment:

The tasks that need to be completed

There are items in some rooms. Every room can hold any number of items. Some items can be picked up by the player

• The player can carry some items with him. Every item has a weight. The player can carry items only up to a certain total weight.

• Implement a command "back" that takes you back to the last room you've been in.

• Add a character to your game. Characters are people or animals or monsters anything that moves, really. Characters are also in rooms (like the player and the items). Unlike items, characters can move around by themselves.

• Extend the parser to recognise three-word commands.

• Add a magic transporter room every time you enter it you are transported to a random room in your game.

* This parser reads user input and tries to interpret it as a "Zork"

 * command. Every time it is called it reads a line from the terminal and

 * tries to interpret the line as a two word command. It returns the command

 * as an object of class Command.

* The parser has a set of known command words. It checks user input against

 * the known commands, and if the input is not one of the known commands, it

 * returns a command object that is marked as an unknown command.

 */

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.StringTokenizer;

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 "nil" 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();

    }

}

* Class Command - Part of the "Zork" game.

 *

 * *

 * This class holds information about a command that was issued by the user.

 * A command currently consists of two strings: a command word and a second

 * word (for example, if the command was "take map", then the two strings

 * obviously are "take" and "map").

 *

 * The way this is used is: Commands are already checked for being valid

 * command words. If the user entered an invalid command (a word that is not

 * known) then the command word is .

 *

 * If the command had only one word, then the second word is .

 *

 * The second word is not checked at the moment. It can be anything. If this

 * game is extended to deal with items, then the second part of the command

 * should probably be changed to be an item rather than a String.

 */

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

    }

}

* This class holds an enumeration of all command words known to the game.

 * It is used to recognise commands as they are typed in.

 *

 * This class is part of the "Zork" game.

 */

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[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();

    }

}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create a simple adventure game
Reference No:- TGS01935778

Now Priced at $25 (50% Discount)

Recommended (96%)

Rated (4.8/5)