PHP樹的深度編歷生成迷宮及A*自動(dòng)尋路算法實(shí)例分析(2)_PHP教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法
      這篇文章主要介紹了PHP實(shí)現(xiàn)扎金花游戲之大小比賽的方法,實(shí)例分析了扎金花游戲的實(shí)現(xiàn)原理與相關(guān)算法技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 本文實(shí)例講述了PHP實(shí)現(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);
      }
      /**
      |---------------------------------------------------------------
      | 自動(dòng)尋路 - 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];
      // 所有可選方向入隊(duì)列
      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);
      }
      }

       

      完整實(shí)例代碼點(diǎn)擊此處本站下載。

      有需要大家可以直接下demo,看看效果!

      希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

      分享:php獲取本周開始日期和結(jié)束日期的方法
      這篇文章主要介紹了php獲取本周開始日期和結(jié)束日期的方法,實(shí)例分析了php操作日期的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 本文實(shí)例講述了php獲取本周開始日期和結(jié)束日期的方法。分享給大家供大家參考。具體如下: 代碼如下://當(dāng)前日期 $sdefaultDate = date(

      共2頁(yè)上一頁(yè)12下一頁(yè)
      來源:模板無憂//所屬分類:PHP教程/更新時(shí)間:2015-03-11
      相關(guān)PHP教程