forked from CaptainClueless/php-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
298 lines (235 loc) · 11.6 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
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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class Game {
private $level;
private $points;
private $current_player_row = 0;
private $current_player_key = 0;
private $current_enemy_row = 0;
private $current_enemy_key = 0;
private $player_has_moved = false;
private $html_output;
private $player_is_alive = true;
private $level_number = 1;
private $debug = [];
private $collected_all_coins = true;
private $acceptable_tiles = ['_', 'C'];
public function get_debug(){
return $this->debug;
}
public function __construct(?array $level_override = null, $expected_level_number)
{
if(isset($_COOKIE['php_game_level_number'])) {
$this->level_number = $_COOKIE['php_game_level_number'];
if ($_COOKIE['php_game_level_number'] != $expected_level_number ) {
$this->level_number = $_COOKIE['php_game_level_number'];
$level_override = null;
$this->debug[] = 'Changing level to '. $this->level_number;
}
}
if(is_null($level_override)){
//Load the first level
$this->level = json_decode(file_get_contents('Levels/level'.$this->level_number.'.json'));
} else {
$this->level = $level_override;
}
}
private function door_check($action_to_take, $door = 'D' ){
if($this->collected_all_coins == true) {
if($action_to_take == $door) {
//Up level count and refresh
echo '<script>document.cookie = "php_game_level_number='. ($this->level_number + 1) .';path=/"; window.location.assign(window.location.href);</script>';
return true;
}
}
return false;
}
public function evaluate($move) {
//Assume we got all coins until we find one in the array
$this->collected_all_coins = true;
foreach($this->level as $row_key => $row) {
if ($key = array_search('C', $this->level[$row_key], true)) {
$this->collected_all_coins = false;
}
if ($key = array_search('P', $this->level[$row_key], true)) {
$this->current_player_key = $key;
$this->current_player_row = $row_key;
//We have a player
// echo 'Found Player @'. $key;
if ($_POST['dir'] == 'left') {
if (in_array($row[$key - 1], $this->acceptable_tiles )) {
$this->level[$row_key][$key] = '_';
$this->level[$row_key][$key - 1] = 'P';
$this->current_player_key = $key - 1;
$this->current_player_row = $row_key;
$this->player_has_moved = true;
}
$this->door_check($row[$key -1]);
}
if ($_POST['dir'] == 'right') {
//Move left
if (in_array($row[$key + 1], $this->acceptable_tiles)) {
$this->level[$row_key][$key] = '_';
$this->level[$row_key][$key + 1] = 'P';
$this->current_player_key = $key + 1;
$this->current_player_row = $row_key;
$this->player_has_moved = true;
}
$this->door_check($row[$key +1]);
}
if($this->player_has_moved == false) {
if ($_POST['dir'] == 'up') {
if (in_array($this->level[$row_key - 1][$key], $this->acceptable_tiles) ) {
$this->level[$row_key][$key] = '_';
$this->level[$row_key - 1][$key] = 'P';
$this->current_player_key = $key;
$this->current_player_row = $row_key - 1;
$this->player_has_moved = true;
}
}
if ($_POST['dir'] == 'down') {
if (in_array($this->level[$row_key + 1][$key], $this->acceptable_tiles)) {
$this->level[$row_key][$key] = '_';
$this->level[$row_key + 1][$key] = 'P';
$this->current_player_key = $key;
$this->current_player_row = $row_key + 1;
$this->player_has_moved = true;
}
}
}
}
}
$this->debug[] = 'the door is '.($this->collected_all_coins ? 'open': 'closed');
//Clear some stuff so we dont have conflicts
unset($row_key);
unset($key);
unset($row);
$enemy_has_moved = false;
$try_to_move_count = 0;
foreach($this->level as $row_key => $row) {
if ($ekey = array_search('E', $this->level[$row_key], true)) {
$this->debug[] = 'Found Enemy';
$this->current_enemy_key = $ekey;
$this->current_enemy_row = $row_key;
while($enemy_has_moved == false && $try_to_move_count < 10) {
$should_move_vertical = (bool) rand(0,1);
$this->debug[] = 'Enemy move attempt '. $try_to_move_count;
if ($enemy_has_moved === false) {
$this->debug[] = 'Enemy is attempting to move '. ($should_move_vertical == 0 ? 'horizontally': 'vertically');
if (!$should_move_vertical) {
if ($ekey > $this->current_player_key) {
if ($this->level[$row_key][$ekey - 1] == '_') {
$this->level[$row_key][$ekey] = '_';
$this->level[$row_key][$ekey - 1] = 'E';
$enemy_has_moved = true;
}
if ($this->level[$row_key][$ekey - 1] == 'P') {
$this->level[$row_key][$ekey - 1] = 'O';
$this->player_is_alive = false;
}
} else if ($ekey < $this->current_player_key) {
if ($this->level[$row_key][$ekey + 1] == '_') {
$this->level[$row_key][$ekey] = '_';
$this->level[$row_key][$ekey + 1] = 'E';
$enemy_has_moved = true;
}
if ($this->level[$row_key][$ekey + 1] == 'P') {
$this->level[$row_key][$ekey + 1] = 'O';
$this->player_is_alive = false;
}
}
}
if($should_move_vertical || (!$should_move_vertical && $enemy_has_moved == false)){
if ($row_key > $this->current_player_row) {
if ($this->level[$row_key - 1][$ekey] == '_') {
$this->level[$row_key][$ekey] = '_';
$this->level[$row_key - 1][$ekey] = 'E';
$enemy_has_moved = true;
}
if ($this->level[$row_key - 1][$ekey] == 'P') {
$this->level[$row_key - 1][$ekey] = 'O';
$this->player_is_alive = false;
}
} else if ($row_key < $this->current_player_row) {
if ($this->level[$row_key + 1][$ekey] == '_') {
$this->level[$row_key][$ekey] = '_';
$this->level[$row_key + 1][$ekey] = 'E';
$enemy_has_moved = true;
}
if ($this->level[$row_key + 1][$ekey] == 'P') {
$this->level[$row_key + 1][$ekey] = 'O';
$this->player_is_alive = false;
}
}
}
}
$try_to_move_count++;
}
}
}
}
private function generate_board()
{
$output = '<div class="jumbotron">';
foreach ($this->level as $row) {
foreach ($row as $part) {
if ($part == 'X') {
$output .= '<button type="button" disabled class="btn btn-secondary board"> </button>';
} else if ($part == 'P') {
$output .= '<button type="button" disabled class="btn btn-primary board"><span class="fas fa-user"></span></button>';
} else if ($part == 'E') {
$output .= '<button type="button" disabled class="btn btn-danger board"><span class="fas fa-dragon"></span></button>';
} else if ($part == 'D') {
if ($this->collected_all_coins == true) {
$output .= '<button type="button" disabled class="btn btn-success board"><span class="fas fa-door-open"></span></button>';
} else {
$output .= '<button type="button" disabled class="btn btn-secondary board"><span class="fas fa-door-closed"></span></button>';
}
} else if ($part == 'O') {
$output .= '<button type="button" disabled class="btn btn-danger board"> </button>';
} else if ($part == 'C') {
$output .= '<button type="button" disabled class="btn btn-warning board"><span class="fas fa-key"></span></button>';
} else {
$output .= '<button type="button" disabled class="btn btn-light board"> </button>';
}
}
$output .= '<br>';
}
return $output.'</div>';
}
private function generate_form(){
return '<form method="POST" >
<button name="dir" class="btn left-button " value="left">Left</button>
<button name="dir" class="btn right-button " value="right">Right</button>
<button name="dir" class="btn up-button " value="up">Up</button>
<button name="dir" class="btn down-button" value="down">Down</button>
<input type="hidden" name="level_number" value="'.$this->level_number.'" >
<input type="hidden" name="level_data" value="'.urlencode(json_encode($this->level)).'">
</form>
<p>You are on level '.$this->level_number.'</p>
<button value=\'document.cookie ="php_game_level_number=1; expires = Thu, 01 Jan 1970 00:00:00 GMT"\'>Clear Progress</button>
';
}
public function render(){
$this->html_output = file_get_contents('templates/header.html');
$this->html_output .= $this->generate_board();
if($this->player_is_alive){
$this->html_output.= $this->generate_form();
}
$this->html_output.= file_get_contents('templates/footer.html');
echo $this->html_output;
}
}
$level = null;
$level_number = 1;
if(isset($_POST) && isset($_POST['dir'])){
$level = json_decode(urldecode($_POST['level_data']), true);
$level_number = $_POST['level_number'];
}
$game = new Game($level, $level_number);
if(isset($_POST) && isset($_POST['dir'])) {
$game->evaluate($_POST['dir']);
}
$game->render();