summaryrefslogtreecommitdiffstats
path: root/pages/test.php
blob: a5fe882a5ed8254651b17bc59ede3a46cee14c2b (plain)
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<!-- iso-8859-1 -->
<head> 
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
   <link href="css/mapstyles.css" rel="stylesheet" type="text/css" />
   <title>Maze TowerD</title>

<?PHP
//  <script src="js/mapspecs.js" type="text/javascript"></script> 

include('../mazetd/includes/maps.php');

//$mygrid = GenerateMap(20, 8, 4, 3);
if (isset($_GET[mapcode])) {
   $mygrid = GenerateMapByCode($_GET[mapcode]);
} else {
	$mygrid = GenerateMap(12, 12, 8);
	$mygrid2 = GenerateMap(rand(12, 18), rand(10, 14), 8);
	//$mygrid = GenerateMap(23, 13, 7, 3);
}
$code = GenerateMapCode($mygrid);


?>
 
   <script src="js/ajax.js" type="text/javascript"></script> 
	
<script type="text/javascript">
window.onload = function(){
   document.getElementById('blocksdisplay').innerHTML = "<b>"+blocks+"</b>";
	//doSend();
}


var solution = new Array();
var blocks = new Array();
var count = new Array();
var mapdata = new Array();

function grid_click(obj) {

	//Prepare data
	tmp = obj.id.split(',');
	mapid = tmp[0] - 0;
	y = tmp[1];
	x = tmp[2];

	if (solution[mapid] == undefined) {
		getmapdata(mapid);
	}
	
   if (obj.cv) {
      obj.cv = false;
		
      //obj.style.backgroundColor = '#ffffff';
      obj.setAttribute("class", "grid_td");
      blocks[mapid]++;
		//alert(obj.id);
		//Remove wall
		solution[mapid] = solution[mapid].replace(y+','+x+'.', '');
   } else {
      if (blocks[mapid] < 1) {
         alert("Outa blocks!");
         return;
      }
      obj.cv = true;
      //obj.style.backgroundColor = '#ff0000';
      obj.setAttribute("class", "grid_td_walls");
		//Add Wall
		solution[mapid] += y+','+x+'.';
      blocks[mapid]--;
   }
   document.getElementById('blocksdisplay').innerHTML = "<b>"+blocks[mapid]+"</b>";
   
	document.getElementById(mapid+',dsptr').innerHTML = " "+blocks[mapid]+" walls remain.";
	
}

function getmapdata(mapid) {
	mapdata[mapid] = JSON.parse(document.getElementById(mapid+',mapdata').innerHTML);
	blocks[mapid] = mapdata[mapid].walls;
	solution[mapid] = '.';
	document.getElementById(mapid+',dsptr').innerHTML = " "+blocks[mapid]+" walls remain.";
	//document.getElementById(mapid+',dsptr').innerHTML = blocks[mapid];
}

//==needs mapid
function doSend(mapid) {
	//mapcode = document.getElementById('mapcode').innerHTML;
	//alert("dosend..");
	//mapid = 1;
	if (solution[mapid] == undefined) {
		getmapdata(mapid);
	}
	
	mapcode = '<? echo $code; ?>';
	reqstr = "";
	reqstr += "&mapcode="+mapdata[mapid].code;
	reqstr += "&mapid="+mapid;
	//alert (mapdata[mapid].code);
	reqstr += "&solution="+solution[mapid];
	
	//alert(reqstr);
	document.getElementById('dispdo').innerHTML = "http://mazetd.4xg.net/do.php?r=getpath"+reqstr
	
	//alert("request string:"+reqstr);
   ajax.requestFile = "do.php?r=getpath"+reqstr; //prepare strdata
   ajax.onCompletion = request_path_done; // Specify function to be executed on response.
   ajax.runAJAX();// Do it!
}
function resetwalls(mapid) {
	answer = confirm("Remove walls and start fresh?");
	if (answer) {
		if (solution[mapid] == undefined) return;
		walls = solution[mapid].split('.');
		for(var i in walls) {
			//alert(walls[i]);
			tmp = walls[i].split(',');
			eid = mapid+','+tmp[0]+','+tmp[1];
			//alert('eid:'+eid);
			if (document.getElementById(eid) != undefined) {
				obj = document.getElementById(eid);
				obj.setAttribute("class", "grid_td");
				obj.cv = false;
				//alert("exe");
			}
		}
		solution[mapid] = undefined;
		getmapdata(mapid);
	}
}

