o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
make a 48x48 minesweeper game in html5
- User
make a 48x48 minesweeper game in html5
- Assistant
make a 48x48 minesweeper game in html5
Sure! Below is a simple implementation of a 48x48 Minesweeper game using HTML5, CSS, and JavaScript. This example creates a grid where you can reveal cells and flag mines.
index.html - Contains the HTML structure.styles.css - Contains the CSS styles.script.js - Contains the JavaScript logic.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Minesweeper 48x48</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Minesweeper 48x48</h1>
<div id="game"></div>
<script src="script.js"></script>
</body>
</html>
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
margin: 20px 0;
}
#game {
display: inline-block;
border: 2px solid #000;
}
.row {
display: flex;
}
.cell {
width: 16px;
height: 16px;
border: 1px solid #999;
background-color: #ccc;
line-height: 16px;
text-align: center;
font-size: 12px;
cursor: pointer;
user-select: none;
}
.cell.revealed {
background-color: #e0e0e0;
border: 1px solid #bbb;
cursor: default;
}
.cell.mine {
background-color: #f00;
}
.cell.flagged {
background-color: #ff0;
}
.cell.number {
color: blue;
font-weight: bold;
}
const gridSize = 48;
const mineProbability = 0.15; // Adjust the probability of mines appearing
const gameContainer = document.getElementById('game');
let grid = [];
function createGrid() {
for (let y = 0; y < gridSize; y++) {
const row = [];
const rowElement = document.createElement('div');
rowElement.classList.add('row');
for (let x = 0; x < gridSize; x++) {
const cell = {
x,
y,
isMine: Math.random() < mineProbability,
revealed: false,
flagged: false,
neighborMines: 0,
element: document.createElement('div')
};
cell.element.classList.add('cell');
cell.element.addEventListener('click', () => revealCell(cell));
cell.element.addEventListener('contextmenu', (e) => {
e.preventDefault();
toggleFlag(cell);
});
rowElement.appendChild(cell.element);
row.push(cell);
}
gameContainer.appendChild(rowElement);
grid.push(row);
}
calculateNeighborMines();
}
function calculateNeighborMines() {
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
const cell = grid[y][x];
if (cell.isMine) {
cell.neighborMines = -1;
continue;
}
let minesCount = 0;
for (let yy = -1; yy <= 1; yy++) {
for (let xx = -1; xx <= 1; xx++) {
if (yy === 0 && xx === 0) continue;
const newY = y + yy;
const newX = x + xx;
if (newY >= 0 && newY < gridSize && newX >= 0 && newX < gridSize) {
if (grid[newY][newX].isMine) {
minesCount++;
}
}
}
}
cell.neighborMines = minesCount;
}
}
}
function revealCell(cell) {
if (cell.revealed || cell.flagged) return;
cell.revealed = true;
cell.element.classList.add('revealed');
if (cell.isMine) {
cell.element.classList.add('mine');
alert('Game Over!');
revealAllMines();
return;
}
if (cell.neighborMines > 0) {
cell.element.textContent = cell.neighborMines;
cell.element.classList.add('number');
} else {
// Reveal neighboring cells recursively
for (let yy = -1; yy <= 1; yy++) {
for (let xx = -1; xx <= 1; xx++) {
if (yy === 0 && xx === 0) continue;
const newY = cell.y + yy;
const newX = cell.x + xx;
if (newY >= 0 && newY < gridSize && newX >= 0 && newX < gridSize) {
revealCell(grid[newY][newX]);
}
}
}
}
checkWin();
}
function toggleFlag(cell) {
if (cell.revealed) return;
cell.flagged = !cell.flagged;
if (cell.flagged) {
cell.element.classList.add('flagged');
} else {
cell.element.classList.remove('flagged');
}
}
function revealAllMines() {
for (const row of grid) {
for (const cell of row) {
if (cell.isMine) {
cell.element.classList.add('revealed', 'mine');
}
}
}
}
function checkWin() {
let revealedCells = 0;
let totalCells = gridSize * gridSize;
let totalMines = 0;
for (const row of grid) {
for (const cell of row) {
if (cell.isMine) {
totalMines++;
} else if (cell.revealed) {
revealedCells++;
}
}
}
if (revealedCells === totalCells - totalMines) {
alert('Congratulations! You won!');
revealAllMines();
}
}
createGrid();
Create Project Files: Create a new folder for your Minesweeper game and create the three files: index.html, styles.css, and script.js.
Copy the Code: Copy the code snippets provided above into their respective files.
Run the Game: Open index.html in your web browser to play the game.
mineProbability variable in script.js to increase or decrease the number of mines.styles.css to customize the appearance of the game grid and cells.Enjoy your Minesweeper game!