0

I am building a board game of sorts and currently dealing with player movement. I want to restrict the player so that they can move only within the game board itself. The board is stored in a nested array, the Actionscript 3 equivalent of a 2D array - like array[x][y]. I know the length, and I know the target I'm trying to find. All I need to do is identify if that target exists within the array, in order to confirm if the player can actually move to that slot, and return true or false. Can anyone offer any suggestions? This doesn't seem to be a very easy question.

Merlin
  • 725
  • 9
  • 23
  • 2
    I presume it's a matter of first checking the new x,y indices < the array length-1. Depending on what type your nested array contains, if the x,y indices are valid you might also want to check for null/positions – George Profenza Mar 28 '13 at 18:19
  • In some ways this kind of makes it sound like you're asking a very basic syntactical question, but I doubt you really are. Could you please elaborate on your question and what the specific problem is? – Panzercrisis Mar 28 '13 at 20:30

2 Answers2

0

Assuming your array looks like this:

var board:Array = [
    [a,0,0],
    [0,b,0],
    [0,0,c]
]

Then to check for the presence in the middle of the board would simply be

if (board[1][1] != 0) {
    trace(board[1][1]) // outputs "b"
}

So if you wanted to move b in any direction by one block, you might generically try...

function move(row:int, col:int, direction:string):void {
    switch (direction) {
        case "up":
            if (row - 1 >= 0 && row - 1 < board.length) {
                board[row-1][col] = board[row][col];
                board[row][col] = 0;
            }
            break;
        case "down":
            if (row + 1 >= 0 && row + 1 < board.length) {
                board[row+1][col] = board[row][col];
                board[row][col] = 0;
            }
            break;
        case "left":
            if (col - 1 >= 0 && col - 1 < board[row].length) {
                board[row][col - 1] = board[row][col];
                board[row][col] = 0;
            }
            break;
        case "right":
            if (col + 1 >= 0 && col + 1 < board[row].length) {
                board[row][col + 1] = board[row][col];
                board[row][col] = 0;
            }
            break;
    }
}
Atriace
  • 2,569
  • 1
  • 11
  • 26
0

I didn't make it a comment, because it's too long, though this isn't an exhaustive answer, just a couple of things you need to clarify:

  1. It may or may not be the best choice of emulating 2D arrays. Some other choices would include: a single one-dimensional array, where you access items modulo width. This has the benefit of faster lookup and that many built-in functions, which work on arrays will work on it out of the box. The disadvantage would be the row insertion operation - something you'd have to write on your own, while inserting a row into array of arrays is trivial. Another choice - using Cantor's mapping function (example here: Mapping two integers to one, in a unique and deterministic way ), there are more then one such functions with integer hash-table. This has the advantage of taking just as much space as there are actual values (if you have only few elements on the map, it will take less memory). Searching for items in such array is a lot faster then in a normal array (because you don't look into "empty" items - which don't even exist. However, you will need to write a lot of functionality all by yourself.

  2. Finding path is a subject of many algorithms all cumulatively known as "path finding algorithms". The best one to choose will vary significantly upon your circumstances. Before you choose one (or invent your own), it is important to answer questions like this:

    • Do all movements have the same cost?
    • Is moving back, or even just revisiting of the same location allowed?
    • Are you looking for the solution that guarantees you to find the best way, or just any way?
    • If the movement is not straight-forward, is it possible that you may find a heuristic function that estimates the cost of intermediate solution?
    • Finally, what are you optimizing for? The speed? The memory?

However, if you just wanted to know whether the point is within rectangular boundary, I'd just have a Rectangle the size of the map and use http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Rectangle.html#containsPoint%28%29 - this will spare you writing the switch similar to the one in Artiace's answer.

Community
  • 1
  • 1