-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgameHanoi.js
154 lines (132 loc) · 3.34 KB
/
gameHanoi.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
//select DOM elements
var numOfMoves = document.getElementById('num');
var success = document.getElementById('success-container');
var invalid = document.getElementsByClassName('invalid')[0];
//logic part
var pop = false; // global state
var moves = 0;
//make 3 poles
var poppedDisk;
var pole1 = [1,2,3];
var pole2 = [];
var pole3 = [];
function popAndGet(pole){
if(!pop && pole.length){
poppedDisk = pole.pop();
pop = !pop;
}
else if(poppedDisk && (!pole.length || (poppedDisk > pole[pole.length - 1]))){
pole.push(poppedDisk);
poppedDisk = null;
pop = !pop;
moves++;
num.innerText = moves;
}
else
invalid.classList.remove('hidden');
}
//canvas parts
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var width = canvas.width;
var height = canvas.height;
var p1p2 = width * 0.33;
var p2p3 = width * 0.66;
//metrics for block and poles
var bHeight = 50;
var pLength = height * 0.7;
var pWidth = width * 0.02;
var p1X = p1p2 / 2 - pWidth / 2,
p2X = (p2p3 - p1p2) / 2 - pWidth / 2 + p1p2,
p3X = (width - p2p3) / 2 - pWidth / 2 + p2p3;
var py = 100;
var bY = py + pLength;
function Shape(x, y, w, h, fill){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.fill = fill;
}
Shape.prototype.draw = function(){
ctx.fillStyle = this.fill;
ctx.fillRect(this.x, this.y, this.w, this.h);
}
function Pole(x, y){
Shape.call(this, x, y);
this.w = pWidth;
this.h = pLength;
this.fill = 'black';
}
Pole.prototype = Object.create(Shape.prototype);
Pole.prototype.constructor = Pole;
Pole.prototype.draw = function(){
Shape.prototype.draw.call(this);
}
var p1 = new Pole(p1X, py);
var p2 = new Pole(p2X, py);
var p3 = new Pole(p3X, py);
function drawPole(){
p1.draw();
p2.draw();
p3.draw();
}
function blocksToDraw(block, i, x, y){
if(block === 1)
return function(){
new Shape(x + pWidth / 2 - 70, y - bHeight * (i + 1), 140, bHeight, 'rgba(61, 114, 232, .5)').draw();
};
else if(block === 2)
return function(){
new Shape(x + pWidth / 2 - 60, y - bHeight * (i + 1), 120, bHeight, 'rgba(20, 217, 94, .5)').draw();
};
else if(block === 3)
return function(){
new Shape(x + pWidth / 2 - 50, y - bHeight * (i + 1), 100, bHeight, 'rgba(231, 82, 82, .5)').draw();
};
}
function drawStack(stack, x){
if(poppedDisk)
blocksToDraw(poppedDisk, 0, width / 2, bHeight + 40)();
stack.map(function(block, i){
blocksToDraw(block, i, x, bY)();
});
}
function redraw(){
//clear previous stuff
ctx.clearRect(1, 1, width - 2, height - 2);
//loop all three poles
drawPole();
if(pole1.length)
drawStack(pole1, p1X);
if(pole2.length)
drawStack(pole2, p2X);
if(pole3.length)
drawStack(pole3, p3X);
}
//initial state
redraw();
numOfMoves.innerText = moves;
ctx.strokeRect(0, 0, canvas.width, canvas.height);
canvas.addEventListener('click', function(e){
var mx = e.offsetX; // layerX in firefox? needs checking for browser compatibility
var my = e.offsetY;
if(!invalid.getAttribute('hidden'))
invalid.classList.add('hidden');
if(mx <= p1p2){
popAndGet(pole1);
redraw();
console.log('p1');
}
else if (mx < p2p3 && mx > p1p2){
popAndGet(pole2);
redraw();
console.log('p2');
}
else if (mx >= p2p3){
popAndGet(pole3);
redraw();
if(pole3.length === 3)
success.classList.remove('hidden');
}
});