俄罗斯方块算法详解小游戏的ai算法有哪些

1151人阅读
小游戏开发(11)
A*这个经典的最短路径搜索算法--很多人都用过它快速计算迷宫出路,也有人用来进行2d游戏的路径搜索。总之在搜索路径时大都第首先想到它。
备注:我之前对A*算法的执行顺序上有认识上的误区,我是广度优先查找的,实际A*是类似深度优先方式查找的,在一定程度上,降低了遍历的次数,现在对本文进行进一步纠正.
简述A*最短路径算法的方法:
目标:从当前位置A到目标位置B找到一条最短的行走路径。
结构_路径点(MapPoint)
当前位置坐标,
到达当前位置的最小距离数 ,
到达目标的理论最小距离数 ,
理论到达最小值 =&到达当前位置的最小距离数 +&到达目标的理论最小距离数 ,
谁到达这个MapPoint使距离数最小
初始化过程:
建立两个集合:
1.所有开启的路径点集合(待选路径点)简称OpenL
2.所有关闭的路径点集合(已经走过的或者不应该再走到的路径点)简称CloseL
这两个集合不应该有重复的元素,初始化过程,只有OpenList中存在A点这个点,而CloseList为空
查找OpenList中"理论到达最小值"最小的点,把其从OpenList移动到CloseList中,并把其下一步可达的路径集合
添加到OpenList中,一直到找到B点为止.
查找所有一步可达路径的过程时,如果发现某点存在于OpenList或CloseList中时,判断其新到达值,如果小于原值,
则更新(更新其"到达此处的上一个MapPoint"和其"到达当前位置的最小距离数"),大于则放弃&
当找到B后,从它的上一个点开始一个一个找回去,就找到了整个路线.
总结:A*算法实际是个深度优先和策略结合的算法
课本上教的是广度优先,从出发点开始遍历,如果到达某点有两条路,则屏蔽掉长的那条,一直到找到最终节点。
进入正题:A*在俄罗斯方块游戏中的应用。
根据算法本身的特点,我们建立一个结构体MapPoint:
internal class MapPoint
XYP //坐标点
SetOfTetris theT
//方块样式
int nowC //到达当前位置的最小距离
int willC //到达终点的理论最小距离
int horizontalT //连续横向移动的次数
MapPoint theComeP //来到当前点的上一个点
public MapPoint(XYPair position, SetOfTetris theTetris, XYPair aimingPosition, SetOfTetris theAimTetris, int nowCost, int horizontalTimes, MapPoint theComePoint)
this.position =
this.theTetris = theT
this.nowCost = nowC
this.willCost = calculateWillCost(position, theTetris, aimingPosition, theAimTetris);
this.horizontalTimes = horizontalT
this.theComePoint = theComeP
private int calculateWillCost(XYPair nowPosition, SetOfTetris theTetris, XYPair aimingPosition, SetOfTetris theAimTetris)
int tempCost = 0;
//计算需要变形几次,一次cost 15;
if (theTetris.CanChangeTimes == 1 || theTetris == theAimTetris)
tempCost += 0;
else if (theTetris.CanChangeTimes == 2 || theTetris.LeftNext == theAimTetris || theAimTetris.LeftNext == theTetris)
tempCost += 15;
else if (theTetris.CanChangeTimes == 4 && theTetris.LeftNext.LeftNext == theAimTetris)
tempCost += 30;
return 999999;
//计算高度差,一个高度差cost 10;
if (nowPosition.Y & aimingPosition.Y) return 999999;
tempCost += (aimingPosition.Y - nowPosition.Y) * 10;
//计算横向差,一个横向差用其高度*5+10 如横向的长条,就是15,竖条就是30
tempCost += Math.Abs(nowPosition.X - aimingPosition.X) * (theAimTetris.Height * 5 + 10);
return tempC
结构体中有个变量horizontalTimes是记录连续横向移动的次数,如果横向次数太多,势必造成自动下降,这样就不能达到指定拐点位置了,
所以最好的办法是减少连续横向移动次数,因此这里记录它就是为了给出惩罚算法.
2.A*算法:内部哈希映射函数
此函数的目的,更快速的判断一个对象是否存在,计算的内容包含,其位置/方块样式/
比如(3,0)坐标上的一个横向的长条 会表示为getHashValueOfAPoint(坐标(3,0),横向长条)
private int getHashValueOfAPoint(XYPair position, SetOfTetris theTetris){&&& return (theTetris.GetHashCode() && 4 | position.X) && 5 | position.Y;}
3.A*算法:主体 第一部分,计算直到找到目标
&Dictionary&int, MapPoint& OpenPoint = new Dictionary&int, MapPoint&();Dictionary&int, MapPoint& ClosePoint = new Dictionary&int, MapPoint&();// Dictionary &int,int& allIndexs = new Dictionary&int,int&();//作为所有已经添加元素的索引,如果重复了则不能添加;value表达到达此的最小距离&&&&&&&&&&&& OpenPoint.Add(getHashValueOfAPoint(theDroppingPosition, theDroppingTetris),&new MapPoint(theDroppingPosition, theDroppingTetris, AimPosition, theAimTetris, 0, 0,null));
int theAimHash = getHashValueOfAPoint(AimPosition, theAimTetris);bool finish =#region toFindThePathwhile (!finish && OpenPoint.Count&0){&finish =&int theOperKey=0;&MapPoint theMostSmallestPoint =&//找到FullCost最小的点;&foreach (int theTempKey in OpenPoint.Keys)&{&&if (theMostSmallestPoint == null || theMostSmallestPoint.FullCost &= OpenPoint[theTempKey].FullCost)&&{&&&theOperKey = theTempK&&&theMostSmallestPoint = OpenPoint[theTempKey];&&}&}&if (theMostSmallestPoint == null || theOperKey==0)&OpenPoint.Remove(theOperKey);&ClosePoint.Add(theOperKey,theMostSmallestPoint);
&//判断其可能的下一步,并分别添加到OpenPoint中; &//下移一次10分;横向移动,按照越高,横向连续次数越多扣分越多,5*高度*横移次数 + 10 ;变形一次15&//横向这样处理是避免竖条一类的长时间横向移动,造成卡住的现象!&//下移&XYPair newPosition = theMostSmallestPoint.Position.Add(new XYPair(0, 1));&int theH&int theV&if (insidePositionIsPermit(nowSituation,newPosition, theMostSmallestPoint.TheTetris))&{&&//得到目标位置的哈希值&&theHash = getHashValueOfAPoint(newPosition, theMostSmallestPoint.TheTetris);&&//得到到达它这一步所花费的实际路径值&&theValue = theMostSmallestPoint.NowCost + 10;&&//如果在两个集合中没有找到这个点,才继续进行&&if (!OpenPoint.ContainsKey(theHash) && !ClosePoint.ContainsKey(theHash))&&{&&&finish =&&&//为open列表添加节点&&&OpenPoint.Add(theHash, new MapPoint(newPosition, theMostSmallestPoint.TheTetris, AimPosition, theAimTetris, theValue, 0, theMostSmallestPoint));&&}&&//当在Open集合中找到,则判断它是否有必要更新给Open列表&&else if (OpenPoint.ContainsKey(theHash) && OpenPoint[theHash].NowCost & theValue)&&{&&&OpenPoint[theHash].NowCost = theV&&&OpenPoint[theHash].TheComePoint = theMostSmallestP&&}&&//当在close集合中找到,则判断它是否有必要更新给Close列表&&else if (ClosePoint.ContainsKey(theHash) && ClosePoint[theHash].NowCost & theValue)&&{&&&ClosePoint[theHash].NowCost = theV&&&ClosePoint[theHash].TheComePoint = theMostSmallestP&&}&&//如果找到了目标节点,则退出&&if (theHash == theAimHash)&}&//右移...略&//左移...略&//左变形...略&//右变形 ...略
}#endregion&
4.A*算法:主体 从终点往回找各个路径,形成路线
&if (!OpenPoint.ContainsKey(theAimHash))//说明没有找到#region 找到路点//从路点开始往回找,一直其周边最小的,正常应该在close里面找,不再回到敞开式里的了MapPoint theAimPoint = OpenPoint[theAimHash];WayPoint endWay = new WayPoint(theAimPoint.Position, theAimPoint.TheTetris);theRoad.Insert(0, endWay);while (theAimPoint!= null && theAimPoint.TheComePoint!=null){&&&&&&&&&&&&&&& &//当时往上走的时候,不切换路点,否则根据变化切换&if (theAimPoint.TheTetris != theAimPoint.TheComePoint.TheTetris || theAimPoint.Position.X != theAimPoint.TheComePoint.Position.X)&{&&WayPoint oneTempWay = new WayPoint(theAimPoint.Position, theAimPoint.TheTetris);&&theRoad.Insert(0, oneTempWay);&}&theAimPoint = theAimPoint.TheComeP}#endregion
源代码思想上就是这样,但是实际代码可能会根据版本不同有一定调整,希望看到最新代码就下载下面的连接。
或者返回开场那个连接,那里总是最新的
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:61659次
排名:千里之外
原创:28篇
评论:26条
(1)(1)(4)(15)(7)(1)&>&&>&&>&&>&俄罗斯方块AI算法
俄罗斯方块AI算法
上传大小:493KB
AI算法,可以相互攻击,可以通过插件进行加载,然后测试两个算法
综合评分:2.7(3位用户评分)
所需积分:10
下载次数:25
审核通过送C币
创建者:blackwoodcliff
创建者:csnd_ayo
创建者:xamhaha
课程推荐相关知识库
上传者其他资源上传者专辑
开发技术热门标签
VIP会员动态
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
android服务器底层网络模块的设计方法
所需积分:0
剩余积分:720
您当前C币:0
可兑换下载积分:0
兑换下载分:
兑换失败,您当前C币不够,请先充值C币
消耗C币:0
你当前的下载分为234。
俄罗斯方块AI算法
会员到期时间:
剩余下载次数:
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:1153人阅读
小游戏开发(11)
A*这个经典的最短路径搜索算法--很多人都用过它快速计算迷宫出路,也有人用来进行2d游戏的路径搜索。总之在搜索路径时大都第首先想到它。
备注:我之前对A*算法的执行顺序上有认识上的误区,我是广度优先查找的,实际A*是类似深度优先方式查找的,在一定程度上,降低了遍历的次数,现在对本文进行进一步纠正.
简述A*最短路径算法的方法:
目标:从当前位置A到目标位置B找到一条最短的行走路径。
结构_路径点(MapPoint)
当前位置坐标,
到达当前位置的最小距离数 ,
到达目标的理论最小距离数 ,
理论到达最小值 =&到达当前位置的最小距离数 +&到达目标的理论最小距离数 ,
谁到达这个MapPoint使距离数最小
初始化过程:
建立两个集合:
1.所有开启的路径点集合(待选路径点)简称OpenL
2.所有关闭的路径点集合(已经走过的或者不应该再走到的路径点)简称CloseL
这两个集合不应该有重复的元素,初始化过程,只有OpenList中存在A点这个点,而CloseList为空
查找OpenList中"理论到达最小值"最小的点,把其从OpenList移动到CloseList中,并把其下一步可达的路径集合
添加到OpenList中,一直到找到B点为止.
查找所有一步可达路径的过程时,如果发现某点存在于OpenList或CloseList中时,判断其新到达值,如果小于原值,
则更新(更新其"到达此处的上一个MapPoint"和其"到达当前位置的最小距离数"),大于则放弃&
当找到B后,从它的上一个点开始一个一个找回去,就找到了整个路线.
总结:A*算法实际是个深度优先和策略结合的算法
课本上教的是广度优先,从出发点开始遍历,如果到达某点有两条路,则屏蔽掉长的那条,一直到找到最终节点。
进入正题:A*在俄罗斯方块游戏中的应用。
根据算法本身的特点,我们建立一个结构体MapPoint:
internal class MapPoint
XYP //坐标点
SetOfTetris theT
//方块样式
int nowC //到达当前位置的最小距离
int willC //到达终点的理论最小距离
int horizontalT //连续横向移动的次数
MapPoint theComeP //来到当前点的上一个点
public MapPoint(XYPair position, SetOfTetris theTetris, XYPair aimingPosition, SetOfTetris theAimTetris, int nowCost, int horizontalTimes, MapPoint theComePoint)
this.position =
this.theTetris = theT
this.nowCost = nowC
this.willCost = calculateWillCost(position, theTetris, aimingPosition, theAimTetris);
this.horizontalTimes = horizontalT
this.theComePoint = theComeP
private int calculateWillCost(XYPair nowPosition, SetOfTetris theTetris, XYPair aimingPosition, SetOfTetris theAimTetris)
int tempCost = 0;
//计算需要变形几次,一次cost 15;
if (theTetris.CanChangeTimes == 1 || theTetris == theAimTetris)
tempCost += 0;
else if (theTetris.CanChangeTimes == 2 || theTetris.LeftNext == theAimTetris || theAimTetris.LeftNext == theTetris)
tempCost += 15;
else if (theTetris.CanChangeTimes == 4 && theTetris.LeftNext.LeftNext == theAimTetris)
tempCost += 30;
return 999999;
//计算高度差,一个高度差cost 10;
if (nowPosition.Y & aimingPosition.Y) return 999999;
tempCost += (aimingPosition.Y - nowPosition.Y) * 10;
//计算横向差,一个横向差用其高度*5+10 如横向的长条,就是15,竖条就是30
tempCost += Math.Abs(nowPosition.X - aimingPosition.X) * (theAimTetris.Height * 5 + 10);
return tempC
结构体中有个变量horizontalTimes是记录连续横向移动的次数,如果横向次数太多,势必造成自动下降,这样就不能达到指定拐点位置了,
所以最好的办法是减少连续横向移动次数,因此这里记录它就是为了给出惩罚算法.
2.A*算法:内部哈希映射函数
此函数的目的,更快速的判断一个对象是否存在,计算的内容包含,其位置/方块样式/
比如(3,0)坐标上的一个横向的长条 会表示为getHashValueOfAPoint(坐标(3,0),横向长条)
private int getHashValueOfAPoint(XYPair position, SetOfTetris theTetris){&&& return (theTetris.GetHashCode() && 4 | position.X) && 5 | position.Y;}
3.A*算法:主体 第一部分,计算直到找到目标
&Dictionary&int, MapPoint& OpenPoint = new Dictionary&int, MapPoint&();Dictionary&int, MapPoint& ClosePoint = new Dictionary&int, MapPoint&();// Dictionary &int,int& allIndexs = new Dictionary&int,int&();//作为所有已经添加元素的索引,如果重复了则不能添加;value表达到达此的最小距离&&&&&&&&&&&& OpenPoint.Add(getHashValueOfAPoint(theDroppingPosition, theDroppingTetris),&new MapPoint(theDroppingPosition, theDroppingTetris, AimPosition, theAimTetris, 0, 0,null));
int theAimHash = getHashValueOfAPoint(AimPosition, theAimTetris);bool finish =#region toFindThePathwhile (!finish && OpenPoint.Count&0){&finish =&int theOperKey=0;&MapPoint theMostSmallestPoint =&//找到FullCost最小的点;&foreach (int theTempKey in OpenPoint.Keys)&{&&if (theMostSmallestPoint == null || theMostSmallestPoint.FullCost &= OpenPoint[theTempKey].FullCost)&&{&&&theOperKey = theTempK&&&theMostSmallestPoint = OpenPoint[theTempKey];&&}&}&if (theMostSmallestPoint == null || theOperKey==0)&OpenPoint.Remove(theOperKey);&ClosePoint.Add(theOperKey,theMostSmallestPoint);
&//判断其可能的下一步,并分别添加到OpenPoint中; &//下移一次10分;横向移动,按照越高,横向连续次数越多扣分越多,5*高度*横移次数 + 10 ;变形一次15&//横向这样处理是避免竖条一类的长时间横向移动,造成卡住的现象!&//下移&XYPair newPosition = theMostSmallestPoint.Position.Add(new XYPair(0, 1));&int theH&int theV&if (insidePositionIsPermit(nowSituation,newPosition, theMostSmallestPoint.TheTetris))&{&&//得到目标位置的哈希值&&theHash = getHashValueOfAPoint(newPosition, theMostSmallestPoint.TheTetris);&&//得到到达它这一步所花费的实际路径值&&theValue = theMostSmallestPoint.NowCost + 10;&&//如果在两个集合中没有找到这个点,才继续进行&&if (!OpenPoint.ContainsKey(theHash) && !ClosePoint.ContainsKey(theHash))&&{&&&finish =&&&//为open列表添加节点&&&OpenPoint.Add(theHash, new MapPoint(newPosition, theMostSmallestPoint.TheTetris, AimPosition, theAimTetris, theValue, 0, theMostSmallestPoint));&&}&&//当在Open集合中找到,则判断它是否有必要更新给Open列表&&else if (OpenPoint.ContainsKey(theHash) && OpenPoint[theHash].NowCost & theValue)&&{&&&OpenPoint[theHash].NowCost = theV&&&OpenPoint[theHash].TheComePoint = theMostSmallestP&&}&&//当在close集合中找到,则判断它是否有必要更新给Close列表&&else if (ClosePoint.ContainsKey(theHash) && ClosePoint[theHash].NowCost & theValue)&&{&&&ClosePoint[theHash].NowCost = theV&&&ClosePoint[theHash].TheComePoint = theMostSmallestP&&}&&//如果找到了目标节点,则退出&&if (theHash == theAimHash)&}&//右移...略&//左移...略&//左变形...略&//右变形 ...略
}#endregion&
4.A*算法:主体 从终点往回找各个路径,形成路线
&if (!OpenPoint.ContainsKey(theAimHash))//说明没有找到#region 找到路点//从路点开始往回找,一直其周边最小的,正常应该在close里面找,不再回到敞开式里的了MapPoint theAimPoint = OpenPoint[theAimHash];WayPoint endWay = new WayPoint(theAimPoint.Position, theAimPoint.TheTetris);theRoad.Insert(0, endWay);while (theAimPoint!= null && theAimPoint.TheComePoint!=null){&&&&&&&&&&&&&&& &//当时往上走的时候,不切换路点,否则根据变化切换&if (theAimPoint.TheTetris != theAimPoint.TheComePoint.TheTetris || theAimPoint.Position.X != theAimPoint.TheComePoint.Position.X)&{&&WayPoint oneTempWay = new WayPoint(theAimPoint.Position, theAimPoint.TheTetris);&&theRoad.Insert(0, oneTempWay);&}&theAimPoint = theAimPoint.TheComeP}#endregion
源代码思想上就是这样,但是实际代码可能会根据版本不同有一定调整,希望看到最新代码就下载下面的连接。
或者返回开场那个连接,那里总是最新的
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:61661次
排名:千里之外
原创:28篇
评论:26条
(1)(1)(4)(15)(7)(1)通过俄罗斯方块浅谈游戏中的AI(六)A*算法
通过俄罗斯方块浅谈游戏中的AI(六)A*算法
发布时间: 5:50:22
编辑:www.fx114.net
本篇文章主要介绍了"通过俄罗斯方块浅谈游戏中的AI(六)A*算法",主要涉及到通过俄罗斯方块浅谈游戏中的AI(六)A*算法方面的内容,对于通过俄罗斯方块浅谈游戏中的AI(六)A*算法感兴趣的同学可以参考一下。
A*这个经典的最短路径搜索算法--很多人都用过它快速计算迷宫出路,也有人用来进行2d游戏的路径搜索。总之在搜索路径时大都第首先想到它。
备注:我之前对A*算法的执行顺序上有认识上的误区,我是广度优先查找的,实际A*是类似深度优先方式查找的,在一定程度上,降低了遍历的次数,现在对本文进行进一步纠正.
简述A*最短路径算法的方法:
目标:从当前位置A到目标位置B找到一条最短的行走路径。
结构_路径点(MapPoint)
当前位置坐标,
到达当前位置的最小距离数 ,
到达目标的理论最小距离数 ,
理论到达最小值 =&到达当前位置的最小距离数 +&到达目标的理论最小距离数 ,
谁到达这个MapPoint使距离数最小
初始化过程:
建立两个集合:
1.所有开启的路径点集合(待选路径点)简称OpenL
2.所有关闭的路径点集合(已经走过的或者不应该再走到的路径点)简称CloseL
这两个集合不应该有重复的元素,初始化过程,只有OpenList中存在A点这个点,而CloseList为空
查找OpenList中"理论到达最小值"最小的点,把其从OpenList移动到CloseList中,并把其下一步可达的路径集合
添加到OpenList中,一直到找到B点为止.
查找所有一步可达路径的过程时,如果发现某点存在于OpenList或CloseList中时,判断其新到达值,如果小于原值,
则更新(更新其"到达此处的上一个MapPoint"和其"到达当前位置的最小距离数"),大于则放弃&
当找到B后,从它的上一个点开始一个一个找回去,就找到了整个路线.
总结:A*算法实际是个深度优先和策略结合的算法
课本上教的是广度优先,从出发点开始遍历,如果到达某点有两条路,则屏蔽掉长的那条,一直到找到最终节点。
进入正题:A*在俄罗斯方块游戏中的应用。
根据算法本身的特点,我们建立一个结构体MapPoint:
internal class MapPoint
XYP //坐标点
SetOfTetris theT
//方块样式
int nowC //到达当前位置的最小距离
int willC //到达终点的理论最小距离
int horizontalT //连续横向移动的次数
MapPoint theComeP //来到当前点的上一个点
public MapPoint(XYPair position, SetOfTetris theTetris, XYPair aimingPosition, SetOfTetris theAimTetris, int nowCost, int horizontalTimes, MapPoint theComePoint)
this.position =
this.theTetris = theT
this.nowCost = nowC
this.willCost = calculateWillCost(position, theTetris, aimingPosition, theAimTetris);
this.horizontalTimes = horizontalT
this.theComePoint = theComeP
private int calculateWillCost(XYPair nowPosition, SetOfTetris theTetris, XYPair aimingPosition, SetOfTetris theAimTetris)
int tempCost = 0;
//计算需要变形几次,一次cost 15;
if (theTetris.CanChangeTimes == 1 || theTetris == theAimTetris)
tempCost += 0;
else if (theTetris.CanChangeTimes == 2 || theTetris.LeftNext == theAimTetris || theAimTetris.LeftNext == theTetris)
tempCost += 15;
else if (theTetris.CanChangeTimes == 4 && theTetris.LeftNext.LeftNext == theAimTetris)
tempCost += 30;
return 999999;
//计算高度差,一个高度差cost 10;
if (nowPosition.Y & aimingPosition.Y) return 999999;
tempCost += (aimingPosition.Y - nowPosition.Y) * 10;
//计算横向差,一个横向差用其高度*5+10 如横向的长条,就是15,竖条就是30
tempCost += Math.Abs(nowPosition.X - aimingPosition.X) * (theAimTetris.Height * 5 + 10);
return tempC
结构体中有个变量horizontalTimes是记录连续横向移动的次数,如果横向次数太多,势必造成自动下降,这样就不能达到指定拐点位置了,
所以最好的办法是减少连续横向移动次数,因此这里记录它就是为了给出惩罚算法.
2.A*算法:内部哈希映射函数
此函数的目的,更快速的判断一个对象是否存在,计算的内容包含,其位置/方块样式/
比如(3,0)坐标上的一个横向的长条 会表示为getHashValueOfAPoint(坐标(3,0),横向长条)
private int getHashValueOfAPoint(XYPair position, SetOfTetris theTetris){&&& return (theTetris.GetHashCode() && 4 | position.X) && 5 | position.Y;}
3.A*算法:主体 第一部分,计算直到找到目标
&Dictionary&int, MapPoint& OpenPoint = new Dictionary&int, MapPoint&();Dictionary&int, MapPoint& ClosePoint = new Dictionary&int, MapPoint&();// Dictionary &int,int& allIndexs = new Dictionary&int,int&();//作为所有已经添加元素的索引,如果重复了则不能添加;value表达到达此的最小距离&&&&&&&&&&&& OpenPoint.Add(getHashValueOfAPoint(theDroppingPosition, theDroppingTetris),&new MapPoint(theDroppingPosition, theDroppingTetris, AimPosition, theAimTetris, 0, 0,null));
int theAimHash = getHashValueOfAPoint(AimPosition, theAimTetris);bool finish =#region toFindThePathwhile (!finish && OpenPoint.Count&0){&finish =&int theOperKey=0;&MapPoint theMostSmallestPoint =&//找到FullCost最小的点;&foreach (int theTempKey in OpenPoint.Keys)&{&&if (theMostSmallestPoint == null || theMostSmallestPoint.FullCost &= OpenPoint[theTempKey].FullCost)&&{&&&theOperKey = theTempK&&&theMostSmallestPoint = OpenPoint[theTempKey];&&}&}&if (theMostSmallestPoint == null || theOperKey==0)&OpenPoint.Remove(theOperKey);&ClosePoint.Add(theOperKey,theMostSmallestPoint);
&//判断其可能的下一步,并分别添加到OpenPoint中; &//下移一次10分;横向移动,按照越高,横向连续次数越多扣分越多,5*高度*横移次数 + 10 ;变形一次15&//横向这样处理是避免竖条一类的长时间横向移动,造成卡住的现象!&//下移&XYPair newPosition = theMostSmallestPoint.Position.Add(new XYPair(0, 1));&int theH&int theV&if (insidePositionIsPermit(nowSituation,newPosition, theMostSmallestPoint.TheTetris))&{&&//得到目标位置的哈希值&&theHash = getHashValueOfAPoint(newPosition, theMostSmallestPoint.TheTetris);&&//得到到达它这一步所花费的实际路径值&&theValue = theMostSmallestPoint.NowCost + 10;&&//如果在两个集合中没有找到这个点,才继续进行&&if (!OpenPoint.ContainsKey(theHash) && !ClosePoint.ContainsKey(theHash))&&{&&&finish =&&&//为open列表添加节点&&&OpenPoint.Add(theHash, new MapPoint(newPosition, theMostSmallestPoint.TheTetris, AimPosition, theAimTetris, theValue, 0, theMostSmallestPoint));&&}&&//当在Open集合中找到,则判断它是否有必要更新给Open列表&&else if (OpenPoint.ContainsKey(theHash) && OpenPoint[theHash].NowCost & theValue)&&{&&&OpenPoint[theHash].NowCost = theV&&&OpenPoint[theHash].TheComePoint = theMostSmallestP&&}&&//当在close集合中找到,则判断它是否有必要更新给Close列表&&else if (ClosePoint.ContainsKey(theHash) && ClosePoint[theHash].NowCost & theValue)&&{&&&ClosePoint[theHash].NowCost = theV&&&ClosePoint[theHash].TheComePoint = theMostSmallestP&&}&&//如果找到了目标节点,则退出&&if (theHash == theAimHash)&}&//右移...略&//左移...略&//左变形...略&//右变形 ...略
}#endregion&
4.A*算法:主体 从终点往回找各个路径,形成路线
&if (!OpenPoint.ContainsKey(theAimHash))//说明没有找到#region 找到路点//从路点开始往回找,一直其周边最小的,正常应该在close里面找,不再回到敞开式里的了MapPoint theAimPoint = OpenPoint[theAimHash];WayPoint endWay = new WayPoint(theAimPoint.Position, theAimPoint.TheTetris);theRoad.Insert(0, endWay);while (theAimPoint!= null && theAimPoint.TheComePoint!=null){&&&&&&&&&&&&&&& &//当时往上走的时候,不切换路点,否则根据变化切换&if (theAimPoint.TheTetris != theAimPoint.TheComePoint.TheTetris || theAimPoint.Position.X != theAimPoint.TheComePoint.Position.X)&{&&WayPoint oneTempWay = new WayPoint(theAimPoint.Position, theAimPoint.TheTetris);&&theRoad.Insert(0, oneTempWay);&}&theAimPoint = theAimPoint.TheComeP}#endregion
源代码思想上就是这样,但是实际代码可能会根据版本不同有一定调整,希望看到最新代码就下载下面的连接。
或者返回开场那个连接,那里总是最新的
版权声明:本文为博主原创文章,未经博主允许不得转载。
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。
本文标题:
本页链接:}

我要回帖

更多关于 俄罗斯方块下落算法 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信