Skip to content

Commit

Permalink
fix hang up|page no response|predictTetromino while (result.collised)…
Browse files Browse the repository at this point in the history
…|when lines eliminated
  • Loading branch information
huoyijie committed Nov 8, 2023
1 parent 6492278 commit 10ed92b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
32 changes: 13 additions & 19 deletions src/lib/tetris.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function rotatePoints(type, points) {
return rotated
}

function eliminateLines(tetromino, tetrominoes, setTetrominoes) {
function eliminateLines(tetromino, tetrominoes) {
const { y, points } = tetromino

const candidates = []
Expand Down Expand Up @@ -118,10 +118,6 @@ function eliminateLines(tetromino, tetrominoes, setTetrominoes) {
}
})

if (eliminatedLines > 0) {
setTetrominoes([...tetrominoes])
}

return eliminatedLines
}

Expand Down Expand Up @@ -201,7 +197,7 @@ export function rotateTetromino(tetromino, tetrominoes) {
return collised ? tetromino : rotated
}

export function moveDownTetromino(tetromino, tetrominoes, onCollise, onGameOver, setTetrominoes) {
export function moveDownTetromino(tetromino, tetrominoes, onCollise, onGameOver) {
const moved = { ...tetromino, y: tetromino.y + 1 }

const { collised, reachTop } = detectCollision(moved, DOWN, tetrominoes)
Expand All @@ -210,7 +206,7 @@ export function moveDownTetromino(tetromino, tetrominoes, onCollise, onGameOver,
return moved
}

const eliminatedLines = eliminateLines(tetromino, tetrominoes, setTetrominoes)
const eliminatedLines = eliminateLines(tetromino, tetrominoes)
if (eliminatedLines == 0 && reachTop) {
onGameOver()
} else {
Expand All @@ -236,7 +232,7 @@ export function moveRightTetromino(tetromino, tetrominoes) {
return collised ? tetromino : moved
}

export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver, setTetrominoes) {
export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver) {
let result
const moved = { ...tetromino }
do {
Expand All @@ -246,7 +242,7 @@ export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver,

tetromino.y = moved.y - 1

const eliminatedLines = eliminateLines(tetromino, tetrominoes, setTetrominoes)
const eliminatedLines = eliminateLines(tetromino, tetrominoes)
if (eliminatedLines == 0 && result.reachTop) {
onGameOver()
} else {
Expand All @@ -255,14 +251,12 @@ export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver,
}

export function predictTetromino(tetromino, tetrominoes) {
if (tetromino) {
let result
const predicted = { ...tetromino }
do {
predicted.y++
result = detectCollision(predicted, DOWN, tetrominoes)
} while (!result.collised)
predicted.y--
return predicted
}
let result
const predicted = { ...tetromino }
do {
predicted.y++
result = detectCollision(predicted, DOWN, tetrominoes)
} while (!result.collised)
predicted.y--
return predicted
}
49 changes: 26 additions & 23 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var audio
export default function Home() {
const [tetrominoes, setTetrominoes] = useState([])
const [currentTetromino, setCurrentTetromino] = useState()
const predictedTetromino = predictTetromino(currentTetromino, tetrominoes)
const currentTetrominoValid = currentTetromino && currentTetromino.points.length > 0
const predictedTetromino = currentTetrominoValid ? predictTetromino(currentTetromino, tetrominoes) : null
const [nextTetromino, setNextTetromino] = useState()
const [score, setScore] = useState(0)
const [eliminatedLines, setEliminatedLines] = useState(0)
Expand Down Expand Up @@ -83,23 +84,15 @@ export default function Home() {
}
}

const frozen = () => {
const next = () => {
if (currentTetromino) {
tetrominoes.push(currentTetromino)
setTetrominoes([...tetrominoes.filter(({ points }) => points.length > 0)])
}
}

const next = () => {
frozen()
setCurrentTetromino(nextTetromino)
setNextTetromino(randomTetromino())
}

const rotate = () => {
setCurrentTetromino(rotateTetromino(currentTetromino, tetrominoes))
}

const onCollise = (elimiLines) => {
if (elimiLines > 0) {
setEliminatedLines(eliminatedLines + elimiLines)
Expand All @@ -109,33 +102,43 @@ export default function Home() {
queueMicrotask(next)
}

const rotate = () => {
if (!gameOver && currentTetrominoValid) {
setCurrentTetromino(rotateTetromino(currentTetromino, tetrominoes))
}
}

const down = () => {
setCurrentTetromino(
moveDownTetromino(
currentTetromino,
tetrominoes,
onCollise,
() => setGameOver(true),
setTetrominoes)
)
if (!gameOver && currentTetrominoValid) {
setCurrentTetromino(
moveDownTetromino(
currentTetromino,
tetrominoes,
onCollise,
() => setGameOver(true))
)
}
}

const left = () => {
setCurrentTetromino(moveLeftTetromino(currentTetromino, tetrominoes))
if (!gameOver && currentTetrominoValid) {
setCurrentTetromino(moveLeftTetromino(currentTetromino, tetrominoes))
}
}

const right = () => {
setCurrentTetromino(moveRightTetromino(currentTetromino, tetrominoes))
if (!gameOver && currentTetrominoValid) {
setCurrentTetromino(moveRightTetromino(currentTetromino, tetrominoes))
}
}

const fallDown = () => {
if (!gameOver && currentTetromino) {
if (!gameOver && currentTetrominoValid) {
fallDownTetromino(
currentTetromino,
tetrominoes,
onCollise,
() => setGameOver(true),
setTetrominoes)
() => setGameOver(true))
}
}

Expand Down

0 comments on commit 10ed92b

Please sign in to comment.