Best way to move around a 2d Array (Board Game)

IZI_Shadow_IZI picture IZI_Shadow_IZI · Jul 11, 2013 · Viewed 7.8k times · Source

I am curious about what would be the easiest way to move around a 2d array in Java? I created a 2d array for a game board and use that to keep track of the positions of game pieces. So if I have a 16x16 2d array how do I make it so players can move around the board X number of spaces.

I know the pieces would move from:

[0][0] -> [0][16] - TOP

then

[0][16] -> [16][16] - RIGHT SIDE

then

[16][16] -> [16][0] - BOTTOM

then finally back to the home space being [0][0].

[16][0] -> [0][0] - LEFT SIDE

Any help or suggestions would be greatly appreciated!

EDIT: I wish I could accept multiple correct answers.... sigh ;(

Answer

Kon picture Kon · Jul 11, 2013

Based on your comment, you could implement your restriction on movement with the following pseudocode. I'm not 100% sure if this is what you want, but hopefully it's somewhat helpful

if (user tries to move right) {
    if (posX < 15 && posY == 0)
         //move right
    else
         //don't
}

if (user tries to move left) {
    if (posX > 0 && posY == 15)
         //move left
    else
         //don't
}

And so on. Is this similar to what you're looking for? Restrictions on array traversal? I'm assuming that your board only allows movement on the boundaries of the array based on what you say, so the following O positions are legal, and X are illegal:

OOOOOOOOOOOOOOO
OXXXXXXXXXXXXXO
OXXXXXXXXXXXXXO
...
OXXXXXXXXXXXXXO
OOOOOOOOOOOOOOO