-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
189 lines (157 loc) · 5.54 KB
/
index.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
const EMPTY_ROD = 11;
const rods = [false,1,EMPTY_ROD,EMPTY_ROD];
var position = { x: 0, y: 0 };
function init() {
reset();
initInteractables();
$("#auto").click((e) => {
algorithm($("#delay").val());
});
$("#reset").click((e) => {
reset();
});
$(() => $('[data-toggle="tooltip"]').tooltip());
}
function initInteractables() {
interact('.disk-wrapper').draggable({
listeners: {
start (event) {
position.x = 0;
position.y = 0;
},
move (event) {
position.x += event.dx;
position.y += event.dy;
event.target.style.transform =
`translate(${position.x}px, ${position.y}px)`;
event.target.style.position= "relative";
event.target.style.zIndex="1000";
},
end(event) {
event.target.style = "";
},
}
})
interact('.rod').dropzone({
overlap: 0.5,
accept: '.top',
ondrop: function (event) {
moveDiskIfPossible(event.target.id,$("#" + event.relatedTarget.id));
return false;
},
})
}
// auto solve optimal algorithm for the towers.
function algorithm(delay){
//reset game to starting conditions
reset();
// determine amount of disks and optimal move count
const diskCount = $(".disk-wrapper").length;
const moves = $("#optimal").val();
var functions = [];
// compute moves and queue them with a delay to be executed synchronously
for (var i=0;i<moves;i++){
functions.push(() => {
var move = nextMove(diskCount%2);
const disk = $("#disk-" + rods[move.src]);
console.log(delay);
return disk.delay(delay).queue(moveDiskIfPossible(move.dest,disk));
});
}
// actual visual execution
execute(functions);
}
// execute the queue of actions recursively to allow
// synchronously animation of disk moves
function execute(functionArray) {
if(functionArray.length > 0 ) {
const func = functionArray.splice(0,1);
func[0]().promise().done(() => execute(functionArray));
}
}
function nextMove(direction) {
var validRods = {src: null, dest: null};
//moves alternate between smallest disk and second smallest disk
if (rods[0]) {
// second smallest disk move
// find the two rods without the smallest disk on top
var validRodsArray = [];
for (var i = 1;i<=3;i++)
if (rods[i] != 1){
validRodsArray.push({rod:i,top:rods[i]});
}
// determine the second smallest disk and where to move it
if (validRodsArray[0].top < validRodsArray[1].top){
validRods.src = validRodsArray[0].rod;
validRods.dest = validRodsArray[1].rod;
}
else {
validRods.src = validRodsArray[1].rod;
validRods.dest = validRodsArray[0].rod;
}
} else {
// smallest disk move.
// Always move in the same direction,
// which depends on whether there is an even or odd number of disks
validRods.src = rods.indexOf(1);
// the direction changes according to the disk count to ensure
// that all disks start in rod 1 and end in rod 3
validRods.dest = getDestRod(validRods.src,direction);
}
//alternate the next move between the two possible ones
rods[0] = !rods[0];
return validRods;
}
function getDestRod(src,direction) {
if (direction)
return src===1?3:(src-1); // move to the next rod: 1->3, 3->2, 2->1
return 1 + src%3; // move to the next rod: 1->2, 2->3, 3->1
}
// checks if it is possible to move the given disk to the given rod
// if possible make the move, update the move count,
// and update the rods array with current top disks
function moveDiskIfPossible(rodTarget,disk){
// check if move is possible
if (rods[parseInt(rodTarget)] > parseInt(disk.attr("data-value"))){
// make the move
$("#" + rodTarget).prepend(disk);
// update move count in the UI
$("#move").val(parseInt($("#move").val())+1);
// update rods with current top disks
updateTops();
}
console.log(rods);
}
// update rods array with current top disks
function updateTops(){
for (var i = 1;i <=3;i++){
// update current top disk in the UI
// and get its value for the rods array
rods[i] = parseInt($("#"+i).find(".disk-wrapper").removeClass("top").first().addClass("top").attr("data-value"));
rods[i] = rods[i]?rods[i]:EMPTY_ROD;
}
}
function reset(){
rods[0] = false;
$(".rod").empty();
$("#move").val(0);
const regex = new RegExp('^([1-9]|10)$');
var disks = $("#disks").val();
disks = regex.test(disks)?parseInt(disks):5;
$("#disks").val(disks);
$("#optimal").val(Math.pow(2,disks)-1);
for (var i = 1;i <= disks;i++)
$("#1.rod").append(createDisk(i,disks));
updateTops();
}
function createDisk(i,disks) {
return $("#template").clone().removeClass("d-none")
.attr("id","disk-" + i)
.attr("data-value",i)
.addClass("disk-wrapper")
.find(".disk")
.addClass("disk-"+i)
.css("width", (i/disks)*100+"%")
.find("span").text(i)
.end().end();
}