-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
390 lines (389 loc) · 14.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>汉诺塔/河内塔/堆栈塔</title>
<script src="./vue.min.js"></script>
<style type="text/css">
[v-cloak] {
display: none;
}
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
#app {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
margin-top: 0;
}
.container {
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-webkit-justify-content: space-between;
-moz-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
.box {
border: 1px solid #ccc;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
width: 298px;
height: 400px;
-webkit-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
}
.step_display {
border: 1px solid #ccc;
width: 298px;
height: 400px;
}
.step_info{
font-size: 18px;
font-weight: bold;
margin: 5px;
}
.step_moves {
width: 288px;
height: 355px;
padding: 5px;
overflow: auto;
}
.tower {
margin: auto;
position: relative;
width: 10px;
height: 350px;
-webkit-border-radius: 3px 3px 0px 0px;
-moz-border-radius: 3px 3px 0px 0px;
border-radius: 3px 3px 0px 0px;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-moz-box-orient: vertical;
-moz-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: end;
-webkit-justify-content: flex-end;
-moz-box-pack: end;
-ms-flex-pack: end;
justify-content: flex-end;
background: -webkit-gradient(linear, left top, right top, from(#707070) , to(#d1d1d1));
background: -webkit-linear-gradient(left, #707070 , #d1d1d1);
background: -moz-linear-gradient(left, #707070 , #d1d1d1);
background: -o-linear-gradient(left, #707070 , #d1d1d1);
background: linear-gradient(left, #707070 , #d1d1d1);
}
.tower_select {
background-color: #ddd;
}
.base {
width: 300px;
height: 50px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
background: #bbb;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.disk {
position: relative;
width: 60px;
height: 30px;
-webkit-transition: all 0.5s;
-o-transition: all 0.5s;
-moz-transition: all 0.5s;
transition: all 0.5s;
border: 2px solid #99aaff;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
background: -webkit-gradient(linear, left top, right top, from(#00aeff) , to(#93e9ff));
background: -webkit-linear-gradient(left, #00aeff , #93e9ff);
background: -moz-linear-gradient(left, #00aeff , #93e9ff);
background: -o-linear-gradient(left, #00aeff , #93e9ff);
background: linear-gradient(left, #00aeff , #93e9ff);
}
.text{
color: #fff;
text-shadow: #99aaff 3px 3px 3px;
}
.disk_select {
background: -webkit-gradient(linear, left top, right top, from(#eeccff), to(#cc55ff));
background: -webkit-linear-gradient(left, #eeccff, #cc55ff);
background: -moz-linear-gradient(left, #eeccff, #cc55ff);
background: -o-linear-gradient(left, #eeccff, #cc55ff);
background: linear-gradient(left, #eeccff, #cc55ff);
}
.info {
margin-top: 20px;
}
.controls {
margin-top: 20px;
}
</style>
</head>
<body>
<div id="app" v-cloak>
<h1>汉诺塔游戏</h1>
<div class="container">
<div class="box" v-for="(tower, towerIndex) in towers" :key="towerIndex" @click="selectTower(towerIndex, tower)">
<div class="tower">
<div class="disk" v-for="(disk, diskIndex) in tower" :key="diskIndex" :class="{'disk_select': towerIndex === selectedTowerIndex && diskIndex === 0}" :style="{width: 60 + disk * (200 / startLevels) + 'px' }">
<div class="text" v-text="disk"></div>
</div>
</div>
<div class="base" v-text="String.fromCharCode(towerIndex + 65)"></div>
</div>
<div class="step_display">
<div class="step_info">步骤展示</div>
<div class="step_moves" ref="step_moves">
<ol>
<li v-for="(move, moveIndex) in moves" :key="moveIndex" v-text="String.fromCharCode(move[0] + 65) + '柱移到' + String.fromCharCode(move[1] + 65) + '柱'"></li>
</ol>
</div>
</div>
</div>
<div class="info">
<div>规则:每次移动一块,小的放在大的上面, 将它移动到另一根柱子上</div>
<div>最小步数:<span v-text="minSteps"></span></div>
<div>计时(秒):<span v-text="second"></span></div>
<div>移动步数:<span v-text="steps"></span></div>
</div>
<div class="controls">
<label for="levels">选择层数:</label>
<input type="number" id="levels" v-model.number="levels" max="10" min="2">
<button @click="startGame">开始游戏</button>
<button @click="recall">撤回到上一步</button>
<button @click="autoMove">自动移动</button>
</div>
</div>
</body>
<script type="text/javascript">
vm = new Vue({
el: '#app',
data: {
timer: {
timekeeper: null,
autoMove: null,
},
selectedTowerIndex: null,
second: 0,
steps: 0,
minSteps: 0,
levels: 3,
start: false,
autoStart: false,
startLevels: 0,
towers: [[], [], []],
moves: [],
},
methods: {
// 手动移动
selectTower(index, tower) {
if (!this.start) {
return;
}
if (this.selectedTowerIndex === null) {
if (tower.length > 0) {
this.selectedTowerIndex = index;
}
} else if (this.selectedTowerIndex === index) {
this.selectedTowerIndex = null;
} else {
selectedTower = this.towers[this.selectedTowerIndex];
if (tower.length === 0 || tower[0] > selectedTower[0]) {
tower.unshift(selectedTower.shift());
this.moves.push([this.selectedTowerIndex, index]); //记录步骤
this.selectedTowerIndex = null;
this.steps++;
if (tower.length === this.startLevels) {
this.start = false;
clearInterval(this.timer.timekeeper);
alert('完成');
}
} else {
alert('小的放在大的上面');
}
}
},
// 开始游戏
startGame() {
if (this.start || this.autoStart) {
if (confirm("游戏正在进行,确认重新开始?")) {
this.restartGame();
}
} else {
this.restartGame();
}
},
// 重置游戏
restartGame() {
clearInterval(this.timer.autoMove);
clearInterval(this.timer.timekeeper);
this.moves = [];
this.start = true;
this.autoStart = false;
this.setTower();
var that = this;
this.timer.timekeeper = setInterval(function(){
that.second++;
}, 1000);
},
// 设定层数
setTower() {
if (this.levels >= 2 && this.levels <= 10) {
this.towers = [[], [], []]
for (let i = 1; i <= this.levels; i++) {
this.towers[0].push(i)
}
this.selectedTowerIndex = null
this.startLevels = this.levels;
this.second = 0;
this.steps = 0;
this.minSteps = Math.pow(2, this.startLevels) - 1;
} else {
alert("层数在2-10");
}
},
/**
* 核心,递归算出解法
* levels 层数
* a_index A柱索引
* b_index B柱索引
* c_index C柱索引
* moves 步骤集合
*/
hanoi(levels, a_index, b_index, c_index, moves) {
if (levels <= 0) {
return moves
}
if (levels === 1) {
moves.push([a_index, c_index]);
} else {
this.hanoi(levels - 1, a_index, c_index, b_index, moves);
moves.push([a_index, c_index]);
this.hanoi(levels - 1, b_index, a_index, c_index, moves);
}
return moves
},
// 开始自动移动
autoMove() {
if (this.start || this.autoStart) {
if (confirm("游戏正在进行,确认重新开始?")) {
this.handle();
}
} else {
this.handle();
}
},
// 定时处理步骤集合
handle() {
clearInterval(this.timer.timekeeper);
clearInterval(this.timer.autoMove);
this.moves = [];
this.start = false;
this.autoStart = true;
this.setTower();
let moves = this.hanoi(this.startLevels, 0, 1, 2, []); //获取步数集
let that = this;
let i = 0;
let len = moves.length;
this.timer.autoMove = setInterval(function(){
if (i < len) {
that.move(moves[i][0], moves[i][1])
that.moves.push(moves[i]); //记录步骤
this.steps++;
i++;
} else {
that.autoStart = false;
clearInterval(that.timer.autoMove);
}
}, 1000);
},
// 移动
move(a_index, b_index) {
sourceTower = this.towers[a_index];
destTower = this.towers[b_index];
destTower.unshift(sourceTower.shift());
this.scrollToBottom();
this.selectedTowerIndex = null;
},
// 撤回到上一步
recall() {
if (this.start) {
let len = this.moves.length;
if (len > 0) {
this.move(this.moves[len - 1][1], this.moves[len - 1][0]);
this.moves.pop();
this.selectedTowerIndex = null;
this.steps--;
}
}
},
// 步骤滚动到底部
scrollToBottom() {
let step_moves = this.$refs.step_moves;
let diff_height = step_moves.scrollHeight - step_moves.clientHeight;
if (diff_height > 0) {
step_moves.scrollTop = diff_height;
}
},
}
})
// console.log(vm.$refs.step_moves, vm.$el.querySelector('.step_moves'));
console.log("1、汉诺塔模块为奇数时,最上面的移动到目标柱;2、汉诺塔模块为偶数时,最上面的移动到过渡柱");
</script>
</html>