gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
create me a minesweeper using html.css and js
- User
create me a minesweeper using html.css and js
- Assistant
create me a minesweeper using html.css and js
Sure! Below is a simple implementation of a Minesweeper game using HTML, CSS, and JavaScript. You can copy and paste this code into a local file and open it in your web browser to play the game.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Minesweeper</title>
</head>
<body>
<h1>Minesweeper</h1>
<div id="game-board"></div>
<button id="restart" onclick="restartGame()">Restart</button>
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
text-align: center;
}
#game-board {
display: grid;
grid-template-columns: repeat(10, 40px);
grid-template-rows: repeat(10, 40px);
gap: 2px;
margin: 20px auto;
}
.cell {
width: 40px;
height: 40px;
background-color: #ddd;
display: flex;
justify-content: center;
align-items: center;
font-size: 20px;
cursor: pointer;
}
.cell.revealed {
background-color: #f0f0f0;
}
.cell.mined {
background-color: red;
}
.cell.flagged {
background-color: yellow;
}
const boardSize = 10;
const mineCount = 10;
let board = [];
let revealedCount = 0;
let gameOver = false;
function initGame() {
board = Array.from({ length: boardSize }, () => Array(boardSize).fill(0));
placeMines();
calculateNumbers();
createBoard();
}
function placeMines() {
let minesPlaced = 0;
while (minesPlaced < mineCount) {
const x = Math.floor(Math.random() * boardSize);
const y = Math.floor(Math.random() * boardSize);
if (board[x][y] !== '💣') {
board[x][y] = '💣';
minesPlaced++;
}
}
}
function calculateNumbers() {
for (let x = 0; x < boardSize; x++) {
for (let y = 0; y < boardSize; y++) {
if (board[x][y] === '💣') continue;
let count = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
const nx = x + i;
const ny = y + j;
if (nx >= 0 && ny >= 0 && nx < boardSize && ny < boardSize && board[nx][ny] === '💣') {
count++;
}
}
}
board[x][y] = count;
}
}
}
function createBoard() {
const gameBoard = document.getElementById('game-board');
gameBoard.innerHTML = '';
for (let x = 0; x < boardSize; x++) {
for (let y = 0; y < boardSize; y++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.x = x;
cell.dataset.y = y;
cell.addEventListener('click', () => handleClick(cell));
gameBoard.appendChild(cell);
}
}
}
function handleClick(cell) {
if (gameOver) return;
const x = cell.dataset.x;
const y = cell.dataset.y;
if (board[x][y] === '💣') {
cell.classList.add('mined');
cell.innerText = '💣';
alert('Game Over! You hit a mine!');
gameOver = true;
} else {
revealCell(x, y);
}
}
function revealCell(x, y) {
const cell = document.querySelector(`[data-x="${x}"][data-y="${y}"]`);
if (!cell || cell.classList.contains('revealed')) return;
cell.classList.add('revealed');
revealedCount++;
cell.innerText = board[x][y] === 0 ? '' : board[x][y];
if (revealedCount === (boardSize * boardSize - mineCount)) {
alert('Congratulations! You cleared the minefield!');
gameOver = true;
}
if (board[x][y] === 0) {
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue;
revealCell(+x + i, +y + j);
}
}
}
}
function restartGame() {
gameOver = false;
revealedCount = 0;
initGame();
}
initGame();
index.html, styles.css, and script.js.index.html file in your web browser.Feel free to modify and enhance the game's features as you like!
Not yet included in a published release.