public List <IMazeElement> FindPath() { List <IMazeElement> unexploredWalkableMazeElementsList = unexploredMazeElements.GetUnexploredList(); bool destinationReach = false; startMazeElement.PathFindWeight = 0; while (unexploredWalkableMazeElementsList.Count > 0 && destinationReach == false) { unexploredWalkableMazeElementsList.Sort((x, y) => x.PathFindWeight.CompareTo(y.PathFindWeight)); IMazeElement currentMazeElement = unexploredWalkableMazeElementsList[0]; pathFindProcessMetric.IncreaseNumberOfVisitedNodes(); if (currentMazeElement == destinationMazeElement) { destinationReach = true; continue; } unexploredWalkableMazeElementsList.Remove(currentMazeElement); List <IMazeElement> neighbourMazeElementList = planeBuilder.GetNeighboursOfMazeElement(currentMazeElement); ProcessNeighboursPathFindParameters(neighbourMazeElementList, unexploredWalkableMazeElementsList, currentMazeElement); } List <IMazeElement> listFromStartToDestination = DestinationPath.GetListFromStartToDestination(destinationMazeElement); pathFindProcessMetric.SetPathLengthExpressedInNumberOfNodes(listFromStartToDestination.Count); return(DestinationPath.GetListFromStartToDestination(destinationMazeElement)); }
public void ProcessNeighboursPathFindParameters( IMazeElement currentMazeElement, IOpenCloseListController openCloseListController, IPathFindProcessMetric pathFindProcessMetric ) { List <IMazeElement> neighbourMazeElementList = planeBuilder.GetNeighboursOfMazeElement(currentMazeElement); foreach (IMazeElement neighbourMazeElement in neighbourMazeElementList) { if (!openCloseListController.CloseListContains(neighbourMazeElement) && !neighbourMazeElement.IsMazeWall) { int discanceToNeighbour = 1; float newNeighbourWeight = currentMazeElement.PathFindWeight + discanceToNeighbour; if (newNeighbourWeight < neighbourMazeElement.PathFindWeight || !openCloseListController.OpenListContains(neighbourMazeElement)) //(openCloseListController.OpenListContains(neighbourMazeElement) && neighbourMazeElement.PathFindWeight < newNeighbourWeight) //|| //openCloseListController.CloseListContains(neighbourMazeElement) && neighbourMazeElement.PathFindWeight < newNeighbourWeight) { neighbourMazeElement.PathFindWeight = newNeighbourWeight; neighbourMazeElement.PathFindDistanceHeuristic = aStarDistanceHeuristic.GetDistanceBetween(neighbourMazeElement, destinationMazeElement); neighbourMazeElement.PathFindParent = currentMazeElement; if (!openCloseListController.OpenListContains(neighbourMazeElement)) { openCloseListController.AddToOpenList(neighbourMazeElement); } } } } pathFindProcessMetric.ProcessJunctionParametersBaseOn(neighbourMazeElementList); }
public void SetOutPoint(IMazeElement mazeElement) { endPoint = mazeElement; mazeElement.Tag = "PathFindDestinationNode"; pathFindEndPointIsAlreadySet = true; mazeElement.ChangeOnMazeEndPointColor(); }
public void SetInPoint(IMazeElement mazeElement) { startPoint = mazeElement; mazeElement.Tag = "PathFindStartNode"; pathFindStartPointIsAlreadySet = true; mazeElement.ChangeOnMazeStartPointColor(); }
void AssertionOfReciveOnVertical(Vector2 _firstMazeElementToReverseIndex, Vector2 _lastMazeElementToReverseIndex, List <IMazeElement> mazeElementsToProcess, Direction processDirection) { Vector2 firstMazeElementToReverseIndex; Vector2 lastMazeElementToReverseIndex; if (processDirection == Direction.Right || processDirection == Direction.Down) { firstMazeElementToReverseIndex = _firstMazeElementToReverseIndex; lastMazeElementToReverseIndex = _lastMazeElementToReverseIndex; } else { firstMazeElementToReverseIndex = _lastMazeElementToReverseIndex; lastMazeElementToReverseIndex = _firstMazeElementToReverseIndex; } for (int i = 0; i < firstMazeElementToReverseIndex.y; i++) { IMazeElement changedMazeElement = mazeElementsToProcess.Find(x => x.Index == new Vector2(firstMazeElementToReverseIndex.x, i)); //Debug.Log(changedMazeElement.Index); //for test problems MazeElementDidNotReceivedCall(changedMazeElement); } for (int i = (int)firstMazeElementToReverseIndex.y; i <= lastMazeElementToReverseIndex.y; i++) { IMazeElement changedMazeElement = mazeElementsToProcess.Find(x => x.Index == new Vector2(firstMazeElementToReverseIndex.x, i)); //Debug.Log(changedMazeElement.Index); //for test problems MazeElementDidReceivedCall(changedMazeElement); } for (int i = (int)lastMazeElementToReverseIndex.y + 1; i <= gamePlaneSize.y; i++) { IMazeElement changedMazeElement = mazeElementsToProcess.Find(x => x.Index == new Vector2(firstMazeElementToReverseIndex.x, i)); //Debug.Log(changedMazeElement.Index); //for test problems MazeElementDidNotReceivedCall(changedMazeElement); } }
private void AddToRoute(IMazeElement start) { var route = _mazeFactory.CreateRoute(start.Location); _visitedRoute.Add(route); _mazeRoute.Push(route); }
void MazeElementDidNotReceivedCall(IMazeElement changedMazeElement) { if (changedMazeElement != null) { changedMazeElement.DidNotReceive().ReverseIsMazeWall(); } }
List <IMazeElement> GetMazeElementsToProcess(Vector2 gamePlaneSize, Direction processDirection, Vector2 firstMazeElementToReverseIndex) { List <IMazeElement> mazeElementsList = new List <IMazeElement>(); if (processDirection == Direction.Left || processDirection == Direction.Right) { for (int i = 0; i <= gamePlaneSize.x; i++) { IMazeElement mazeElement = Substitute.For <IMazeElement>(); mazeElement.Index.Returns(new Vector2(i, firstMazeElementToReverseIndex.y)); mazeElementsList.Add(mazeElement); } } else { for (int i = 0; i <= gamePlaneSize.y; i++) { IMazeElement mazeElement = Substitute.For <IMazeElement>(); mazeElement.Index.Returns(new Vector2(firstMazeElementToReverseIndex.x, i)); mazeElementsList.Add(mazeElement); } } return(mazeElementsList); }
public List <IMazeElement> FindPath() { startMazeElement.PathFindWeight = 0; openCloseListController.AddToOpenList(startMazeElement); while (openCloseListController.OpenListCount() > 0 && destinationReach == false) { IMazeElement currentMazeElement = openCloseListController.GetMazeElementWithLowestWeight(); Debug.Log(currentMazeElement.Index); openCloseListController.RemoveFirstElementFromOpenList(currentMazeElement); openCloseListController.AddToCloseList(currentMazeElement); pathFindProcessMetric.IncreaseNumberOfVisitedNodes(); if (currentMazeElement == destinationMazeElement) { destinationReach = true; continue; } currentMazeElement.Tag = "PathFindSolution"; neighboursPathFindParametersProcessor.ProcessNeighboursPathFindParameters(currentMazeElement, openCloseListController, pathFindProcessMetric); } List <IMazeElement> listFromStartToDestination = DestinationPath.GetListFromStartToDestination(destinationMazeElement); pathFindProcessMetric.SetPathLengthExpressedInNumberOfNodes(listFromStartToDestination.Count); return(listFromStartToDestination); }
private void AddToListIfNotBelongToAnotherWall(IMazeElement MazeElementBoundary, bool newIsMazeWallForRootMazeElement) { if (newIsMazeWallForRootMazeElement == true && !MazeElementBoundary.IsElementOfAnotherWall || newIsMazeWallForRootMazeElement == false && MazeElementBoundary.IsElementOfAnotherWall) { mazeElementsToProcess.Add(MazeElementBoundary); } }
public NeighboursPathFindParametersProcessor(IPlaneBuilder _planeBuilder, IMazeElement _destinationMazeElement, IAStarDistanceHeuristic _aStarDistanceHeuristic) { planeBuilder = _planeBuilder; destinationMazeElement = _destinationMazeElement; aStarDistanceHeuristic = _aStarDistanceHeuristic; }
public void UpdateList(List <IMazeElement> _mazeElementsToProcess, Direction _processDirection, bool newIsMazeWallForRootMazeElement) { mazeElementsToProcess = _mazeElementsToProcess; Vector2 LastMouseClickMazeElementIndex = mouseBoundary.GetLastMouseClickMazeElementIndex(); Vector2 CurrenMouseOntMazeElementIndex = mouseBoundary.GetCurrentMouseOnMazeElementIndex(); AddToListMouseClickOnMazeElement(LastMouseClickMazeElementIndex, newIsMazeWallForRootMazeElement); switch (_processDirection) { case Direction.Left: { for (int i = (int)LastMouseClickMazeElementIndex.x - 1; i >= (int)CurrenMouseOntMazeElementIndex.x; i--) { IMazeElement MazeElementBoundary = planeBuilder.GetFromMazeArray(i, (int)LastMouseClickMazeElementIndex.y); AddToListIfNotBelongToAnotherWall(MazeElementBoundary, newIsMazeWallForRootMazeElement); } break; } case Direction.Right: { for (int i = (int)LastMouseClickMazeElementIndex.x + 1; i <= (int)CurrenMouseOntMazeElementIndex.x; i++) { IMazeElement MazeElementBoundary = planeBuilder.GetFromMazeArray(i, (int)LastMouseClickMazeElementIndex.y); AddToListIfNotBelongToAnotherWall(MazeElementBoundary, newIsMazeWallForRootMazeElement); } break; } case Direction.Up: { for (int i = (int)LastMouseClickMazeElementIndex.y - 1; i >= (int)CurrenMouseOntMazeElementIndex.y; i--) { IMazeElement MazeElementBoundary = planeBuilder.GetFromMazeArray((int)LastMouseClickMazeElementIndex.x, i); AddToListIfNotBelongToAnotherWall(MazeElementBoundary, newIsMazeWallForRootMazeElement); } break; } case Direction.Down: { for (int i = (int)LastMouseClickMazeElementIndex.y + 1; i <= (int)CurrenMouseOntMazeElementIndex.y; i++) { IMazeElement MazeElementBoundary = planeBuilder.GetFromMazeArray((int)LastMouseClickMazeElementIndex.x, i); AddToListIfNotBelongToAnotherWall(MazeElementBoundary, newIsMazeWallForRootMazeElement); } break; } default: { throw new System.NotImplementedException(); } } }
public void RemoveInPoint() { if (startPoint != null) { startPoint.Tag = "MazeElement"; pathFindStartPointIsAlreadySet = false; startPoint.ChangeOnNormalColor(); startPoint = null; } }
public void RemoveEndPoint() { if (endPoint != null) { endPoint.Tag = "MazeElement"; pathFindEndPointIsAlreadySet = false; endPoint.ChangeOnNormalColor(); endPoint = null; } }
private void SetSpecialPoints(int mazeNumber) { Vector2 startPlaceForPathFinding = saveTextReader.GetStartPlaceForPathFinding(mazeNumber); Vector2 destinationPlaceForPathFinding = saveTextReader.GetDestinationPlaceForPathFinding(mazeNumber); IMazeElement startMazeElementForPathfind = planeBuilder.GetFromMazeArray((int)startPlaceForPathFinding.x, (int)startPlaceForPathFinding.y); IMazeElement destinationMazeElementForPathFind = planeBuilder.GetFromMazeArray((int)destinationPlaceForPathFinding.x, (int)destinationPlaceForPathFinding.y); mazeInOutPoints.SetInOutPointsAt(startMazeElementForPathfind, destinationMazeElementForPathFind); }
List <IMazeElement> CreateNeighbourList(IPlaneBuilder planeBuilder, IMazeElement mazeElement) { List <IMazeElement> mazeElementsList = new List <IMazeElement>(); mazeElementsList.Add(planeBuilder.GetFromMazeArray((int)mazeElement.Index.x - 1, (int)mazeElement.Index.y)); mazeElementsList.Add(planeBuilder.GetFromMazeArray((int)mazeElement.Index.x + 1, (int)mazeElement.Index.y)); mazeElementsList.Add(planeBuilder.GetFromMazeArray((int)mazeElement.Index.x, (int)mazeElement.Index.y - 1)); mazeElementsList.Add(planeBuilder.GetFromMazeArray((int)mazeElement.Index.x, (int)mazeElement.Index.y + 1)); return(mazeElementsList); }
public void RestartMazeElementsParameters() { for (int i = 0; i < planeBuilder.IntagerNumberOfMazeElementsOnXAndY.x; i++) { for (int j = 0; j < planeBuilder.IntagerNumberOfMazeElementsOnXAndY.y; j++) { IMazeElement mazeElement = planeBuilder.GetFromMazeArray(i, j); ResetPathFindingParameters(mazeElement); } } }
public List <IMazeElement> GetNeighboursOfMazeElement(IMazeElement mazeElement) { List <IMazeElement> NeighboursList = new List <IMazeElement>(); NeighboursList.Add(PlaneBuilder.GetFromMazeArray(Mathf.Clamp((int)mazeElement.Index.x - 1, 0, (int)PlaneBuilder.IntagerNumberOfMazeElementsOnXAndY.x - 1), (int)mazeElement.Index.y)); NeighboursList.Add(PlaneBuilder.GetFromMazeArray(Mathf.Clamp((int)mazeElement.Index.x + 1, 0, (int)PlaneBuilder.IntagerNumberOfMazeElementsOnXAndY.x - 1), (int)mazeElement.Index.y)); NeighboursList.Add(PlaneBuilder.GetFromMazeArray((int)mazeElement.Index.x, Mathf.Clamp((int)mazeElement.Index.y - 1, 0, (int)PlaneBuilder.IntagerNumberOfMazeElementsOnXAndY.y - 1))); NeighboursList.Add(PlaneBuilder.GetFromMazeArray((int)mazeElement.Index.x, Mathf.Clamp((int)mazeElement.Index.y + 1, 0, (int)PlaneBuilder.IntagerNumberOfMazeElementsOnXAndY.y - 1))); return(NeighboursList); }
List <IMazeElement> GetNeighbours(IMazeElement currentMazeElement, float mazeElementsWeight) { Vector2 currentMazeElementIndex = currentMazeElement.Index; List <IMazeElement> neighbours = new List <IMazeElement>(); neighbours.Add(GetMazeElementMock(new Vector2(currentMazeElementIndex.x - 1, currentMazeElementIndex.y), mazeElementsWeight)); neighbours.Add(GetMazeElementMock(new Vector2(currentMazeElementIndex.x + 1, currentMazeElementIndex.y), mazeElementsWeight)); neighbours.Add(GetMazeElementMock(new Vector2(currentMazeElementIndex.x, currentMazeElementIndex.y - 1), mazeElementsWeight)); neighbours.Add(GetMazeElementMock(new Vector2(currentMazeElementIndex.x, currentMazeElementIndex.y + 1), mazeElementsWeight)); return(neighbours); }
public void SortOpenList_OneElementInList() { List <IMazeElement> openList = new List <IMazeElement>(); List <IMazeElement> closeList = new List <IMazeElement>(); openList.Add(GetMazeElementMock(1.0f, 2.0f)); OpenCloseListController openCloseListController = new OpenCloseListController(openList, closeList); IMazeElement lowestWeightMazeElement = openCloseListController.GetMazeElementWithLowestWeight(); Assert.AreEqual(lowestWeightMazeElement.PathFindWeight, 1.0f); Assert.AreEqual(lowestWeightMazeElement.PathFindDistanceHeuristic, 2.0f); }
public static List <IMazeElement> GetListFromStartToDestination(IMazeElement destinationPoint) { List <IMazeElement> pathFromStartToEnd = new List <IMazeElement>(); IMazeElement mazeElement = destinationPoint; while (mazeElement.PathFindParent != null) { pathFromStartToEnd.Add(mazeElement); mazeElement = mazeElement.PathFindParent; } pathFromStartToEnd.Reverse(); return(pathFromStartToEnd); }
IPlaneBuilder GetPlaneBuilderMock(IMazeElement currentMazeElement, float mazeElementsWeight) { var planeBuilder = Substitute.For <IPlaneBuilder>(); var neighboursList = GetNeighbours(currentMazeElement, mazeElementsWeight); foreach (var neighbour in neighboursList) { Vector2 mazeElementIndex = neighbour.Index; planeBuilder.GetFromMazeArray((int)mazeElementIndex.x, (int)mazeElementIndex.y).Returns(neighbour); } planeBuilder.GetNeighboursOfMazeElement(currentMazeElement).Returns(neighboursList); return(planeBuilder); }
public AStarPathFinder( IMazeElement _startMazeElement, IMazeElement _destinationMazeElement, IOpenCloseListController _openCloseListController, INeighboursPathFindParametersProcessor _neighboursPathFindParametersProcessor, IPathFindProcessMetric _pathFindProcessMetric) { startMazeElement = _startMazeElement; destinationMazeElement = _destinationMazeElement; openCloseListController = _openCloseListController; neighboursPathFindParametersProcessor = _neighboursPathFindParametersProcessor; pathFindProcessMetric = _pathFindProcessMetric; }
private void CreateList() { for (int i = 0; i < planeBuilder.IntagerNumberOfMazeElementsOnXAndY.x; i++) { for (int j = 0; j < planeBuilder.IntagerNumberOfMazeElementsOnXAndY.y; j++) { IMazeElement mazeElement = planeBuilder.GetFromMazeArray(i, j); if (!IsMazeElementMazeWall(mazeElement)) { unexploredMazeElementsList.Add(mazeElement); } } } }
public DijkstraPathFinder(IPlaneBuilder _planeBuilder, IUnexploredMazeElements _unexploredMazeElements, IMazeElement _startMazeElement, IMazeElement _destinationMazeElement, IPathFindProcessMetric _pathFindProcessMetric) { planeBuilder = _planeBuilder; unexploredMazeElements = _unexploredMazeElements; startMazeElement = _startMazeElement; destinationMazeElement = _destinationMazeElement; pathFindProcessMetric = _pathFindProcessMetric; }
public void _2_2_GetNeighboursOfMazeElement_CheckIfresultIsEqualWithCorrect() { Vector2 gamePlaneSize = new Vector2(10, 10); IPlaneBuilder planeBoundry = GetPlaneBuilderMock(gamePlaneSize); PlaneBuilder planeBuilder = Substitute.For <PlaneBuilder>(); planeBuilder.SetPlaneController(planeBoundry); IMazeElement currentMazeElement = planeBoundry.GetFromMazeArray(2, 8); List <IMazeElement> neighbourMazeElementList = planeBuilder.GetNeighboursOfMazeElement(currentMazeElement); List <IMazeElement> list = CreateNeighbourList(planeBoundry, currentMazeElement); Assert.AreEqual(neighbourMazeElementList, list); }
private bool FindAndAddStartNodeToMyRoute() { IMazeElement startNode = _maze.FindNode <Start>(); IMazeElement finishNode = _maze.FindNode <Destination>(); if (startNode != null && finishNode != null) { AddToRoute(startNode); return(true); } else { return(false); } }
private void FindTarget() { IMazeElement way = GetNonVisitedWay(); if (way == null) { _mazeRoute.Pop(); //No Way! lets go back FindTarget(); } else { AddToRoute(way); if (!(way is Destination)) { FindTarget(); } } }
public void SetMazeMetric() { for (int i = 0; i < planeBuilder.IntagerNumberOfMazeElementsOnXAndY.x; i++) { for (int j = 0; j < planeBuilder.IntagerNumberOfMazeElementsOnXAndY.y; j++) { IncreaseNumberOfNodes(); IMazeElement currentMazeElement = planeBuilder.GetFromMazeArray(i, j); if (!currentMazeElement.IsMazeWall) { IncreaseNumberOfWalkableNodes(); List <IMazeElement> neighbourMazeElementList = planeBuilder.GetNeighboursOfMazeElement(currentMazeElement); ProcessJunctionParametersBaseOn(neighbourMazeElementList); } } } }
public void SetNeighboursForMazeElement(IPlaneBuilder planeBuilder) { Vector2 gamePlaneSize = planeBuilder.IntagerNumberOfMazeElementsOnXAndY; for (int i = 0; i < (int)gamePlaneSize.x; i++) { for (int j = 0; j < (int)gamePlaneSize.y; j++) { IMazeElement mazeElement = planeBuilder.GetFromMazeArray(i, j); List <IMazeElement> NeighboursList = new List <IMazeElement>(); //Debug.Log(planeBuilder.GetFromMazeArray(Mathf.Clamp((int)mazeElement.Index.x - 1, 0, (int)planeBuilder.IntagerNumberOfMazeElementsOnXAndY.x - 1), (int)mazeElement.Index.y).Index); NeighboursList.Add(planeBuilder.GetFromMazeArray(Mathf.Clamp((int)mazeElement.Index.x - 1, 0, (int)planeBuilder.IntagerNumberOfMazeElementsOnXAndY.x - 1), (int)mazeElement.Index.y)); NeighboursList.Add(planeBuilder.GetFromMazeArray(Mathf.Clamp((int)mazeElement.Index.x + 1, 0, (int)planeBuilder.IntagerNumberOfMazeElementsOnXAndY.x - 1), (int)mazeElement.Index.y)); NeighboursList.Add(planeBuilder.GetFromMazeArray((int)mazeElement.Index.x, Mathf.Clamp((int)mazeElement.Index.y - 1, 0, (int)planeBuilder.IntagerNumberOfMazeElementsOnXAndY.y - 1))); NeighboursList.Add(planeBuilder.GetFromMazeArray((int)mazeElement.Index.x, Mathf.Clamp((int)mazeElement.Index.y + 1, 0, (int)planeBuilder.IntagerNumberOfMazeElementsOnXAndY.y - 1))); planeBuilder.GetNeighboursOfMazeElement(planeBuilder.GetFromMazeArray(i, j)).Returns(NeighboursList); } } }