first commit

This commit is contained in:
Boris
2024-01-15 20:14:10 +00:00
commit 8c81ee28b7
3106 changed files with 474415 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
#BlueJ class context
comment0.params=firstWord\ secondWord
comment0.target=Command(java.lang.String,\ java.lang.String)
comment0.text=\n\ Create\ a\ command\ object.\ First\ and\ second\ word\ must\ be\ supplied,\ but\n\ either\ one\ (or\ both)\ can\ be\ null.\n\ @param\ firstWord\ The\ first\ word\ of\ the\ command.\ Null\ if\ the\ command\n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ was\ not\ recognised.\n\ @param\ secondWord\ The\ second\ word\ of\ the\ command.\n
comment1.params=
comment1.target=java.lang.String\ getCommandWord()
comment1.text=\n\ Return\ the\ command\ word\ (the\ first\ word)\ of\ this\ command.\ If\ the\n\ command\ was\ not\ understood,\ the\ result\ is\ null.\n\ @return\ The\ command\ word.\n
comment2.params=
comment2.target=java.lang.String\ getSecondWord()
comment2.text=\n\ @return\ The\ second\ word\ of\ this\ command.\ Returns\ null\ if\ there\ was\ no\n\ second\ word.\n
comment3.params=
comment3.target=boolean\ isUnknown()
comment3.text=\n\ @return\ true\ if\ this\ command\ was\ not\ understood.\n
comment4.params=
comment4.target=boolean\ hasSecondWord()
comment4.text=\n\ @return\ true\ if\ the\ command\ has\ a\ second\ word.\n
numComments=5

View File

@@ -0,0 +1,73 @@
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure 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 <null>.
*
* If the command had only one word, then the second word is <null>.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
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.
* @param firstWord The first word of the command. Null if the command
* was not recognised.
* @param secondWord The second word of the command.
*/
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.
* @return The command word.
*/
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);
}
}

View File

@@ -0,0 +1,11 @@
#BlueJ class context
comment0.params=
comment0.target=CommandWords()
comment0.text=\n\ Constructor\ -\ initialise\ the\ command\ words.\n
comment1.params=aString
comment1.target=boolean\ isCommand(java.lang.String)
comment1.text=\n\ Check\ whether\ a\ given\ String\ is\ a\ valid\ command\ word.\ \n\ @return\ true\ if\ it\ is,\ false\ if\ it\ isn't.\n
comment2.params=
comment2.target=void\ showAll()
comment2.text=\n\ Print\ all\ valid\ commands\ to\ System.out.\n
numComments=3

View File

@@ -0,0 +1,51 @@
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* This class holds an enumeration of all command words known to the game.
* It is used to recognise commands as they are typed in.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
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[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(String command: validCommands) {
System.out.print(command + " ");
}
System.out.println();
}
}

View File

@@ -0,0 +1,26 @@
#BlueJ class context
comment0.params=
comment0.target=Game()
comment0.text=\n\ Create\ the\ game\ and\ initialise\ its\ internal\ map.\n
comment1.params=
comment1.target=void\ createRooms()
comment1.text=\n\ Create\ all\ the\ rooms\ and\ link\ their\ exits\ together.\n
comment2.params=
comment2.target=void\ play()
comment2.text=\n\ \ Main\ play\ routine.\ \ Loops\ until\ end\ of\ play.\n
comment3.params=
comment3.target=void\ printWelcome()
comment3.text=\n\ Print\ out\ the\ opening\ message\ for\ the\ player.\n
comment4.params=command
comment4.target=boolean\ processCommand(Command)
comment4.text=\n\ Given\ a\ command,\ process\ (that\ is\:\ execute)\ the\ command.\n\ @param\ command\ The\ command\ to\ be\ processed.\n\ @return\ true\ If\ the\ command\ ends\ the\ game,\ false\ otherwise.\n
comment5.params=
comment5.target=void\ printHelp()
comment5.text=\n\ Print\ out\ some\ help\ information.\n\ Here\ we\ print\ some\ stupid,\ cryptic\ message\ and\ a\ list\ of\ the\ \n\ command\ words.\n
comment6.params=command
comment6.target=void\ goRoom(Command)
comment6.text=\ \n\ Try\ to\ in\ to\ one\ direction.\ If\ there\ is\ an\ exit,\ enter\ the\ new\n\ room,\ otherwise\ print\ an\ error\ message.\n
comment7.params=command
comment7.target=boolean\ quit(Command)
comment7.text=\ \n\ "Quit"\ was\ entered.\ Check\ the\ rest\ of\ the\ command\ to\ see\n\ whether\ we\ really\ quit\ the\ game.\n\ @return\ true,\ if\ this\ command\ quits\ the\ game,\ false\ otherwise.\n
numComments=8