function request_path_done() {
	//document.getElementById('mapdisplay').innerHTML = ajax.response;
	//alert ("resp: "+ajax.response);
	//if (ajax.response == undefined) return;
	
	//alert ("resp: "+ajax.response);
	var JO = JSON.parse(ajax.response);

	for(var i in JO.error) {
		alert('\n JO error ' + JO.error[i]);
	}
	
	//document.getElementById('disp').innerHTML = JO.map;
	
	if (JO.blocked) {
		alert("Path is blocked make sure there is a way for it to go!");
		return;
	}
	//document.write(JO.map);
	//alert ("moves: "+JO.moves);
	//alert ("path: "+JO.start);
	document.getElementById('disppath').innerHTML = JO.path;
	document.getElementById('disptotalmoves').innerHTML = JO.moves;
	
	AnimatePath(JO.path, JO.mapid, JO.start);
	
	//document.getElementById('mapdisplay').innerHTML = JO.map;
	
}


//This needs a start location.
function AnimatePath(path, mapid, start) {
	tmp = start.split(',');
	y = tmp[0];
	x = tmp[1];
	
	p = path;
	//alert("Begin");
	t = p.length;
	//count[mapid] = 0;
	doanimate(x, y, p, t, mapid);
	//setTimeout("doanimate("+x+","+y+",'"+p+"',"+t+")",500);
}

var snake = new Array();

function doanimate(x, y, p, t, mapid) {
	//Animate current square, and move to next one.
	if (count[mapid] == undefined) {
		count[mapid] = 0;
	}
	//alert("test");
	if (snake[mapid] == undefined) {
		snake[mapid] = new Array();
		snake[mapid]['color'] = '#4444bb';
	}
	
	//==
	// document.getElementById('1count').innerHTML = count[mapid];
	document.getElementById(mapid+',dspbr').innerHTML = count[mapid]+ " moves";
	
	eid = mapid+','+x+','+y;
	
	//Verify.
	if (document.getElementById(eid) == undefined) {
		alert("Path exited field..?");
		return;
	}
	handle = document.getElementById(eid);
	
	//Maintain our original classname, no matter the cost!
	origclass = handle.className;
	if (	origclass == 'grid_td_path1' || 
			origclass == 'grid_td_path2' || 
			origclass == 'grid_td_path3' || 
			origclass == 'grid_td_path4' || 
			origclass == 'grid_td_animate_pre' ) {
	// if (origclass == 'grid_td_animate' || origclass == 'grid_td_animate_pre' ) {
		origclass = handle.classOrigName;
		//setTimeout(handle.setAttribute('class', origclass), 850);
		//alert('ex ' + origclass);
	}
	//handle.style.backgroundColor = snake[mapid]['color'];
	handle.classOrigName = origclass;
	
	
	//Just moves
	switch(t) {
		case '1': 		//1 - Up
		case '2':		//2 - Right
		case '3': 		//3 - Down
		case '4': 		//4 - Left
		count[mapid]++;
		handle.setAttribute('class', 'grid_td_path'+t);
		break;
	}
	handle.style.backgroundColor = snake[mapid]['color'];
	
	//setTimeout(handle.setAttribute('class', origclass), 850);
	setTimeout("document.getElementById('"+eid+"').setAttribute('class', '"+origclass+"')", 855);
	setTimeout("document.getElementById('"+eid+"').style.backgroundColor = '';", 855);
	
	//Okay lets make it look cool now.
	//document.getElementById(handle).setAttribute('class', 'grid_td_animate_pre');
	//Needs a delay, or simply wont work.
	//setTimeout("document.getElementById('"+handle+"').setAttribute('class', 'grid_td_animate')", 55);
	
	//document.getElementById('blocksdisplay').innerHTML = "<b>"+origclass+"</b>";

	//if (origclass != 'grid_td_animate' && origclass != 'grid_td_animate_pre' ) {
		//document.getElementById('blocksdisplay').innerHTML = "<b>"+origclass+"</b>";
	//	setTimeout("document.getElementById('"+handle+"').setAttribute('class', '"+origclass+"')", 850);
	//} else {
		//document.getElementById('blocksdisplay').innerHTML = "<b>HELLO WORLD!?</b>";
	//}
	
	//Moving on:
	speed = 85;
	t = p.substring(0, 1);
	if (t == '') {
		count[mapid] = 0;
		snake[mapid]['color'] = '#4444bb';
		return;
	}
	switch(t) {
		case '1': x--; break; 		//1 - Up
		case '2': y++; break; 		//2 - Right
		case '3': x++; break; 		//3 - Down
		case '4': y--; break; 		//4 - Left
		//Special codes.
		//Reached Target
		case 'a': 
			speed = 800; 
			snake[mapid]['color'] = '#F777FF';
		break;
		case 'b': 
			speed = 800; 
			snake[mapid]['color'] = '#FFFFAA';
		break;
		case 'c': 
			speed = 800;
			snake[mapid]['color'] = '#44EE66';
		break; 
		case 'd': 
			speed = 800; 
			snake[mapid]['color'] = '#ff9911';
		break; 
		case 'e': 
			speed = 800; 
			snake[mapid]['color'] = '#44ddee';
		break; 
		case 'f': 
			speed = 800; 
			snake[mapid]['color'] = '#4444bb';
		break; 
		//The mighty teleport.
		case 'u': 
			//alert(p);
			tmp = p.split('u');
			//alert(tmp[1]);
			//alert(tmp[2]);
			AnimatePath(tmp[2], mapid, tmp[1]);
			//setTimeout("doanimate("+x+","+y+",'"+p+"','"+t+"','"+mapid+"')",speed);
			return;
			speed = 800; 
		break; 
		case 'n': 
			//alert(p);
			tmp = p.split('n');
			//alert(tmp[1]);
			//alert(tmp[2]);
			AnimatePath(tmp[2], mapid, tmp[1]);
			//setTimeout("doanimate("+x+","+y+",'"+p+"','"+t+"','"+mapid+"')",speed);
			return;
			speed = 800; 
		break; 
	}
	//Remove move from p
	p = p.substring(1);
	//speed = (10 * p.length) + 40;

	setTimeout("doanimate("+x+","+y+",'"+p+"','"+t+"','"+mapid+"')",speed);
}
var de = false;
function displaymap(obj) {
	if (de == false) {
		obj.setAttribute('class', 'mapdisplay_up');
	}
}
function minmap(obj) {
	de = true;
	obj.setAttribute('class', 'mapdisplay');
	setTimeout("de = false;", 1000);
}
   
