html 4×4数字华容道
时间: 2024-01-24 22:17:41 浏览: 172
数字华容道
以下是一个简单的 4x4 数字华容道的 HTML 实现:
```html
<!DOCTYPE html>
<html>
<head>
<title>数字华容道</title>
<style>
table {
border-collapse: collapse;
margin: 20px auto;
}
td {
border: 1px solid black;
width: 60px;
height: 60px;
text-align: center;
font-size: 30px;
cursor: pointer;
}
td.empty {
background-color: white;
}
td.filled {
background-color: lightgray;
}
</style>
</head>
<body onload="init()">
<table>
<tr>
<td id="cell00"></td>
<td id="cell01"></td>
<td id="cell02"></td>
<td id="cell03"></td>
</tr>
<tr>
<td id="cell10"></td>
<td id="cell11"></td>
<td id="cell12"></td>
<td id="cell13"></td>
</tr>
<tr>
<td id="cell20"></td>
<td id="cell21"></td>
<td id="cell22"></td>
<td id="cell23"></td>
</tr>
<tr>
<td id="cell30"></td>
<td id="cell31"></td>
<td id="cell32"></td>
<td id="cell33"></td>
</tr>
</table>
<button onclick="shuffle()">打乱</button>
<script>
var board = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]];
var emptyRow = 3;
var emptyCol = 3;
function init() {
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
if (board[i][j] == 0) {
document.getElementById("cell" + i + j).className = "empty";
} else {
document.getElementById("cell" + i + j).className = "filled";
}
document.getElementById("cell" + i + j).innerHTML = board[i][j];
document.getElementById("cell" + i + j).onclick = function() { move(this); };
}
}
}
function shuffle() {
var numShuffles = 100;
while (numShuffles > 0) {
var direction = Math.floor(Math.random() * 4);
if (canMove(direction)) {
makeMove(direction);
numShuffles--;
}
}
init();
}
function canMove(direction) {
switch (direction) {
case 0: // up
return emptyRow > 0;
case 1: // right
return emptyCol < 3;
case 2: // down
return emptyRow < 3;
case 3: // left
return emptyCol > 0;
}
}
function makeMove(direction) {
switch (direction) {
case 0: // up
var temp = board[emptyRow - 1][emptyCol];
board[emptyRow - 1][emptyCol] = 0;
board[emptyRow][emptyCol] = temp;
emptyRow--;
break;
case 1: // right
var temp = board[emptyRow][emptyCol + 1];
board[emptyRow][emptyCol + 1] = 0;
board[emptyRow][emptyCol] = temp;
emptyCol++;
break;
case 2: // down
var temp = board[emptyRow + 1][emptyCol];
board[emptyRow + 1][emptyCol] = 0;
board[emptyRow][emptyCol] = temp;
emptyRow++;
break;
case 3: // left
var temp = board[emptyRow][emptyCol - 1];
board[emptyRow][emptyCol - 1] = 0;
board[emptyRow][emptyCol] = temp;
emptyCol--;
break;
}
}
function move(cell) {
var row = parseInt(cell.id.charAt(4));
var col = parseInt(cell.id.charAt(5));
if (row == emptyRow && col == emptyCol + 1 && canMove(1)) { // right
makeMove(1);
} else if (row == emptyRow && col == emptyCol - 1 && canMove(3)) { // left
makeMove(3);
} else if (row == emptyRow + 1 && col == emptyCol && canMove(2)) { // down
makeMove(2);
} else if (row == emptyRow - 1 && col == emptyCol && canMove(0)) { // up
makeMove(0);
}
init();
if (isSolved()) {
alert("恭喜你,成功了!");
}
}
function isSolved() {
var value = 0;
for (var i = 0; i < 4; i++) {
for (var j = 0; j < 4; j++) {
if (board[i][j] != value) {
return false;
}
value++;
}
}
return true;
}
</script>
</body>
</html>
```
这个实现使用了一个 4x4 的表格来显示华容道的状态。每个单元格都是一个可点击的元素,单击后会尝试移动数字块。打乱按钮会随机打乱数字块的位置。当成功还原华容道时,会弹出一个对话框提示用户。
阅读全文