In that game, the user played a ghost who moved around a house using a text inte
ID: 3751748 • Letter: I
Question
In that game, the user played a ghost who moved around a house using a text interface, typing commands like “north” to go to the room north of their current position. Rooms can contain characters of different kinds (player, adult, child), and items which can be manipulated in certain ways (shake, possess, throw). The users goal is to scare characters out of the house by continually manipulating objects.
In this assignment you will design a simple GUI level editor for some aspects of the game. Specifically:
Create a JFrame for your program. Be sure to give your JFrame a title and initial size. The frame will contain a JPanel as described below.
Create a JPanel on which the user will build the house. Draw a 5×5 grid, where each space represents a potential room location (we’ll assume rooms are always only one grid square in size).
Create a Room class which contains two String fields – name and description – and an appropriate constructor.
Create a two dimensional array, the same size as the grid, to store your Rooms in.
When users click an unoccupied grid square, open a new JFrame which allows the user to enter the room name and description. Using the entered data, create a new Room object and store it in the appropriate place in the Room array.
When a square is occupied by a Room, indicate visually that the space is occupied on the grid by printing the room name in the appropriate location. You may choose to do more advanced drawing as long as the name is still printed. When drawing each grid space, you can check to see if the array contains an element at that location to determine how to draw it.
you don’t need to build the game
Explanation / Answer
class
public class Location
{
private String description;
private Location north;
private Location south;
private Location east;
private Location west;
public Location(String newDescription)
{
description=newDescription;
}
public Location()
{
}
public String getDescription()
{
return description;
}
public Location getNorth()
{
return north;
}
public Location getSouth()
{
return south;
}
public Location getEast()
{
return east;
}
public Location getWest()
{
return west;
}
public void setNorth (Location newNorth)
{
north=newNorth;
}
public void setSouth (Location newSouth)
{
south=newSouth;
}
public void setEast (Location newEast)
{
east=newEast;
}
public void setWest (Location newWest)
{
west=newWest;
}
public boolean isNorth(Location c)
{
if(north!=null)
return true;
else
return false;
}
public boolean isSouth(Location c)
{
if(south!=null)
return true;
else
return false;
}
public boolean isEast(Location c)
{
if(east!=null)
return true;
else
return false;
}
public boolean isWest(Location c)
{
if(west!=null)
return true;
else
return false;
}
}
driver
import edu.colorado.graphs.Graph;
import java.util.Scanner;
public class Project5
{
public static void main(String[] args)
{
System.out.println("Welcome to the Haunted House!");
Location location= new Location();
Location frontFoyer = new Location("Front Foyer");
Location backFoyer = new Location("Back Foyer");
Location diningRoom = new Location("Dining Room");
Location pantry = new Location("Pantry");
Location living = new Location("Living Room");
Location study = new Location("Study");
Location kitchen = new Location("Kitchen");
frontFoyer.setNorth(backFoyer);
frontFoyer.setEast(living);
frontFoyer.setWest(diningRoom);
living.setWest(frontFoyer);
diningRoom.setNorth(pantry);
diningRoom.setEast(frontFoyer);
pantry.setNorth(kitchen);
pantry.setSouth(diningRoom);
kitchen.setSouth(pantry);
kitchen.setEast(backFoyer);
backFoyer.setSouth(frontFoyer);
backFoyer.setEast(study);
backFoyer.setWest(kitchen);
study.setWest(backFoyer);
Location currentLocation;
currentLocation= frontFoyer;
String choice;
do
{
System.out.println("You are in:" + currentLocation);
System.out.println("Possible directions are: " );
if (location.isNorth(currentLocation))
System.out.print(location.getNorth());
if (location.isSouth(currentLocation))
System.out.print(location.getSouth());
if (location.isEast(currentLocation))
System.out.print(location.getEast());
if (location.isWest(currentLocation))
System.out.print(location.getWest());
System.out.print("Where would you like to go? ");
Scanner input= new Scanner(System.in);
choice = input.nextLine();
if ( choice.equalsIgnoreCase("N") && location.isNorth(currentLocation))
{
currentLocation=location.getNorth();
}
else if ( choice.equalsIgnoreCase("S") && location.isSouth(currentLocation))
{
currentLocation=location.getSouth();
}
else if ( choice.equalsIgnoreCase("E") && location.isEast(currentLocation))
{
currentLocation=location.getEast();
}
else if ( choice.equalsIgnoreCase("W") && location.isWest(currentLocation))
{
currentLocation=location.getWest();
}
else if ( choice.equalsIgnoreCase("A") )
{
System.out.println("Coward!");
}
else
System.out.println("Invalid Choice");
}while(!choice.equalsIgnoreCase("A"));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.