Creating a grid in Java

Soldato
Joined
17 Apr 2003
Posts
6,518
Location
hOme
Hey guys

i'm trying to create a grid of squares in java, not a visual grid but which can be used so that each square can store an individual symbol or character and you can find out the co-ordinates of the symbol at any time

i'm imagining you need to use some sort of array, but all the information i can find on the interweb is only telling me how to draw a grid, when all i really need is a list of co-ordinates for each square in the grid if that makes sense

any ideas? i can get my head around how its logically supposed to be done (you enter the X value for the array, and the Y value) but im stumped as to you actually go to start it :(

cheers guys
 
its basically a grid for a maze game so you need to be able to insert objects (just symbols) at set locations, while other objects need to appear randomly

the program needs to be able to read where any symbol is in the array and print it out to the user too

the program needs to see it as a physical grid like a chessboard although no GUI is needed, i was thinking of a 2D array too with a basic X value and Y value

i know what needs to be and i can see it logically in my head but just cant apply it:(
 
right so each position in the grid would be stored as an integer number, so thats 1681 squares i think (41*41)

i'm trying to get my head around your code, so you'd do each row at a time? How would you be able to figure out the X and Y co-ordinates of a specific position (integer)?
 
this may sound stupid but bear with me:

{1, 3, 7, 8, 8, 9, 12}

the numbers there are just random yeah? so mine would go:

{1,2,3,4,5,6,7,8,9} all the way up to 41, then the next row would start on 42 and so on?

thanks :)
 
i'm presuming it would be like this:

Code:
        int[][] array2D = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 (all the way up to 41},
                         {42,43,44,45,46,47,48,48 etc etc};

all the way up to 1681 :)
 
right guys im getting the hang of this now, one thing though, how do i show the position of a specific symbol in the array? for example i have set the position of the letter P at [20,20] but what code do i need to actually print this out so the user can see where it is?

thanks
 
i was using one java file and each class as a public void called by public static void main(String[]args) at the beginning of the program, i've got a feeling i should be using seperate java files for classes...:(
 
Hard to understand what you mean there because classes don't have return types.

Usually you would have 1 class in it's own file (there are, as always, exceptions to this rule). Without trying to patronise, I think you have 1 class with several methods in? Which is certainly one way of doing things but you lose all the benefits of OO design then.

I would have a separate class for Location and one for Grid (probably one to launch the program too), but without knowing the full context of the problem it is hard to say.


yes i have 1 class with lots of methods but would prefer to use lots of classes linked together

i'm just not sure how to link the classes together, i keep getting errors when i copy whatevers in a method to a new class...:(

gah i couldnt add one one you guys to msn could i?...:(
 
hey guys

im really having a lot of trouble with this, i probably should have done the easier java module :(

i need to allow the user to move the position of the player on the grid (letter P) using the keyboard, i have the keyboard class and presume i need to make the class recognise an ASCII input to change the co-ordinates of the player by 1 position left for example. would it use an if else statement to do the 4 directions?
 
i have the keyboard class which i could use and just if ascii = whatever W is then move player 1 position up

it would be better off using the readChar part of the kb class than the readString yeah?
 
Back
Top Bottom