Changed around line 1
+ const boardSize = 8;
+ const cherryCount = 10;
+ let board = [];
+ let revealedCount = 0;
+ let flaggedCount = 0;
+ let gameOver = false;
+
+ const gameBoard = document.getElementById('gameBoard');
+ const cherriesLeft = document.getElementById('cherriesLeft');
+ const resetButton = document.getElementById('reset');
+
+ function initGame() {
+ gameBoard.innerHTML = '';
+ gameBoard.style.gridTemplateColumns = `repeat(${boardSize}, 1fr)`;
+ board = createBoard();
+ placeCherries();
+ calculateNumbers();
+ renderBoard();
+ gameOver = false;
+ revealedCount = 0;
+ flaggedCount = 0;
+ cherriesLeft.textContent = cherryCount;
+ }
+
+ function createBoard() {
+ return Array.from({ length: boardSize }, () =>
+ Array.from({ length: boardSize }, () => ({
+ isCherry: false,
+ isRevealed: false,
+ isFlagged: false,
+ number: 0
+ }))
+ );
+ }
+
+ function placeCherries() {
+ let placed = 0;
+ while (placed < cherryCount) {
+ const row = Math.floor(Math.random() * boardSize);
+ const col = Math.floor(Math.random() * boardSize);
+ if (!board[row][col].isCherry) {
+ board[row][col].isCherry = true;
+ placed++;
+ }
+ }
+ }
+
+ function calculateNumbers() {
+ for (let row = 0; row < boardSize; row++) {
+ for (let col = 0; col < boardSize; col++) {
+ if (!board[row][col].isCherry) {
+ board[row][col].number = countAdjacentCherries(row, col);
+ }
+ }
+ }
+ }
+
+ function countAdjacentCherries(row, col) {
+ let count = 0;
+ for (let i = -1; i <= 1; i++) {
+ for (let j = -1; j <= 1; j++) {
+ if (i === 0 && j === 0) continue;
+ const newRow = row + i;
+ const newCol = col + j;
+ if (newRow >= 0 && newRow < boardSize && newCol >= 0 && newCol < boardSize && board[newRow][newCol].isCherry) {
+ count++;
+ }
+ }
+ }
+ return count;
+ }
+
+ function renderBoard() {
+ board.forEach((row, rowIndex) => {
+ row.forEach((cell, colIndex) => {
+ const cellElement = document.createElement('div');
+ cellElement.classList.add('cell');
+ cellElement.dataset.row = rowIndex;
+ cellElement.dataset.col = colIndex;
+
+ if (cell.isRevealed) {
+ cellElement.classList.add('revealed');
+ if (cell.isCherry) {
+ cellElement.classList.add('cherry');
+ cellElement.textContent = '🍒';
+ } else if (cell.number > 0) {
+ cellElement.textContent = cell.number;
+ }
+ } else if (cell.isFlagged) {
+ cellElement.classList.add('flagged');
+ cellElement.textContent = '🚩';
+ }
+
+ cellElement.addEventListener('click', handleCellClick);
+ cellElement.addEventListener('contextmenu', handleRightClick);
+
+ gameBoard.appendChild(cellElement);
+ });
+ });
+ }
+
+ function handleCellClick(event) {
+ if (gameOver) return;
+ const row = parseInt(event.target.dataset.row);
+ const col = parseInt(event.target.dataset.col);
+ const cell = board[row][col];
+
+ if (cell.isFlagged || cell.isRevealed) return;
+
+ cell.isRevealed = true;
+ revealedCount++;
+
+ if (cell.isCherry) {
+ gameOver = true;
+ revealAllCherries();
+ alert('Oops! You found a cherry. Game over!');
+ return;
+ }
+
+ if (cell.number === 0) {
+ revealAdjacentCells(row, col);
+ }
+
+ if (revealedCount === boardSize * boardSize - cherryCount) {
+ gameOver = true;
+ alert('Congratulations! You found all the cherries!');
+ }
+
+ renderBoard();
+ }
+
+ function handleRightClick(event) {
+ event.preventDefault();
+ if (gameOver) return;
+ const row = parseInt(event.target.dataset.row);
+ const col = parseInt(event.target.dataset.col);
+ const cell = board[row][col];
+
+ if (cell.isRevealed) return;
+
+ cell.isFlagged = !cell.isFlagged;
+ flaggedCount += cell.isFlagged ? 1 : -1;
+ cherriesLeft.textContent = cherryCount - flaggedCount;
+ renderBoard();
+ }
+
+ function revealAdjacentCells(row, col) {
+ for (let i = -1; i <= 1; i++) {
+ for (let j = -1; j <= 1; j++) {
+ if (i === 0 && j === 0) continue;
+ const newRow = row + i;
+ const newCol = col + j;
+ if (newRow >= 0 && newRow < boardSize && newCol >= 0 && newCol < boardSize) {
+ const cell = board[newRow][newCol];
+ if (!cell.isRevealed && !cell.isFlagged && !cell.isCherry) {
+ cell.isRevealed = true;
+ revealedCount++;
+ if (cell.number === 0) {
+ revealAdjacentCells(newRow, newCol);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ function revealAllCherries() {
+ board.forEach(row => {
+ row.forEach(cell => {
+ if (cell.isCherry) {
+ cell.isRevealed = true;
+ }
+ });
+ });
+ renderBoard();
+ }
+
+ resetButton.addEventListener('click', initGame);
+
+ // Initialize the game
+ initGame();