帮我写一个扫雷,用html,css,和JavaScript
时间: 2023-02-25 14:01:48 浏览: 105
好的,我可以为您写一个简单的扫雷游戏。
HTML代码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>扫雷游戏</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<table id="board"></table>
</body>
<script src="game.js"></script>
</html>
```
CSS代码:
```
table {
border-collapse: collapse;
margin: 0 auto;
}
td {
width: 20px;
height: 20px;
border: 1px solid black;
text-align: center;
}
```
JavaScript代码:
```
const BOARD_SIZE = 10;
const MINE_PROBABILITY = 0.2;
const generateBoard = () => {
const board = [];
for (let i = 0; i < BOARD_SIZE; i++) {
const row = [];
for (let j = 0; j < BOARD_SIZE; j++) {
row.push(Math.random() < MINE_PROBABILITY ? '*' : '');
}
board.push(row);
}
return board;
};
const renderBoard = (board) => {
const table = document.getElementById('board');
table.innerHTML = '';
for (const row of board) {
const tr = document.createElement('tr');
for (const cell of row) {
const td = document.createElement('td');
td.innerHTML = cell;
tr.appendChild(td);
}
table.appendChild(tr);
}
};
const gameBoard = generateBoard();
renderBoard(gameBoard);
```
以上是一个简单的扫雷游戏代码,您可以根据自己的需求进行更改。
阅读全文