4 Replies - 600 Views - Last Post: 08 August 2012 - 01:05 PM Rate Topic: -----

#1 eekie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 12

Question on implementing a double linked list in a text game

Posted 08 August 2012 - 08:58 AM

I am making a text-based haunted house game using Java. The main format is based off of one I had found on another website and decided to use since it would be my first time creating something like this.

However, I am wondering how exactly a double linked list would be able to be implemented to make moving from room to room a bit easier? It seems like it would make my code look much cleaner and stop my headache. Could someone explain to me how I would use it in concurrence with this code? Or maybe point me in the direction of a good example for double linked lists? Thank you.

import java.util.Scanner;
public class HauntedMansion {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String choice;
		@SuppressWarnings("resource")
		Scanner user_in = new Scanner(System.in);//This creates an 'opening' for user input
		
		System.out.println("Welcome to the Haunted Mansion. You see an expansive staircase ahead of you and rooms to your left and right."+"\n"+"Type 'upstairs' and hit enter to go up the staircase, 'right' to go to the room on your right,"+"\n"+"or 'left to go to the room on your left.");
		choice = user_in.nextLine();//this allows the user to input and continue on
		
		if(choice.equals("upstairs")){
			System.out.println("You are now upstairs."+"\n"+"There is a musky and dark hallway to your left and a door in front of you."+"\n"+"Type 'left' and hit enter to go down the hallway or 'door' to open the door in front of you.");
			choice = user_in.nextLine();
			
			if(choice.equals("left")){
				System.out.println("As you turn to go down the hallway, you notice a dim light floating at the end of the hallway. The rest of the hall is nearly black."+"\n"+" With each step, the floorboard creaks and dust falls from the beams overhead."+"\n"+"The dim light grows brighter the closer you get to it."+"\n"+"'continue' to go on or 'back' to return to the staircase.");
				choice = user_in.nextLine();
				if(choice.equals("continue")){
					System.out.println("The glowing light appears to be coming from a cellphone on the floor."+"\n"+"Pick it up? 'Y' or 'N'");
					choice = user_in.nextLine();
					if(choice.equals("Y")){
						System.out.println("There is a new text message on the cellphone.");
						choice = user_in.nextLine();
					}else if(choice.equals("N")){
						System.out.println("The cellphone keeps flashing that there is a new message. You notice that the battery is nearly empty. This could serve as a good light if you could find a charge for it. You pick it up anyways. Do you read the message?"+"\n"+"'Y' or 'N'");
						choice = user_in.nextLine();
					}
				}else if(choice.equals("back")){
					System.out.println("As you turn your back to the light you feel a cold breeze flow through the room. A clammy hand wraps itself around your neck..."+"\n"+"GAME OVER");
				}
			}
			else if(choice.equals("door")){
				System.out.println("You reach your hand out and turn the door handle. There is a slight rattle as you push open the door.");
			}
		}
		else if(choice.equals("right")){
			System.out.println("You are now in the kitchen.");
		}
		else if(choice.equals("left")){
			System.out.println("You are now in the dining room");
		}
		else{
			System.out.println("That is not a valid answer.");
		}
		
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Question on implementing a double linked list in a text game

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10176
  • View blog
  • Posts: 37,571
  • Joined: 27-December 08

Re: Question on implementing a double linked list in a text game

Posted 08 August 2012 - 09:01 AM

If you want more than previous and next adjacent rooms, you will want to use a Graph data structure to manage this. If your house is laid out in a grid, you can use a two-dimensional array to organize this. You could design a Room class and manage a Room[][]. Then just track the row and column indices of the current room you are in, and use that to navigate to other rooms.
Was This Post Helpful? 2
  • +
  • -

#3 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 533
  • View blog
  • Posts: 1,186
  • Joined: 24-April 12

Re: Question on implementing a double linked list in a text game

Posted 08 August 2012 - 10:47 AM

I think MacOsxNerd101 has got you pointed in the right direction there. A linked list, let alone a doubly linked list is a bit ambitious for a text adventure game. And maybe not even a good choice for this. I would think that the only reason you would do it as a doubly linked list would be just as an academic exercise to learn about linked lists.

But if you were going to do it, you build a linked list out of dynamically allocated structures. The structure has at least 3 elements: the data, a forward pointing address, and a backwards pointing address. You follow those addresses to find the next element in memory. It's "doubly" linked because you have forwards and backwards paths, rather than only a forward path (which is what a linked list has).

You also need a head and a tail. When you reach the head or tail you know that you have reached the end of the list. It's been a really long time since I've written a data structure like this, but I believe you put the head and the tail in a specific fixed memory location. Regardless, you might use a null value in either the forward or backward address to mark that element as the head or tail.


And that's basically how it's done. The value of a linked list is that you dynamically allocate memory from the heap as needed. (Which means you need to deallocate those elements from the heap when no longer needed and fix the chain to not have any breaks after you remove it from the chain.) Compare that to something like an array where you just allocate all elements from the heap when you create the array. A true array can't grow once allocated. A linked list can grow forever until you run out of memory on the heap.

A linked list only allows you to move along the chain in one direction. So, if you need to get the data element at the tail end you have to start at the head and read all the way to the end to read the data from that element.

A doubly linked list has the advantage of providing both a forward path (address) and a backwards path (address). So, with a doubley linked list you can start at either the head or the tail to look for the data element you want.
Was This Post Helpful? 3
  • +
  • -

#4 eekie  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 08-August 12

Re: Question on implementing a double linked list in a text game

Posted 08 August 2012 - 12:56 PM

Thank you both very much for the advice!
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7283
  • View blog
  • Posts: 12,079
  • Joined: 19-March 11

Re: Question on implementing a double linked list in a text game

Posted 08 August 2012 - 01:05 PM

I'd suggest you don't tie yourself to a grid structure. Each room has a HashMap of <String, Room> indicating its exits. To go to a Room, the user enters the associated String and you do a lookup in the exits map. If the string isn't in the keyset (ie, if the lookup returns null) you complain and ask them to try again.

Now your rooms are a graph structure, simple like pie, and you don't have to maintain a separate array.

This post has been edited by jon.kiparsky: 08 August 2012 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1