Workshop › Tetris-style Blocks › Step 9 of 32
Every rule of the game comes down to one question: does this shape fit at this place? First part: every filled cell must be inside the well's sides and above its floor.
```js
function fits(shape, x, y) {
for (let r = 0; r < shape.length; r++) {
for (let c = 0; c < shape[r].length; c++) {
if (!shape[r][c]) continue
const col = x + c
const row = y + r
if (col < 0 || col >= COLS || row >= ROWS) return false
}
}
return true
}
```fits takes the shape and a place as parameters, so it can answer "what if?" questions before anything moves.continue skips the empty ones: empty cells never collide.col and row are where the cell would be in the well. Left of the wall, right of it or under the floor: it does not
fit, and return false ends the function at once.return true.Write fits under the let piece line, after an empty line.
Skills: Collision detection, Functions
← All seven pieces Blocks are in the way →