Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Displaying a 'map'(grid) for a simple game I'm making -- most efficient way to use loops/Array.

Hans Hovan
Ranch Hand
Posts: 31
EDIT: Rewrote most of this post because I figured parts of my question(s) out.

Hello, I'm in a beginning Java class and on my own time am trying to make a little dungeon-exploring-rpg type of game using what I've learned so far to help cement my understanding.

Basically the player moves around a 10x10 grid and different things happen as the player moves onto the various 'spaces'.

So that the player can keep track of where they currently are on the grid, I want to display a 'map' after each time the player moves. I will keep track of the player's location on the grid using height and width (of the grid) variables. Different spaces on the grid will be represented by different symbols.
For example: 'X' is where the player currently is, 'S' is where the store is, 'B' is where a boss is, '*' is an open space, etc. The spaces will be static except that where the player is currently located will be a 'X' instead of whatever else was there before (once the character moves to another space the original space will revert back to what is was before).

I started playing with the idea of just making the array chars (This example is just set up for a 3x3 grid since I'm trying to figure out what I'm doing first before diving in.

Something like this (the chars don't mean anything in particular right now):

Which prints out:

S*?
**B
**d

I would then need to replace one of those chars with a X to represent the player. I'm not really sure what would be the best way to implement that though. Store the location of the player on a coordinate grid and map each coordinate to an array element? So that if the player is on (0,2), assuming they started at (0,0) and moved right twice, that means they are at map[2] (which is a '?' in the above example) and it would temporarily be changed to a char of 'X' until the player moves again. So would it be feasible to do something along the lines of (this is just an example to how how it would work):

height=0
width=0

player moves right

width=width+1

if (height==0 && width==1)
player encounters stuff that I have set up for grid space (0,1)
and
tempSpace = map[1]
map[1] = 'X'
print out 'map'

player moved down

height=height+1

if (height==1 && width==1)
player encounters stuff that I have set up for grid space (1,1)
and
tempSpace2 = map[5]
map[1] = tempSpace
map[5] = 'X'
print out 'map'

And so on. There would have to be if statements for every possible combination of of height and width (all of the different grid spaces) each time the player moved. Would this be a decent way of doing this? Or would using an array of integers be better for some reason? Maybe set every element of the array equal to some number and have if statements while printing out the map that turn numbers into the desired symbol?

Anyway, sorry for the ramble...if you were brave enough to read all of this I would appreciate any insight or suggestions. Just trying to figure out the right/best way to do stuff. Note: Remember I'm basically a Java 101 knowledge-level. I've learned about loops, methods, classes, and am just getting into arrays. I don't know much advanced/fancy stuff yet so relatively simple is better.
Thanks much for the help.

Tony Docherty
Bartender
Posts: 2957
59
• 1
Something like this (the chars don't mean anything in particular right now):

You may be better off using an array of arrays to store your 2D grid and then use nested loops to iterate over it.
Java doesn't support multi-dimensional arrays but it does support arrays of arrays which is more flexible. It can simulate a 2D array very easily ie
I would then need to replace one of those chars with a X to represent the player.

You could do this in a few different ways for example:
1. As you suggest you could move the existing value into a temp variable and put your X in the grid. You are recording the x, y location of the player so before each players move you replace the current X with the temp value and set the temp variable to the value of the players next location.
2. You could leave the grid as is and in your drawing routine just check to see if the location you are about to draw is the players location and if so draw an X rather than what is in the grid.

Hans Hovan
Ranch Hand
Posts: 31
Thanks for the suggestions/help! Greatly appreciated.

At the time I didn't know there was such a thing as a 2-d array (array within an array). Just learned more about it in class today!