基于JavaScript实现十五拼图代码实例介绍
<h1>Fifteen Puzzle</h1>
createSquares();
}
let tempLeft = neigbors[ranNum].style.left;
createSquares();
select.id = "select";
div.innerHTML = i;
a postmaster in Canastota, New York.
else if (spaceColumn == divColumn) {

document.getElementById("output").innerHTML = "";
select.appendChild(option);
JavaScript代码
function setSize() {
function createSquares() {
// 将拼图移动到空白图块处
<p>
<title>CSE 154 Fifteen Puzzle</title>
return false;
}
// creates 15 puzzle tiles and sets them to their initial position
neigbors.push(allPuzzles[i]);
<p>
let spaceRow = 3; // 空白图块所在行
var divRow = parseInt(div.style.top) / WIDTH;
}
<div></div>
cursor: pointer;
var tiles = document.querySelectorAll(".puzzletile");
.puzzletile, #output {
color: red;
};
}
}
<!-- your files that you will write -->
spaceRow = divRow;
function win() {
font-family: cursive;
}
this.classList.remove("highlight"); // when mouse out, remove class "highlight"
})();
var divColumn = parseInt(div.style.left) / WIDTH;
div.style.backgroundPosition = x + " " + y;
// 将与空白图块相邻的拼图 存储到数组neigbors中
let spaceColumn = 3; // 空白图块所在列
if (moveable(this)) {
</div>
// 为每个拼图添加ID
let NUM = 4; //拼图的行列数 the number of rows/cols in the puzzle
var divColumn = parseInt(div.style.left) / WIDTH;
for (var i = 3; i < 7; i++) {
margin: 0 auto;
}
setEvents(div);
};
setSize();
by repeatedly making moves that slide squares into the empty space.
function changeSize() {
for (var i = 0; i < tiles.length; i++) {
}
}
<p>
}
}
let WIDTH = 100; // the pixel width/height of each tile
American puzzle author and mathematician Sam Loyd is often falsely
body {if (win()) {
function helper() {
if (tiles[i].id != "square_" + row + "_" + column) {
div.className = "puzzletile";
function shuffle() {
this area holds the actual fifteen puzzle pieces
<html lang="en">
if (moveable(allPuzzles[i]))
document.getElementById("output").innerHTML = "Congratulations! You win!";
div.id = "square_" + spaceRow + "_" + spaceColumn;
return Math.abs(spaceRow - divRow) == 1;
let neigbors = [];
WIDTH = parseInt(400 / this.value);
(function () {spaceColumn = divColumn;
}
until his death in 1911 that he invented it.
border: 5px solid black;
The puzzle was actually created around 1874 by Noyes Palmer Chapman,
}
return Math.abs(spaceColumn - divColumn) == 1;
}
<script src="https://www.wpmee.com/js/fifteen.js" type="text/javascript"></script>
<button>Shuffle</button>
return true;
if (moveable(this)) {
document.getElementById("select").onchange = changeSize;
div.onmouseout = function () {
}
max = Math.floor(max);
</body>
var y = row * -1 * WIDTH + "px";
// 获取指定区间的随机数
document.getElementById("puzzlearea").appendChild(div);
html代码
} else {
// sets up events for all puzzle tiles
console.log(tiles[i].id);
</head>
var divRow = parseInt(div.style.top) / WIDTH;
css代码
let allPuzzles = document.getElementsByClassName("puzzletile");
.puzzletile {
return false;
position: relative;
text-align: center;
for (let i = 0; i < allPuzzles.length; i++) {
// 获取需要移动的拼图移动之前的偏移量
var row = Math.floor(i / NUM);
// when mouse out, removes class "highlight"
}
// 实现Shuffle算法
}
The goal of the fifteen puzzle is to un-jumble its fifteen squares
spaceRow = this.value - 1;
this.classList.add("highlight"); // when mouse over, adds class "highlight"
顾名思义,十五拼图就是将游戏画面中的数字从上到下,从左到右按顺序从1到15排列下来,看起来很简单,但是玩起来不容易。
while (puzzlearea.contains(document.querySelector(".puzzletile"))) {
document.getElementById("shufflebutton").onclick = shuffle;
font-size: 14pt;
let tempTop = neigbors[ranNum].style.top;
}
NUM = this.value;
var row = Math.floor((i - 1) / NUM);
// shuffles puzzle tiles for 1000 times
}
<div>
spaceRow = parseInt(tempTop) / WIDTH;
// 设置下拉列表 默认选中 option4
div.style.left = column * WIDTH + "px";
<meta charset="UTF-8">
for (let j = 0; j < 1000; j++) {
// make one move for the given tile
div.onmouseover = function () {
}
var puzzlearea = document.getElementById("puzzlearea");
<!--
credited with creating the puzzle; indeed, Loyd claimed from 1891
}
// 设置background 的 position
// 得到一个随机的索引
// add a drop-down list to select difficulty level
}
// gets everything set when the window has loaded
min = Math.ceil(min);
}
option.id = "option" + i;
}
</p>
var column = (i - 1) % NUM;
if (spaceRow == divRow) {
function setEvents(div) {
neigbors[ranNum].style.top = spaceRow * WIDTH + "px";
// return true if all tiles are at their original positions
var select = document.createElement("select");
spaceColumn = this.value - 1;
.highlight {
function moveable(div) {
position: absolute;
for (var i = 1; i < NUM * NUM; i++) {
}
.highlight, #output {
// 更改空白图块的位置
}
// 将所有的拼图 存储到 allPuzzles中
// 减去边框的宽度 并且宽高相等
font-size: 40pt;
document.getElementById("option4").selected = "selected";
};
这篇文章主要介绍了基于JavaScript实现十五拼图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
}
option.innerHTML = i + " * " + i;
<link href="https://www.wpmee.com/css/fifteen.css" type="text/css"/>
<!DOCTYPE html>if (moveable(this)) {
"use strict";
div.style.width = div.style.height;
return Math.floor(Math.random() * (max - min + 1)) + min;
div.id = "square_" + row + "_" + column;
var column = i % NUM;
neigbors[ranNum].style.left = spaceColumn * WIDTH + "px";
add to it as you need to
background-image: url("../background.jpg");
</p>
}
width: 400px;
document.getElementById("controls").appendChild(select);
option.value = i;
div.onclick = helper;
div.style.left = spaceColumn * WIDTH + "px";
neigbors[ranNum].id = "square_" + spaceRow + "_" + spaceColumn;
// displays "congratulations" if the player wins
window.onload = function () {
How quickly can you solve it?
else {
-->
</p>
<head>
makeAMove(this);
// return true if the given tile is moveable
}
puzzlearea.removeChild(document.querySelector(".puzzletile"));
// 设置水平和垂直方向的偏移量
// a helper function for function "makeAMove"
var x = column * -1 * WIDTH + "px";
height: 400px;
spaceColumn = parseInt(tempLeft) / WIDTH;
div.style.top = spaceRow * WIDTH + "px";
border-color: red;
var option = document.createElement("option");
// 判断拼图是否可以移动
}
</html>
#puzzlearea {
function makeAMove(div) {
div.style.height = WIDTH - 10 + "px";
<body>
var div = document.createElement("div");
function getRandomIntInclusive(min, max) {
最后的效果
let ranNum = getRandomIntInclusive(0, neigbors.length - 1);
div.style.top = row * WIDTH + "px";
相关热词: javascript 实例
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jz/wp/5195.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
ludou_se_only); } add_action(s
时间:2021-01-23
-
所以经常导致输错密码的
时间:2021-01-23
-
WordPress措施打开速度慢的
时间:2021-01-22
-
WordPress回覆评论自动添加
时间:2021-01-22
-
我们可以通过下面的代码
时间:2021-01-22
-
如何修改WordPress默认脚色
时间:2021-01-20
-
同样的修改文件就行
时间:2021-01-20
-
少数善意的转载者既使没
时间:2021-01-20
热门文章
-
WordPress新手安装教程(图文)
时间:2020-12-28
-
在设置菜单中增加WordPress私密 隐藏的全部
时间:2021-01-10
-
WordPress博客程序常见错误的解决方法
时间:2020-12-28
-
WordPress程序打开速度慢的三种解决方法
时间:2021-01-10
-
wordpress使用代码在每篇文章尾部添加版权
时间:2020-12-14
-
WordPress博客措施常见错误的办理要领
时间:2021-01-15
-
关于WordPress的SEO优化相关的一些PHP页面脚
时间:2021-01-07
-
WordPress措施打开速度慢的三种办理要领
时间:2021-01-22
-
详解WordPress中的头像缓存和署理中的缓存
时间:2021-01-15
-
少数善意的转载者既使没有看到版权信息
时间:2021-01-20
