练练看游戏JavaScript搜索路径的核心算法实现

 // 搜寻路径
 searchTrack : function(piece) {
  
  if (this.isNear(piece)) {
    
   this.linkTrack(piece);
   
   return;
  }  
  
  if (this.isReach(piece) || this.isReach2(piece)) {
    
   this.linkTrack(piece);
   
   return;
  }  
    
 },

 
 // 是否相邻
 isNear : function(piece) {
 
  var a = (Math.abs(piece.x - this.x) == 1) && (piece.y == this.y)
   || (Math.abs(piece.y - this.y) == 1) && (piece.x == this.x);
 
  return a;
 },
 
 // 直线
 isStraightReach : function(piece) {
  //alert("isStraightReach");
  if (this.isNear(piece)) {
   
   return true;
  
  }
  
  var a = false;
  var b = false;

  // 沿y轴方向搜索
  if (this.x == piece.x) {
   //alert("!!!!!!!!!!!");
   for (var i = this.min(this.y, piece.y) + 1; i < this.max(this.y, piece.y); i ++) {
    //alert("this.x == piece.x: " + piece.x + "," + i);
    if (this.game.pieceMap.get(piece.x + "," + i).isPass()) {
    
     a = true;
     
     this.game.trackList.push(this.game.pieceMap.get(piece.x + "," + i));
     
     continue;
    } else {
    
     a = false;
     this.game.trackList = [];
     
     return a; 
    }
   
   }
      
  }
  
  // 沿x轴方向搜索
  if (this.y == piece.y) {
   //alert("!!!!!!!!!!!");
   for (var i = this.min(this.x, piece.x) + 1; i < this.max(this.x, piece.x); i ++) {
    //alert("this.y == piece.y: " + i + "," + piece.y);
    if (this.game.pieceMap.get(i + "," + piece.y).isPass()) {
     
     b = true;
     this.game.trackList.push(this.game.pieceMap.get(i + "," + piece.y));
     
     continue;
    } else {
    
     b = false
     this.game.trackList = [];
     
     return b;
    }
   
   }
   
  }

  return a || b;
 },

 
 // 拐一次弯搜索
 isReach1 : function(piece) {
  //alert("isReach1");
  var corner_1 = this.game.pieceMap.get(this.x + "," + piece.y);
  var corner_2 = this.game.pieceMap.get(piece.x + "," + this.y);
      
  var _this = this;

  
  if ((_this.isStraightReach(corner_1))
   && (corner_1.isStraightReach(piece))
   && corner_1.isPass()) {
   
    //alert("corner_1: " + this.x + "," + piece.y);
    this.game.trackList.push(corner_1);
   
    return true;
  }
 
  if ((_this.isStraightReach(corner_2))
   && (corner_2.isStraightReach(piece))
   && corner_2.isPass()) {
    //alert("corner_2: " + piece.x + "," + this.y);
    this.game.trackList.push(corner_2);
   
   return true;
  }

  return false;
 },
 
 // 直接或拐一次弯搜索
 isReach : function(piece) {
  
  var a = this.isStraightReach(piece);
  
  var b = this.isReach1(piece);
    
  return a || b;
 },
 
 // 拐两次弯搜索
 isReach2 : function(piece) {
   
  // 沿x轴正向搜索
  for (var i = this.x + 1; i < 17; i ++) {
   
   if (!this.game.pieceMap.get(i + "," + this.y).isPass()) {
    
    this.game.trackList = [];
    
    break;
    
   } else if (this.game.pieceMap.get(i + "," + this.y).isReach(piece)
    && this.game.pieceMap.get(i + "," + this.y).isPass()) {
    
    this.game.trackList.push(this.game.pieceMap.get(i + "," + this.y));
     
    return true;
   } 
  
  }
  
  // 沿x轴搜索
  for (var i = this.x - 1; i >= 0; i --) {
   
   if (!this.game.pieceMap.get(i + "," + this.y).isPass()) {
   
    this.game.trackList = [];
    
    break;
    
   } else if (this.game.pieceMap.get(i + "," + this.y).isReach(piece)
    && this.game.pieceMap.get(i + "," + this.y).isPass()) {
   
    this.game.trackList.push(this.game.pieceMap.get(i + "," + this.y));
     
    return true;
   }
  
  }
  
  // 沿y轴搜索
  for (var i = this.y - 1; i >= 0; i --) {
   
   if (!this.game.pieceMap.get(this.x + "," + i).isPass()) {
    
    this.game.trackList = [];
    
    break;
    
   } else if (this.game.pieceMap.get(this.x + "," + i).isReach(piece)
    && this.game.pieceMap.get(this.x + "," + i).isPass()) {
    
    this.game.trackList.push(this.game.pieceMap.get(this.x + "," + i));
     
    return true;
   }
  
  }

  // 沿y轴正向搜索
  for (var i = this.y + 1; i < 12; i ++) {

   if (!this.game.pieceMap.get(this.x + "," + i).isPass()) {
    
    this.game.trackList = [];
    
    break;
   } else if (this.game.pieceMap.get(this.x + "," + i).isReach(piece)
    && this.game.pieceMap.get(this.x + "," + i).isPass()) {
    
    this.game.trackList.push(this.game.pieceMap.get(this.x + "," + i));
     
    return true;
   }
  
  }
  
  return false;
 },

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值