-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample.html
75 lines (68 loc) · 1.97 KB
/
sample.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
<!doctype html>
<html>
<head>
<style>
.container { position: absolute; bottom: 0; }
.content { position: relative; height: 150px; }
.section { width: 200px; text-align: center; display: inline-block; }
.piece { border: 1px solid blue; margin: 10px auto; height: 20px;}
#piece_1 { width: 50px; }
#piece_2 { width: 75px; }
#piece_3 { width: 100px; }
#piece_4 { width: 125px; }
</style>
</head>
<body>
<h1>Hanoi towers</h1>
<p>Moving the pieces will generate console logs, the diagram will update automatically (inspect page & chose Live Sequence tab)</p>
<div class="content">
<div class="container">
<div class="section area_A">
<div class="piece" id="piece_1"></div>
<div class="piece" id="piece_2"></div>
<div class="piece" id="piece_3"></div>
<div class="piece" id="piece_4"></div>
<input type="button" value="A" onclick="pick(this)" />
</div>
<div class="section area_B">
<input type="button" value="B" onclick="pick(this)" />
</div>
<div class="section area_C">
<input type="button" value="C" onclick="pick(this)" />
</div>
</div>
</div>
<script>
var picked = null;
function move(start, end){
if (start == end){
console.log(start + "->" + start + ": cannot move a piece on itself");
return;
}
var piece = document.querySelector(".area_" + start + " .piece");
if (!piece){
console.log(start + "->" + start + ": error, no piece to move");
} else {
var next = document.querySelector(".area_" + end + " .piece");
if (next && (piece.id > next.id)){
console.log(start + "->" + start + ": cannot move on top of smaller piece");
} else {
if (!next){
next = document.querySelector(".area_" + end + " input");
}
document.querySelector(".area_" + end).insertBefore(piece, next);
console.log(start + "->" + end + ": move");
}
}
}
function pick(e){
if (!picked){
picked = e.value;
} else {
move(picked, e.value);
picked = null;
}
}
</script>
</body>
</html>