-
①
2차원 보드 배열 (6×12 Grid)
LIVE
보드는 12행 × 6열 2차원 배열입니다. 지금 내려오는 젤리 한 쌍은 그 위의 좌표 (x, y) 두 칸일 뿐입니다.
// board[row][col]
board = Array(12).fill()
.map(() => Array(6).fill(null))
board[11][2] = { color: 'R' } // 놓기
piece.x, piece.y // 내 좌표
board 12×6
-
②
낙하·회전 충돌 판정 (Collision & Wall Kick)
LIVE
움직이기 전에 "그 칸이 비었나?"를 먼저 검사합니다. 벽에 막히면 옆으로 한 칸씩 밀어서 다시 시도하는 것이 wall kick입니다.
function canPlace(x, y, axis) {
for ([cx, cy] of cells(x, y, axis))
if (outside(cx, cy) || board[cy][cx])
return false
return true
}
// 벽이면 dx = 0,-1,+1,-2,+2 재시도
canPlace=true · kick=0
-
③
BFS 4연결 탐색 (Connected Components)
LIVE
같은 색을 상하좌우로 이어 붙여 4개 이상인 덩어리를 찾습니다. 큐에 이웃을 넣고 하나씩 꺼내는 BFS가 딱 맞습니다.
function findGroups() {
const seen = new Set(), groups = []
for (cell of allCells()) {
if (seen.has(cell)) continue
const queue = [cell], group = []
while (queue.length) { // BFS
const cur = queue.pop()
group.push(cur); seen.add(cur)
for (n of neighbors(cur))
if (n.color === cur.color &&
!seen.has(n)) queue.push(n)
}
groups.push(group) // 4+ 터뜨림
}
return groups
}
groups=0 · biggest=0
-
④
연쇄 중력 루프 (Chain & Gravity Loop)
LIVE
터뜨린 뒤 위의 젤리를 아래로 내리고, 다시 찾는 것을 반복합니다. 이 반복 횟수가 곧 연쇄(chain)이고 점수 배수가 됩니다.
let chain = 0
while (true) {
const popped = findGroups()
.filter(g => g.length >= 4)
if (popped.length === 0) break
chain += 1
remove(popped); applyGravity() // 낙하
score += popped.length * 10 * chain
}
chain=0 · gravity=0