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!

Recommended Answers

All 5 Replies

public boolean isNorth(Location c)
{
  if(north!=null)
    return true;
  else
    return false;
}

... is a long version of

public boolean isNorth() {
   return north!=null;
}

ie (1) north!=null is a boolean expression, so you don't need to convert it to true or false
and (2) since you don't use the parameter c there's no need to have it (but maybe this is a design mistake?)

what to do next.

Can you explain? Does the program work the way you want?

it does not work. Every choice is an invalid chcoice and the location of the room is just referenced type , not string.

Add some printlns to print out the values of the variables used to make the decisions to see why the code is not doing what you want.

the location of the room is just referenced type , not string.

Can you explain and show an example?

It's not working because your calls to location.isXX(currentLocation); will always return false, location has no neighboring Locations and isXX() checks it instead of current location because as JamesCherrill pointed out, the parameter is never used

replacing location with currentLocation should get it working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.