TryingToImprove
TryingToImprove

Reputation: 7407

Mapping a small grid on a large grid

I am trying to build a little game, where I have a large grid. (10x20) spaces, and a small grid (3x3). I want to map the little grid on till the large grid.

I have to be able to specify a y/row location and a x/cell location for the grid to be placed.

How can I do this?

Example:

    small grid:
    |0|1|0|
    |1|1|1|
    |0|1|0|

    large grid
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0

    result:
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|1|0|0|0|0|0
    |0|0|0|1|1|1|0|0|0|0
    |0|0|0|0|1|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0
    |0|0|0|0|0|0|0|0|0|0

UPDATE The game is tetris. I have a check that a small grid (brick) can't come out of the large grids boundaries.

        A brick holds:
        X: cell position on the large grid
        Y: row position on the large grid
        Width: the width of the small grid
        Height: the height of the small grid
        Fields: the small grid where some fields are 0 (unmarked), and some are 1 (marked)

I have tried something like this: but can't get it to work

        for (var row = brick.y; row < brick.y + brick.height; row++) {
            for (var cell = brick.x; cell < brick.x + brick.width; cell++) {
                for (var fieldRow = 0; fieldRow < brick.type.fields.length; fieldRow++) {
                    for (var fieldCell = 0; fieldCell < brick.type.fields[fieldRow].length; fieldCell++) {
                        console.log(brick.type.fields[fieldRow][fieldCell]);
                        if (brick.type.fields[fieldRow][fieldCell] == 1) {
                            this.grid[row][cell] = 1;
                        }
                    }
                }
            }
        }

Upvotes: 0

Views: 263

Answers (1)

TheodoreV
TheodoreV

Reputation: 304

Assuming that you are using 2 arrays to build your game, your 3x3 array is based in the middle of your big grid.

The center of the small array is smallGrid[1][1] The center of the big array is bigGrid[4][11]. Lets build the transfer algorithm

for(i=0; i<=2; i++){
 for(j=0; j<=2; j++){
   if(smallGrid[i][j] == 1) 
    { 
      bigGrid[i+3][j+10]=1;
    } 
 }
}

Upvotes: 1

Related Questions