Я делаю алгоритм поиска пути на карте с координатами для проекта с реками. Я посмотрел, как работает *, и начал понимать это, поэтому я сделал это в своем собственном коде. Проблема в том, что это не работает вообще. Первооткрыватель только делает первые два квадрата реки, а затем просто сдается. Я не знаю вопроса.
Вот мой код:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class APath
{
public bool[,] open;
private bool[,] closed;
private float width;
private float height;
public int currentX, currentY;
public void FindPath(int startX, int startY, int endX, int endY)
{
open = new bool[75,75];// stores path
open[startX, startY] = true;
bool[,] opens = open;
closed = new bool[75,75] ;
currentY = startY;
currentX = startX;
bool end = false;
for(int l = 0; l< 1000; l++) //while loop went on forever. Had to use for loop
{
float[][] adjacent = Adjacent(currentX,currentY,endX,endY,startX,startY);//gets adjacent squares
float min = adjacent[0][2];//creates temp min
int per = 0;// storage of refrence for later
for(int i = 0; i < 8; i++)// checks adjacent squares
{
if (adjacent[i][2] < min)//if there is a new min
{
min = adjacent[i][2];// new min
per = i; // for later use
}
}
if(currentX == endX && currentY == endY)// makes sure that adjacent isn't at end place
{
open[endX, endY] = true;
}
currentX = Mathf.RoundToInt((int)adjacent[(int)per][0]);//new current
currentY = Mathf.RoundToInt(adjacent[(int)per][1]);
opens[currentX, currentY] = true;// tracks data
closed[currentX, currentY] = false;//tracks data
}
open = opens;
}
float[][] Adjacent (int currentX, int currentY, float endX, float endY, float startX, float startY)
{
//creating adjacent squares areound current square
float[][] adjacent = new float[4][];
adjacent[0] = new float[3] { currentX + 1, currentY +0, HandG( currentX + 1, currentY,endX, endY, startX, startY) };
adjacent[1] = new float[3] { currentX + 1, currentY + -1, HandG(currentX + 1, currentY + -1, endX, endY, startX, startY) };
adjacent[2] = new float[3] { currentX + 0, currentY + -1, HandG(currentX + 0, currentY + -1, endX, endY, startX, startY) };
adjacent[3] = new float[3] { currentX + -1, currentY +- 01, HandG(currentX + -1, currentY +-1, endX, endY, startX, startY) };
adjacent[4] = new float[3] { currentX + -1, currentY + 0, HandG(currentX + -1, currentY, endX, endY, startX, startY) };
adjacent[5] = new float[3] { currentX + -1, currentY + 01, HandG(currentX + -1, currentY + 1, endX, endY, startX, startY) };
adjacent[6] = new float[3] { currentX, currentY + 1, HandG(currentX , currentY + 1, endX, endY, startX, startY) };
adjacent[7] = new float[3] { currentX + 1, currentY + 1, HandG(currentX + 1, currentY + 1, endX, endY, startX, startY) };
return adjacent;
}
float HandG (float currentX, float currentY,float endX, float endY, float startX, float startY )
{
//creates g of a*
float g = Mathf.Sqrt(Mathf.Pow(currentX + -startX, 2) + Mathf.Pow(currentY + -startY, 2));
//creates h of a*
float h = Mathf.Sqrt(Mathf.Pow(currentX + -endX, 2) + Mathf.Pow(currentY + -endY, 2));
//creates f of a*
float f =g + h;
return f;
}
}
Класс реки, использующий это
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rivers : MonoBehaviour
{
// Start is called before the first frame update
public Color[] MakeRiver (Color[] color, int mapHeight, int mapWidth)
{
Color[] colorss = color;// makes color[]
APath path = new APath();
path.FindPath(20,20,9,19);// makes path. Set certain values for testing
bool[,] open = path.open;// gets path from that class
for(int y = 0;y < mapHeight; y++)
{
for(int x = 0; x< mapWidth; x++)
{
if(open[x,y] == true)
{
colorss[y * mapWidth + x] = Color.red;// shows the river.
}
}
}
return colorss;
}
}
Если кто-нибудь сможет увидеть проблему, я был бы очень признателен. Спасибо, что прочитали мой вопрос, и желаю вам чудесного, захватывающего дня.