-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.php
74 lines (69 loc) · 2.12 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
<?php
error_reporting(E_ALL^E_NOTICE);
require 'Maze.php';
require 'AStar.php';
$maze = new Maze();
$aStar = new AStar();
$w = $_POST['w'] ? $_POST['w'] : 6; // 迷宫宽
$h = $_POST['h'] ? $_POST['h'] : 6; // 迷宫高
$maze->set($w, $h);
$maze->create();
$grids = $maze->get();
// 寻路
$aStar->set($w, $h, $grids);
$path = $aStar->search(0, $w * $h - 1); // 从零开始找到35
// 画迷宫方法
function div($x, $y, $v){
global $w, $h, $path;
//if ( $y > 4 ) $v = 0;
$k = $y * $w + $x;
//if ( array_key_exists($k, $this->_enable) ) {
//echo "<div class=\"grid cell_enable_{$this->_enable[$k]}\" style=\"";
//} else {
echo "<div class=\"grid cell_{$v}\" style=\"";
//}
echo 'top:'. 34*$y.'px;';
echo 'left:'. 34*$x.'px;';
//if ($v & 1)
//echo "border-top:1px solid #F5F5F5;";
//if ($v & 2)
//echo "border-right:1px solid #F5F5F5;";
//if ($v & 4)
//echo "border-bottom:solid 1px #F5F5F5;";
//if ($v & 8)
//echo "border-left:solid 1px #F5F5F5;";
if ( in_array($y * $w + $x, $path) )
echo "color:red;";
echo '">';
echo $y * $w + $x;
echo '</div>'."\n";
}
?>
<!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" lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<title>php版本的迷宫生成算法及A*寻路算法</title>
<link href="css/mg.css" rel="stylesheet" type="text/css">
</head>
<body>
<div style="width:612px;height:40px;margin:20px auto;" class="mg">
<form action="" method="post">
<input type="text" name="w" size="6" value="<?php echo $w?>" /> *
<input type="text" name="h" size="6" value="<?php echo $h?>" />
<input type="submit" name="submit" value="生成" />
</form>
</div>
<div style="width: 612px; height: 212px;margin:20px auto;" class="mg">
<?php
for($y = 0; $y < $h; $y++ ){
for($x=0; $x < $w; $x++ ){
$v = $grids[$y * $w + $x];
div($x, $y, $v);
}
}
?>
</div>
</body>
</html>