/
bingPuzzle-it.user.js
176 lines (148 loc) · 5.66 KB
/
bingPuzzle-it.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// ==UserScript==
// @name bing一键拼图
// @namespace knva
// @version 0.1
// @description bing拼图页面一键完成
// @require https://code.jquery.com/jquery-3.2.1.slim.min.js
// @author knva
// @match https://*.bing.com/spotlight/imagepuzzle*
// @icon https://www.google.com/s2/favicons?sz=64&domain=bing.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 创建一个函数来计算矩阵变化
function calculateMatrixChange(matrix1, matrix2) {
if (matrix1.length !== matrix2.length || matrix1[0].length !== matrix2[0].length) {
return "矩阵尺寸不匹配";
}
const rows = matrix1.length;
const cols = matrix1[0].length;
const changeMatrix = [];
for (let i = 0; i < rows; i++) {
const row = [];
for (let j = 0; j < cols; j++) {
let res = matrix2[i][j] - matrix1[i][j];
row.push(res);
}
changeMatrix.push(row);
}
return changeMatrix;
}
function findPositiveElements(matrix) {
const positiveElements = [];
for (let x = 0; x < matrix.length; x++) {
for (let y = 0; y < matrix[x].length; y++) {
if (matrix[x][y] > 0) {
positiveElements.push({ x, y });
}
}
}
return positiveElements;
}
var lastmatrix =[];
var clicklist =[];
function printBoard(board) {
if(lastmatrix.length==3){
let res = calculateMatrixChange(board,lastmatrix)
//console.log(res)
let xy = findPositiveElements(res)[0]
clicklist.push(xy);
}
lastmatrix = board;
}
function findBlank(board) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[i][j] === 0) {
return [i, j];
}
}
}
}
function isValidMove(x, y) {
return x >= 0 && x < 3 && y >= 0 && y < 3;
}
function solvePuzzle(initBoard) {
const targetBoard = [[1, 2, 3], [4, 5, 6], [7, 8, 0]];
const visited = new Set();
const queue = [[initBoard, []]];
while (queue.length > 0) {
const [currentBoard, moves] = queue.shift();
if (JSON.stringify(currentBoard) === JSON.stringify(targetBoard)) {
console.log("移动步骤:");
for (let move of moves) {
printBoard(move);
}
return;
}
const [x, y] = findBlank(currentBoard);
const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]];
for (let [dx, dy] of directions) {
const newX = x + dx;
const newY = y + dy;
if (isValidMove(newX, newY)) {
const newBoard = currentBoard.map(row => [...row]);
[newBoard[x][y], newBoard[newX][newY]] = [newBoard[newX][newY], newBoard[x][y]];
const boardString = JSON.stringify(newBoard);
if (!visited.has(boardString)) {
visited.add(boardString);
queue.push([newBoard, moves.concat([newBoard])]);
}
}
}
}
console.log("未找到解决方案。");
}
$(document).ready(function() {
var button = document.createElement("div");
button.className = "floating-button";
button.textContent = "全自动立刻开拼!";
// Add the button to the page
var insTitleElements = document.querySelectorAll(".insTitle");
insTitleElements.forEach(function(insTitleElement) {
var buttonClone = button.cloneNode(true);
insTitleElement.parentNode.insertBefore(buttonClone, insTitleElement.nextSibling);
// Button click event handler
buttonClone.addEventListener("click", function() {
// 在这里执行您想要的操作
alert("彻底疯狂!");
var alldata = []
var tiles = $('#tiles .tile');
tiles.each(function(index) {
var tile = $(this);
if(tile[0].nodeName=="DIV"){
var tileNumber = tile.find('.tileNumber');
if (tileNumber.length > 0) {
alldata.push(parseInt(tileNumber.text()))
} else {
alldata.push(0);
}
}
});
console.log(alldata);
//
var newArray = [];
var chunkSize = 3; // 每个子数组的长度
for (var i = 0; i < alldata.length; i += chunkSize) {
newArray.push(alldata.slice(i, i + chunkSize));
}
lastmatrix = newArray;
solvePuzzle(newArray);
console.log(clicklist);
function clickCoordsWithDelay(index) {
if (index < clicklist.length) {
const xy = clicklist[index];
$(`div[x='${xy.x}'][y='${xy.y}']`).click();
console.log(xy);
// 在下一个点击之前添加延迟
setTimeout(function () {
clickCoordsWithDelay(index + 1);
}, 500); // 500 毫秒延迟
}
}
clickCoordsWithDelay(0);
});
});
});
})();