</script>
</head>

<body>
<?php


echo DisplayMap($mygrid, 1);
echo DisplayMap($mygrid2, 2);

//Echo Findpath ($mygrid);
$start[0] = 0;
$start[1] = 1;

//$p = Findpath ($mygrid, $start, 'a');
//$pathmap1 = $p['path'];

//echo "<br />BP1:".$p['path']."<br />";
//echo "<br />yx2:".$p['end']."<br />";

//$p = Findpath ($mygrid, explode(",", $p['end']), 'b');
//echo "<br />BP:".$p['path']."<br />";
//echo "<br />yx:".$p['end']."<br />";
// $pathmap1 .= $p['path'];

// $p = Findpath ($mygrid, explode(",", $p['end']), 'c');
// $pathmap1 .= $p['path'];

$p = Findpath ($mygrid, "2,4.3,4", 'f');
$pathmap1 .= $p['path'];

//echo DisplayMap($pathmap);

//==Needs to contain mapid.
//echo "<br /> <b><a href='javascript:doSend()'>Attempt Solution laaaaaaaaaaaaa laaaaaaaaaaaa CLick on this..</a></b>";
echo "<br /><div style='font-size:32pt;' id='1count'><b>b</b></div> ";
echo "<br />";
var_dump ($p);


echo "<br />";

   // $time = microtime();
   // $time = explode(' ', $time);
   // $time = $time[1] + $time[0];
   // $begintime = $time;
	
	//for($i = 1; $i <= 500; $i++) {
		//$mygrid = generateMap(13, 8, 4, 3);
		//echo "$i <br />";
		// $pathmap = Findpath ($mygrid, $start, 'a');
		// $pathmap = Findpath ($mygrid, $start, 'b');
		// $pathmap = Findpath ($mygrid, $start, 'c');
		// $pathmap = Findpath ($mygrid, $start, 'f');
	//}
	// $time = microtime();
   // $time = explode(" ", $time);
   // $time = $time[1] + $time[0];
   // $endtime = $time;
   // $totaltime = round($endtime - $begintime, 5);
   // echo "<br /><b>500 paths executed in: $totaltime s</b>";
	
	//Starting result: 
	//500 paths executed in: 5.24884 s 
	//On a $mygrid = GenerateMap(13, 8, 4, 3); board.
	//Now 1.
echo "Mapcode: $code";
?>
<div id="blocksdisplay"><b>b</b></div>

<div id="disp"><b></b></div>
<div id="disppath"><b></b></div>
<br />
<div id="dispdo"><b></b></div>
<br />
<div id="disptotalmoves"><b></b></div>
</body>
</html>