-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
162 lines (151 loc) · 4.39 KB
/
index.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
<?php
ini_set('memory_limit', '8192M');
require_once('Maze.php');
$w = isset($_GET['w']) ? $_GET['w'] : mt_rand(8, 48);
$h = isset($_GET['h']) ? $_GET['h'] : mt_rand(8, 24);
$m = new Maze($w, $h);
?>
<!doctype HTML>
<html>
<head>
<script
src="http://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<style>* { font-family: monospace; }td { width: 10px; height: 10px; }</style>
<script>
var speed = 50;
var w = <?= $w ?>;
var h = <?= $h ?>;
var exit = false;
var start = true;
var table;
var stack = [];
var minotaur_pos;
var minotaur_prec = null;
var minotaur_bck = { background: 'white', text: ' ' };
var minotaur_sleep = 10;
var minotaur_sleep_every = 40;
var minotaur_count = minotaur_sleep;
var minotaur_is_sleeping = false;
var outcome = function(success, msg) {
var cont = $('h1 > span');
cont.css('background', success ? 'green' : 'red');
table.find('td[data-wall]').css('background', success ? 'green' : 'red');
cont.text(' ' + msg + ' ');
};
var rand = function(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
var minoStep = function() {
var pos = minotaur_pos;
minotaur_count--;
if (minotaur_is_sleeping) {
if (minotaur_count == 0) {
minotaur_is_sleeping = false;
minotaur_count = minotaur_sleep_every;
}
return;
} else if (minotaur_count == 0) {
minotaur_is_sleeping = true;
minotaur_count = minotaur_sleep;
minotaur_pos.text('Z');
minotaur_pos.css('background', 'lightgrey');
return;
}
var prec = minotaur_prec;
var x = parseInt(pos.attr('data-x'));
var y = parseInt(pos.attr('data-y'));
var n = table.find('#' + x + '-' + (y - 1));
var s = table.find('#' + x + '-' + (y + 1));
var w = table.find('#' + (x - 1) + '-' + y);
var e = table.find('#' + (x + 1) + '-' + y);
var cands = [n, s, w, e].filter(function(el) {
return el.length > 0
&& (el.text().trim() == '' || el.text().trim() == '.')
&& (prec == null || el.attr('id') != prec.attr('id'));
});
if (cands.length > 0) {
var cand = cands[rand(0, cands.length - 1)];
minotaur_prec = pos;
minotaur_pos = cand;
} else {
minotaur_prec = pos;
minotaur_pos = prec;
}
minotaur_prec.text(minotaur_bck.text);
minotaur_prec.css('background', minotaur_bck.background);
minotaur_bck.text = minotaur_pos.text();
minotaur_bck.background = minotaur_pos.css('background');
minotaur_pos.text('M');
minotaur_pos.css('background', 'grey');
};
var step = function(pos) {
minoStep();
if (pos.text() == 'X') {
outcome(true, 'WIN!');
return;
}
if (!pos || pos.text() == 'S' && !start) {
outcome(false, 'LOST IN THE MAZE...');
return;
}
if (pos.text() == 'M') {
outcome(false, 'EATEN ALIVE...');
return;
}
start = false;
if (pos.text().trim() == '') {
pos.text('.');
pos.css('background', 'yellow');
} else if (pos.text() == '.') {
pos.css('background', 'orange');
}
var x = parseInt(pos.attr('data-x'));
var y = parseInt(pos.attr('data-y'));
var n = table.find('#' + x + '-' + (y - 1));
var s = table.find('#' + x + '-' + (y + 1));
var w = table.find('#' + (x - 1) + '-' + y);
var e = table.find('#' + (x + 1) + '-' + y);
var ads = [n, s, w, e];
var mino = ads.filter(function(el) {
return el.length > 0 && el.text().trim() == 'M';
});
if (mino.length > 0) {
outcome(false, 'EATEN ALIVE...');
return;
}
var cands = ads.filter(function(el) {
return el.length > 0
&& (el.text().trim() == '' || el.text().trim() == 'X' || el.text().trim() == 'Z');
});
if (cands.length > 0) {
var cand = cands[rand(0, cands.length - 1)];
stack.push(pos);
setTimeout(function() {
step(cand);
}, speed);
} else {
pos.css('background', 'orange');
var backtrack = stack.pop();
setTimeout(function() {
step(backtrack);
}, speed);
}
};
$(function() {
table = $('table');
var start = table.find('td[data-start]');
minotaur_pos = table.find('td[data-minotaur]');
step(start);
});
</script>
</head>
<body>
<h1><?php echo "$w::$h"; ?> <span></span></h1>
<?php
//$m->prettyPrint();
$m->tablePrint();
?>
</body>
</html>