html
<!DOCTYPE html>
<html>
<head>
<title>Minesweeper</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
.game-container {
text-align: center;
}
.header {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
font-size: 24px;
}
.game-board {
display: grid;
grid-template-columns: repeat(8, 40px);
grid-template-rows: repeat(8, 40px);
gap: 2px;
}
.cell {
width: 40px;
height: 40px;
background-color: #ddd;
border: 2px solid #999;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
cursor: pointer;
user-select: none;
}
.cell.revealed {
background-color: #eee;
border-color: #ccc;
}
.cell.mine {
background-color: #f00;
color: white;
}
.cell.flag {
background-color: #ff0;
}
button {
padding: 8px 16px;
font-size: 16px;
cursor: pointer;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="game-container">
<div class="header">
<div id="mines-counter">💣 10</div>
<button id="reset-btn">Reset</button>
<div id="timer">⏱️ 0</div>
</div>
<div class="game-board" id="game-board"></div>
</div>
<script>
const BOARD_SIZE = 8;
const MINE_COUNT = 10;
let cells = [];
let gameActive = true;
let mineCounter = MINE_COUNT;
let timer = 0;
let timerInterval;
// Inicjalizacja planszy
function createBoard() {
const board = document.getElementById('game-board');
board.innerHTML = '';
cells = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill({}));
// Tworzenie komórek
for (let row = 0; row < BOARD_SIZE; row++) {
for (let col = 0; col < BOARD_SIZE; col++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.row = row;
cell.dataset.col = col;
cell.addEventListener('click', () => handleClick(row, col));
cell.addEventListener('contextmenu', (e) => {
e.preventDefault();
handleRightClick(row, col);
});
board.appendChild(cell);
cells[row][col] = {
element: cell,
isMine: false,
isRevealed: false,
isFlagged: false,
adjacentMines: 0
};
}
}
}
// Losowe rozmieszczenie min
function placeMines(firstRow, firstCol) {
let minesPlaced = 0;
while (minesPlaced < MINE_COUNT) {
const row = Math.floor(Math.random() * BOARD_SIZE);
const col = Math.floor(Math.random() * BOARD_SIZE);
// Unikamy pierwszego klikniętego pola i sąsiednich
if ((Math.abs(row - firstRow) <= 1 && Math.abs(col - firstCol) <= 1) ||
cells[row][col].isMine) continue;
cells[row][col].isMine = true;
minesPlaced++;
}
// Oblicz liczbę sąsiednich min
for (let row = 0; row < BOARD_SIZE; row++) {
for (let col = 0; col < BOARD_SIZE; col++) {
if (!cells[row][col].isMine) {
cells[row][col].adjacentMines = countAdjacentMines(row, col);
}
}
}
}
// Liczenie sąsiednich min
function countAdjacentMines(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 (isValidCell(newRow, newCol) && cells[newRow][newCol].isMine) {
count++;
}
}
}
return count;
}
// Obsługa kliknięcia lewym przyciskiem
function handleClick(row, col) {
if (!gameActive || cells[row][col].isFlagged) return;
// Pierwsze kliknięcie - rozmieszczamy miny
if (!timerInterval) {
placeMines(row, col);
startTimer();
}
if (cells[row][col].isMine) {
gameOver(false);
return;
}
revealCell(row, col);
checkWin();
}
// Odkrywanie komórek
function revealCell(row, col) {
const cell = cells[row][col];
if (cell.isRevealed || cell.isFlagged) return;
cell.isRevealed = true;
cell.element.classList.add('revealed');
if (cell.adjacentMines === 0) {
// Rekurencyjne odkrywanie sąsiednich pustych komórek
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 (isValidCell(newRow, newCol)) {
revealCell(newRow, newCol);
}
}
}
} else {
cell.element.textContent = cell.adjacentMines;
}
}
// Obsługa prawego przycisku
function handleRightClick(row, col) {
if (!gameActive || cells[row][col].isRevealed) return;
const cell = cells[row][col];
cell.isFlagged = !cell.isFlagged;
if (cell.isFlagged) {
cell.element.classList.add('flag');
cell.element.textContent = '🚩';
mineCounter--;
} else {
cell.element.classList.remove('flag');
cell.element.textContent = '';
mineCounter++;
}
document.getElementById('mines-counter').textContent = `💣 ${mineCounter}`;
}
// Start timera
function startTimer() {
timerInterval = setInterval(() => {
timer++;
document.getElementById('timer').textContent = `⏱️ ${timer}`;
}, 1000);
}
// Koniec gry
function gameOver(isWin) {
gameActive = false;
clearInterval(timerInterval);
// Odkryj wszystkie miny
for (let row = 0; row < BOARD_SIZE; row++) {
for (let col = 0; col < BOARD_SIZE; col++) {
if (cells[row][col].isMine) {
cells[row][col].element.classList.add('mine');
cells[row][col].element.textContent = '💣';
}
}
}
setTimeout(() => {
alert(isWin ? 'Wygrałeś!' : 'Przegrana!');
}, 100);
}
// Sprawdzenie zwycięstwa
function checkWin() {
let revealedCells = 0;
const totalCells = BOARD_SIZE * BOARD_SIZE;
const mineCells = MINE_COUNT;
for (let row = 0; row < BOARD_SIZE; row++) {
for (let col = 0; col < BOARD_SIZE; col++) {
if (cells[row][col].isRevealed) {
revealedCells++;
}
}
}
if (revealedCells === totalCells - mineCells) {
gameOver(true);
}
}
// Reset gry
function resetGame() {
clearInterval(timerInterval);
timerInterval = null;
timer = 0;
mineCounter = MINE_COUNT;
document.getElementById('timer').textContent = '⏱️ 0';
document.getElementById('mines-counter').textContent = `💣 ${mineCounter}`;
createBoard();
gameActive = true;
}
// Pomocnicza funkcja
function isValidCell(row, col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
}
// Inicjalizacja
document.getElementById('reset-btn').addEventListener('click', resetGame);
createBoard();
</script>
</body>
</html>