3 Replies - 895 Views - Last Post: 29 May 2012 - 08:03 AM Rate Topic: -----

#1 Weigle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-May 12

Basic text based game

Posted 28 May 2012 - 05:19 PM

Well, hello people. This is my first post ever ever. Anyway, this is probably a stupid question that some experienced coders could very laugh at but anyway, I'm trying to make a text based game. The idea is you enter a command telling what direction to head in order to find new quests and such. The part I'm stuck on is the navigation system. I'm trying to store the x and y values in an array and retrieve that data into the main class to test it. I can't get past that part. Here is the code doing most of the work.
 import java.util.Scanner;

public class navigation {

	public void contain(){
		
		location();
		xy();
		
		
	}
	
public Scanner input = new Scanner(System.in);	
	public int location(){
		int choice;
		System.out.println("choices 1 2 3 4");
		choice = input.nextInt();
		return choice;
		
		
	}
	
	public int xy(){
		int choosen;
		choosen = location();
		int xy[] = new int[1];
		switch(choosen){
		double test;
		
		case 1:
			xy[0] = +1;
			break;
		
		case 2:
			xy[0] = -1;
			break;
			
		case 3:
			xy[1] = +1;
			break;
			
			case 4:
				xy[1] = -1;
		
		
		}
		test = xy[0].xy[1];
		
		
		
	}
	
	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Basic text based game

#2 Ruben1123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 19-May 12

Re: Basic text based game

Posted 28 May 2012 - 07:19 PM

So you want to return the new location? Why not have it return an int[2] array with int[0] = x and int[1] = y?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

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

Re: Basic text based game

Posted 28 May 2012 - 07:22 PM

or a java.awt.Point

Point p = new Point();
p.x = 123;
p.y = 456;
Was This Post Helpful? 1
  • +
  • -

#4 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Basic text based game

Posted 29 May 2012 - 08:03 AM

navigation isn't a runnable class
(ie, public void main(String args[]) or a class that implements Runnable or extends Thread)
The rest of the statements aren't encapsulated in a class either.

That's a much larger issue than the one at hand, but there's a time for everything, I suppose.

This might be confusing, but int xy[] = new int[1] only contains the element xy[0]. There's no xy[1].

If you're creating a map for a non-linear textbased game (like ZORK, for instance), I would suggest a multidimensional array.

See Arrays

Feel free to make a second post if you require further assistance.

EDIT: If you're not confident with multidimensional arrays, you can mathematically wrap a single-dimensional array like so:

for(int d = 0; d < HEIGHT; d++)
{
	for(int i = 0; i < WIDTH; i++)
	{
		currentElement = array[(d * WIDTH) + i];
	}
}



It's not conventional, but I wouldn't condemn it. It might give you some nice array practice.

This post has been edited by Gungnir: 29 May 2012 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1