Oto kod HTML, JS i CSS dla pełnofunkcjonalnej gry Saper:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Saper</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f0f0f0;
margin: 0;
padding: 20px;
}
.controls {
margin-bottom: 20px;
display: flex;
gap: 20px;
align-items: center;
}
.game-info {
display: flex;
justify-content: space-between;
width: 100%;
max-width: 500px;
margin-bottom: 10px;
}
.counter {
background-color: black;
color: red;
font-family: monospace;
font-size: 24px;
padding: 5px 10px;
border-radius: 4px;
}
.board {
display: grid;
grid-template-columns: repeat(var(--cols), 30px);
grid-template-rows: repeat(var(--rows), 30px);
gap: 1px;
background-color: #bdbdbd;
border: 3px solid #7b7b7b;
border-right-color: #ffffff;
border-bottom-color: #ffffff;
padding: 5px;
}
.cell {
width: 30px;
height: 30px;
background-color: #c0c0c0;
border: 2px solid;
border-color: #ffffff #7b7b7b #7b7b7b #ffffff;
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
font-size: 18px;
user-select: none;
}
.cell.revealed {
background-color: #d9d9d9;
border: 1px solid #7b7b7b;
}
.cell.mine {
background-color: #ff0000;
}
.cell.flagged::after {
content: "🚩";
}
.cell[data-value="1"] { color: blue; }
.cell[data-value="2"] { color: green; }
.cell[data-value="3"] { color: red; }
.cell[data-value="4"] { color: darkblue; }
.cell[data-value="5"] { color: brown; }
.cell[data-value="6"] { color: teal; }
.cell[data-value="7"] { color: black; }
.cell[data-value="8"] { color: gray; }
button {
padding: 8px 16px;
font-size: 16px;
cursor: pointer;
}
select {
padding: 8px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Saper</h1>
<div class="controls">
<select id="difficulty">
<option value="beginner">Początkujący (9x9, 10 min)</option>
<option value="intermediate">Średni (16x16, 40 min)</option>
<option value="expert">Ekspert (30x16, 99 min)</option>
</select>
<button id="new-game">Nowa gra</button>
</div>
<div class="game-info">
<div class="counter" id="mines-counter">000</div>
<button id="reset">😊</button>
<div class="counter" id="timer">000</div>
</div>
<div class="board" id="board"></div>
<script>
class Minesweeper {
constructor(rows, cols, mines) {
this.rows = rows;
this.cols = cols;
this.mines = mines;
this.board = [];
this.revealed = 0;
this.gameOver = false;
this.firstClick = true;
this.flagsPlaced = 0;
this.timerInterval = null;
this.timeElapsed = 0;
this.initBoard();
this.renderBoard();
this.updateMinesCounter();
this.resetTimer();
}
initBoard() {
// Initialize empty board
this.board = [];
for (let i = 0; i < this.rows; i++) {
this.board[i] = [];
for (let j = 0; j < this.cols; j++) {
this.board[i][j] = {
isMine: false,
isRevealed: false,
isFlagged: false,
adjacentMines: 0
};
}
}
}
placeMines(firstRow, firstCol) {
let minesPlaced = 0;
// Create a safe zone around the first click
const safeZone = [];
for (let i = Math.max(0, firstRow - 1); i <= Math.min(this.rows - 1, firstRow + 1); i++) {
for (let j = Math.max(0, firstCol - 1); j <= Math.min(this.cols - 1, firstCol + 1); j++) {
safeZone.push(`${i},${j}`);
}
}
while (minesPlaced < this.mines) {
const row = Math.floor(Math.random() * this.rows);
const col = Math.floor(Math.random() * this.cols);
// Skip if mine already placed or in safe zone
if (this.board[row][col].isMine || safeZone.includes(`${row},${col}`)) {
continue;
}
this.board[row][col].isMine = true;
minesPlaced++;
// Update adjacent mines count
for (let i = Math.max(0, row - 1); i <= Math.min(this.rows - 1, row + 1); i++) {
for (let j = Math.max(0, col - 1); j <= Math.min(this.cols - 1, col + 1); j++) {
if (i === row && j === col) continue;
this.board[i][j].adjacentMines++;
}
}
}
}
renderBoard() {
const boardElement = document.getElementById('board');
boardElement.innerHTML = '';
boardElement.style.setProperty('--rows', this.rows);
boardElement.style.setProperty('--cols', this.cols);
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
const cell = document.createElement('div');
cell.className = 'cell';
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener('click', () => this.handleCellClick(i, j));
cell.addEventListener('contextmenu', (e) => {
e.preventDefault();
this.handleRightClick(i, j);
});
boardElement.appendChild(cell);
}
}
}
updateBoard() {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
const cell = document.querySelector(`.cell[data-row="${i}"][data-col="${j}"]`);
const cellData = this.board[i][j];
if (cellData.isRevealed) {
cell.classList.add('revealed');
if (cellData.isMine) {
cell.classList.add('mine');
cell.textContent = '💣';
} else if (cellData.adjacentMines > 0) {
cell.textContent = cellData.adjacentMines;
cell.dataset.value = cellData.adjacentMines;
}
} else {
cell.classList.remove('revealed');
cell.textContent = '';
if (cellData.isFlagged) {
cell.classList.add('flagged');
} else {
cell.classList.remove('flagged');
}
}
}
}
}
handleCellClick(row, col) {
if (this.gameOver || this.board[row][col].isFlagged || this.board[row][col].isRevealed) {
return;
}
if (this.firstClick) {
this.firstClick = false;
this.placeMines(row, col);
this.startTimer();
}
if (this.board[row][col].isMine) {
this.gameOver = true;
this.revealAllMines();
this.stopTimer();
document.getElementById('reset').textContent = '😵';
return;
}
this.revealCell(row, col);
this.updateBoard();
if (this.checkWin()) {
this.gameOver = true;
this.stopTimer();
document.getElementById('reset').textContent = '😎';
this.flagAllMines();
}
}
handleRightClick(row, col) {
if (this.gameOver || this.board[row][col].isRevealed) {
return;
}
const cell = this.board[row][col];
if (cell.isFlagged) {
cell.isFlagged = false;
this.flagsPlaced--;
} else {
if (this.flagsPlaced < this.mines) {
cell.isFlagged = true;
this.flagsPlaced++;
}
}
this.updateBoard();
this.updateMinesCounter();
}
revealCell(row, col) {
const cell = this.board[row][col];
if (cell.isRevealed || cell.isFlagged) {
return;
}
cell.isRevealed = true;
this.revealed++;
if (cell.adjacentMines === 0) {
// Reveal adjacent cells if no mines nearby
for (let i = Math.max(0, row - 1); i <= Math.min(this.rows - 1, row + 1); i++) {
for (let j = Math.max(0, col - 1); j <= Math.min(this.cols - 1, col + 1); j++) {
if (i === row && j === col) continue;
this.revealCell(i, j);
}
}
}
}
revealAllMines() {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
if (this.board[i][j].isMine) {
this.board[i][j].isRevealed = true;
}
}
}
this.updateBoard();
}
flagAllMines() {
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
if (this.board[i][j].isMine && !this.board[i][j].isFlagged) {
this.board[i][j].isFlagged = true;
this.flagsPlaced++;
}
}
}
this.updateBoard();
this.updateMinesCounter();
}
checkWin() {
return this.revealed === (this.rows * this.cols - this.mines);
}
updateMinesCounter() {
const counter = document.getElementById('mines-counter');
const remainingMines = this.mines - this.flagsPlaced;
counter.textContent = remainingMines.toString().padStart(3, '0');
}
startTimer() {
this.stopTimer();
this.timerInterval = setInterval(() => {
this.timeElapsed++;
this.updateTimer();
}, 1000);
}
stopTimer() {
if (this.timerInterval) {
clearInterval(this.timerInterval);
this.timerInterval = null;
}
}
resetTimer() {
this.stopTimer();
this.timeElapsed = 0;
this.updateTimer();
}
updateTimer() {
const timer = document.getElementById('timer');
timer.textContent = Math.min(999, this.timeElapsed).toString().padStart(3, '0');
}
}
// Game initialization
let game;
function startNewGame() {
const difficulty = document.getElementById('difficulty').value;
let rows, cols, mines;
switch (difficulty) {
case 'beginner':
rows = 9;
cols = 9;
mines = 10;
break;
case 'intermediate':
rows = 16;
cols = 16;
mines = 40;
break;
case 'expert':
rows = 16;
cols = 30;
mines = 99;
break;
default:
rows = 9;
cols = 9;
mines = 10;
}
if (game) {
game.stopTimer();
}
game = new Minesweeper(rows, cols, mines);
document.getElementById('reset').textContent = '😊';
}
document.getElementById('new-game').addEventListener('click', startNewGame);
document.getElementById('reset').addEventListener('click', startNewGame);
document.getElementById('difficulty').addEventListener('change', startNewGame);
// Prevent context menu on right-click
document.getElementById('board').addEventListener('contextmenu', (e) => {
e.preventDefault();
});
// Start the game
startNewGame();
</script>
</body>
</html>
Czy chciałbyś, żebym wyjaśnił lub omówił jakąś część kodu?