Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
dragon731012
GitHub Repository: dragon731012/-WORKING-bookmarklets-and-games
Path: blob/main/games/MineSweeper(v4)
17138 views
javascript:(function () { class Cell { constructor(row, col, board) { this.row = row; this.col = col; this.bomb = false; this.board = board; this.revealed = false; this.flagged = false; this.adjBombs = 0; } getAdjCells() { var cells = []; var lastRow = this.board.length - 1; var lastCol = this.board[0].length - 1; if (this.row > 0 && this.col > 0) cells.push(this.board[this.row - 1][this.col - 1]); if (this.row > 0) cells.push(this.board[this.row - 1][this.col]); if (this.row > 0 && this.col < lastCol) cells.push(this.board[this.row - 1][this.col + 1]); if (this.col < lastCol) cells.push(this.board[this.row][this.col + 1]); if (this.row < lastRow && this.col < lastCol) cells.push(this.board[this.row + 1][this.col + 1]); if (this.row < lastRow) cells.push(this.board[this.row + 1][this.col]); if (this.row < lastRow && this.col > 0) cells.push(this.board[this.row + 1][this.col - 1]); if (this.col > 0) cells.push(this.board[this.row][this.col - 1]); return cells; } calcAdjBombs() { var adjCells = this.getAdjCells(); this.adjBombs = adjCells.reduce((count, cell) => count + (cell.bomb ? 1 : 0), 0); } flag() { if (!this.revealed) { this.flagged = !this.flagged; return this.flagged; } return false; } reveal() { if (this.revealed) return false; this.revealed = true; if (this.bomb) return true; if (this.adjBombs === 0) { this.getAdjCells().forEach(cell => { if (!cell.revealed) cell.reveal(); }); } return false; } } var levels = { easy: { size: 9, bombs: 10 }, mid: { size: 16, bombs: 40 }, hard: { size: 30, bombs: 160 } }; var currentLevel = levels.mid; var boardSize = currentLevel.size; var timerId, hitBomb, board, timer, bombCounter; var createBoard = () => { var table = document.createElement(%27table%27); table.id = %27board%27; table.style.borderCollapse = %27collapse%27; table.style.margin = %27auto%27; table.style.position = %27fixed%27; table.style.top = %2750%%27; table.style.left = %2750%%27; table.style.transform = %27translate(-50%, -50%)%27; table.style.backgroundColor = %27#333'; table.style.padding = '10px'; table.style.border = '2px solid #666'; table.style.zIndex = '10000'; document.body.appendChild(table); return table; }; var initGame = (level) => { currentLevel = levels[level] || currentLevel; boardSize = currentLevel.size; clearInterval(timerId); board.innerHTML = ''; var header = document.createElement('tr'); var headerCell = document.createElement('td'); headerCell.colSpan = boardSize; headerCell.style.textAlign = 'center'; headerCell.style.background = '#ccc'; headerCell.style.color = '#000'; var statusBar = document.createElement('div'); statusBar.id = 'status-bar'; bombCounter = document.createElement('span'); bombCounter.id = 'bomb-counter'; bombCounter.textContent = currentLevel.bombs.toString().padStart(3, '0'); timer = document.createElement('span'); timer.id = 'timer'; timer.textContent = '000'; var resetButton = document.createElement('button'); resetButton.id = 'reset'; resetButton.textContent = '😐'; statusBar.appendChild(bombCounter); statusBar.appendChild(timer); statusBar.appendChild(resetButton); headerCell.appendChild(statusBar); header.appendChild(headerCell); board.appendChild(header); for (var i = 0; i < boardSize; i++) { var row = document.createElement('tr'); for (var j = 0; j < boardSize; j++) { var cell = document.createElement('td'); cell.classList.add('game-cell'); cell.style.width = '20px'; cell.style.height = '20px'; cell.style.border = '1px solid #000'; cell.style.background = '#aaa'; cell.setAttribute('data-row', i); cell.setAttribute('data-col', j); row.appendChild(cell); } board.appendChild(row); } setupBoard(); addEventListeners(); startTimer(); }; var setupBoard = () => { boardArray = Array(boardSize).fill(null).map(() => Array(boardSize).fill(null)); for (var i = 0; i < boardSize; i++) { for (var j = 0; j < boardSize; j++) { boardArray[i][j] = new Cell(i, j, boardArray); } } var bombsToPlace = currentLevel.bombs; while (bombsToPlace > 0) { var row = Math.floor(Math.random() * boardSize); var col = Math.floor(Math.random() * boardSize); if (!boardArray[row][col].bomb) { boardArray[row][col].bomb = true; bombsToPlace--; } } boardArray.forEach(row => row.forEach(cell => cell.calcAdjBombs())); }; var addEventListeners = () => { board.addEventListener('click', handleCellClick); board.addEventListener('contextmenu', handleCellRightClick); document.getElementById('reset').addEventListener('click', resetGame); document.addEventListener('keydown', handleKeyPress); }; var handleCellClick = (event) => { var target = event.target; if (target.tagName === 'TD' && !hitBomb) { var row = parseInt(target.getAttribute('data-row')); var col = parseInt(target.getAttribute('data-col')); hitBomb = boardArray[row][col].reveal(); if (hitBomb) { document.getElementById('reset').textContent = '😞'; } else if (checkWin()) { document.getElementById('reset').textContent = '😁'; } updateBoard(); } }; var handleCellRightClick = (event) => { event.preventDefault(); var target = event.target; if (target.tagName === 'TD') { var row = parseInt(target.getAttribute('data-row')); var col = parseInt(target.getAttribute('data-col')); boardArray[row][col].flag(); updateBoard(); } }; var handleKeyPress = (event) => { if (event.key === '1') { initGame('easy'); } else if (event.key === '2') { initGame('mid'); } else if (event.key === '3') { initGame('hard'); } else if (event.key === '/') { toggleBoardVisibility(); } }; var toggleBoardVisibility = () => { var boardElement = document.getElementById('board'); if (boardElement.style.display === 'none') { boardElement.style.display = ''; } else { boardElement.style.display = 'none'; } }; var updateBoard = () => { var colors = ["#0000FA", "#4B802D", "#DB1300", "#202081", "#690400", "#457A7A", "#1B1B1B", "#7A7A7A"]; Array.from(document.querySelectorAll('td.game-cell')).forEach(cell => { var row = parseInt(cell.getAttribute('data-row')); var col = parseInt(cell.getAttribute('data-col')); var boardCell = boardArray[row][col]; if (boardCell.revealed) { cell.style.background = '#ddd'; if (boardCell.bomb) { cell.style.background = 'red'; } else if (boardCell.adjBombs > 0) { cell.textContent = boardCell.adjBombs; cell.style.color = colors[boardCell.adjBombs - 1]; } } else if (boardCell.flagged) { cell.textContent = '🚩'; } else { cell.textContent = ''; } }); }; var resetGame = () => { clearInterval(timerId); timerId = null; hitBomb = false; document.getElementById('reset').textContent = '😐'; initGame(currentLevel); }; var startTimer = () => { var time = 0; timerId = setInterval(() => { time += 1; timer.textContent = time.toString().padStart(3, '0'); }, 1000); }; var checkWin = () => { return boardArray.every(row => row.every(cell => (cell.bomb && !cell.revealed) || (!cell.bomb && cell.revealed))); }; var board = document.getElementById('board') || createBoard(); initGame('mid'); })();