If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Threaded View

Using array for x and y coordinates?

Hey guys, I have recently been trying to redo a game I made a while back to make the code more efficient and readable, so that people looking at the source actually know what their looking at. So, I'm redoing a few parts that I did in very inefficient, bulky ways, and replacing those methods with newer compact ones.

Now, thats been working just great, until I tried to replace the way I used to do x and y coordinates with an array. The way the game used to work was that when the user typed in a command like "north" or "west", the program would add 1 to the y and subtract 1 from the x, respectively. Now, to reduce possible bugs with using the x and y directly, I assigned a number to each spot, called z, with a very long, complicated mess of code that looked like this:

Now because of how unbelievably horribly long this was, I ended up just replacing all that code with an equation:

Code:

z = ( ( 5 - y ) * 5 ) + x;

This works just fine, however, I do want to just get rid of the x, y, and z all together and replace it with a 2-dimensional array. Since I don't have much experience with arrays, however, I need a bit of help.

What I want the array to do is have each of the 25 squares numbered using a slightly modified version of the above equation (to compensate for the fact that it goes from 0-4 instead of 1-5). But then, I also want to have some kind of way to be able to store the location of the player inside of one of the squares in the array and then have the program be able to compare the location of the player in an if statement, in something like this:

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.