感谢之前几个朋友在贴吧讨论解拼图的方法,写了个js版本的,省去了手动把拼图块转换成数字的时间,使用比较方便。
具体用法是先登陆活动页面,按下F12功能键 或者如下图右键点击页面,选择菜单中的检查按钮。

然后在页面弹出的窗口的标签栏选择“控制台”

在光标处复制以下的代码并回车即可。
//从这里开始复制
startflag = false;
if(typeof d == "undefined"){
alert("请在拼图页面进行操作,已在页面请刷新页面重试。");
d = [,1,2,3,4,5,6,7,8,0]
startflag = false;
}
else{
startflag = true;
}
function CheckFinished(array){
for (let index = 1; index < 9; index++) {
if( array[index] != index)
return false;
}
return true;
}
function printresult(arr){
arr.forEach(element => {
console.log("点击拼图块:", element);
});
}
_direct = [[], [2,4], [1,3,5], [2,6], [1,5,7], [2,4,6,8], [3,5,9], [4,8], [5,7,9],[6,8]];
function mywhereCanTo(i, arr){
let movepos = 0;
availpos = _direct[i];
availpos.forEach(element => {
if(arr[element] == 0){
movepos = element;
}
});
return movepos;
}
const puzzleobj = {
moves: [],
status: []
};
walkedstatus = new Set();
bfsque = new Array();
inits = Object.create(puzzleobj);
inits.moves = [];
inits.status = d.join();
walkedstatus.add(d.join());
flag = false;
bfsque.push(inits);
while (startflag && bfsque.length != 0 ) {
s = bfsque.shift();
if(CheckFinished(s.status.split(','))){
console.log("此拼图还原需要的步数为:", s.moves.length);
flag = true;
printresult(s.moves);
break;
}
bfsarr = s.status.split(',');
for (let index = 1; index < 10; index++) {
const element = bfsarr[index];
const tarid = mywhereCanTo(index, bfsarr);
if(tarid != 0){
newbfsarr = bfsarr.concat();
newbfsarr[tarid] = newbfsarr[index];
newbfsarr[index] = 0;
if(walkedstatus.has(newbfsarr.join()))continue;
walkedstatus.add(newbfsarr.join())
newstatus = Object.create(puzzleobj);
newstatus.status = newbfsarr.join();
newstatus.moves = s.moves.concat();
newstatus.moves.push(index);
bfsque.push(newstatus);
}
}
}
if (!flag && startflag){
alert("这份拼图无解,我们的游戏没有BUG哦。");
}
//到这里复制结束
最后执行效果是这样子的:

如果没有达到预期效果可以截图在下面回复。
具体用法是先登陆活动页面,按下F12功能键 或者如下图右键点击页面,选择菜单中的检查按钮。

然后在页面弹出的窗口的标签栏选择“控制台”

在光标处复制以下的代码并回车即可。
//从这里开始复制
startflag = false;
if(typeof d == "undefined"){
alert("请在拼图页面进行操作,已在页面请刷新页面重试。");
d = [,1,2,3,4,5,6,7,8,0]
startflag = false;
}
else{
startflag = true;
}
function CheckFinished(array){
for (let index = 1; index < 9; index++) {
if( array[index] != index)
return false;
}
return true;
}
function printresult(arr){
arr.forEach(element => {
console.log("点击拼图块:", element);
});
}
_direct = [[], [2,4], [1,3,5], [2,6], [1,5,7], [2,4,6,8], [3,5,9], [4,8], [5,7,9],[6,8]];
function mywhereCanTo(i, arr){
let movepos = 0;
availpos = _direct[i];
availpos.forEach(element => {
if(arr[element] == 0){
movepos = element;
}
});
return movepos;
}
const puzzleobj = {
moves: [],
status: []
};
walkedstatus = new Set();
bfsque = new Array();
inits = Object.create(puzzleobj);
inits.moves = [];
inits.status = d.join();
walkedstatus.add(d.join());
flag = false;
bfsque.push(inits);
while (startflag && bfsque.length != 0 ) {
s = bfsque.shift();
if(CheckFinished(s.status.split(','))){
console.log("此拼图还原需要的步数为:", s.moves.length);
flag = true;
printresult(s.moves);
break;
}
bfsarr = s.status.split(',');
for (let index = 1; index < 10; index++) {
const element = bfsarr[index];
const tarid = mywhereCanTo(index, bfsarr);
if(tarid != 0){
newbfsarr = bfsarr.concat();
newbfsarr[tarid] = newbfsarr[index];
newbfsarr[index] = 0;
if(walkedstatus.has(newbfsarr.join()))continue;
walkedstatus.add(newbfsarr.join())
newstatus = Object.create(puzzleobj);
newstatus.status = newbfsarr.join();
newstatus.moves = s.moves.concat();
newstatus.moves.push(index);
bfsque.push(newstatus);
}
}
}
if (!flag && startflag){
alert("这份拼图无解,我们的游戏没有BUG哦。");
}
//到这里复制结束
最后执行效果是这样子的:

如果没有达到预期效果可以截图在下面回复。