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]; } }
Basic text based game
Page 1 of 13 Replies - 1605 Views - Last Post: 29 May 2012 - 08:03 AM
#1
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.
Replies To: Basic text based game
#2
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?
#3
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;
Point p = new Point();
p.x = 123;
p.y = 456;
#4
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:
It's not conventional, but I wouldn't condemn it. It might give you some nice array practice.
(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
Page 1 of 1