1

I am planning a kids' version of Mahjong Solitaire (starting with just the Turtle board layout and working my way from there). I am trying to wrap my head around how to store the data for each layer of the Turtle layout tileset. See here for an example: http://icarus.cs.weber.edu/~dab/cs3230/labs/lab.5/tile_layers.pdf

Ordinarily I'd use a 2D array for each layer, and a 1D array of the layers, from 0 (bottom-most) to 4 (topmost), with the allowance for layers above that (5, 6, ...). However, there are the tiles that occupy more than one row and/or column at once. For example, in Layer 0 (bottom-most), the far left tile and the 2 far right tiles occupy two rows at once, and the single tile in Layer 4 (topmost) occupies two columns and two rows at the same time.

What is the best data model to store this sort of tileset? Should each tile have a flag for shifting it halfway into the next row and column?

I'm thinking, there is a Tile object, each instance of which represents 1 of the 144 tiles on the board. Then all tiles are arranged in layers as I described above (2D array for each layer, all layers stored in a 1D array).

Note: I am considering using Javascript & HTML5 for this project. It won't be something I release to the public, just a programming exercise.

Is this the best method? Am I missing something?

TerranRich
  • 1,263
  • 3
  • 20
  • 38

2 Answers2

2

I would indeed use a finer grid as alikox suggested. I would use a 2 times finer grid, give each tile an unique id, so one regular tile now uses four squares of the grid, when you have to delete one tile, you only have to check for surrounding squares with the same id and delete them as well.

Simon
  • 2,419
  • 2
  • 18
  • 30
1

There would be more than one way to implement this, you can set x, y and z coordinates of each tile for example.

class Tile {
    int x
    int y
    int z
}
ali köksal
  • 1,217
  • 1
  • 12
  • 19
  • That's a good way to store all the tiles (store the Z axis value, rather than place them into a 1D array of layers. But it doesn't answer my question regarding how to store tile placement, where some tiles are shifted halfway into the next column and/or row. – TerranRich Jun 04 '14 at 04:18
  • 1
    One solution would be to duplicate the number of rows and columns. Use even numbered coordinates for "regular" tiles. use odd numbered coordinates in between for others. Another solution, use a finer grained coordinate system and use tile areas (x, y, width, height) to determine overlap. – ali köksal Jun 04 '14 at 10:44