PHP樹的深度編歷生成迷宮及A*自動尋路算法實例分析(2)_PHP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:PHP實現(xiàn)扎金花游戲之大小比賽的方法這篇文章主要介紹了PHP實現(xiàn)扎金花游戲之大小比賽的方法,實例分析了扎金花游戲的實現(xiàn)原理與相關(guān)算法技巧,具有一定參考借鑒價值,需要的朋友可以參考下 本文實例講述了PHP實現(xiàn)扎金花游戲之大小比賽的方法。分享給大家供大家參考。具體分析如下: 程序離不開算法,前面討論
A*尋路算法
代碼如下: class AStar{// A-star
private $_open;
private $_closed;
private $_start;
private $_end;
private $_grids;
private $_w;
private $_h;
// Construct
public function AStar(){
$this->_w = null;
$this->_h = null;
$this->_grids = null;
}
public function set($width, $height, $grids) {
$this->_w = $width;
$this->_h = $height;
$this->_grids = $grids;
return $this;
}
// 迷宮中尋路
public function search($start = false, $end = false) {
return $this->_search($start, $end);
}
/**
|---------------------------------------------------------------
| 自動尋路 - A-star 算法
|---------------------------------------------------------------
*/
public function _search($start = false, $end = false) {
if ( $start !== false ) $this->_start = $start;
if ( $end !== false ) $this->_end = $end;
$_sh = $this->_getH($start);
$point['i'] = $start;
$point['f'] = $_sh;
$point['g'] = 0;
$point['h'] = $_sh;
$point['p'] = null;
$this->_open[] = $point;
$this->_closed[$start] = $point;
while ( 0 < count($this->_open) ) {
$minf = false;
foreach( $this->_open as $key => $maxNode ) {
if ( $minf === false || $minf > $maxNode['f'] ) {
$minIndex = $key;
}
}
$nowNode = $this->_open[$minIndex];
unset($this->_open[$minIndex]);
if ( $nowNode['i'] == $this->_end ) {
$tp = array();
while( $nowNode['p'] !== null ) {
array_unshift($tp, $nowNode['p']);
$nowNode = $this->_closed[$nowNode['p']];
}
array_push($tp, $this->_end);
break;
}
$this->_setPoint($nowNode['i']);
}
$this->_closed = array();
$this->_open = array();
return $tp;
}
private function _setPoint($me) {
$point = $this->_grids[$me];
// 所有可選方向入隊列
if ( $point & 1 ) {
$next = $me - $this->_w;
$this->_checkPoint($me, $next);
}
if ( $point & 2 ) {
$next = $me + 1;
$this->_checkPoint($me, $next);
}
if ( $point & 4 ) {
$next = $me + $this->_w;
$this->_checkPoint($me, $next);
}
if ( $point & 8 ) {
$next = $me - 1;
$this->_checkPoint($me, $next);
}
}
private function _checkPoint($pNode, $next) {
if ( $this->_closed[$next] ) {
$_g = $this->_closed[$pNode]['g'] + $this->_getG($next);
if ( $_g < $check['g'] ) {
$this->_closed[$next]['g'] = $_g;
$this->_closed[$next]['f'] = $this->_closed[$next]['g'] + $this->_closed[$next]['h'];
$this->_closed[$next]['p'] = $pNode;
}
} else {
$point['p'] = $pNode;
$point['h'] = $this->_getH($next);
$point['g'] = $this->_getG($next);
$point['f'] = $point['h'] + $point['g'];
$point['i'] = $next;
$this->_open[] = $point;
$this->_closed[$next] = $point;
}
}
private function _getG($point) {
return abs($this->_start - $point);
}
private function _getH($point) {
return abs($this->_end - $point);
}
}
完整實例代碼點擊此處本站下載。
有需要大家可以直接下demo,看看效果!
希望本文所述對大家的php程序設計有所幫助。
分享:php獲取本周開始日期和結(jié)束日期的方法這篇文章主要介紹了php獲取本周開始日期和結(jié)束日期的方法,實例分析了php操作日期的技巧,具有一定參考借鑒價值,需要的朋友可以參考下 本文實例講述了php獲取本周開始日期和結(jié)束日期的方法。分享給大家供大家參考。具體如下: 代碼如下://當前日期 $sdefaultDate = date(
相關(guān)PHP教程:
- PHP實現(xiàn)扎金花游戲之大小比賽的方法
- php獲取本周開始日期和結(jié)束日期的方法
- php數(shù)組轉(zhuǎn)成json格式的方法
- php實現(xiàn)將數(shù)組轉(zhuǎn)換為XML的方法
- php返回字符串中所有單詞的方法
- php通過正則表達式記取數(shù)據(jù)來讀取xml的方法
- PHP實現(xiàn)算式驗證碼和漢字驗證碼實例
- PHP實現(xiàn)指定字段的多維數(shù)組排序函數(shù)分享
- PHP多線程之內(nèi)部多線程實例分析
- php建立Ftp連接的方法
- Thinkphp調(diào)用Image類生成縮略圖的方法
- PHP實現(xiàn)懶加載的方法
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP樹的深度編歷生成迷宮及A*自動尋路算法實例分析(2)
。