I'm building a grid where a new item must fit in the next available spot. Items can be 3:2 or 2:3, the grid is 12 columns wide.
How can I rewrite this looped conditional statement to remove the hardcoded limit and accepts input of 3:2 or 2:3 (currently x:3,y:2)?
const check = (
!grid[rowY + 0][columnX + 0] && // @TODO: Hard coded limit
!grid[rowY + 0][columnX + 1] && // @TODO: Hard coded limit
!grid[rowY + 0][columnX + 2] && // @TODO: Hard coded limit
!grid[rowY + 1][columnX + 0] && // @TODO: Hard coded limit
!grid[rowY + 1][columnX + 1] && // @TODO: Hard coded limit
!grid[rowY + 1][columnX + 2] // @TODO: Hard coded limit
);
if (check) {
openX = columnX;
openY = rowY;
found = true;
}
return found;
You can use the every
or some
array method. Given values for width
and height
, you can dynamically calculate check
as follows:
const check = !grid.slice(rowY, rowY + height).some( row =>
row.slice(columnX, columnX + width).some(Boolean)
);