Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Project : JAVA Home Tour Application Background When we think of computer programs and applications today, we think of graphical interfaces with buttons and

JAVA Project : JAVA Home Tour Application

Background

When we think of computer programs and applications today, we think of graphical interfaces with buttons and expandable menus and other kinds of displays. But the consumer computers started finding their ways into homes 5 years before the technology for what we would think of as "graphics" debuted, and it would take another 5 years for those first graphics cards to become commonplace. So for a solid decade, almost all software had to display output in a text format, and collect input from users through text-based commands.

Some of the first games for these home computers then were similar to Choose-Your-Own-Adventure books, and defined the "adventure" genre accordingly (sometimes the genre name is attributed to the game Colossal Cave Adventure, a pioneer of the format) . In these kinds of games, the player is presented with a description of a place or an event, and has to type in a command to do something. The game parses this command, and then prints some new output accordingly. Generally, the player is navigating through different "rooms" or areas using cardinal directions to explore the environment.

For a good example of this, check out the adventure game Zork, which you can play from the link in the reference section below. You'll find that while the complete list of commands was only printed in the manual, many of them are intuitive - open mailbox, take leaflet, read leaflet, go south, look...

For this project, you will be building a similar kind of application, but rather than an adventure game we'll just use it to present an interactive "tour" of a home - maybe your current home, maybe your dream home. In any case, the pattern will be exactly the same as other text-based games:

  1. Display prompt
  2. Collect input
  3. Process input as a command
  4. Print output of command

Here's an example of what your game might look like:

Instructions

For now, we're just going to create the shell for our project, based on what we understand of the requirements. Later, as your skills improve and you continue to learn, you'll probably make changes to these classes or their structure - a process called refactoring - to make your project easier to expand.

  1. In Eclipse, create a project named HomeTour
  2. In the HomeTour project, create the following packages and classes:
    1. fixtures
      1. fixtures.Fixture (abstract)
      2. fixtures.Room
    2. game
      1. game.Main
      2. game.Player
      3. game.RoomManager

These classes will work as follows:

fixtures.Fixture: This abstract class will be used as a base for anything that can be looked at or interacted with. This class should define (at least) the following properties:

  • String name : a short name / title for the fixture
  • String shortDescription : a one-sentence-long description of a fixture, used to briefly mention the fixture
  • String longDescription : a paragraph-long description of the thing, displayed when the player investigates the fixture thoroughly (looks at it, or enters a room)

Here's an example of what your game might look like, so these make sense:

fixtures.Room: This class represents a room in the house. It will extend fixtures.Fixture, and so will inherit the descriptive properties. The Room will also have the following properties:

  • Room[] exits : the rooms adjacent to this one. You might decide that a room in a particular direction always uses a certain index, e.g. a north exit always goes in index 0, an east exit always goes in index 1, etc. If so, then the size of this array depends on how many directions you want to support.

The Room class should also have a constructor that accepts a name, shortDescription, and longDescription. You might also find it convenient to create a getter not just for all the exits, but for a particular exit given a direction:

public Room(String name, String shortDescription, String longDescription) {

super(name, shortDescription, longDescription);

this.exits = new Room[?]; // size is your choice

}

public Room[] getExits() {

}

public Room getExit(String direction) {

}

game.Main: This class will store the main(String[]) method for our game (and of course, it will be the only class that has a main(String[]) method). This is where the game-loop will go, where we'll display a prompt, collect input, and parse that input

The printRoom(Player) method will print a prompt to the console for the player's current room, similar to the above image.

The collectInput() method will use a Scanner object to collect console input from the user, and then will divide that input into multiple parts. Generally those parts will look like this:

  1. An action
  2. The target of an action (if any)

For example, "go east" -> "go" is the command, "east" is the target. This method will break the input into a String[], and return that.

The parse(String[], Player) method will take the output of the above collectInput() method and a player object, and will resolve that command. This can actually be simpler than it sounds - the first index of the passed-in String[] should be the action, so you can switch on that and handle the target differently for each case. The Player object is there so you can modify it if needed (like changing the Player's currentRoom based on the direction moved)

public static void main(String[] args) {

}

private static void printRoom(Player player) {

}

private static String[] collectInput() {

}

private static void parse(String[] command, Player player) {

}

game.Player: This class represents the player moving through these rooms. The Player class has these properties:

  • Room currentRoom : the room the player is currently in.

game.RoomManager: This class will be responsible for "loading" our rooms into memory. When game.Main is executed, it will invoke the init() method in this class that will instantiate all our Room objects, link them together as exits, and designate a startingRoom.

  • Room startingRoom : the room a player should start in.
  • Room[] rooms : all the rooms in the house.

public void init() {

Room foyer = new Room(

"The Foyer",

"a small foyer",

"The small entryway of a neo-colonial house. A dining room is open to the south, where a large table can be seen." + " "

+ "The hardwood floor leads west into doorway, next to a staircase that leads up to a second floor." + " "

+ "To the north is a small room, where you can see a piano.");

this.rooms[0] = foyer;

this.startingRoom = foyer;

}

That's it for now! As you continue to learn more about the Java language, you'll fill in more of this application.

Continue Working on Home Tour

Instructions

Depending on how much of your Home Tour application you have done, you'll want to spend this time either...

  1. Getting the methods outlined in the original instructions working. Make sure you have a functional prototype of at least two rooms that you can move between. or if you already have that...
  2. Expand your Home Tour app to support additional functionalities. Here are two ideas:
    1. Items in the house that a player can look at (Item being a new subtype of Fixture).
      • Some items might be able to picked up and added to the player's inventory. Then, they can be looked at no matter which room the player is in.
      • Maybe you expand your command parsing to support a secondary target, so you can use on .
    2. Doors that the player can open and close.
      • Doors are a new type of Fixture, but also count as exits from Rooms. So you'll need to refactor Room so exits are general Fixture references instead of rooms specifically.
      • Doors can be open or closed (open = true or false)
      • If a door is open, it should prevent movement to the next room

Both of these expansions can be trickier than they initially seem, so they're completely optional. If you have a functional prototype and aren't up to the challenge of expanding the features of your program, just add to the content (more rooms).

Room Exits

Instructions

Up until now, you have been tracking the exits in each Room of your Home Tour application using a Room[]. This might have been functional, but it's not optimal, and you have probably noticed how clunky it feels.

So what do we know about room exits? We know that the exit of one Room is another Room, and we know those exits are situated in a certain direction from the current room. For example:

foyer:

north: library

This scenario seems like a good application of a Map, doesn't it? Instead of a Room[], you could have a Map where the key is the direction of an exit and the value is the room in that direction.

Take some time to refactor your Room class to use this Map for its exits, instead of Arrays. This will also entail refactoring other classes accordingly, like Main.parse(String[], Player). Instead of accessing rooms by index, they have to use the get(String) method.

You could also probably refactor RoomManager to use a collection for its collection of Rooms, instead of a Room[]. This will make it slightly easier to add new rooms in the future.

Finish Home Tour

Instructions

Take this time to add any polish or finishing touches required for your Home Tour application.

  • Test it thoroughly, to make sure it works without crashing
  • Make sure you have enough rooms to make for an attractive final product.
  • Make sure all your exits are two directional:
    • If you go west from room A to room B, then going east from room B should return you to room A.
    • Unless your non-euclidian geometry is intentional...
  • Make sure any additional features you have added are complete and functional.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions