Text based Game

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1985 Views - Last Post: 11 October 2012 - 08:17 AM Rate Topic: -----

#1 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Text based Game

Posted 07 October 2012 - 07:39 AM

So I am trying to do the HunttheWumpus game. Using the Cave API.
Now so far I have writtin this:
public class caveLayout extends java.lang.Object{
	/**
	 * 
	 */
	private String name;
	private int caveNumber;
	private int adj1;
	private int adj2;
	private int adj3;
	private boolean visited = false;
	public caveLayout(String name, int caveNumber, int adj1, int adj2, int adj3) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.caveNumber = caveNumber;
		this.adj1 = adj1;
		this.adj2 = adj2;
		this.adj3 = adj3;
	}
	public String getCaveName(){
		return name;
	}
	public int getCaveNumber(){
		return caveNumber;
	}
	public int getAdjNumber(int room){
		return 0;
	}
	public boolean hasBeenVisited(){
		return visited;
	}
	public void setContents(){
		
	}
	public void markAsVisited(){
		
	}

}


There are a few things that have puzzled me, example, how to determine is a location has been visited and how to get the adjecte from from a string of 4 ints (more like how to pass over the first one and proceed to the next)

Is This A Good Question/Topic? 0
  • +

Replies To: Text based Game

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Text based Game

Posted 07 October 2012 - 09:11 AM

I have never made a text game with the Cave API.
However I'm slightly confused...Are you using the API or rewriting it?

From the looks of this though. You would need a method that you can call anytime the player enters that specific area that will make visited equal true.

**HINT HINT markAsVisited look like that method WINK WINK**

As for your other problem. I haven no clue what you mean by "get the adjecte"

This post has been edited by Kinaces: 07 October 2012 - 09:13 AM

Was This Post Helpful? 0
  • +
  • -

#3 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Text based Game

Posted 07 October 2012 - 10:31 AM

Ok, a better explanation:
So far currentLocation() within gamePlay.java is giving me an error with the return statement. How do you implement the shootArrow() properly and how to implement the getAdjNumber() of caves located in the caveLayout.java.
My program is reading lines from a file which I included. I havent gotten to the remaining parts but no biggie.
import java.io.*; // import for scanner and reading file
import java.io.File;
import java.util.*;
public class gamePlay {
	protected File gameFile = new File("layout.txt");
	private caveLayout[] caves;
	private caveLayout currentCave;
	private boolean alive;
	
	public gamePlay() throws FileNotFoundException{
		// TODO
		Scanner input = new Scanner(gameFile);
		int numofCave = input.nextInt();
		this.caves = new caveLayout[numofCave];
		
		for(int i = 0; i < numofCave; i++){
			int caveNumber = input.nextInt();
			int adj1 = input.nextInt();
			int adj2 = input.nextInt();
			int adj3 = input.nextInt();
			String room = input.nextLine().trim();
			this.caves[caveNumber] = new caveLayout(room, caveNumber, adj1, adj2, adj3);
		}
		this.alive = true;
		this.currentCave = this.caves[0];
		this.currentCave.markAsVisited();
	}
	public void getFile() throws FileNotFoundException{
		// FileNotFoundException should the file be missing
		Scanner input = new Scanner(gameFile);
		while(input.hasNext()){
			String result = input.nextLine(); //prints the next line
			System.out.println(result);
		} input.close();
	}
	/* Moves the player from the current room to the next specified room.
	 * The new location must be marked as visited (markAsVisited()) and
	 * the next appropriate action taken if the new location is not an
	 * empty room.
	 * @param room then number of the room to the traversed (1-3)
	 */
	public String playerMove(int room){
		this.currentCave = this.caves[this.currentCave.getAdjNumber(room)];
		this.currentCave.markAsVisited();
		return "OK";
	}
	/* Shoots an arrow (if the player has any) through the specified tunnel.
	 * If there is a wumpus, it is killed, however if the wumpus is in another
	 * tunnel, it will be alerted to move.
	 * @param room the number of the room to be shot (1-3)
	 */
	public String shootArrow(int room){
		return "NOT YET IMPLEMENTED";
	}
	/* Displays the current room (cave) and names of the adjacent caves (if
	 * those caves have been visited). Caves that have not yet been visited 
	 * are displayed as "unknown". Also a warning message is displayed if the
	 * player is adjacent to a wumpus, bats, or a pit.
	 */
	public String currentLocation(){
		String message = "You are currently in " + this.currentCave.getCaveName();
		for(int i = 1; 1 <= 3; i++){
			caveLayout adjCave = this.caves[this.currentCave.getAdjNumber(i)];
			if(adjCave.hasBeenVisited()){
				message += "\n  (" + i + ") " + adjCave.getCaveName(); 
			}
			else{
				message += "\n  (" + i + ") unknown";
			}	
		}
		return message;	
	}
	/* Reports if the player is still alive.
	 * @return true if alive, false otherwise
	 */
	public boolean stillAlive(){
		return this.alive;
	}
	/* Reports if there is still a wumpus remaining
	 * @return true if wumpus remaining in the cave, false otherwise
	 */
	public boolean hasWumpus(){
		return true;
	}
}


public class caveLayout extends java.lang.Object{
	/**
	 * 
	 */
	private int caveNumber;
	private String room;
	private int adj1;
	private int adj2;
	private int adj3;
	private boolean visited = false;
	public caveLayout(String room, int caveNumber, int adj1, int adj2, int adj3) {
		// TODO Auto-generated constructor stub
		this.room = room;
		this.caveNumber = caveNumber;
		this.adj1 = adj1;
		this.adj2 = adj2;
		this.adj3 = adj3;
	}
	public String getCaveName(){
		return room;
	}
	public int getCaveNumber(){
		return caveNumber;
	}
	public int getAdjNumber(int room){
		return 0;
	}
	public boolean hasBeenVisited(){
		return visited;
	}
	public void setContents(){
		
	}
	public void markAsVisited(){
		visited = true;		
	}

}


import java.util.*;
import java.io.FileNotFoundException;
public class wurmGame {

	/**
	 * @param args
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		gamePlay player =  new gamePlay();
		System.out.println("Welcome to **Hunt The Wumpus!**");
		System.out.println("To move to an adjacent cave, enter 'M' and the cave number.");
        System.out.println("To shoot an arrow into a cave, enter 'S' and the cave number.");
        Scanner playerAction = new Scanner(System.in);
        while(player.stillAlive() && player.hasWumpus()){
        	System.out.println(player.currentLocation());
        	
        	String actionTaken = playerAction.next();
        	int target = playerAction.nextInt();
        	if(actionTaken.toLowerCase().charAt(0)) == 'm' && target >=1 && target <= 3){
        		System.out.println(player.shootArrow(target));
        	}
        	else if(actionTaken.toLowerCase().charAt(0) == 'm' && target >= 1 && target <= 3){
        		System.out.println(player.playerMove(target));
        	}
        	else{
        		System.out.print("Unrecognized Command -- please try again.");
        	}
        }
        System.out.println("GAME OVER");  
	}
}


10
1 2 6 10 Dark 
A wooden sign reads "Beware of the Wumpus!"
2 1 3 7 Bubble 
There is a black pool of water in the corner.
3 2 4 8 Light 
You see a Tyrannosaurus Rex fossil embedded in the wall.
4 3 5 9 Stinky 
There is an empty Diet Rite can here.
5 4 6 10 Fresh
You almost step on a broken cellular phone.
6 1 5 7 Tasty
A couple of evil rats stare at you from under a pile of rocks.
7 2 6 8 Lovely
You find a Spanish doubloon on the floor.
8 3 7 9 Freaky
The ceiling is very low and you have to stoop.
9 4 8 10 Insane
You step on a slippery spot, slip, and fall on your keester.
10 1 5 9 Crazy
You get a strong sense of deja vu.


Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Text based Game

Posted 07 October 2012 - 10:52 AM

Well I can't help you unless you tell me what the error says, and what line is going wrong.
Was This Post Helpful? 0
  • +
  • -

#5 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Text based Game

Posted 07 October 2012 - 11:40 AM

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at gamePlay.<init>(gamePlay.java:25)
at wurmGame.main(wurmGame.java:20)
Was This Post Helpful? 0
  • +
  • -

#6 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Text based Game

Posted 07 October 2012 - 12:03 PM

You have the scanner reading an int and then a String. You need to empty the scanner before you can do this.
Was This Post Helpful? 0
  • +
  • -

#7 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Text based Game

Posted 07 October 2012 - 12:28 PM

would that be using a close(), reset() after every declaration? My problem is the txt file has a seriels of numbers on the same line. wouldn't it pick up on the same number again.

This post has been edited by kendel: 07 October 2012 - 12:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Text based Game

Posted 07 October 2012 - 04:58 PM

I have tried multiple sulutions but none works, I dont actually access the file until called, and the main is just asking for the user input.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Text based Game

Posted 08 October 2012 - 01:40 PM

View PostKinaces, on 07 October 2012 - 03:03 PM, said:

You have the scanner reading an int and then a String. You need to empty the scanner before you can do this.

What ???
int x = scanner.nextInt();
String str = scanner.next();
should perfectly work. What do you have in mind by empty a Scanner ?
You should have only one Scanner assign to System.in in any application and you should never have to reset/empty it
Was This Post Helpful? 0
  • +
  • -

#10 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Text based Game

Posted 08 October 2012 - 04:10 PM

It is probably my confused explanation. Take a look at the code below, I made changes and changed the name just to be more specific. Right now it works when I do not add any form of loops. The last portion of it that has been commented out causes an error I will include below. The idea is so it reads the file I listed above and picks out the int, then i have to create another variable that will read all the strings. (the System.out.println() was used to verify that I'm getting an result.
public void newGame() throws FileNotFoundException{
		// TODO
		Scanner input = new Scanner(gameFile);
		int count = 0;
		int numofCave = input.nextInt();
		int caveNumber, adj1, adj2, adj3;
		
		caveNumber = input.nextInt();
		adj1 = input.nextInt();
		adj2 = input.nextInt();
		adj3 = input.nextInt();
		System.out.println(" " + caveNumber + " " + adj1 + " " + adj2 + " " + adj3);
		//this.caves[caveNumber] = new caveLayout(caveNumber, adj1, adj2, adj3);
		//this.alive = true;
		//this.currentCave = this.caves[0];
		//this.currentCave.markAsVisited();


The Error:
To move to an adjacent cave, enter 'M' and the cave number.
To shoot an arrow into a cave, enter 'S' and the cave number.
1 2 6 10
Exception in thread "main" java.lang.NullPointerException
at gamePlay.newGame(gamePlay.java:32)
at wurmGame.main(wurmGame.java:25)


It does work up until that point, and even without that, when I try to use a for loop to read the rest of the txt file I get another error.
Was This Post Helpful? 0
  • +
  • -

#11 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Text based Game

Posted 08 October 2012 - 08:13 PM

View Postpbl, on 08 October 2012 - 01:40 PM, said:

View PostKinaces, on 07 October 2012 - 03:03 PM, said:

You have the scanner reading an int and then a String. You need to empty the scanner before you can do this.

What ???
int x = scanner.nextInt();
String str = scanner.next();
should perfectly work. What do you have in mind by empty a Scanner ?
You should have only one Scanner assign to System.in in any application and you should never have to reset/empty it


I did word that in a terrible way. However this solution could potentially cause a problem.

Which line exactly are you receiving the error?

This post has been edited by Kinaces: 08 October 2012 - 08:25 PM

Was This Post Helpful? 0
  • +
  • -

#12 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Text based Game

Posted 09 October 2012 - 05:10 PM

I am not able to get the remaining int from the txt file. it has to do with how it is written but I cant seem to write it in a better way to keep reading int from the file because it then gives a Mismatch error. right now this is how it looks and I attempted to use a for loop as well same result.
	public void readCave() throws FileNotFoundException{
		Scanner input = new Scanner(fileName);
		int numofCaves;
		int caveRoom;
		int adj1;
		int adj2;
		int adj3;
		int count = 0;
		numofCaves = input.nextInt();
		while(input.hasNextLine()){
			caveRoom = input.nextInt();
			adj1 = input.nextInt();
			adj2 = input.nextInt();
			adj3 = input.nextInt();
			input.nextLine();
			count++;
			System.out.println(""+numofCaves+" -- "+caveRoom+" "+adj1+" "+adj2+" "+adj3+"\n");
		} 
		input.close();
	}


Was This Post Helpful? 0
  • +
  • -

#13 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: Text based Game

Posted 09 October 2012 - 05:20 PM

I must ask. What is the point of putting all these attributes in a text file? It seems like these are attributes in a class that you can define with objects.
Was This Post Helpful? 0
  • +
  • -

#14 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Text based Game

Posted 10 October 2012 - 11:44 AM

How do you fix this error?
Exception in thread "main" java.lang.NullPointerException
at gameFile.<init>(gameFile.java:26)
at gameMain.main(gameMain.java:15)

The Output I am going for needs to be something along the lines of Room 1 = (2, 6, 10);

public class gameFile {
	protected File layoutFile = new File("layout.txt");
	private gameMap caves;
	public gameFile() throws FileNotFoundException{
		Scanner input = new Scanner(layoutFile);
		int numofCaves;
		int caveRoom;
		int adj1;
		int adj2;
		int adj3;
		int count = 0;
		numofCaves = input.nextInt();
		while(count < numofCaves){
			caveRoom = input.nextInt();
			adj1 = input.nextInt();
			adj2 = input.nextInt();
			adj3 = input.nextInt();
			this.caves.storeCave(caveRoom, adj1, adj2, adj3);
			count++;
		} 
		input.close();
	}


	public void storeCave(int caveRoom, int adj1, int adj2, int adj3){
		this.caves.put(Integer.toString(caveRoom), adj1);
		this.caves.put(Integer.toString(caveRoom), adj2);
		this.caves.put(Integer.toString(caveRoom), adj3);
		
	}



This is all I have in Main so far:
	public static void main(String[] args) throws FileNotFoundException {
		// TODO Auto-generated method stub
		gameFile p = new gameFile();
	}


This post has been edited by kendel: 10 October 2012 - 11:51 AM

Was This Post Helpful? 0
  • +
  • -

#15 kendel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 04-October 08

Re: Text based Game

Posted 10 October 2012 - 02:55 PM

OK I solved this part... (so much stress) but now I'm on this silly thing:

	public int getNumofCaves() {
		System.out.println(numofCaves);
		return numofCaves;  // The return value is 0
	}
	public void setNumofCaves(int numofCaves) {
		this.numofCaves = numofCaves;
		System.out.println(numofCaves); // The return value is 10
	}



Anyone has any ideas why I am getting two seperate results and this is the only part this problem is called. except the declaration of "private int numofCaves;"

This post has been edited by kendel: 10 October 2012 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2