php A star(A *)寻路算法代码

代码语言:php

所属分类:算法

代码描述:php A star(A *)寻路算法代码,从A到B,绿色背景表示最短路径,黑色背景表示障碍,刷新会重新生成路障。

代码标签: ( A * ) 寻路 算法

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<?php

$config = array(
    'start' => array(1, 2), // 开始坐标
    'end' => array(9, 10), // 结束坐标
    'x' => 10, // 最大x
    'y' => 10, // 最大y
    'disable_num' => 30, // 障碍点个数
);
 
$a = new aStar($config['start'], $config['end'], $config['x'], $config['y'], $config['disable_num']);
$a->displayPic();
 
 
/**
 * astar寻路算法
 */
class aStar
{
 
    private $_start; // 开始点
    private $_end; // 结束点
    private $_x; // 最大x轴
    private $_y; // 最大y轴
    private $_num; // 障碍点数量
 
    private $_around; // 当前节点的可能四周节点数组
    private $_g; // g值数组
 
    public $open; // 开放节点数组
    public $close; // 关闭节点数组
    public $disable = array(); // 随机生成的障碍点数组
 
    public $route = array(); // 结果路径数组
 
    /**
     * @param $start array 开始点
     * @param $end array 结束点
     * @param $x int 最大x轴
     * @param $y int 最大y轴
     * @param $num int 最大随机障碍点数量
     */
    public function __construct($start, $end, $x, $y, $num)
    {
        $this->_start = $start;
        $this->_end = $end;
        $this->_x = $x;
        $this->_y = $y;
        $this->_num = $num;
 
        // 开始寻路
        $this->_route();
    }
 
    private function _route()
    {
        // 生成随机路障点
        $this->_makeDisable();
        // 算法开始
        $this->_start();
    }
 
    private function _start()
    {
        // 设置初始点的各项值
        $point[0] = $this->_start[0]; // x
        $point[1] = $this->_start[1]; // y
    .........完整代码请登录后点击上方下载按钮下载查看

网友评论0