const boardSize = 10; const mineCount = 20; let board = []; let gameOver = false; const boardElement = document.getElementById('ms_board'); // Updated ID const statusElement = document.getElementById('ms_status'); // Updated ID // Initialize the board function createBoard() { boardElement.innerHTML = ''; board = []; for (let row = 0; row < boardSize; row++) { let rowArr = []; for (let col = 0; col < boardSize; col++) { const cell = { isMine: false, isRevealed: false, isFlagged: false, neighborMines: 0 }; rowArr.push(cell); } board.push(rowArr); } placeMines(); updateBoard(); } // Place mines randomly function placeMines() { let placedMines = 0; while (placedMines < mineCount) { const row = Math.floor(Math.random() * boardSize); const col = Math.floor(Math.random() * boardSize); if (!board[row][col].isMine) { board[row][col].isMine = true; placedMines++; } } calculateNeighborMines(); } // Calculate the number of mines around each cell function calculateNeighborMines() { for (let row = 0; row < boardSize; row++) { for (let col = 0; col < boardSize; col++) { if (!board[row][col].isMine) { let count = 0; for (let r = -1; r <= 1; r++) { for (let c = -1; c <= 1; c++) { const newRow = row + r; const newCol = col + c; if (isValidCell(newRow, newCol) && board[newRow][newCol].isMine) { count++; } } } board[row][col].neighborMines = count; } } } } // Check if a cell is valid (within bounds) function isValidCell(row, col) { return row >= 0 && col >= 0 && row < boardSize && col < boardSize; } // Reveal a cell function revealCell(row, col) { if (gameOver || board[row][col].isRevealed || board[row][col].isFlagged) { return; } board[row][col].isRevealed = true; if (board[row][col].isMine) { gameOver = true; revealMines(); statusElement.textContent = 'Game Over! You stepped on a mine.'; } else { if (board[row][col].neighborMines === 0) { // Reveal neighboring cells if no adjacent mines for (let r = -1; r <= 1; r++) { for (let c = -1; c <= 1; c++) { const newRow = row + r; const newCol = col + c; if (isValidCell(newRow, newCol) && !board[newRow][newCol].isRevealed) { revealCell(newRow, newCol); } } } } checkWin(); } updateBoard(); } // Reveal all mines after game over function revealMines() { for (let row = 0; row < boardSize; row++) { for (let col = 0; col < boardSize; col++) { if (board[row][col].isMine) { board[row][col].isRevealed = true; } } } } // Flag or unflag a cell function flagCell(row, col) { if (gameOver || board[row][col].isRevealed) { return; } board[row][col].isFlagged = !board[row][col].isFlagged; updateBoard(); } // Update the visual board function updateBoard() { boardElement.innerHTML = ''; for (let row = 0; row < boardSize; row++) { for (let col = 0; col < boardSize; col++) { const cellElement = document.createElement('div'); cellElement.classList.add('cell'); const cell = board[row][col]; if (cell.isRevealed) { if (cell.isMine) { cellElement.classList.add('mine'); } else { cellElement.classList.add('empty'); if (cell.neighborMines > 0) { cellElement.textContent = cell.neighborMines; } } } else if (cell.isFlagged) { cellElement.classList.add('flagged'); cellElement.textContent = 'F'; } // Add click event listener for left click and right click cellElement.addEventListener('click', () => revealCell(row, col)); cellElement.addEventListener('contextmenu', (e) => { e.preventDefault(); flagCell(row, col); }); boardElement.appendChild(cellElement); } } } // Check if the player has won function checkWin() { let unrevealedCells = 0; for (let row = 0; row < boardSize; row++) { for (let col = 0; col < boardSize; col++) { if (!board[row][col].isRevealed && !board[row][col].isMine) { unrevealedCells++; } } } if (unrevealedCells === 0) { gameOver = true; statusElement.textContent = 'Congratulations! You win!'; } } // Start the game createBoard();