View File

@@ -0,0 +1,179 @@
/**
* This class is the main class of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game. Users
* can walk around some scenery. That's all. It should really be extended
* to make it more interesting!
*
* To play this game, create an instance of this class and call the "play"
* method.
*
* This main class creates and initialises all the others: it creates all
* rooms, creates the parser and starts the game. It also evaluates and
* executes the commands that the parser returns.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
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, theater, pub, lab, office;
// create the rooms
outside = new Room("outside the main entrance of the university");
theater = new Room("in a lecture theater");
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", theater);
outside.setExit("south", lab);
outside.setExit("west", pub);
theater.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.
* @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 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 university.");
System.out.println();
System.out.println("Your command words are:");
parser.showCommands();
}
/**
* Try to in 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
}
}
}

View File

@@ -0,0 +1,11 @@
#BlueJ class context
comment0.params=
comment0.target=Parser()
comment0.text=\n\ Create\ a\ parser\ to\ read\ from\ the\ terminal\ window.\n
comment1.params=
comment1.target=Command\ getCommand()
comment1.text=\n\ @return\ The\ next\ command\ from\ the\ user.\n
comment2.params=
comment2.target=void\ showCommands()
comment2.text=\n\ Print\ out\ a\ list\ of\ valid\ command\ words.\n
numComments=3

View File

@@ -0,0 +1,73 @@
import java.util.Scanner;
/**
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* This parser reads user input and tries to interpret it as an "Adventure"
* 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.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Parser
{
private CommandWords commands; // holds all valid command words
private Scanner reader; // source of command input
/**
* Create a parser to read from the terminal window.
*/
public Parser()
{
commands = new CommandWords();
reader = new Scanner(System.in);
}
/**
* @return The next command from the user.
*/
public Command getCommand()
{
String inputLine; // will hold the full input line
String word1 = null;
String word2 = null;
System.out.print("> "); // print prompt
inputLine = reader.nextLine();
// Find up to two words on the line.
Scanner tokenizer = new Scanner(inputLine);
if(tokenizer.hasNext()) {
word1 = tokenizer.next(); // get first word
if(tokenizer.hasNext()) {
word2 = tokenizer.next(); // get second word
// 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();
}
}

View File

@@ -0,0 +1,27 @@
Project: zuul-better
Authors: Michael Kölling and David J. Barnes
This project is part of the material for the book
Objects First with Java - A Practical Introduction using BlueJ
Sixth edition
David J. Barnes and Michael Kölling
Pearson Education, 2016
This project is a simple framework for an adventure game. In this version,
it has a few rooms and the ability for a player to walk between these rooms.
That's all.
To start this application, create an instance of class "Game" and call its
"play" method.
This project was written as the starting point of a small Java project.
The goal is to extend the game:
- add items to rooms (items may have weight)
- add multiple players
- add commands (pick, drop, examine, read, ...)
- (anything you can think of, really...)
Read chapter 8 of the book to get a detailed description of the project.

View File

@@ -0,0 +1,20 @@
#BlueJ class context
comment0.params=description
comment0.target=Room(java.lang.String)
comment0.text=\n\ Create\ a\ room\ described\ "description".\ Initially,\ it\ has\n\ no\ exits.\ "description"\ is\ something\ like\ "a\ kitchen"\ or\n\ "an\ open\ court\ yard".\n\ @param\ description\ The\ room's\ description.\n
comment1.params=direction\ neighbor
comment1.target=void\ setExit(java.lang.String,\ Room)
comment1.text=\n\ Define\ an\ exit\ from\ this\ room.\n\ @param\ direction\ The\ direction\ of\ the\ exit.\n\ @param\ neighbor\ \ The\ room\ to\ which\ the\ exit\ leads.\n
comment2.params=
comment2.target=java.lang.String\ getShortDescription()
comment2.text=\n\ @return\ The\ short\ description\ of\ the\ room\n\ (the\ one\ that\ was\ defined\ in\ the\ constructor).\n
comment3.params=
comment3.target=java.lang.String\ getLongDescription()
comment3.text=\n\ Return\ a\ description\ of\ the\ room\ in\ the\ form\:\n\ \ \ \ \ You\ are\ in\ the\ kitchen.\n\ \ \ \ \ Exits\:\ north\ west\n\ @return\ A\ long\ description\ of\ this\ room\n
comment4.params=
comment4.target=java.lang.String\ getExitString()
comment4.text=\n\ Return\ a\ string\ describing\ the\ room's\ exits,\ for\ example\n\ "Exits\:\ north\ west".\n\ @return\ Details\ of\ the\ room's\ exits.\n
comment5.params=direction
comment5.target=Room\ getExit(java.lang.String)
comment5.text=\n\ Return\ the\ room\ that\ is\ reached\ if\ we\ go\ from\ this\ room\ in\ direction\n\ "direction".\ If\ there\ is\ no\ room\ in\ that\ direction,\ return\ null.\n\ @param\ direction\ The\ exit's\ direction.\n\ @return\ The\ room\ in\ the\ given\ direction.\n
numComments=6

View File

@@ -0,0 +1,91 @@
import java.util.Set;
import java.util.HashMap;
/**
* Class Room - a room in an adventure game.
*
* This class is part of the "World of Zuul" application.
* "World of Zuul" is a very simple, text based adventure game.
*
* A "Room" represents one location in the scenery of the game. It is
* connected to other rooms via exits. For each existing exit, the room
* stores a reference to the neighboring room.
*
* @author Michael Kölling and David J. Barnes
* @version 2016.02.29
*/
public class Room
{
private String description;
private HashMap<String, Room> exits; // stores exits of this room.
/**
* Create a room described "description". Initially, it has
* no exits. "description" is something like "a kitchen" or
* "an open court yard".
* @param description The room's description.
*/
public Room(String description)
{
this.description = description;
exits = new HashMap<>();
}
/**
* Define an exit from this room.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
/**
* @return The short description of the room
* (the one that was defined in the constructor).
*/
public String getShortDescription()
{
return description;
}
/**
* Return a description of the room in the form:
* You are in the kitchen.
* Exits: north west
* @return A long description of this room
*/
public String getLongDescription()
{
return "You are " + description + ".\n" + getExitString();
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
* @return Details of the room's exits.
*/
private String getExitString()
{
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys) {
returnString += " " + exit;
}
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.
* @param direction The exit's direction.
* @return The room in the given direction.
*/
public Room getExit(String direction)
{
return exits.get(direction);
}
}

View File

@@ -0,0 +1,96 @@
#BlueJ package file
dependency1.from=Parser
dependency1.to=CommandWords
dependency1.type=UsesDependency
dependency2.from=Parser
dependency2.to=Command
dependency2.type=UsesDependency
dependency3.from=Game
dependency3.to=Parser
dependency3.type=UsesDependency
dependency4.from=Game
dependency4.to=Room
dependency4.type=UsesDependency
dependency5.from=Game
dependency5.to=Command
dependency5.type=UsesDependency
objectbench.height=76
objectbench.width=788
package.editor.height=394
package.editor.width=680
package.editor.x=70
package.editor.y=80
package.numDependencies=5
package.numTargets=5
package.showExtends=true
package.showUses=true
project.charset=UTF-8
readme.editor.height=666
readme.editor.width=839
readme.editor.x=41
readme.editor.y=23
target1.editor.height=722
target1.editor.width=885
target1.editor.x=160
target1.editor.y=67
target1.height=60
target1.name=Game
target1.naviview.expanded=true
target1.showInterface=false
target1.type=ClassTarget
target1.typeParameters=
target1.width=100
target1.x=90
target1.y=110
target2.editor.height=707
target2.editor.width=891
target2.editor.x=50
target2.editor.y=60
target2.height=60
target2.name=Command
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=110
target2.x=190
target2.y=200
target3.editor.height=728
target3.editor.width=857
target3.editor.x=50
target3.editor.y=60
target3.height=60
target3.name=CommandWords
target3.naviview.expanded=true
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=130
target3.x=490
target3.y=200
target4.editor.height=735
target4.editor.width=830
target4.editor.x=50
target4.editor.y=60
target4.height=60
target4.name=Room
target4.naviview.expanded=true
target4.showInterface=false
target4.type=ClassTarget
target4.typeParameters=
target4.width=110
target4.x=190
target4.y=280
target5.editor.height=735
target5.editor.width=854
target5.editor.x=50
target5.editor.y=60
target5.height=60
target5.name=Parser
target5.naviview.expanded=true
target5.showInterface=false
target5.type=ClassTarget
target5.typeParameters=
target5.width=100
target5.x=340
target5.y=60