IZI_Shadow_IZI
IZI_Shadow_IZI

Reputation: 1931

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

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 ;(

Upvotes: 3

Views: 9104

Answers (3)

Alvaro Cavalcanti
Alvaro Cavalcanti

Reputation: 3078

Based on your comment on your original question I understand that it's a matter of semantics.

From the player's point of view, the board isn't a matrix, as you implied, it's merely a straight line, that happens to "teleport" a player after the last position (16 * 4 = 64) to the first position.

And from the GameEngine point of view is a matter of converting the position on the line to a cell on the border of the matrix.

Thus, condider that your GamePiece object to have a position property, initialized with 0. The BoardGame has the property boardSide initialized with 16 and another property boardLength that's equals boardSide * 4.

Now, everytime your player tries to move you must ensure that it does not "fall off" the board, and then position it properly onscreen.

The code would be just like this:

// When updating the game state
private void move(GamePiece piece, int spaces) {
    int destination = piece.position + spaces;
    if (destination >= BoardGame.boardLength) {
        destination -= BoardGame.boardLength;
    }
    piece.position = destination;
}

// When updating the game view
private void updateView() {
    (...)
    // Considering you store the game pieces on the gamePieces collection
    for (GamePiece p: gamePieces) {
        int side = Math.floor(p.position % BoardGame.boardSide); // Either 0, 1, 2 or 3
        switch (side) {
            case 0: // Top
                // Place Piece method takes: the piece, the X position and the Y position
                BoardGame.placePiece(p, BoardGame.boardSide - p.position, 0);
                break;
            case 1: // Right

                break;
            case 2: // Bottom

                break;
            case 3: // Left
                break;
        }
    }
    (...)
}

P.S.: I'm much in a hurry right now and can't finish the code properly. Hope this helps, but I'll come back later and try to finish it.

Upvotes: 1

Mash
Mash

Reputation: 1349

You could hold the position of the player(s) in 2 variables, let's say x and y.

If it's a circular board game (from what I understand it is), you could have a function

advance_one_space() { //let's suppose it turns clockwise and your array is board[y][x]
    if ( y==0 && x<15 ) 
        x++;
    else if ( y<15 && x==15 ) 
        y++;
    else if ( y==15 && x>0 ) 
        x--;
    else if ( y>0 && x==0 ) 
        y--;
}

Calling this function 60 times would make the play make one turn, and maybe recieve 200$ :-)

And if you want to advance n spaces:

advance(n) {
    advance_one_space();
    if (n>1)
        advance(n-1);
}

Upvotes: 2

Kon
Kon

Reputation: 10810

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

Upvotes: 2

Related Questions