/
index.html
414 lines (391 loc) · 13.4 KB
/
index.html
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="./styles.css" rel="stylesheet">
<title>Hello sudoku!</title>
<script src="./lib/element_lib/vue.js"></script>
<script src="./lib/element_lib/index.js"></script>
<script src="./js/backtracking.js"></script>
<script src="./js/dancingLink.js"></script>
<script src="./lib/element_lib/index.js"></script>
<script src="./lib/element_lib/index.js"></script>
<!-- Insert this line above script imports -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<!-- Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>
<script src="./lib/jquery.min.js"></script>
<script>
const path = require('path');
const fs = require("fs");
const {ipcRenderer} =require('electron');
</script>
<style>
@import url("./lib/element_lib/index.css");
</style>
</head>
<body style="width: 780px">
<div class="head" style="height: 110px">
<!-- <div style="display:flex;align-items:center;justify-content:center;height: 100px;width: 300px">-->
<div id="app2" style="width: 780px" class="grid1">
<div ></div>
<div >
<div>
<div>
<template>
<div class="block" >
<el-carousel trigger="click" :autoplay="autoplay" height="100px" >
<el-carousel-item v-for="item in arr" :key="item">
<h1 class="large algorithm">{{ item }}</h1>
</el-carousel-item>
</el-carousel>
</div>
</template>
</div>
</div>
</div>
<div style="padding-top: 30px;padding-left: 34px;width: 150px;">时间:{{time}} ms</div>
<div ></div>
</div>
<style>
.grid1{
display: grid;
grid-template-columns: 1fr 350px 1fr 1fr;
}
</style>
<!-- </div>-->
</div>
<!--负责整体居中-->
<div style="display:flex;align-items:center;justify-content:center;height: 100%" >
<div>
<div style="display: inline-block;vertical-align: top;">
<div>
<div style="padding: 0px" id="app1">
<div style="" class="btn-pri">
<el-button type="primary" onclick="loadSudoku()">加载数据</el-button>
</div>
<div style="" class="btn-pri">
<el-button type="primary" onclick="runDown()">连续运行</el-button>
</div>
<div style="" class="btn-pri">
<el-button type="primary" onclick="runStep()">求解数独</el-button>
</div>
<div style="" class="btn-pri">
<el-button type="primary" onclick="runDownSlowly()">慢速运行</el-button>
</div>
<div style="" class="btn-pri">
<el-button type="primary" onclick="generateInitSudoku()">生成初盘</el-button>
</div>
<div style="" class="btn-pri">
<el-button type="primary" onclick="renew()">恢复初盘</el-button>
</div>
<!-- <div style="">-->
<!-- <el-button type="primary">主要按钮</el-button>-->
<!-- </div>-->
</div>
</div>
</div>
<div style="display: inline-block;height: 100%;width: 80px"></div>
<div style="display: inline-block">
<div style="display:flex;align-items:center;justify-content:center;height: 100%" >
<!--作为数独九宫格的外侧框架-->
<div>
<!--九个宫-->
<table>
<tr>
<td class="place"></td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
<script>
</script>
<script>
//sudoku数据就是初盘数据,后面的变化存放在矩阵中
sudoku=readFilePath();
var places=$(".place");
var vueInstance1=new Vue().$mount('#app1');
var carousel={
data:()=>{
return {
autoplay:false,
arr:[
"回溯法",
"启发式搜索",
"舞蹈链",
"分支限界"
],
time:9999
};
}
}
var f2=Vue.extend(carousel);
var vueInstance2=new f2().$mount('#app2');
for(let i=0;i<9;i++){
let tr=document.createElement("tr")
for(let k=0;k<9;k++){
let td=document.createElement("td");
td.setAttribute("class","lattice");
tr.append(td);
}
places[0].append(tr)
}
//获得所有lattice(顺序不可以改变)
var lattice=$(".lattice");
for(let i=0;i<9;i++){
lattice[i*9+2].setAttribute("style","border-right:4px solid;")
lattice[i*9+5].setAttribute("style","border-right:4px solid;")
lattice[2*9+i].setAttribute("style","border-bottom:4px solid;")
lattice[5*9+i].setAttribute("style","border-bottom:4px solid;")
lattice[2*9+2].setAttribute("style","border-bottom:4px solid;border-right:4px solid;")
lattice[5*9+2].setAttribute("style","border-bottom:4px solid;border-right:4px solid;")
lattice[2*9+5].setAttribute("style","border-bottom:4px solid;border-right:4px solid;")
lattice[5*9+5].setAttribute("style","border-bottom:4px solid;border-right:4px solid;")
}
function render(sudoku){
for(let i=0;i<9;i++){
for(let j=0;j<9;j++){
$(lattice[9*i+j]).removeClass("blue")
if(sudoku[i][j]===0){
lattice[9*i+j].innerText='';
}else {
lattice[9*i+j].innerText=sudoku[i][j];
$(lattice[9*i+j]).addClass("blue");
}
}
}
}
var renderSudoku=(data)=>{
console.log(data)
let sudokuStat=data.sudoku;
console.log(sudokuStat);
//逐个判断
for(let i=0;i<9;i++){
for(let j=0;j<9;j++){
$(lattice[9*i+j]).removeClass("orange")
$(lattice[9*i+j]).removeClass("blue")
//如果为0则设置为空,不为0则添加蓝色
if(sudokuStat[i][j]===0){
lattice[9*i+j].innerText='';
}else {
lattice[9*i+j].innerText=sudokuStat[i][j];
$(lattice[9*i+j]).addClass("blue")
}
//如果选中则设置为橘色
if(i===data.i){
$(lattice[9*i+j]).addClass("orange");
}
if(j===data.j){
$(lattice[9*i+j]).addClass("orange")
}
if(Math.floor(i/3)===Math.floor(data.i/3)&&Math.floor(j/3)===Math.floor(data.j/3)){
$(lattice[9*i+j]).addClass("orange")
}
}
}
}
//超级无敌大状态矩阵
var statMatrix={
backtracking:[1,0,[]],
dancingLink:[1,0,[]],
heuristicSearch:[1,0,[]]
}
// worker.postMessage({frist:1,second:2})
const workerBacktracking = new Worker("./js/backtracking.js")
const workerDancingLink = new Worker("./js/dancingLink.js")
const workerHeuristicSearch = new Worker("./js/heuristicSearch.js")
//注册消息事件处理函数
workerBacktracking.addEventListener("message",(event)=>{
if (event.data.type==="generate"){
//棋盘生成之后修改sudoku初盘
sudoku=event.data.sudoku;
//根据初盘再次发送求解请求
cleanMatrix();
noticeThread();
cleanTime();
}else if(event.data.type==="main"){
statMatrix.backtracking[0]=event.data.time;
statMatrix.backtracking[1]=event.data.isHaveSolution;
let temp={i:-1,j:-1,k:-1,sudoku:[]};
{
temp.i = event.data.i;
temp.j = event.data.j;
temp.k = event.data.k;
temp.sudoku = event.data.sudoku;
}
statMatrix.backtracking[2].push(temp);
}
})
workerDancingLink.addEventListener("message",(event)=>{
// debugger
// render(event.data)
statMatrix.dancingLink[0]=event.data.time;
statMatrix.dancingLink[1]=event.data.isHaveSolution;
statMatrix.dancingLink[2].push(event.data.sudoku);
})
workerHeuristicSearch.addEventListener("message",(event)=>{
statMatrix.heuristicSearch[0]=event.data.time;
statMatrix.heuristicSearch[1]=event.data.isHaveSolution;
statMatrix.heuristicSearch[2].push(event.data.sudoku);
})
///////////////////////////////////////////////////////////////////////
//服务函数
function readFilePath() {
//获取本地json文件的路径
const newFile_path = path.join(__dirname, 'data/data1.json').replace(/\\/g, "\/");
return JSON.parse(fs.readFileSync(newFile_path));
}
//获取当前算法类别1回溯,2启发式搜索,3舞蹈链,4分支限界
function getCurAlgorithm(){
switch ($(".is-active .algorithm")[0].innerText){
case "回溯法":{
return statMatrix.backtracking;
}
case "启发式搜索":{
return statMatrix.heuristicSearch;
}
case "舞蹈链":{
return statMatrix.dancingLink;
}
case "分支限界":{
return 4;
}
}
}
function cleanMatrix(){
statMatrix={
backtracking:[1,0,[]],
dancingLink:[1,0,[]],
heuristicSearch:[1,0,[]]
}
}
function noticeThread() {
//通知线程开始执行
workerBacktracking.postMessage({type:"main","sudoku":sudoku});
workerDancingLink.postMessage({type:"main","sudoku":sudoku});
workerHeuristicSearch.postMessage({type:"main","sudoku":sudoku});
}
var renderAnimation=(t,eframes)=>{
let arr=getCurAlgorithm()[2];
let stepLength=0;
if(arr.length<t*60){
stepLength=1;
}else {
stepLength=Math.floor(arr.length/600);
}
let cur=0;
let frames=eframes
var runDownAnimation=()=>{
if (cur<arr.length) {
// debugger;
if(frames>0){
frames--;
}else {
frames=eframes;
renderSudoku(arr[cur]);
cur+=stepLength;
}
window.requestAnimationFrame(runDownAnimation);
}
}
window.requestAnimationFrame(runDownAnimation);
}
var updateTime=()=>{
vueInstance2.time=getCurAlgorithm()[0].toString().substring(0,5);
}
var cleanTime=()=>{
vueInstance2.time=0;
}
///////////////////////////////////////////////////////////////////////////////////////
// 响应一系列按钮点击事件
// 按钮点击事件
var loadSudoku=()=> {
ipcRenderer.send('open-directory-dialog', 'openFile');
ipcRenderer.on('selectedItem', (event,message) => {
sudoku=JSON.parse(message);
render(sudoku);
//当接收到数独数据的时候立即创建worker执行四种算法,并将过程状态空间存放在内存中
//清空以往数据
cleanMatrix();
noticeThread();
cleanTime();
})
}
// 响应连续运行
var runDown=()=>{
renderAnimation(10,1);
updateTime();
}
// 慢速运行
var runDownSlowly=()=>{
renderAnimation(20,5);
updateTime();
}
// 单步运行
var runStep=()=>{
}
// 生成棋盘
var generateInitSudoku=()=>{
workerBacktracking.postMessage({type:"generate"})
//更新棋盘和备份
}
//恢复棋盘
var renew=()=>{
render(sudoku);
}
</script>
<style>
table{
border-collapse: collapse;
border:4px solid #000000
}
td{
border:1px solid #000000;
padding: 0;
text-align: center;
}
.lattice{
width: 40px;
height: 40px;
}
.btn-pri{
padding: 10px;
}
</style>
<!--走马灯-->
<style>
.el-carousel__item h3 {
color: #475669;
font-size: 14px;
opacity: 0.75;
line-height: 150px;
margin: 0;
}
.el-carousel__button{
background-color: #dd6161;
}
.el-carousel__container{
text-align: center;
}
/*.el-carousel__item:nth-child(2n) {*/
/* background-color: #99a9bf;*/
/*}*/
/*.el-carousel__item:nth-child(2n+1) {*/
/* background-color: #d3dce6;*/
/*}*/
</style>
<style>
.orange{
background-color:rgb(248,201,171)!important;
}
.blue{
background-color:rgb(173,216,253);
}
</style>
</body>
</html>