练练看游戏JavaScript搜索路径的核心算法实现
来源:互联网 发布:电子报刊网站程序源码 编辑:程序博客网 时间:2024/05/07 00:39
// 搜寻路径
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;
},
- 练练看游戏JavaScript搜索路径的核心算法实现
- 连连看游戏的核心算法
- 广度优先搜索算法的典型应用——消灭小星星游戏的核心代码实现与解析
- 基于bfs搜索算法的迷宫最短路径游戏
- 三消类游戏的核心算法
- 消除游戏的核心算法
- 广度优先搜索迷宫路径算法的实现
- 深度优先搜索迷宫路径算法的实现
- 基于Dragonboard 410c的 A路径搜索算法实现
- 原生JavaScript实现连连看游戏
- 连连看游戏核心代码(C++实现)
- 连连看(C#版)——搜索路径算法
- 连连看游戏的实现
- 用 JAVA 开发游戏连连看(之二)实现游戏的算法
- 连连看核心算法
- HDU 1175 练练看
- 棋牌源码游戏五子棋的核心算法
- 图的搜索算法javascript
- QLineEdit光标问题
- LeetCode - Merge Intervals
- Linux常用命令大全
- du: 查询文件或文件夹的磁盘使用空间
- 在solr4.3.1中配置IK分词器
- 练练看游戏JavaScript搜索路径的核心算法实现
- Android利用XmlPullParse对XML进行解析读取(一)
- openrisc toolchain 工具链从源代码编译、安装
- ios学习(二):视图控制器对象的生命周期
- 解决“无法定位程序输入点 _invalid_parameter_noinfo_noreturn于动态链接库MSVCR100.DLL”
- 为什么要使用DNS智能解析
- Linux网络编程:TCP服务器(单进程多用户),使用select方法实现
- VC2012 生成的执行档在 server2003 执行报错
- UVA 11584 Partitioning by Palindromes (回文DP,4级)