-
Notifications
You must be signed in to change notification settings - Fork 0
/
hanoi.js
62 lines (56 loc) · 1.58 KB
/
hanoi.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
'use strict';
var $left_tower;
var $middle_tower;
var $right_tower;
var $reset;
var tower_selected = false;
var selected_tower;
var previous_tower;
$(document).ready(init);
function init() {
$left_tower = $('.left_tower');
$middle_tower = $('.middle_tower');
$right_tower = $('.right_tower');
$left_tower.on('click', towerClicked);
$middle_tower.on('click', towerClicked);
$right_tower.on('click', towerClicked);
}
function towerClicked(event) {
if (!tower_selected) {
tower_selected = true;
previous_tower = $(this);
}
else {
selected_tower = $(this);
tower_selected = false;
if (previous_tower === selected_tower) {
return;
}
else {
paddleMove();
}
}
}
function paddleMove() {
var move_me = (previous_tower.children(':nth-child(1)')).children(':nth-child(1)');
console.log('move 1st child:',move_me);
if (selected_tower.children(':nth-child(1)').children().length < 1) { //no children
selected_tower.children(':nth-child(1)').prepend(move_me);
console.log('to this location:',selected_tower.children(':nth-child(1)'));
}
else {
var paddle_check = (selected_tower.children(':nth-child(1)')).children(':nth-child(1)');
var goto_id = (paddle_check.attr('id').replace(/p/,''));
var move_me_id = (move_me.attr('id').replace(/p/,''));
if (move_me_id < goto_id) {
selected_tower.children(':nth-child(1)').prepend(move_me);
}
else {
return;
}
}
if (($right_tower.children(':nth-child(1)')).children().length >= 4) {
event.stopPropagation();
$('.fancy_title').text('You won!')
}
}