-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
277 lines (260 loc) · 10.8 KB
/
renderer.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* This file is loaded via the <script> tag in the index.html file and will
* be executed in the renderer process for that window. No Node.js APIs are
* available in this process because `nodeIntegration` is turned off and
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
* to expose Node.js functionality from the main process.
*/
window.onload=function () {
moveHorse = function (x,y) {
let h = document.getElementById("horse")
h.style.left = -40 + 60*x +"px"
h.style.top = -40 + 60*y +"px"
}
addTrace = function(t,x,y){
step = document.getElementsByClassName("inner-step")[0]
step.innerHTML += t+":("+x+","+y+")<br>"
}
clearTrace = function(){
step = document.getElementsByClassName("inner-step")[0]
step.innerHTML = ""
}
leaveX = function(n,x,y,i){
let mark = document.getElementsByClassName("x")[(y-1)*n+x-1]
mark.style.display = "block"
mark.innerText=i
}
calcRoute = function (x,y,startx,starty) {
let moveStack = []
let ifTouch = []
let access= []
let direction = []
for(let i = 1;i<=x;i++){
ifTouch[i]=[]
access[i]=[]
direction[i]=[]
for(let j=1;j<=y;j++){
ifTouch[i][j]=false
access[i][j]=8
direction[i][j]=-1
}
}
for(let i=1;i<=x;i++){
access[i][1]=4
access[i][2]=6
access[i][y-1]=6
access[i][y]=4
}
for(let i=1;i<=y;i++){
access[1][i]=4
access[2][i]=6
access[x-1][i]=6
access[x][i]=4
}
access[1][1]=2;access[x][1]=2;access[1][y]=2;access[x][y]=2
access[2][2]=4;access[x-1][2]=4;access[2][y-1]=4;access[x-1][y-1]=4
access[1][2]=3;access[2][1]=3;access[x][2]=3;access[x-1][1]=3;access[1][y-1]=3;access[2][y]=3;access[x-1][y]=3;access[x][y-1]=3;
fx=[1,2,2,1,-1,-2,-2,-1];
fy=[2,1,-1,-2,-2,-1,1,2];
move = function (now) {
if(moveStack[now].x<1||moveStack[now].x>x||moveStack[now].y<1||moveStack[now].y>y){
return false
}
if(ifTouch[moveStack[now].x][moveStack[now].y]){
return false
}
if(now === x*y){
return true
}
//console.log(now)
//处理ifTouch和access
ifTouch[moveStack[now].x][moveStack[now].y]=true
for(let i = 0;i<8;i++){
//console.log(now,nowx,nowy)
if(moveStack[now].x+fx[i]>=1&&moveStack[now].x+fx[i]<=x&&moveStack[now].y+fy[i]>=1&&moveStack[now].y+fy[i]<=y) {
access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]--
}
}
// for(let i = 0;i<8;i++){
// //console.log(now,nowx,nowy)
// moveStack[now+1]={x:moveStack[now].x+fx[i],y:moveStack[now].y+fy[i]}
// if(move(now+1)) {
// return true
// }else {
// moveStack[now+1]=null
// }
// }
//递归
while (true){
//该点第一次遍历
//console.log(now,moveStack[now].x,moveStack[now].y)
//console.log(direction)
//console.log(1)
if (direction[moveStack[now].x][moveStack[now].y]===-1){
//console.log(2)
min = 10
di = -1
//找最小方向
for(let i = 0;i<8;i++){
if((moveStack[now].x+fx[i]>=1&&moveStack[now].x+fx[i]<=x&&moveStack[now].y+fy[i]>=1&&moveStack[now].y+fy[i]<=y)&&!ifTouch[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]) {
if(access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]<min){
min=access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]
di=i
}
}
}
//有无最小方向
if(di===-1){
break;
}else {
direction[moveStack[now].x][moveStack[now].y]=di
moveStack[now+1]={x:moveStack[now].x+fx[di],y:moveStack[now].y+fy[di]}
if(move(now+1)) {
return true
}else {
moveStack[now+1]=null
}
}
}
//非第一次遍历
else {
//console.log(3)
min=access[moveStack[now].x+fx[direction[moveStack[now].x][moveStack[now].y]]][moveStack[now].y+fy[direction[moveStack[now].x][moveStack[now].y]]]
di = -1
//查找相同值
for(let i =direction[moveStack[now].x][moveStack[now].y]+1;i<8;i++){
if((moveStack[now].x+fx[i]>=1&&moveStack[now].x+fx[i]<=x&&moveStack[now].y+fy[i]>=1&&moveStack[now].y+fy[i]<=y)&&!ifTouch[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]) {
if(access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]===min){
min=access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]
di=i
break;
}
}
}
//存在相同值
if(di!==-1){
direction[moveStack[now].x][moveStack[now].y]=di
moveStack[now+1]={x:moveStack[now].x+fx[di],y:moveStack[now].y+fy[di]}
if(move(now+1)) {
return true
}else {
moveStack[now+1]=null
}
}
//不存在相同值
else {
lastmin = min
min = 10
for(let i = 0;i<8;i++){
if((moveStack[now].x+fx[i]>=1&&moveStack[now].x+fx[i]<=x&&moveStack[now].y+fy[i]>=1&&moveStack[now].y+fy[i]<=y)&&!ifTouch[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]) {
if(access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]<min&&access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]>lastmin){
min=access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]
di=i
}
}
}
//有无最小方向
if(di===-1){
break;
}else {
direction[moveStack[now].x][moveStack[now].y]=di
moveStack[now+1]={x:moveStack[now].x+fx[di],y:moveStack[now].y+fy[di]}
if(move(now+1)) {
return true
}else {
moveStack[now+1]=null
}
}
}
}
}
//还原ifTouch和access
for(let i = 0;i<8;i++){
if(moveStack[now].x+fx[i]>=1&&moveStack[now].x+fx[i]<=x&&moveStack[now].y+fy[i]>=1&&moveStack[now].y+fy[i]<=y) {
//console.log(access,moveStack[now].x+fx[i],moveStack[now].y+fy[i])
access[moveStack[now].x+fx[i]][moveStack[now].y+fy[i]]++
}
}
ifTouch[moveStack[now].x][moveStack[now].y]=false
direction[moveStack[now].x][moveStack[now].y]=-1
return false
}
moveStack[1]={x:startx,y:starty}
return {flag:move(1),stack:moveStack}
}
x = document.getElementById("x")
y = document.getElementById("y")
startx = document.getElementById("start-x")
starty = document.getElementById("start-y")
process = document.getElementById("process")
const { ipcRenderer } = require('electron')
var interval
document.getElementById("quit").addEventListener("click",function () {
ipcRenderer.send("close")
})
document.getElementById("start").addEventListener("click",function () {
xValue = parseInt(x.value)
yValue = parseInt(y.value)
startxValue = parseInt(startx.value)
startyValue = parseInt(starty.value)
ifProcess = !process.checked
console.log(ifProcess)
checksize = function (t,max,min) {
return t<=max && t>=min &&t
}
if(checksize(xValue,99,1) &&
checksize(yValue,99,1)&&
checksize(startxValue,xValue,1)&&
checksize(startyValue,yValue,1)){
//绘制棋盘格
grid = document.getElementsByClassName("grid")[0]
str = "<div id=\"horse\"></div>"
for (let i=0;i<(xValue-1)*(yValue-1);i++){
str += "<div class=\"item\"></div>"
}
for(let i=1;i<=yValue;i++){
for (let j=1;j<=xValue;j++){
str += "<div class='x' style='left: "+ (-40 + 60*j) +"px;top:"+ (-40 + 60*i) + "px' >x</div>"
}
}
grid.innerHTML = str
//移动马到初始位置
grid.style.gridTemplateColumns = "repeat("+(xValue-1)+", 60px)"
grid.style.gridTemplateRows = "repeat("+(yValue-1)+", 60px)"
moveHorse(startxValue,startyValue)
obj = calcRoute(xValue,yValue,startxValue,startyValue)
stack = obj.stack
if(!obj.flag) {
ipcRenderer.send("nosolution")
}
let t = 1
clearTrace()
clearInterval(interval)
if(ifProcess){
interval = setInterval(function () {
if(!stack[t]){
clearInterval(interval)
return
}
if(t>1)
leaveX(xValue,stack[t-1].x,stack[t-1].y,t-1)
moveHorse(stack[t].x,stack[t].y)
addTrace(t,stack[t].x,stack[t].y)
t++
},500)
}
else {
while (stack[t]){
if(t>1)
leaveX(xValue,stack[t-1].x,stack[t-1].y,t-1)
moveHorse(stack[t].x,stack[t].y)
addTrace(t,stack[t].x,stack[t].y)
t++
}
}
}
else {
ipcRenderer.send("inputerror")
}
})
}