지뢰찾기 게임 코드 공개, 게임 만들기 렛츠고

1. HTML 코드

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>지뢰찾기 게임</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>지뢰찾기 게임</h1>
    <div id="game"></div>
    <script src="script.js"></script>
</body>
</html>

 

2. CSS(styles.css)

body {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-family: Arial, sans-serif;
}

#game {
    display: grid;
    grid-template-columns: repeat(10, 30px);
    grid-template-rows: repeat(10, 30px);
    gap: 2px;
}

.cell {
    width: 30px;
    height: 30px;
    background-color: #ddd;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    cursor: pointer;
}

.cell.revealed {
    background-color: #bbb;
    cursor: default;
}

.cell.mine {
    background-color: red;
}

 

3. JavaScript (script.js)

const rows = 10;
const cols = 10;
const minesCount = 20;
const game = document.getElementById('game');

let board = [];
let revealedCount = 0;

function createBoard() {
    board = Array(rows).fill().map(() => Array(cols).fill({
        mine: false,
        revealed: false,
        adjacentMines: 0
    }));

    placeMines();
    calculateAdjacentMines();
    renderBoard();
}

function placeMines() {
    let placedMines = 0;
    while (placedMines < minesCount) {
        const row = Math.floor(Math.random() * rows);
        const col = Math.floor(Math.random() * cols);
        if (!board[row][col].mine) {
            board[row][col].mine = true;
            placedMines++;
        }
    }
}

function calculateAdjacentMines() {
    for (let row = 0; row < rows; row++) {
        for (let col = 0; col < cols; col++) {
            if (board[row][col].mine) continue;
            let adjacentMines = 0;
            for (let r = -1; r <= 1; r++) {
                for (let c = -1; c <= 1; c++) {
                    if (r === 0 && c === 0) continue;
                    const newRow = row + r;
                    const newCol = col + c;
                    if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && board[newRow][newCol].mine) {
                        adjacentMines++;
                    }
                }
            }
            board[row][col].adjacentMines = adjacentMines;
        }
    }
}

function renderBoard() {
    game.innerHTML = '';
    for (let row = 0; row < rows; row++) {
        for (let col = 0; col < cols; col++) {
            const cell = document.createElement('div');
            cell.classList.add('cell');
            cell.dataset.row = row;
            cell.dataset.col = col;
            cell.addEventListener('click', handleCellClick);
            game.appendChild(cell);
        }
    }
}

function handleCellClick(event) {
    const row = event.target.dataset.row;
    const col = event.target.dataset.col;
    revealCell(parseInt(row), parseInt(col));
}

function revealCell(row, col) {
    const cell = board[row][col];
    if (cell.revealed) return;
    cell.revealed = true;
    revealedCount++;

    const cellElement = game.children[row * cols + col];
    cellElement.classList.add('revealed');
    if (cell.mine) {
        cellElement.classList.add('mine');
        cellElement.innerHTML = '💣';
        alert('Game Over!');
        resetGame();
    } else {
        if (cell.adjacentMines > 0) {
            cellElement.innerHTML = cell.adjacentMines;
        } else {
            for (let r = -1; r <= 1; r++) {
                for (let c = -1; c <= 1; c++) {
                    const newRow = row + r;
                    const newCol = col + c;
                    if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
                        revealCell(newRow, newCol);
                    }
                }
            }
        }
    }

    if (revealedCount === rows * cols - minesCount) {
        alert('Congratulations! You won!');
        resetGame();
    }
}

function resetGame() {
    revealedCount = 0;
    createBoard();
}

createBoard();

 

각각 파일 저장하고 구현하시면 됩니다.

 

물론 전 프로그래밍 개똥도 모릅니다. 우리들의 위대한 AI가 짜준 것이지요 ㅋㅋㅋ 그냥 공유해봅니다. 이거 어떻게 해야 만들 수 있는지 아시는분 댓글 부탁드려요.

728x90
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유