Skip to content

Commit ff7f1cf

Browse files
committed
Hanoi init
0 parents  commit ff7f1cf

File tree

6 files changed

+222
-0
lines changed

6 files changed

+222
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

cut.PNG

10 KB
Loading

hanoi.js

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/**
2+
* Created by ajax on 2015/12/4.
3+
*/
4+
5+
(function () {
6+
function Rect(index, opt) {
7+
this.index = index;
8+
this.min = 20; // 20px
9+
this.step = 8;
10+
this.h = 10;
11+
this.w = this.min + this.step * this.index;
12+
this.div = null;
13+
this.from = null;
14+
this.tower = null;
15+
this.speed = opt.speed;
16+
this.draw();
17+
}
18+
Rect.prototype = {
19+
draw : function () {
20+
this.div = $(document.createElement("div"));
21+
//var opacity = (0.5 + (this.index % 10) / 10).toFixed(1);
22+
var opacity = 0.3 + Math.random() * 0.7;
23+
this.div.css({
24+
position : "absolute",
25+
backgroundColor : "#000",
26+
opacity : opacity,
27+
width : this.w + "px",
28+
height : this.h + 'px',
29+
left : "100px",
30+
top : 100 + this.index * 10 + "px",
31+
borderRadius : "2px",
32+
marginLeft : -(this.min + this.step * this.index) / 2 + "px",
33+
borderBottom : "1px solid #ccc"
34+
});
35+
$(document.body).append(this.div);
36+
},
37+
moveTo : function(fn) {
38+
var tower = this.tower;
39+
return tower.pushRect(this, fn);
40+
}
41+
};
42+
43+
function Tower(index) {
44+
this.index = index;
45+
this.h = 100;
46+
this.w = 50;
47+
this.top = 200;
48+
this.left = 200;
49+
this.div = null;
50+
this.origin = {
51+
x : this.left + this.index * this.h + this.h,
52+
y : this.top + this.h
53+
};
54+
this.rects = []; // 该塔拥有的矩形
55+
this.draw(); // draw self
56+
}
57+
Tower.prototype = {
58+
draw : function () {
59+
var div = document.createElement("div");
60+
var div2 = document.createElement("div");
61+
div = $(div);
62+
div2 = $(div2);
63+
div.css({
64+
borderLeft : "1px solid #aaa",
65+
height : this.h + "px",
66+
width : 0,
67+
position : "absolute",
68+
//left : this.left + this.index * this.h + this.h + "px",
69+
left : this.origin.x + "px",
70+
top : this.origin.y - this.h + "px"
71+
});
72+
div2.css({
73+
position : "absolute",
74+
width : this.w + "px",
75+
height : 0,
76+
borderBottom : "1px solid #aaa",
77+
bottom : 0,
78+
left : (- this.w / 2) + 'px'
79+
});
80+
div.append(div2);
81+
this.div = div;
82+
$(document.body).append(div);
83+
},
84+
pushRect : function (rect, fn) { // bool
85+
var canPush = false;
86+
if (this.rects.length === 0) {
87+
canPush = true;
88+
} else {
89+
var top = this.rects[this.rects.length -1];
90+
if (top.index > rect.index) {
91+
canPush = true;
92+
}
93+
}
94+
if (canPush) {
95+
if (rect.from) {
96+
rect.from.rects = rect.from.rects.filter(function (item) {
97+
return item.index !== rect.index;
98+
});
99+
}
100+
this.rects.push(rect);
101+
rect.from = this;
102+
var index = this.rects.length;
103+
rect.div.animate({
104+
top : this.origin.y - rect.h * index + "px",
105+
left : this.origin.x + "px"
106+
}, rect.speed, fn);
107+
}
108+
return canPush;
109+
}
110+
};
111+
112+
function main(n, opt) {
113+
var towerNum = 3,
114+
towers = [],
115+
rects = [],
116+
i = 0;
117+
for (i = 0; i < towerNum; i++) {
118+
towers.push(new Tower(i));
119+
}
120+
for (i = 0; i < n; i++) {
121+
rects.push(new Rect(i, {
122+
speed : parseInt(opt.speed)
123+
}));
124+
}
125+
// for (i = rects.length - 1; i >=0; i--) {
126+
// rects[i].tower = towers[0];
127+
// rects[i].moveTo();
128+
// }
129+
var index = rects.length - 1;
130+
Q(index, towers[0]);
131+
function Q(i, to) {
132+
if (i < 0){
133+
start();
134+
return;
135+
}
136+
move(i, to, function () {
137+
i = i-1;
138+
Q(i, towers[0]);
139+
});
140+
}
141+
function move(i, to, fn) {
142+
rects[i].tower = to;
143+
rects[i].moveTo(fn)
144+
}
145+
function start() {
146+
var steps = [];
147+
h(n - 1, towers[0], towers[1], towers[2], function (prop) {
148+
steps.push(prop);
149+
});
150+
var i = 0,
151+
len = steps.length;
152+
console.log(steps);
153+
loop(i);
154+
155+
function loop(i){
156+
if (i >= len) return;
157+
move(steps[i].index, steps[i].to, function () {
158+
i = i + 1;
159+
loop(i);
160+
});
161+
}
162+
}
163+
164+
function h(n, a, b, c, fn){
165+
if (n === 0) {
166+
fn({
167+
index : n,
168+
to : c
169+
});
170+
} else {
171+
h(n -1, a, c, b, fn);
172+
fn({
173+
index : n,
174+
to : c
175+
});
176+
h(n-1, b, a, c, fn);
177+
}
178+
}
179+
180+
}
181+
182+
var isRunning = false;
183+
$("#btn").on("click", function () {
184+
if (isRunning) return;
185+
isRunning = true;
186+
var speed = $("#speed").val();
187+
var val = $("#input").val();
188+
main(val, {
189+
speed : speed
190+
});
191+
192+
});
193+
})();

index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title>汉诺塔(可视化)</title>
6+
<script src="jquery.min.js"></script>
7+
</head>
8+
<body>
9+
10+
几层塔
11+
<input type="text" id="input" value="4"/>
12+
速度(毫秒)
13+
<input type="text" id="speed" value="1000"/>
14+
<span>重新设定参数请刷新页面(塔请不要超过10)</span><br/>
15+
<button id="btn">Run</button>
16+
<script src="hanoi.js"></script>
17+
</body>
18+
</html>

jquery.min.js

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Hanoi
2+
2D汉诺塔(193 lines JS)<br>
3+
点击试试http://nigeerhuo.com/Hanoi/index.html
4+
![ScreenShot](https://raw.github.com/AJLoveChina/Hanoi/master/cut.PNG)

0 commit comments

Comments
 (0)