๐ฃ ์ฅ์ ๋ฌผ์ด ์ฐ์ํด์ ๊ฐ์ ์์น์ ์์ฑ - FRONTENDBOOTCAMP-13th/JS-07-7zzang-Arcade GitHub Wiki
๊ตฌ๋ถ
- ๋ ์ง : 2025.05.20
- ์ข ๋ฅ : ๋ฒ๊ทธ
- ์ค์๋ : ๐ด
1. ๋ฌธ์ ์ํฉ
-
์ธ๊ฒ์์์ ์ฅ์ ๋ฌผ์ด ์ฐ์ํด์ ๊ฐ์ ์์น์ ์์ฑ ๋๋ ๊ฒฝ์ฐ๊ฐ ์์ฃผ ๋ฐ์ํจ.
cellIndex = 2
์๋ค๋ฉด, ๋ค์์๋ ๋2
๊ฐ ๋์ค๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ โ ํจํด์ด ๋๊ปด์ ธ ๊ฒ์์ด ๋จ์กฐ๋ก์์ง
function spawnObstacle(): void { const cellIndex = getRandomCell(); console.log("์์ฑ๋ cellIndex:", cellIndex); const cell = cells[cellIndex]; const obstacle = document.createElement('div'); obstacle.classList.add('obstacle'); cell.appendChild(obstacle); }
console.log()
๋ก ํ์ธํ ๊ฒฐ๊ณผ, ๊ฐ์ ์ด index๊ฐ ์ฐ์ํด์ ์์ฑ๋จ. cellIndex๊ฐ ๊ณ์ ๋ฐ๋ณต๋จ โ ์ค๋ณต ๋ฐฉ์ง๊ฐ ํ์ํ๋ค๊ณ ํ๋จ
2. ์์ธ
- ์ฅ์ ๋ฌผ ์์น๋
Math.floor(Math.random() * 6)
์ผ๋ก ์์ ๋๋ค์ด๋ผ ์ค๋ณต์ ๋ง์ง ์์. ๊ฒฐ๊ตญ ์ค๋ณต๋์ง ์๋๋ก ์ง์ ๋ง๋ ๋ก์ง์ด ํ์ํจ
3. ํด๊ฒฐ ๊ณผ์
function spawnObstacle(): void {
const cellIndex = getRandomCell();
console.log("์ด์ :" lastCellIndex, "ํ์ฌ:"cellIndex);
const cell = cells[cellIndex];
const obstacle = document.createElement('div');
obstacle.classList.add('obstacle');
cell.appendChild(obstacle);
}
-
console.log("์ด์ :" lastCellIndex, "ํ์ฌ:"cellIndex);
๋ก ์ฅ์ ๋ฌผ ์์ฑ ์์น์ index๊ฐ ์ฐ์ํด์ ๋ฐ๋ณต๋๋์ง ํ์ธlastCellIndex === cellIndex
๊ฐ์์ ํ์ธํจ. ๊ทธ๋ผ ์์น ๊ธฐ์ตํด์ ๊ฐ์ ์์น๋ฉด ๋ค์ ๋ฝ์์ผ๊ฒ ๋ค๊ณ ์๊ฐํจ
let lastCellIndex = -1; function getRandomCell(): number { let index: number; do { index = Math.floor(Math.random() * 6); } while (index === lastCellIndex); // ์ง์ ๊ณผ ๊ฐ์ผ๋ฉด ๋ค์ ๋ฝ๊ธฐ lastCellIndex = index; return index; }
- ์ ์ญ ๋ณ์ ์์ฑ : ์ง์ ์ฅ์ ๋ฌผ์ ์ด ์์น ๊ธฐ์ต์ฉ
- ์ค๋ณต ๋ฐฉ์ง ํจ์ : ๊ฐ์ index์ผ ๊ฒฝ์ฐ ๋ค์ ๋ฝ๋๋ก
do-while
์ฌ์ฉ - const prevIndex = lastCellIndex; // ํจ์ ํธ์ถ ์ ์ํ ๊ธฐ๋ก
function spawnObstacle(): void { const prevIndex = lastCellIndex; // ํจ์ ํธ์ถ ์ ์ํ ์ ์ฅ const cellIndex = getRandomCell(); console.log("์ด์ :", prevIndex, "ํ์ฌ:", cellIndex); const cell = cells[cellIndex]; const obstacle = document.createElement('div'); obstacle.classList.add('obstacle'); cell.appendChild(obstacle); }
- ์ด์ index์ ํ์ฌ index๊ฐ ๋ค๋ฅธ์ง ํ์ธํ๋ ๋ก๊ทธ ์ถ๋ ฅํจ. ๋๋ฒ๊น
๊ฒฐ๊ณผ
"์ด์ : 2 ํ์ฌ: 4"
โ ์ค๋ณต ๋ฐฉ์ง ๋ก์ง ์ ์ ์๋ ํ์ธ.
4. ๋ณด์ํ ์
- ์๋ํ ๋ฐ๊ฐ ๋ง๋์ง ๋์์์ด ์์ฌํ๊ธฐ
- ์ต๊ด์ ์ผ๋ก ์คํ ์ /ํ ๊ฐ์ ๋น๊ตํ๋ ๊ตฌ์กฐ์ ๋ก๊ทธ ์ฐ๊ธฐ
- ์ด ๋ถ๋ถ ๋๋ฌธ์ ๊ฐ์ ์์ ์ ์ฌ๋ฌ ๋ฒ ํ๊ฒ ๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํจ
- ๋ก์ง์ ๋ถ๋ฆฌํด์ ํจ์ํํ๋ ์ต๊ด
- ํ๋์ ํจ์๊ฐ ํ ๊ฐ์ง ์ญํ ๋ง ํ๋๋ก ๊ตฌ์ฑ
- ํ ๋ฒ์ ๋ง์ ๋ด์ฉ์ ์์ ํ๋ ค๊ณ ํ๋ค ๋ณด๋, ๋๋ฒ๊น ๊น์ง๋ ์ด๋ ค์ด ์ํฉ์ด ์๊น
- ์ํ ๊ธฐ์ต๊ณผ ์กฐ๊ฑด ์ ์ด ๊ตฌ์กฐ ์ตํ๊ธฐ โญโญโญโญโญ
- ์ ์ญ ์ํ, ์กฐ๊ฑด ์ฌ์๋ (
do-while
) ํจํด ๋ฑ ์์ฃผ ์จ๋ณด๊ธฐ
- ์ ์ญ ์ํ, ์กฐ๊ฑด ์ฌ์๋ (