Hi guys,
I was supposed to create a program that simulates a haunted house.
the output was supposed to be like:
Welcome to the Haunted House!
You are in: Front Foyer
Possible directions are: North(N), East(E), West(W), Abandon(A)
Where would you like to go? s
Invalid Command
You are in: Front Foyer
Possible directions are: North(N), East(E), West(W), Abandon(A)
Where would you like to go? e
You are in: Living Room
Possible directions are: West(W), Abandon(A)
Where would you like to go? s
Invalid Command
You are in: Living Room..........................
I created a location class and a driver file according to the instructions
I just need someone to debug my programs and help me. both compile but I am getting confused on what to do next.
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:
// Programmed by:
// Date Written: 5/9/2012
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"));
}
}
Thanks for looking!