Short description:
I'm trying to generate tilings of a square with dominoes or in other words with 2x1 and 1x2 tiles.
Sometimes my algorithm puts a vertical tile in a way, that makes it impossible to fill the last row.
My current approach is to initialise a grid with 0's (say a 8x8 grid). I represent the left half of a horizontal tile with 1 and the right half with 2 in the grid.
Accordingly the upper half of vertical tile is 3 and the lower half is 4.
Then I go through each row of the grid from left to right and where a 0 is i put a tile:
- Whenever I'm at the right edge of the grid or the space to the right is not 0, i put a vertical tile (3).
- When I'm at the bottom edge, i (can) only put horizontal tiles.
- When none of the above has set a value i put a random value (either 1 or 3).
Whenever I put a tile (1 or 3), i also put the corresponding half accordingly for the adjacent space of the grid. Say i put a 1 at (i,j) i then put a 2 at (i,j+1).
StoneMatrix = int[8][8];
for (int i = 0; i < StoneMatrix.length; i++) {
for (int j = 0; j < StoneMatrix.length; j++) {
if (StoneMatrix[i][j] != 0){
//field already set as a tile
continue;
}
if (i == StoneMatrix.length - 1) {
//bottom row
StoneMatrix[i][j] = 1;
StoneMatrix[i][j + 1] = 2;
continue;
}
if (j == StoneMatrix.length - 1) {
//right edge
StoneMatrix[i][j] = 3;
StoneMatrix[i + 1][j] = 4;
continue;
}
if (StoneMatrix[i][j + 1] != 0) {
//field to the right taken.
StoneMatrix[i][j] = 3;
StoneMatrix[i + 1][j] = 4;
continue;
}
StoneMatrix[i][j] = putOneOrThreeRandomly();
putCorrespondingAdjacentTile();
}
}
I feel like there has to be an easy or easier way to generate such tiling, but i couldn't find one yet. What im looking for is either some source, where such a generating algorithm is described or an easy solution, that fixes my error.