博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
连连看.NET 1.41 发布(改造路径提示,提供算法源码)
阅读量:6495 次
发布时间:2019-06-24

本文共 11086 字,大约阅读时间需要 36 分钟。

除路径提示外,其他未做任何改变,可能播放音乐列表有问题,暂时不做修正了,大不了关一下再开吧;
关于修正路径提示:
本来写这个游戏是为了做个迷宫求解的算法,结果被做成这个了;一直路径绘制还是用的寻路的算法,今天改了改,改成实际的连线路径,因为有2位说过寻路路径太不顺眼;
关于源码:
我相信真正的开发人员是不屑一顾的,毕竟这些只是很基本的一些算法罢了,关于我先前写了两片笔记『 』『 』,如果肯动手的估计已经做好了,实在思路不清楚的可以阅读我的代码,我加了详细的注解,包括考虑绘图的时候图块的安排;
开发记录:
连连看.net到今天为止,我已经努力满足了大多数用过的朋友的要求,如果还有什么不满意的,只能说我尽力了;连连看的核心无非就是寻折点(这个不同于寻路),具体的实现可以看到关键的方法“CheckOneCorner”函数,它是寻找一个折点的目标,关于折点换算说出来是很简单的,还是看『 』一文,这个代码就是它的翻译体。如果有兴趣做做看,建议还是自己去想,自己做才能懂得更多;
以下是源码,可以判断两点是否在2折范围内,并记录路径点,2折的时候输出路径时取最短路径
None.gif
using System;
None.gif
using System.Drawing;
None.gif
using System.Collections;
None.gif
None.gif
namespace LLK.AI
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
/**/
///
<summary>
InBlock.gif
///
连连看的连线算法
InBlock.gif
///
作者:随飞
InBlock.gif
///
编写日期:2005-6-6
InBlock.gif
///
首先是 x to x ,这个是横向比较直连
InBlock.gif
///
然后是 y to y ,这个是竖向比较直连
InBlock.gif
///
InBlock.gif
///
然后是 1个折点
InBlock.gif
///
算法很简单;比如
InBlock.gif
///
7x3的
InBlock.gif
///
0001000
InBlock.gif
///
0000000
InBlock.gif
///
0000001
InBlock.gif
///
InBlock.gif
///
p1 是 3,0
InBlock.gif
///
p2 是 6,2
InBlock.gif
///
InBlock.gif
///
那么折点是
InBlock.gif
///
000100x
InBlock.gif
///
0000000
InBlock.gif
///
000x001
InBlock.gif
///
折点1是 6,0 ,折点2是 3,2
InBlock.gif
///
注意这个值和p1,p2的比较,是不是很简单,折点出来了,就可以比较x/y直线了,如果成立则通过.
InBlock.gif
///
InBlock.gif
///
2折就复杂一点,在上面都不成立后,
InBlock.gif
///
一次根据上下左右分别按1折算法再比较即可.
ExpandedSubBlockEnd.gif
///
</summary>
InBlock.gif
InBlock.gif
public
class TestPath
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
int pathPointCount = 0;
InBlock.gif ArrayList PathRecord =
new ArrayList();
InBlock.gif
int[,] map =
null;
//
地图
InBlock.gif
Point startPos;
//
起点
InBlock.gif
Point endPos;
//
终点
InBlock.gif
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
public TestPath()
dot.gif{;}
InBlock.gif
InBlock.gif
public TestPath(Point StartPos,Point EndPos,
int[,] Map)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.startPos = StartPos;
InBlock.gif
this.endPos = EndPos;
InBlock.gif
this.map = Map;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
InBlock.gif
private
void EmptyPathRecord()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif PathRecord.Clear();
InBlock.gif PathRecord.TrimToSize();
InBlock.gif PathRecord.Add(
this.startPos);
InBlock.gif PathRecord.Add(
this.endPos);
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool IsEmptyPoint(Point p)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.map[p.X,p.Y]==0;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckHorizontal(Point p1,Point p2)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.CheckHorizontal(p1,p2,
false);
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckHorizontal(Point p1,Point p2,
bool isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
检查水平,x位移,y相同
InBlock.gif
InBlock.gif Point sp = p1.X<p2.X?p1:p2;
InBlock.gif Point ep = p2.X<p1.X?p1:p2;
InBlock.gif
InBlock.gif
for(
int x =sp.X+1;x<ep.X;x++)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(
new Point(x,p1.Y));
ExpandedSubBlockEnd.gif }
InBlock.gif
//
不可通过
InBlock.gif
if(map[x,p1.Y]!=0)
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
true;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckVertical(Point p1,Point p2)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return CheckVertical(p1,p2,
false);
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckVertical(Point p1,Point p2,
bool isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
检查垂直,y位移,x相同
InBlock.gif
InBlock.gif Point sp = p1.Y<p2.Y?p1:p2;
InBlock.gif Point ep = p2.Y<p1.Y?p1:p2;
InBlock.gif
InBlock.gif
for(
int y =sp.Y+1; y<ep.Y; y++)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(
new Point(p1.X,y));
ExpandedSubBlockEnd.gif }
InBlock.gif
//
不可通过
InBlock.gif
if(map[p1.X,y]!=0)
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
true;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckOneCorner(Point p1,Point p2)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return CheckOneCorner(p1,p2,
true,
true);
ExpandedSubBlockEnd.gif }
InBlock.gif
private
bool CheckOneCorner(Point p1,Point p2,
bool isAddPath,
bool isClear)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
计算夹角
InBlock.gif
InBlock.gif
//
取起始x
InBlock.gif
Point CrossStart;
InBlock.gif Point CrossEnd;
InBlock.gif
InBlock.gif
if(p1.X<p2.X)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif CrossStart = p1;
InBlock.gif CrossEnd = p2;
ExpandedSubBlockEnd.gif }
InBlock.gif
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif CrossStart = p2;
InBlock.gif CrossEnd = p1;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
交叉点X
InBlock.gif
//
正向
InBlock.gif
Point CrossX ;
InBlock.gif
//
交叉点Y
InBlock.gif
//
反向
InBlock.gif
Point CrossY ;
InBlock.gif
InBlock.gif
if(CrossStart.Y<CrossEnd.Y)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif CrossX =
new Point(
InBlock.gif Math.Max(CrossStart.X,CrossEnd.X),
InBlock.gif Math.Min(CrossStart.Y,CrossEnd.Y)
InBlock.gif );
InBlock.gif CrossY =
new Point(
InBlock.gif Math.Min(CrossStart.X,CrossEnd.X),
InBlock.gif Math.Max(CrossStart.Y,CrossEnd.Y)
InBlock.gif );
ExpandedSubBlockEnd.gif }
InBlock.gif
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif CrossX =
new Point(
InBlock.gif Math.Min(CrossStart.X,CrossEnd.X),
InBlock.gif Math.Min(CrossStart.Y,CrossEnd.Y)
InBlock.gif );
InBlock.gif CrossY =
new Point(
InBlock.gif Math.Max(CrossStart.X,CrossEnd.X),
InBlock.gif Math.Max(CrossStart.Y,CrossEnd.Y)
InBlock.gif );
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
检查交叉点是否为可通过
InBlock.gif
if(!
this.IsEmptyPoint(CrossX) && !
this.IsEmptyPoint(CrossY))
InBlock.gif
return
false;
InBlock.gif
InBlock.gif
//
检查交叉点X
InBlock.gif
if(
this.IsEmptyPoint(CrossX))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
检查横位
InBlock.gif
if(CrossStart.Y<CrossEnd.Y)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
方向为~|
InBlock.gif
if(
this.CheckHorizontal(CrossStart,CrossX) &&
this.CheckVertical(CrossX,CrossEnd))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
允许清空
InBlock.gif
if(isClear)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.EmptyPathRecord();
ExpandedSubBlockEnd.gif }
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(CrossX);
//
记录当前交叉点
ExpandedSubBlockEnd.gif
}
InBlock.gif
return (
this.CheckHorizontal(CrossStart,CrossX,
true) &&
this.CheckVertical(CrossX,CrossEnd,
true));
ExpandedSubBlockEnd.gif }
InBlock.gif
//
不跳出,留下一点检测
ExpandedSubBlockEnd.gif
}
InBlock.gif
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
方向为|~
InBlock.gif
if(
this.CheckHorizontal(CrossX,CrossEnd) &&
this.CheckVertical(CrossX,CrossStart))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
允许清空
InBlock.gif
if(isClear)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.EmptyPathRecord();
ExpandedSubBlockEnd.gif }
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(CrossX);
//
记录当前交叉点
ExpandedSubBlockEnd.gif
}
InBlock.gif
return (
this.CheckHorizontal(CrossX,CrossEnd,
true) &&
this.CheckVertical(CrossX,CrossStart,
true));
ExpandedSubBlockEnd.gif }
InBlock.gif
//
不跳出,留下一点检测
ExpandedSubBlockEnd.gif
}
InBlock.gif
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
检查交叉点Y
InBlock.gif
if(
this.IsEmptyPoint(CrossY))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
检查竖位
InBlock.gif
if(CrossStart.Y<CrossEnd.Y)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
方向为|_
InBlock.gif
if(
this.CheckVertical(CrossStart,CrossY) &&
this.CheckHorizontal(CrossY,CrossEnd))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
允许清空
InBlock.gif
if(isClear)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.EmptyPathRecord();
ExpandedSubBlockEnd.gif }
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(CrossY);
//
记录当前交叉点
ExpandedSubBlockEnd.gif
}
InBlock.gif
return (
this.CheckVertical(CrossStart,CrossY,
true) &&
this.CheckHorizontal(CrossY,CrossEnd,
true));
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
方向_|
InBlock.gif
if(
this.CheckVertical(CrossEnd,CrossY) &&
this.CheckHorizontal(CrossY,CrossStart))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
允许清空
InBlock.gif
if(isClear)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.EmptyPathRecord();
ExpandedSubBlockEnd.gif }
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(CrossY);
//
记录当前交叉点
ExpandedSubBlockEnd.gif
}
InBlock.gif
return (
this.CheckVertical(CrossEnd,CrossY,
true) &&
this.CheckHorizontal(CrossY,CrossStart,
true));
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
false;
//
全部不成立,留给2次折点检测
InBlock.gif
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckTwoCornerLeft(Point p1,Point p2,
bool isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
InBlock.gif
//
清空记录
InBlock.gif
this.EmptyPathRecord();
InBlock.gif
for(
int x = p1.X -1; x>-1; x--)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
if(
this.map[x,p1.Y]!=0)
InBlock.gif
return
false;
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(
new Point(x,p1.Y));
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
不允许清空
InBlock.gif
if(
this.CheckOneCorner(
new Point(x,p1.Y),p2,
false,
false))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.CheckOneCorner(
new Point(x,p1.Y),p2,isAddPath,
false);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckTwoCornerRight(Point p1,Point p2,
bool isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
InBlock.gif
//
清空记录
InBlock.gif
this.EmptyPathRecord();
InBlock.gif
for(
int x = p1.X +1; x<
this.map.GetLength(0); x++)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
if(
this.map[x,p1.Y]!=0)
InBlock.gif
return
false;
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(
new Point(x,p1.Y));
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
不允许清空
InBlock.gif
if(
this.CheckOneCorner(
new Point(x,p1.Y),p2,
false,
false))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.CheckOneCorner(
new Point(x,p1.Y),p2,isAddPath,
false);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckTwoCornerUp(Point p1,Point p2,
bool isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
InBlock.gif
//
清空记录
InBlock.gif
this.EmptyPathRecord();
InBlock.gif
for(
int y = p1.Y -1; y>-1; y--)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
if(
this.map[p1.X,y]!=0)
InBlock.gif
return
false;
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(
new Point(p1.X,y));
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
不允许清空
InBlock.gif
if(
this.CheckOneCorner(
new Point(p1.X,y),p2,
false,
false))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.CheckOneCorner(
new Point(p1.X,y),p2,isAddPath,
false);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckTwoCornerDown(Point p1,Point p2,
bool isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
InBlock.gif
//
清空记录
InBlock.gif
this.EmptyPathRecord();
InBlock.gif
for(
int y = p1.Y +1; y<
this.map.GetLength(1); y++)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
if(
this.map[p1.X,y]!=0)
InBlock.gif
return
false;
InBlock.gif
//
允许记录
InBlock.gif
if(isAddPath)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Add(
new Point(p1.X,y));
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
不允许清空
InBlock.gif
if(
this.CheckOneCorner(
new Point(p1.X,y),p2,
false,
false))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.CheckOneCorner(
new Point(p1.X,y),p2,isAddPath,
false);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
void CopyArrayListTo(
ref ArrayList srcAl,
ref ArrayList destAl)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif destAl.Clear();
InBlock.gif destAl.TrimToSize();
InBlock.gif
InBlock.gif
foreach(Point p
in srcAl)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif Point newPnt =
new Point(p.X,p.Y);
InBlock.gif destAl.Add(newPnt);
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
private
bool CheckTwoCorner(Point p1,Point p2)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
//
取起始x
InBlock.gif
Point CrossStart;
InBlock.gif Point CrossEnd;
InBlock.gif
InBlock.gif
if(p1.X<p2.X)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif CrossStart = p1;
InBlock.gif CrossEnd = p2;
ExpandedSubBlockEnd.gif }
InBlock.gif
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif CrossStart = p2;
InBlock.gif CrossEnd = p1;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
这里为寻找最短路径的算法
InBlock.gif
//
方法很简单,因为当前的起点方向不确定,可以选择重复尝试能成功的起点
InBlock.gif
//
然后找到路径最少的
InBlock.gif
InBlock.gif
//
测试4次,寻找最短的路径
InBlock.gif
InBlock.gif ArrayList[] backFindPath =
new ArrayList[4];
InBlock.gif
bool isFind =
false;
InBlock.gif
int nextPathList=0;
InBlock.gif
while(nextPathList<4)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif backFindPath[nextPathList] =
null;
InBlock.gif
InBlock.gif
if(
this.CheckTwoCornerUp(CrossStart,CrossEnd,
false))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Clear();
InBlock.gif
this.CheckTwoCornerUp(CrossStart,CrossEnd,
true);
InBlock.gif backFindPath[nextPathList] =
new ArrayList();
InBlock.gif CopyArrayListTo(
ref
this.PathRecord,
ref backFindPath[nextPathList]);
InBlock.gif isFind =
true;
InBlock.gif nextPathList ++;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
if(
this.CheckTwoCornerDown(CrossStart,CrossEnd,
false))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Clear();
InBlock.gif
this.CheckTwoCornerDown(CrossStart,CrossEnd,
true);
InBlock.gif backFindPath[nextPathList] =
new ArrayList();
InBlock.gif CopyArrayListTo(
ref
this.PathRecord,
ref backFindPath[nextPathList]);
InBlock.gif isFind =
true;
InBlock.gif nextPathList ++;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
if(
this.CheckTwoCornerLeft(CrossStart,CrossEnd,
false))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Clear();
InBlock.gif
this.CheckTwoCornerLeft(CrossStart,CrossEnd,
true);
InBlock.gif backFindPath[nextPathList] =
new ArrayList();
InBlock.gif CopyArrayListTo(
ref
this.PathRecord,
ref backFindPath[nextPathList]);
InBlock.gif isFind =
true;
InBlock.gif nextPathList ++;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
if(
this.CheckTwoCornerRight(CrossStart,CrossEnd,
false))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.PathRecord.Clear();
InBlock.gif
this.CheckTwoCornerRight(CrossStart,CrossEnd,
true);
InBlock.gif backFindPath[nextPathList] =
new ArrayList();
InBlock.gif CopyArrayListTo(
ref
this.PathRecord,
ref backFindPath[nextPathList]);
InBlock.gif isFind =
true;
InBlock.gif nextPathList ++;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
没有结果
InBlock.gif
if(!isFind)
InBlock.gif
break;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
有结果返回
InBlock.gif
if(isFind)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
//
检查起点最小数
InBlock.gif
int minCount = 0;
InBlock.gif
//
记录位
InBlock.gif
int index = 0;
InBlock.gif
for(
int i=0;i<4;i++)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
if(backFindPath[i]!=
null)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
if(backFindPath[i].Count>0)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
if(minCount==0)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif minCount=backFindPath[i].Count;
InBlock.gif index = i;
ExpandedSubBlockEnd.gif }
InBlock.gif
else
if(backFindPath[i].Count<minCount)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif minCount = backFindPath[i].Count;
InBlock.gif index = i;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
回复属性数据
InBlock.gif
this.PathRecord.Clear();
InBlock.gif
this.PathRecord.TrimToSize();
InBlock.gif CopyArrayListTo(
ref backFindPath[index],
ref
this.PathRecord);
InBlock.gif
InBlock.gif
return
true;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
InBlock.gif
public
bool Execute()
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif pathPointCount = -1;
InBlock.gif
//
检查目标位置是否一样
InBlock.gif
if(
this.startPos.X==
this.endPos.X &&
this.startPos.Y==
this.endPos.Y)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif pathPointCount = -1;
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
检查为同一横坐标
InBlock.gif
if(
this.startPos.Y==
this.endPos.Y)
InBlock.gif
if(
this.CheckHorizontal(
this.startPos,
this.endPos))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.EmptyPathRecord();
InBlock.gif pathPointCount = 1;
InBlock.gif
return (
this.CheckHorizontal(
this.startPos,
this.endPos,
true));
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
检查为同一纵坐标
InBlock.gif
if(
this.startPos.X==
this.endPos.X)
InBlock.gif
if(
this.CheckVertical(
this.startPos,
this.endPos))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.EmptyPathRecord();
InBlock.gif pathPointCount = 1;
InBlock.gif
return (
this.CheckVertical(
this.startPos,
this.endPos,
true));
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
检查一个折点
InBlock.gif
if(
this.CheckOneCorner(
this.startPos,
this.endPos))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif pathPointCount =2;
InBlock.gif
return
true;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
//
检查两个折点
InBlock.gif
if(
this.CheckTwoCorner(
this.startPos,
this.endPos))
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif pathPointCount =3;
InBlock.gif
return
true;
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
return
false;
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
/**/
///
<summary>
InBlock.gif
///
起点坐标
ExpandedSubBlockEnd.gif
///
</summary>
InBlock.gif
public Point StartPosition
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.startPos;
ExpandedSubBlockEnd.gif }
InBlock.gif
set
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.startPos = value;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
/**/
///
<summary>
InBlock.gif
///
终点坐标
ExpandedSubBlockEnd.gif
///
</summary>
InBlock.gif
public Point EndPosition
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.endPos;
ExpandedSubBlockEnd.gif }
InBlock.gif
set
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.endPos = value;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
/**/
///
<summary>
InBlock.gif
///
地图数据
ExpandedSubBlockEnd.gif
///
</summary>
InBlock.gif
public
int[,] MapData
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.map;
ExpandedSubBlockEnd.gif }
InBlock.gif
set
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
this.map = value;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
/**/
///
<summary>
InBlock.gif
///
获取折点数
ExpandedSubBlockEnd.gif
///
</summary>
InBlock.gif
public
int PathPointCount
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.pathPointCount;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
/**/
///
<summary>
InBlock.gif
///
获取路径表
ExpandedSubBlockEnd.gif
///
</summary>
InBlock.gif
public Point[] PathPointList
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
if(
this.PathRecord.Count>=0)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif Point[] tmpPointList =
new Point[
this.PathRecord.Count];
InBlock.gif
int i = 0;
InBlock.gif
foreach(Point p
in
this.PathRecord)
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif tmpPointList[i] = p;
InBlock.gif i++;
ExpandedSubBlockEnd.gif }
InBlock.gif
return tmpPointList;
ExpandedSubBlockEnd.gif }
InBlock.gif
else
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
null;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
InBlock.gif
public ArrayList PathPointArray
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
get
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{
InBlock.gif
return
this.PathRecord;
ExpandedSubBlockEnd.gif }
ExpandedSubBlockEnd.gif }
InBlock.gif
ExpandedSubBlockEnd.gif }
ExpandedBlockEnd.gif}
None.gif
调用测试,这个测试代码可能版本不符,根据以上类的方法和说明修改测试:
None.gif
int[,] map =
new
int[14,10]
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,0,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,2,0,3,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,1,1,1,1,1,1,1,1,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,1,1,1,1,1,1,1,1,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,1,0,0,0,0,0},
ExpandedSubBlockStart.gif
ContractedSubBlock.gif
dot.gif{0,0,0,0,0,0,0,0,0,0}
ExpandedBlockEnd.gif };
None.gif AI_Engine.TestPathing TP =
new AI_Engine.TestPathing();
None.gif TP.StartPosition =
new Point((
int)numericUpDown1.Value,(
int)numericUpDown2.Value);
None.gif TP.EndPosition =
new Point((
int)numericUpDown3.Value,(
int)numericUpDown4.Value);
None.gif TP.MapData =
this.map;
None.gif
None.gif
if(TP.Execute())
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
InBlock.gif lbInfo.Text = "连通!";
InBlock.gif AddPathInfo(TP.PathPointList);
//
绘制路径
ExpandedBlockEnd.gif
}
None.gif
else
ExpandedBlockStart.gif
ContractedBlock.gif
dot.gif {
InBlock.gif lbInfo.Text = "不通!";
ExpandedBlockEnd.gif }
本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2005/06/06/169153.html,如需转载请自行联系原作者
你可能感兴趣的文章
spin_lock、spin_lock_irq、spin_lock_irqsave区别【转】
查看>>
删除 mac 垃圾桶内清除不掉的文件
查看>>
【响应式编程的思维艺术】 (5)Angular中Rxjs的应用示例
查看>>
/bin/bash^M: bad interpreter: No such file or dire
查看>>
python xml rpc
查看>>
Java设置以及获取JavaBean私有属性进阶
查看>>
db2表结构导出导入,数据库备份
查看>>
策略模式
查看>>
第二 周作业总结
查看>>
OrderOnline——项目概述
查看>>
POJ-2739(Water)
查看>>
【转】第三节 UNIX文件系统结构
查看>>
为什么sql里面not in后面的子查询如果有记录为NULL的,主查询就查不到记录
查看>>
Angular7里面实现 debounce search
查看>>
Linux 内核链表
查看>>
git学习------>Git 分支管理最佳实践
查看>>
括号和出栈所有序列问题
查看>>
第一次操刀数据库分表的教训与经验
查看>>
录音声音小
查看>>
Ubuntu 12.04 安装 Chrome浏览器
查看>>