WordPress

推荐列表 站点导航

当前位置:首页 > 建站教程 > WordPress >

基于JavaScript实现十五拼图代码实例介绍

来源:网络整理  作者:  发布时间:2020-12-18 13:00
这篇文章主要介绍了基于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) {

基于JavaScript实现十五拼图代码实例介绍 (https://www.wpmee.com/) javascript教程 第1张

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

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

基于JavaScript实现十五拼图代码实例介绍

2020-12-18 编辑:

<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) {

基于JavaScript实现十五拼图代码实例介绍 (https://www.wpmee.com/) javascript教程 第1张

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";

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jz/wp/5195.shtml

相关文章

风云图片

推荐阅读

返回WordPress频道首页