-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame random fix.php
More file actions
520 lines (466 loc) · 21.1 KB
/
game random fix.php
File metadata and controls
520 lines (466 loc) · 21.1 KB
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
<?php
$startingGridNum;
if ($_POST['game-mode'] == 'easy') {
$startingGridNum = 4;
} else if ($_POST['game-mode'] == 'normal') {
$startingGridNum = 5;
} else if ($_POST['game-mode'] == 'hard') {
$startingGridNum = 6;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Memory Path</title>
<link href="style.css" rel="stylesheet" type="text/css"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>var size = <?php echo json_encode($startingGridNum) ?>; </script>
<script>
var gameStatus = false; // False if game is paused/stopped, true otherwise.
var gameClear = false; // Represents the clear status of a stage. Becomes true when player reaches the last column.
var stageNumber = 1; // Represents the current stage number.
var totalScore = 0; // Total score of player for this game.
var stageScore = 0; // Score for current stage.
var life = 3; // Player's life for current stage.
var timer = 60; // Placeholder variable to represent timer, acts as an integer
var pathArray = []; // Records the correct path's coordinate.
var stepOrder = 0;
var isDown = false;
var path_col;
var path_row;
//timer object
var counter;
//the place where current time is storaged when you pause
var currentTime;
//execute pause function
var pauseOn = false;
timer = new Countdown();
// Changes grid to a square
// If grid width is greater than 800, it is changed to 50%
$(document).ready(function () {
var cw = $('.game-panel').width();
if (cw > 800) {
$('.game-panel').css({ 'width': 35 + '%' });
cw = $('.game-panel').width();
}
$('.game-panel').css({ 'height': cw + 'px' });
});
$(document).mousedown(function () {
isDown = true; // When mouse goes down, set isDown to true
})
$(document).mouseup(function () {
isDown = false; // When mouse goes up, set isDown to false
});
// If game has been started, the cells of the table will
// respond to clicks.
$(document).on("pagecreate", "#pageone", function () {
$("td.panel").mousedown(function () {
if (gameStatus) {
if ($(this).hasClass("step" + stepOrder)) {
$(this).css("background-color", "green");
stepOrder++;
if (stepOrder == pathArray.length) {
stageClearScreen();
gameClear = true;
}
} else {
if (!$(this).hasClass("clicked")) {
$(this).css("background-color", "red");
life--;
updateLifeMessage();
if (life == 0) {
gameOver();
}
}
}
$(this).addClass("clicked");
}
});
$("td.panel").mouseover(function () {
if (gameStatus) {
if (isDown) {
if ($(this).hasClass("step" + stepOrder)) {
$(this).css("background-color", "green");
stepOrder++;
if (stepOrder == pathArray.length) {
stageClearScreen();
gameClear = true;
}
} else {
if (!$(this).hasClass("clicked")) {
$(this).css("background-color", "red");
life--;
updateLifeMessage();
if (life == 0) {
gameOver();
}
}
}
$(this).addClass("clicked");
}
}
});
});
// If timer counts down to zero, check if player's life is zero.
// If life is zero, continue onto gameover process.
// If life is not zero, take away one life and restart stage or reset stage.
if (timer == 0) {
if (lifeZero()) {
gameOver();
} else {
lifeMinusOne();
// restart or reset stage.
}
}
// If the stage is cleared, a list of functions will be called.
// It will end with starting a new stage.
// **This code snippet needs to be constantly checked, possibly need to do something more.**
if (gameClear) {
stageClearScreen();
calculateScore();
showScoreAchieved();
addToTotalScore();
startNewStage();
}
// Function to reset all game status.
// Used for testing stage
function resetGame() {
gameStatus = false;
life = 3;
updateLifeMessage();
resetGrid();
stepOrder = 0;
}
// Function to continue onto next stage
function nextGame() {
gameStatus = false;
life = 3;
updateLifeMessage();
resetGrid();
stepOrder = 0;
stageNumber++;
updateStageNumber();
pathArray = [];
resetGridClass();
}
function updateLifeMessage() {
$("#footer h3").html('Life: ' + life);
}
function updateStageNumber() {
$("#header h2").html('Stage ' + stageNumber);
}
// This function contains the entire gameover process.
// Includes showing gameover screen, showing score achieved, entering name
// for ranking, and anything else that needs to be done.
function gameOver() {
calculateScore();
addToTotalScore();
clearInterval(counter);
$("#timer").html("Time: ");
$("#gameover").popup("open");
}
// Shows a stage clear screen.
// May be a popup, or just some texts.
function stageClearScreen() {
calculateScore();
addToTotalScore();
clearInterval(counter);
$("#timer").html("Time: ");
$("#clear").popup("open");
$("#placeForScore").html("<h3>Score: " + totalScore + "</h3>");
stageScore = 0;
}
// Calculate the score achieved by the player for the current stage.
// Calculation involves the time and life remaining.
function calculateScore() {
stageScore = 120* (timer.seconds);
}
// Shows the score achieved by the player for the current stage.
// Can be done in any method desired.
function showScoreAchieved() {
// Implementation here.
}
// Add the current score to total score and reset the current score.
function addToTotalScore() {
totalScore += stageScore;
}
// Reset timer, grid/table, life remaining, start button, and update total score.
// Grid/table size may be changed depending on stage number.
function startNewStage() {
// Implementation here.
}
// This function starts the game.
function gameAct() {
if (!gameStatus) {
gameStatus = true;
startGame();
// Try to make the button disappear. (Not done)
}
}
// This function will start the game. Timer will start to count down.
// Only called by the start button.
function startGame() {
generatePath(); // Generate random path. (Not done)
showPath(); // Show the user the path by making the corresponding grid/cell change color. (Not done)
resetGrid(); // Make all grid/cell go back to original color. (Not done)
gameStart(); // Implementation of resuming the timer. (Not done)
}
// This function will generate the random path for each stage.
// No validation of game status is needed because this function will
// only be called when validation is done.
function generatePath() {
path_row = 0;
path_col = 0;
path_row = randomIntFromInterval(0, size - 1);
pathArray.push(path_row + " " + path_col);
while (path_col < size - 1) {
rowMovement = checkTwoSides();
if (rowMovement == 3) {
path_col++;
} else {
direction = randomIntFromInterval(0, 1);
if (direction == 0) {
path_col++;
} else {
changeRow();
}
}
pathArray.push(path_row + " " + path_col);
}
alert(pathArray.length);
alert(pathArray);
/* hard coded path.
window.pathArray = ["3 0", "3 1", "2 1", "1 1", "1 2", "1 3", "2 3", "2 4"];
x_coord = 2;
y_coord = 5;
pathArray.push(x_coord + " " + y_coord);
while (pathArray.length <= size) {
pathArray.push(path_row + " " + path_col);
path_col++;
}*/
}
function changeRow() {
upRow = path_row + 1;
downRow = path_row - 1;
if (path_row == 0) {
path_row++;
} else if (path_row == size - 1) {
path_row--;
} else if (checkTwoSides() == 0) {
path_row++;
} else if (checkTwoSides() == 1) {
path_row--;
} else {
dir = randomIntFromInterval(0, 1);
if (dir == 0) {
path_row++;
} else {
path_row--;
}
}
}
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function checkTwoSides() {
upRow = path_row + 1;
downRow = path_row - 1;
array = window.pathArray[window.pathArray.length - 1];
if (window.pathArray.length > 1) {
array = window.pathArray[window.pathArray.length - 2];
}
row = parseInt(array.substring(0, 1));
col = parseInt(array.substring(2, 3));
if (col == path_col) {
if (((path_row == 0) && (row == path_row + 1)
|| (path_row == size - 1) && (row == path_row - 1))
&& (col == path_col)) {
return 3; // Represent no movement of row possible
} else
if (
path_row == row + 1
//$.inArray((upRow + ' ' + path_column), array )
) {
return 0; // Represent upward direction
} else if (
path_row == row - 1
//$.inArray((downRow + ' ' + path_column), array )
) {
return 1; // Represent downward direction
} else {
return 2; // Represent no used tile up or down
}
}
}
// This function will use the random path generated in another function
// and show the path to the player by making the corresponding tiles
// change color in order.
// Time interval between the change of color of each tile depends on
// the game mode and current stage number.
function showPath() {
var i = 0;
var interval = setInterval(function () {
array = window.pathArray[i];
row = parseInt(array.substring(0, 1));
col = parseInt(array.substring(2, 3));
var table = $("#grid")[0];
var cell = table.rows[row].cells[col];
$(cell).css('background-color', 'white');
$(cell).addClass('step' + i);
//y.rows[row].cells[col].className += ' step' + i;
i++;
if (i == window.pathArray.length) {
clearInterval(interval);
setTimeout(resetGrid, 200);
timer.init();
}
}, 200);
}
// This function will reset the all the tiles inside the grid to original
// state, which means original color. The recorded randomized path is not
// affected.
function resetGrid() {
var table = $("#grid")[0];
for (row = 0; row < window.size; row++) {
for (col = 0; col < window.size; col++) {
var cell = table.rows[row].cells[col];
$(cell).css('background-color', '#808080');
}
col = 0;
}
}
function resetGridClass() {
var table = $("#grid")[0];
for (row = 0; row < window.size; row++) {
for (col = 0; col < window.size; col++) {
var cell = table.rows[row].cells[col];
$(cell).removeClass();
}
col = 0;
}
}
// This function basically makes the timer start counting down.
// It will be called when the start button is clicked on, or when
// the menu popup is cleared.
function gameStart() {
// Implementation needed.
}
function Countdown() {
//time declared
if (pauseOn == false) {
this.start_time = 20+(5*stageNumber);
} else {
this.start_time = currentTime;
}
this.target_id = "#timer";
this.name = "timer";
}
// execute other functions
Countdown.prototype.init = function () {
this.reset();
counter = setInterval(this.name + '.tick()', 1000);
}
// divide time declared into min, second and millisecond
// storaged in an array whose values are able to be used
//individually
Countdown.prototype.reset = function () {
this.seconds = this.start_time;
this.update_target();
}
// basic calculation decrementing time as countdown.
Countdown.prototype.tick = function () {
if (gameClear == true) {
clearInterval(counter);
gameClear = false;
}
if (this.seconds > 0) {
this.seconds = this.seconds - 1;
}
this.update_target();
}
//pause timer when user click the menu button
Countdown.prototype.pauseTimer = function () {
pauseOn = true;
clearInterval(counter);
if (seconds < 10) this.seconds = "0" + this.seconds;
currentTime = this.seconds;
$(game-menu).append("<div>Pause</div><br><div>currentTime</div>");
}
Countdown.prototype.restart = function () {
counter = setInterval(this.name + ".tick()", 1000);
}
// update a new time from tick function every 10 millisecond
Countdown.prototype.update_target = function () {
seconds = this.seconds;
if (seconds < 10) seconds = "0" + seconds;
if ( seconds == 0) {
$(this.target_id).html("Time Out!!");
gameClear = true;
if (gameClear == true) {
clearInterval(counter);
gameClear = false;
}
gameOver();
} else {
$(this.target_id).html("Time: " + seconds);
}
}
</script>
</head>
<body>
<div data-role="page" id="pageone">
<video autoplay id="space">
<source src="space1.mp4">
</video>
<div data-role="header" id="header">
<h1 id="timer">Time: </h1>
<h2>Stage 1</h2>
<a href="#game-menu" data-rel="popup" data-transition="slideup" class="ui-btn ui-corner-all ui-btn-inline" data-position-to="window" onclick="timer.pauseTimer()">Menu</a>
<div data-role="popup" data-theme="b" class="ui-content ui-corner-all" data-dismissible="false" id="game-menu">
<a href="#in-game-instruction" data-rel="popup" data-transition="popup" class="ui-btn ui-corner-all" data-position-to="window">Instruction</a>
<a href="index.html" class="ui-btn ui-corner-all">Back to main menu</a>
<a href="#" data-rel="back" class="ui-btn ui-corner-all" data-transition="slidedown" onclick="timer.restart()">Return to game</a>
</div>
</div>
<div data-role="content" style="text-align: center;">
<div id="game-screen">
<?php
$row = 1;
$col = 1;
echo '<table class="game-panel" id="grid">';
while ($row <= $startingGridNum) {
echo '<tr>';
while ($col <= $startingGridNum) {
echo '<td class="panel" onmousedown="event.preventDefault ? event.preventDefault() : event.returnValue = false">' . '</td>';
$col++;
}
echo '</tr>';
$row++;
$col = 1;
}
echo '</table>';
?>
<a href="#" id="actButton" class="ui-btn ui-corner-all ui-btn-inline" onclick="gameAct()">Start</a>
</div>
<div id="gameover" data-role="popup" data-transition="pop" data-theme="b" data-overlay-theme="a" class="ui-content ui-corner-all" data-dismissible="false">
<h1>Game Over!</h1>
<a href="index.html" class="ui-btn ui-corner-all">Return to main menu</a>
<a data-rel="back" class="ui-btn ui-corner-all" onclick="resetGame()">Restart stage</a>
</div>
<div id="clear" data-role="popup" data-transition="pop" data-theme="b" data-overlay-theme="a" class="ui-content ui-corner-all" data-dismissible="false">
<h1>Stage Cleared!</h1>
<div id="placeForScore"></div>
<a href="index.html" class="ui-btn ui-corner-all">Return to main menu</a>
<a data-rel="back" class="ui-btn ui-corner-all" onclick="nextGame()">Next stage</a>
</div>
</div>
<div data-role="footer" id="footer">
<h3 id="life">Life: <script>document.write(life);</script></h3>
</div>
</div>
</body>
</html>