-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.php
568 lines (472 loc) · 15.3 KB
/
game.php
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
include('connect.php');
$nickname = $_POST['nickname'];
$_SESSION['nickname'] = $_POST['nickname'];
$_SESSION['difficulty'] = $_POST['difficulty'];
error_reporting("E_ALL &~ E_NOTICE");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link href="css/style.css" rel="stylesheet" />
<link href="css/widgets.css" rel="stylesheet" />
<script src="js/jquery.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/scripts.js"></script>
<script>
//紀錄步數
function st(ff,tt,data_i,bc)
{
if(bc != 'bc') //如果不是上一步的話
run_hanoi(startTime,endTime,ff,tt,data_i);
sessionStorage.setItem('mf',JSON.stringify(ff));
sessionStorage.setItem('mt',JSON.stringify(tt));
sessionStorage.setItem('mdata_i',JSON.stringify(data_i));
var fromStackId = ff;
var toStackId= tt;
$.ajax({
url : 'step.php?step='+step+'&fromStackId='+fromStackId+'&toStackId='+toStackId+'&brickId='+data_i,
success: function(data){
$('#num').text(data);
if($('#num').text() >= '2'){
$('#back').prop('hidden','');
}
}
})
//處理存入session陣列
saveToStorage('re',ff,tt,data_i);
//成功
success();
}
function success(){
if($('#col3').children().length == i){
$('.success').removeAttr('hidden');
}
}
//拿出session記憶陣列
function loadToStorage(){
var storedArray = JSON.parse(sessionStorage.getItem("items"));
for (j = 1; j < storedArray.length; j++) {
var str = String(storedArray[j]);
str = str.split(",");
for(var m = 1; m<= i ; m++){
if(str[m] != '0'){
$("#col"+j).append('<div class="brick b'+m+'" draggable="true" id="'+m+'" ondragstart="drag(event);" data-id="'+m+'"></div>');
}
}
console.log(str);
}
}
//記憶陣列存到session
function saveToStorage(re,ff,tt,data_i) {
if(re == 're'){ //如果移動的話 存到session 陣列
var storedArray = JSON.parse(sessionStorage.getItem("items"));
var temp = storedArray[ff][data_i];
storedArray[ff][data_i] = '0';
storedArray[tt][data_i] = temp;
sessionStorage.setItem("items",JSON.stringify(storedArray));
}else{
//如果初始的話 將初始陣列存到session 陣列
sessionStorage.setItem("items", JSON.stringify(st_arr));
var storedArray = JSON.parse(sessionStorage.getItem("items"));//no brackets
var j;
for (j = 1; j < storedArray.length; j++) {
var str = String(storedArray[j]);
str = str.split(",");
console.log(str);
}
}
}
</script>
<title>河內塔</title>
</head>
<body>
<div hidden class="success">
<div class="container">
<div class="message">
遊戲成功!
<input type="button" value="重新開始" onClick="location.href='index.php'">
</div>
</div>
</div>
<div id="container-game" class="gameRunning">
<div class="row">
<div class="layout">
<h2>河內塔</h2>
<div class="game">
<div class="clear colNumber">
<div class="col3">
<span>3</span>
</div>
<div class="col3">
<span>2</span>
</div>
<div class="col3">
<span>1</span>
</div>
</div>
<div id="col3" class="col" data-id="3" ondragover="allowdrop(event);" ondrop="drop(event);">
</div>
<div id="col2" class="col" data-id="2" ondragover="allowdrop(event);" ondrop="drop(event);">
</div>
<div id="col1" class="col" data-id="1" ondragover="allowdrop(event);" ondrop="drop(event);">
</div>
<div class="clear moveButton">
<div class="col3">
<button id="col3_2">2</button>
<button id="col3_1">1</button>
</div>
<div class="col3">
<button id="col2_3">3</button>
<button id="col2_1">1</button>
</div>
<div class="col3">
<button id="col1_3">3</button>
<button id="col1_2">2</button>
</div>
</div>
</div>
<div class="desc left">
<a href="index.php" class="btn btn-secondary">回到設定頁面</a>
</div>
</div>
<div class="module">
<div class="mod">
<h4>暱稱</h4>
<h2><?=$nickname?></h2>
</div>
<div class="mod" id="move">
<h4>移動次數</h4>
<h2 id="num"><?=$_SESSION['step']?></h2>
</div>
<div class="mod" id="asideFuncButtons">
<button id="back" hidden>上一步</button>
<button id="auto">自動解答</button>
<button id="record">重播</button>
</div>
</div>
</div>
</div>
<script type="text/javascript" language="javascript">
//function 河內塔 手動處理
//需要紀錄 時間 走的位置
var startTime = 0;
var endTime = 0;
//時間差
var Time = new Array();
//重播的元素陣列
var Record = new Array();
var t; //控制暫停
function timedCount()
{
endTime+=1;
t=setTimeout("timedCount()",1000)
}
timedCount();
function run_hanoi(startTime,endTime,fromStackId,toStackId,brickId){
endTime = endTime - startTime;
startTime = endTime;
Time.push(endTime);
Record.push({fromStackId,toStackId,brickId});
sessionStorage.setItem('Record',JSON.stringify(Record));
sessionStorage.setItem('endTime',JSON.stringify(Time));
}
var step = <?=$_SESSION['step']?>;
//紀錄方塊在哪邊
var st_arr = new Array();
var i = <?=$_SESSION['difficulty']?>;
$(document).ready(function(e) {
//初始生成
if($('#num').text() >= '2'){
$('#back').prop('hidden','');
}
<?php
if($_SESSION['reset'] == true){
?>
for(var k = 1;k<=i;k++){
$('#col1').append('<div class="brick b'+k+'" draggable="true" ondragstart="drag(event);" value="'+k+'" id="'+k+'" data-id="'+k+'"></div>')
//產生 1-n個
}
for(var z=3; z>= 1;z--)
{
st_arr[z] = new Array();
//產生1-n個盤子
for(var q=1;q<=i;q++){
if(z == 1){
st_arr[z][q] = String(q);
}else{
st_arr[z][q] = '0';
}
}
}
saveToStorage();
<?php
$_SESSION['reset'] = false;
}else{
?>
//如果沒有重製的話
loadToStorage();
for(var z=3; z>= 1;z--)
{
st_arr[z] = new Array();
//產生1-n個盤子
for(var q=1;q<=i;q++){
st_arr[z][q] = String(); //拿session的值
}
}
<?php
}
?>
//滑鼠點擊操作事件
//1->3
$('#col1_3').on('click',function(){
var temp = $('#col1').children()
var temp_to = $('#col3').children();
var data_i = $(temp[0]).data('id')
var data_j = $(temp_to[0]).data('id')
if(data_j == null && data_i != null){
$('#col3').append(temp[0]);
step++;
st(1,3,data_i);
}else if(data_i > data_j){
console.log('false')
}else if(data_i < data_j){
$('#col3').prepend(temp[0]);
step++;
st(1,3,data_i);
}
})
//1->2
$('#col1_2').on('click',function(){
var temp = $('#col1').children()
var temp_to = $('#col2').children();
var data_i = $(temp[0]).data('id')
var data_j = $(temp_to[0]).data('id')
if(data_j == null && data_i != null){
$('#col2').append(temp[0]);
step++;
st(1,2,data_i);
}else if(data_i > data_j){
console.log('false')
}else if(data_i < data_j){
$('#col2').prepend(temp[0]);
step++;
st(1,2,data_i);
}
})
/*---------------------------------------------*/
//2->3
$('#col2_3').on('click',function(){
var temp = $('#col2').children()
var temp_to = $('#col3').children();
var data_i = $(temp[0]).data('id')
var data_j = $(temp_to[0]).data('id')
if(data_j == null && data_i != null){
$('#col3').append(temp[0]);
step++;
st(2,3,data_i);
}else if(data_i > data_j){
console.log('false')
}else if(data_i < data_j){
$('#col3').prepend(temp[0]);
step++;
st(2,3,data_i);
}
})
//2->1
$('#col2_1').on('click',function(){
var temp = $('#col2').children()
var temp_to = $('#col1').children();
var data_i = $(temp[0]).data('id')
var data_j = $(temp_to[0]).data('id')
if(data_j == null && data_i != null){
$('#col1').append(temp[0]);
step++;
st(2,1,data_i);
}else if(data_i > data_j){
console.log('false')
}else if(data_i < data_j){
$('#col1').prepend(temp[0]);
step++;
st(2,1,data_i);
}
})
/*---------------------------------------------*/
//3->1
$('#col3_1').on('click',function(){
var temp = $('#col3').children()
var temp_to = $('#col1').children();
var data_i = $(temp[0]).data('id')
var data_j = $(temp_to[0]).data('id')
if(data_j == null && data_i != null){
$('#col1').append(temp[0]);
step++;
st(3,1,data_i);
}else if(data_i > data_j){
console.log('false')
}else if(data_i < data_j){
$('#col1').prepend(temp[0]);
step++;
st(3,1,data_i);
}
})
//3->2
$('#col3_2').on('click',function(){
var temp = $('#col3').children()
var temp_to = $('#col2').children();
var data_i = $(temp[0]).data('id')
var data_j = $(temp_to[0]).data('id')
if(data_j == null && data_i != null){
$('#col2').append(temp[0]);
step++;
st(3,2,data_i);
}else if(data_i > data_j){
console.log('false')
}else if(data_i < data_j){
$('#col2').prepend(temp[0]);
step++;
st(3,2,data_i);
}
})
//河內塔自動解答
$('#auto').on('click',function(){
$('#col1').children().remove();
$('#col2').children().remove();
$('#col3').children().remove();
for(var k = 1;k<=i;k++){
$('#col1').append('<div class="brick b'+k+'" data-id="'+k+'"></div>')
}
hanoi(i,'#col1','#col2','#col3').forEach(function loop(move) {
//start ---- a by ---- b end ---- c
//console.log(move); //move 已經被參照成一個物件了 object.屬性
console.log('盤從 ' + move.from + ' 移至 ' + move.to);
animation_set(move.from,move.to);
});
animation_move();
})
var fr = [];
var t = [];
function animation_set(from,to){
fr.push(from);
t.push(to);
}
function animation_move(){
var i = 0;
var k = setInterval(function()
{
$(t[i]).prepend($(fr[i]).children()[0]);
i++;
if(i > fr.length){
clearInterval(k);
location.reload();
}
}
,500);
}
///河內塔上一步
$('#back').on('click',function b(){
var xxx = JSON.parse(sessionStorage.getItem("Record"));
var xxxlen = xxx.length;
var p = xxx.pop();
var ttt = JSON.parse(sessionStorage.getItem("endTime"));
ttt.pop();
sessionStorage.setItem("endTime",JSON.stringify(ttt));
sessionStorage.setItem("Record",JSON.stringify(xxx));
var mt = p.toStackId;
var mf = p.fromStackId;
var mdata_i = p.brickId;
//var mdata_i = JSON.parse(sessionStorage.getItem("mdata_i"));
step--;
st(mt,mf,mdata_i,'bc');
window.location.reload();
})
//function河內塔 自動處理
function hanoi(n,a,b,c){
//a-b
//a-c
//b-c
//$(c).append($(a).children()[0]);
if(n == 1){
return [{ from : a, to : c }]; //參照類別概念了 from to 都為屬性 a . c 皆為數值
}else{
return hanoi(n-1,a,c,b).concat( hanoi(1,a,b,c), hanoi(n - 1,b ,a , c));
}
console.log(move.from + 'ss' + move.to );
}
//重播事件
$('#record').on('click',function(){
clearTimeout(t)
animation_run();
})
});
//河內塔重播
function animation_run(){
var et = JSON.parse(sessionStorage.getItem("endTime"));
var g = JSON.parse(sessionStorage.getItem("Record"));
var len = et.length;
var n = 0;
console.log(et);
console.log(g);
$('#col1').children().remove();
$('#col2').children().remove();
$('#col3').children().remove();
for(var k = 1;k<=i;k++){
$('#col1').append('<div class="brick b'+k+'" draggable="true" ondragstart="drag(event);" value="'+k+'" id="'+k+'" data-id="'+k+'"></div>')
//產生 1-n個
}
for(var z=3; z>= 1;z--)
{
st_arr[z] = new Array();
//產生1-n個盤子
for(var q=1;q<=i;q++){
if(z == 1){
st_arr[z][q] = String(q);
}else{
st_arr[z][q] = '0';
}
}
}
//0 2 5
var lk = setInterval(function gg(){
console.log(n + '' +len);
//console.log($('.brick b'+g[n].brickId));
var obj = document.getElementsByClassName('brick b'+g[n].brickId);
$('#col'+g[n].toStackId).prepend(obj[0]);
if(n >= len-1){
clearInterval(lk);
window.location.reload();
}
n++;
},et[n]*1000);
}
//滑鼠拖拉操作事件
function drag(event){
event.dataTransfer.setData("text",event.currentTarget.id);
}
function allowdrop(event){
event.preventDefault();
}
function drop(event){
event.preventDefault();
var data_id = event.dataTransfer.getData("text"); //取要傳的id 的text資料型態
var to_obj = event.currentTarget; //我要過去的物件 位置
var len = to_obj.children.length; //我要過去的物件子節點長度有多少
var ob_data_id = document.getElementById(data_id)
var from_id = ob_data_id.parentElement.getAttribute('data-id');
var to_id = to_obj.getAttribute('data-id');
if(len == 0){
event.currentTarget.prepend(document.getElementById(data_id));
step++;
st(from_id,to_id,data_id);
}else if(to_obj.children[0].id > data_id){ //判斷可否過去
step++;
st(from_id,to_id,data_id);
event.currentTarget.prepend(document.getElementById(data_id));
}else{
console.log('false')
}
}
</script>
</body>
</html>