老师,你说你AStar算法寻找8个点坐标不允许斜着走,但是运行结果是斜着走的
如果不允许斜着走应该是这样写的吧:
/// <summary>
/// 找寻附近点
/// </summary>
private List<Point> GetSurroundPoints(Point point)
{
List<Point> surroundPoints = new List<Point>();
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if ((i == -1 && j == 0) || (i == 0 && j == -1) || (i == 0 && j == 1) || (i == 1 && j == 0))
{
if (i != 0 || j != 0)
{
int x = point.X + i;
int y = point.Y + j;
if (x >= 0 && x < map.GetLength(0) && y >= 0 && y < map.GetLength(1))
{
if (map[x, y] != point && map[x, y].IsWall == false)
{
surroundPoints.Add(map[x, y]);
}
}
}
}
}
}
return surroundPoints;
}
运行结果是这样的
如果允许斜着走代码应该是这样:
/// <summary>
/// 找寻附近点
/// </summary>
private List<Point> GetSurroundPoints(Point point)
{
List<Point> surroundPoints = new List<Point>();
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
//if ((i == -1 && j == 0) || (i == 0 && j == -1) || (i == 0 && j == 1) || (i == 1 && j == 0))
//不计算自身
if (i != 0 || j != 0)
{
int x = point.X + i;
int y = point.Y + j;
if (x >= 0 && x < map.GetLength(0) && y >= 0 && y < map.GetLength(1))
{
if (map[x, y] != point && map[x, y].IsWall == false)
{
surroundPoints.Add(map[x, y]);
}
}
}
}
}
return surroundPoints;
}
运行结果是这样:
老师,你好
Siki老师的代码就是这个
private List<Point> GetSurroundPoints(Point point)
{
Point up = null, down = null, left = null, right = null;
Point lu = null, ru = null, ld = null, rd = null;
if (point.Y < mapHeight- 1)
{
up = map[point.X, point.Y + 1];
}
if (point.Y > 0)
{
down = map[point.X, point.Y - 1];
}
if (point.X > 0)
{
left = map[point.X - 1, point.Y];
}
if(point.X <mapWith-1)
{
right = map[point.X + 1, point.Y];
}
if (up != null && left != null)
{
lu = map[point.X - 1, point.Y + 1];
}
if (up != null && right != null)
{
ru = map[point.X + 1, point.Y + 1];
}
if (down != null && left != null)
{
ld = map[point.X - 1, point.Y - 1];
}
if (down != null && right != null)
{
rd = map[point.X + 1, point.Y - 1];
}
List<Point> list = new List<Point>();
if (down != null && down.IsWall == false)
{
list.Add(down);
}
if (up != null && up.IsWall == false)
{
list.Add(up);
}
if (left != null && left.IsWall == false)
{
list.Add(left);
}
if (right != null && right.IsWall == false)
{
list.Add(right);
}
if (lu != null && lu.IsWall == false && left.IsWall == false && up.IsWall == false)
{
list.Add(lu);
}
if (ld != null && ld.IsWall == false && left.IsWall == false && down.IsWall == false)
{
list.Add(ld);
}
if (ru != null && ru.IsWall == false && right.IsWall == false && up.IsWall == false)
{
list.Add(ru);
}
if (rd != null && rd.IsWall == false && right.IsWall == false && down.IsWall == false)
{
list.Add(rd);
}
return list;
}
我理解的就是,如果上下左右有障碍物那么不允许斜着走,但是没有障碍物就可以斜着走! 我觉得这样的思路和Astar的设计不合理