<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>在线扫雷</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f9fc;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
h1 {
text-align: center;
margin-bottom: 20px;
font-size: 36px;
color: #333;
}
.game-container {
background-color: #fff;
border-radius: 10px;
padding: 20px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}
.input-group {
margin-top: 10px;
display: flex;
align-items: center;
}
label {
font-size: 16px;
color: #555;
margin-right: 5px;
}
input[type="number"] {
width: 60px;
padding: 5px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
background-color: #007bff;
color: white;
border: none;
padding: 8px 15px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #0056b3;
}
#timer {
font-size: 18px;
color: #555;
margin-left: 10px;
}
.board {
display: grid;
grid-template-columns: repeat(var(--cols), 40px);
grid-template-rows: repeat(var(--rows), 40px);
gap: 1px;
margin-top: 20px;
justify-content: center;
align-items: center;
}
.cell {
width: 40px;
height: 40px;
border: 1px solid #aaa;
text-align: center;
line-height: 40px;
font-size: 20px;
cursor: pointer;
background-color: #ccc;
}
.cell.clicked {
background-color: #ddd;
}
</style>
</head>
<body>
<div class="game-container">
<h1>扫雷</h1>
<div class="input-group">
<label for="rows">长:</label>
<input type="number" id="rows" value="10" min="1">
<label for="cols">宽:</label>
<input type="number" id="cols" value="10" min="1">
<label for="mines">雷的个数:</label>
<input type="number" id="mines" value="15" min="1">
<button onclick="startGame()">开始游戏</button>
<span id="timer">时间: 0 s</span>
</div>
<div class="board" id="board"></div>
</div>
<script>
let boardRows = 10;
let boardCols = 10;
let mineCount = 20;
let gameStarted = false;
let firstClick = true;
let timerInterval;
let seconds = 0;
const boardElement = document.getElementById('board');
const cells = [];
let minePositions = new Set();
let revealedCount = 0;
function createCell(index) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.addEventListener('click', () => {
if (!gameStarted) {
startTimer();
gameStarted = true;
}
revealCell(index);
});
return cell;
}
function revealCell(index) {
const cell = cells[index];
if (cell.classList.contains('clicked')) return;
cell.classList.add('clicked');
if (minePositions.has(index)) {
cell.textContent = '💣';
if (firstClick) {
alert('你很幸运,一点就点到了雷!');
} else {
handleGameOver();
}
} else {
revealedCount++;
const nearbyMines = getNearbyMineCount(index);
if (nearbyMines > 0) {
cell.textContent = nearbyMines;
} else {
const nearbyIndexes = getNearbyIndexes(index);
for (const nearbyIndex of nearbyIndexes) {
revealCell(nearbyIndex);
}
}
checkVictory();
}
if (firstClick) {
firstClick = false;
}
}
function handleGameOver() {
clearInterval(timerInterval);
for (const mineIndex of minePositions) {
cells[mineIndex].textContent = '💣';
}
alert('游戏结束你输了');
}
function checkVictory() {
const totalCells = boardRows * boardCols;
const nonMineCells = totalCells - mineCount;
if (revealedCount === nonMineCells) {
clearInterval(timerInterval);
alert('太好了你赢了!花费' + seconds + ' s!');
initializeGame();
}
}
function startTimer() {
timerInterval = setInterval(() => {
seconds++;
document.getElementById('timer').textContent = '时间: ' + seconds + ' s';
}, 1000);
}
function getNearbyIndexes(index) {
const indexes = [];
const row = Math.floor(index / boardCols);
const col = index % boardCols;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
const newRow = row + i;
const newCol = col + j;
if (newRow >= 0 && newRow < boardRows && newCol >= 0 && newCol < boardCols) {
indexes.push(newRow * boardCols + newCol);
}
}
}
return indexes;
}
function getNearbyMineCount(index) {
const nearbyIndexes = getNearbyIndexes(index);
let count = 0;
for (const nearbyIndex of nearbyIndexes) {
if (minePositions.has(nearbyIndex)) {
count++;
}
}
return count;
}
function initializeGame() {
boardElement.innerHTML = '';
minePositions.clear();
revealedCount = 0;
gameStarted = false;
firstClick = true;
seconds = 0;
clearInterval(timerInterval);
boardRows = parseInt(document.getElementById('rows').value);
boardCols = parseInt(document.getElementById('cols').value);
mineCount = parseInt(document.getElementById('mines').value);
if (boardRows <= 0 || boardCols <= 0 || mineCount <= 0) {
alert('Rows, columns, and mine count must be positive integers.');
return;
}
if (mineCount >= boardRows * boardCols) {
alert('Mine count must be less than the total number of cells.');
return;
}
while (minePositions.size < mineCount) {
const randomPosition = Math.floor(Math.random() * (boardRows * boardCols));
minePositions.add(randomPosition);
}
boardElement.style.setProperty('--cols', boardCols);
boardElement.style.setProperty('--rows', boardRows);
cells.length = 0;
for (let i = 0; i < boardRows * boardCols; i++) {
const cell = createCell(i);
cells.push(cell);
boardElement.appendChild(cell);
}
document.getElementById('timer').textContent = '时间: 0 s';
}
function startGame() {
initializeGame();
}
initializeGame();
</script>
<script type="text/javascript" src="js/hua.js"></script>
</body>
</html>
有手就行,下个版本推出标记雷的功能