html在线扫雷

<!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>

有手就行,下个版本推出标记雷的功能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IamATM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值