This repository has been archived by the owner on Dec 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhra_pato.js
316 lines (244 loc) · 6.68 KB
/
hra_pato.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// JavaScript Document
imgdir = 'img/';
var score = 0;
var kliky = 0;
var selectedr = null;
var selectedc = null;
var game_is_over = false;
var show_messages = false;
var maxdisks;
var plocha = new Array(2);
var theAnim;
function nastavVezu(disks) {
var len = plocha[0].length;
for (i = 0; i < len; i++) {
plocha[0][i] = 0;
plocha[1][i] = 0;
plocha[2][i] = 0;
}
for (i = len-disks, j = 0; i < len; i++, j++) {
plocha[1][i] = len - j - 1;
}
}
function getName( num ) {
if (num == 0)
return "post.png";
return "disk" + num + ".png";
}
function message(str, force) {
if (force || !game_is_over && !show_messages)
document.getElementById('area').value = str;
}
function isempty(num) {
for (i = 0; i < plocha[num].length; i++) {
if ( plocha[num][i] != 0)
return false;
}
return true;
}
function topmost(num) {
for (i = 0; i < plocha[num].length; i++) {
if (plocha[num][i] != 0)
return i;
}
return -1;
}
function ispost(i,j) {
return (plocha[j][i] == 0);
}
function nakresliHru() {
var draw = '';
draw += "<table id=\"tabulka\" class=\"hanoi\" cellspacing=0 cellpadding=0 border=0>";
draw += "<tr>";
for (j = 0; j < plocha.length; j++) {
draw += "<td>";
for (i=0; i< plocha[0].length; i++) {
if(i==1){
draw += "<a href='javascript:zakliknute("+i+","+j+")'>";
draw += "<img id=\"stlp\" src='" + imgdir + getName(plocha[j][i]) + "' name='pos"+ j + i + "' border=0>";
draw += "</a>";
}
else{
draw += "<a href='javascript:zakliknute("+i+","+j+")'>";
draw += "<img id=\"stlp\" src='" + imgdir + getName(plocha[j][i]) + "' name='pos"+ j + i + "' border=0>";
draw += "</a>";
}
}
draw += "</td>";
}
draw += "</tr></table><br>";
draw += "<form name='disp'><textarea id=\"area\" name='message' wrap=virtual rows=2 cols=40></textarea><br>";
draw += '<div id="stav"></div>'
document.getElementById('hra').innerHTML = draw;
initdrag();
}
function initdrag() {
$("td").each(function(){
$(this).droppable({
drop: function( event, ui ) {
$( this ).find("a img").first().click();
console.log($( this ).find("a img").first());
}
});
});
$("img").each(function(){
if ($(this).attr("src") != "images/post.png") {
$(this).draggable({
revert: true,
revertDuration: 0,
start: function() {
$(this).click();
}
});
}
});
}
function animuj(x,y,name) {
theAnim.addFrame( "pos"+x+""+y, imgdir + name);
}
function zakliknute(i,j) {
document.forms[0].message.focus();
document.forms[0].message.blur();
initdrag();
if (game_is_over)
window.location.reload();
if (!isselection() && ispost(i,j)) {
message("Vyber kus na presun.");
return;
} //stav ked kliknem na zeleny disk
if (!ispost(i,j)) {
//spustim dragable
stlacenie(j);
return;
};
if (ispost(i,j) && selectedc == j) {
message("Presun kus na inu poziciu");
return;
}
if (!bodovanie(j)) {
message("Nemozes to tu umiestnit.");
return;
}
pohyb(j);
return;
}
function bodovanie(j) {
if (isempty(j)){
score++;
kliky++;
document.getElementById('stav').innerHTML = 'Počet posunov: '
+ kliky + '<br>' + 'Úspešnosť vašej hry: ' + score/kliky*100;
return true;
}
if(plocha[j][topmost(j)] < plocha[selectedc][selectedr])
score++;
kliky++;
document.getElementById('stav').innerHTML = 'Počet premiestnení: '
+ kliky + '<br>' + 'Úspešnosť vašej hry: ' + score/kliky*100;
return (plocha[j][topmost(j)] < plocha[selectedc][selectedr]);
}
function isselection() {
return selectedc != null;
}
function stlacenie( num ) {
var toppos = topmost(num);
if (selectedc == num && selectedr == toppos) {
selectedc = null; selectedr = null;
animuj(num,toppos,"disk" + plocha[num][toppos] + ".png");
message("Vyber kus na presun");
return;
}
if (isselection()) {
animuj(selectedc,selectedr,"disk" + plocha[selectedc][selectedr] + ".png");
}
selectedc = num; selectedr = toppos;
animuj(num,toppos,"disk" + (plocha[num][toppos]+6) + ".png");
message("Vyber si miesto, kde chces premiestnit kus.");
}
function pohyb( num ) {
var toppos = (!isempty(num) ? topmost(num) : plocha[num].length);
plocha[num][toppos-1] = plocha[selectedc][selectedr];
plocha[selectedc][selectedr] = 0;
animuj(selectedc,selectedr,"post.png");
animuj(num,toppos-1,"disk" + plocha[num][toppos-1] + ".png");
selectedc = null; selectedr = null;
message("Vyber kus na presun.");
game_over();
}
function game_over(forceMsg) {
var filledpost = null;
var val = 0;
for (k = 0; k < plocha.length; k++) {
val += ( isempty(k) ? 1 : 0 );
if (!isempty(k))
filledpost = k;
}
if (val == 2 && isempty(1)) {
message("Vyhral si!", forceMsg);
game_is_over = true;
}
return game_is_over;
}
function Animation() {
this.imageNum = new Array();
this.imageSrc = new Array();
this.frameIndex = 0;
this.alreadyPlaying = false;
this.getFrameCount = getframecount;
this.moreFrames = moreframes;
this.addFrame = addframe;
this.drawNextFrame = drawnextframe;
this.startAnimation = startanimation;
}
function getframecount() {
return this.imageNum.length;
}
function moreframes() {
return this.frameIndex < this.getFrameCount();
}
function startanimation() {
if (!this.alreadyPlaying) {
theAnim.alreadyPlaying = true;
setTimeout('theAnim.drawNextFrame()',5);
}
}
function addframe(num, src) {
var theIndex = theAnim.imageNum.length;
theAnim.imageSrc[theIndex] = src;
theAnim.imageNum[theIndex] = num;
theAnim.startAnimation();
}
function drawnextframe() {
if (theAnim.moreFrames()) {
document.images[ theAnim.imageNum[theAnim.frameIndex] ].src = theAnim.imageSrc[theAnim.frameIndex];
theAnim.frameIndex++;
setTimeout('theAnim.drawNextFrame()', 30);
} else {
theAnim.alreadyPlaying = false;
}
}
function validacia(pocet){
if(pocet<3){
alert('Zadaj cislo od 3 do 6 !!');
window.location.reload();
}
if(pocet>6){
alert('Zadaj cislo od 3 do 6 !!');
window.location.reload();
}
}
function zadane(){
var pocet = parseInt( document.getElementById('pocet').value);
validacia(pocet);
for (var i = 0; i <12; i++) {
this[i] = new Image();
this[i].src = imgdir + "disk"+[i+1]+".png";
}
plocha[0] = new Array(pocet + 1);
plocha[1] = new Array(pocet + 1);
plocha[2] = new Array(pocet + 1);
nastavVezu(pocet);
nakresliHru();
theAnim = new Animation();
message("Vyber kus na presunutie.");
}