/// <summary> /// Вспомогательная функция для запуска со-программы визуализации /// </summary> private IEnumerator VisualizeAndMoveCoro(Labirynth labirynth, List <Point> algo, List <Point> path) { float visualizeTime = 3f; float displayTime = 3f; int frames = Mathf.RoundToInt(visualizeTime / Time.fixedDeltaTime); int pointPerFrame = Mathf.CeilToInt(1.0f * algo.Count / frames); int pos = 0; float time = visualizeTime + displayTime; while (pos < algo.Count) { for (int i = 0; i < pointPerFrame && pos < algo.Count; i++) { Color color = Color.HSVToRGB(1.0f * pos / (algo.Count - 1), 1, 1); labirynth.DrawSquare(algo [pos], color, time); pos++; } time -= Time.fixedDeltaTime; yield return(0); } yield return(new WaitForSeconds(displayTime)); Move(labirynth, path); yield return(null); }
public static void MainX() { var l = Labirynth.CreateLabirynth(); var str = "([{()[][]{}}])"; var stack = new Stack <char>(); var dict = new Dictionary <char, char> { { '}', '{' }, { ')', '(' }, { ']', '[' } }; foreach (var e in str) { if (dict.Values.Contains(e)) { stack.Push(e); } else if (dict.Keys.Contains(e)) { if (dict[e] != stack.Pop()) { throw new Exception(""); } } } }
public static void Main() { map = Labirynth.CreateLabirynth(); var queue = new Queue <Point>(); queue.Enqueue(new Point { X = 0, Y = 0 }); while (queue.Count != 0) { var point = queue.Dequeue(); if (!CanMove(point.X, point.Y)) { continue; } map[point.X, point.Y] = Space.Visited; Visualize(); queue.Enqueue(new Point { X = point.X - 1, Y = point.Y }); queue.Enqueue(new Point { X = point.X + 1, Y = point.Y }); queue.Enqueue(new Point { X = point.X, Y = point.Y - 1 }); queue.Enqueue(new Point { X = point.X, Y = point.Y + 1 }); } }
public List <Point> FindPath(Labirynth labirynth, Point source, Point destination) { List <Point> path = new List <Point> (); path.Add(source); path.Add(destination); return(path); }
/// <summary> /// Пройти по заданному пути /// </summary> /// <param name="labirynth">Лабиринт.</param> /// <param name="path">Путь.</param> public void Move(Labirynth labirynth, List <Point> path) { if (path == null) { return; } StopAllCoroutines(); _labirynth = labirynth; _path = path; DrawPath(path); }
/// <summary> /// Отобразить работу алгоритма, после этого вызвать метод Move() /// </summary> /// <param name="labirynth">Лабиринт.</param> /// <param name="algo">Работа алгоритма.</param> /// <param name="path">Путь.</param> public void VisualizeAndMove(Labirynth labirynth, List <Point> algo, List <Point> path) { if (algo.Count <= 1) { Move(labirynth, path); } else { Move(null, new List <Point> ()); StartCoroutine(VisualizeAndMoveCoro(labirynth, algo, path)); } }
public List <Point> FindPath(Labirynth labirynth, Point source, Point destination) { NavMeshPath navMeshPath = new NavMeshPath(); NavMesh.CalculatePath(source.position, destination.position, NavMesh.AllAreas, navMeshPath); List <Point> path = new List <Point>(); foreach (Vector3 corner in navMeshPath.corners) { path.Add(labirynth.GetPoint(corner)); } return(path); }
/// <summary> /// Ищет путь в графе-лабиринте /// </summary> /// <returns>Путь по вершинам в графе</returns> /// <param name="labirynth">Лабиринт-граф</param> /// <param name="source">Вершина начала пути</param> /// <param name="destination">Вершина конца пути</param> public List <Point> FindPath(Labirynth labirynth, Point source, Point destination) { // Карта "обратных" перемещений (вершина) -> (откуда мы в нее попали) Dictionary <Point, Point> cameFrom = new Dictionary <Point, Point> (); // Стек вершин, по которым предстоит пройти // Является границей текущей карты "обратных" перемещений Stack <Point> frontier = new Stack <Point> (); // Добавляем информацию о начале cameFrom.Add(source, null); frontier.Push(source); while (frontier.Count > 0) { // Берем первую вершину в стеке Point current = frontier.Pop(); if (current == destination) { break; } // Ищем все соседние вершины List <Point> neighbours = labirynth.GetNeighbours(current); // Перемешаем соседей для драмматизма Shuffle(neighbours); foreach (Point neighbour in neighbours) { // Эту вершину мы включили в нашу карту перемещений, // значит мы смогли в нее прийти другим способом if (cameFrom.ContainsKey(neighbour)) { continue; } // Записываем что в `neighbour` мы попали из `current` cameFrom.Add(neighbour, current); frontier.Push(neighbour); } } return(BuildPath(cameFrom, source, destination)); }
// Use this for initialization void Start() { instance = this; LevelFinishedController.instance.setDecoyFixed (false); sizeX = LevelFinishedController.instance.getMazeSizeX(); sizeZ = LevelFinishedController.instance.getMazeSizeZ(); spaceX = planeSizeX / (sizeX * 2f); spaceZ = planeSizeZ / (sizeZ * 2f); string[,] grid = null; Puzzle puzzle = null; if (LevelFinishedController.instance.getPuzzleName() != null) { System.Type type = System.Type.GetType (LevelFinishedController.instance.getPuzzleName()); puzzle = (Puzzle) ScriptableObject.CreateInstance(type); grid = puzzle.getGrid(); puzzle.create(); } labirynth = new Labirynth (sizeX, sizeZ, grid); labirynth.generate (); drawMachines (); drawDevice (); if (LevelFinishedController.instance.getPuzzleName() == null) { drawKeys (labirynth.getKeys ()); } drawJumps (); drawSmallWalls (labirynth, 0); drawHorisontalWalls (labirynth, 0); drawVerticalWalls (labirynth, 0); hidePlayers (); // must be called before A* createNodes (); if (LevelFinishedController.instance.getPuzzleName() != null) { puzzle.finish(); } }
public List <Point> FindPath(Labirynth labirynth, Point source, Point destination) { // Карта "обратных" перемещений (вершина) -> (откуда мы в нее попали) Dictionary <Point, Point> cameFrom = new Dictionary <Point, Point> (); // Очередь вершин, по которым предстоит пройти // Является границей текущей карты "обратных" перемещений PriorityQueue frontier = new PriorityQueue(); // Добавляем информацию о начале cameFrom[source] = null; frontier.Enqueue(source, 0f); while (!frontier.Empty()) { // Берем первую вершину в очереди Point current = frontier.Dequeue(); if (current == destination) { break; } // Ищем все соседние вершины List <Point> neighbours = labirynth.GetNeighbours(current); foreach (Point neighbour in neighbours) { if (cameFrom.ContainsKey(neighbour)) { continue; } // Записываем что в `neighbour` мы попали из `current` float priority = Heuristic(neighbour, destination); cameFrom[neighbour] = current; frontier.Enqueue(neighbour, priority); } } return(BuildPath(cameFrom, source, destination)); }
public static void MainX() { map = Labirynth.CreateLarge(); Stack <Point> points = new Stack <Point>(); points.Push(new Point { X = 0, Y = 0 }); while (points.Count != 0) { var point = points.Pop(); if (!CanMove(point.X, point.Y)) { continue; } map[point.X, point.Y] = Space.Visited; // Visualize(); points.Push(point.X - 1, point.Y); points.Push(point.X + 1, point.Y); points.Push(point.X, point.Y - 1); points.Push(point.X, point.Y + 1); } }
public List<GameObject> createNewWalls() { labirynth = new Labirynth (sizeX, sizeZ, null); labirynth.generate (); List<GameObject> walls = new List<GameObject> (); walls.AddRange (drawHorisontalWalls (labirynth, -2.5f)); walls.AddRange (drawVerticalWalls (labirynth, -2.5f)); walls.AddRange (drawSmallWalls (labirynth, -2.5f)); return walls; }
List<GameObject> drawVerticalWalls(Labirynth labirynth, float yOffset) { List<GameObject> walls = new List<GameObject> (); float scaleFactorZ = 2*spaceZ - 1f; for (int z=1; z<=sizeZ * 2 - 1; z+=2) { for (int x=2; x<=sizeX * 2 -2 ; x+=2) // don't draw edges { if (labirynth.getWalls(x, z).Equals (TileType.WALL)) { Vector3 pos = new Vector3 (-planeSizeX/2f + spaceX * x, wallPrefab.transform.position.y + yOffset, offsetZ + planeSizeZ/2f - spaceZ * z); GameObject obj = (GameObject) Instantiate (wallPrefab, pos, Quaternion.Euler(0, 90, 0)); obj.name = obj.name + labirynth.getParams(x, z); obj.transform.localScale = new Vector3(scaleFactorZ + compensatePillarInnerRadius, wallPrefab.transform.localScale.y, wallPrefab.transform.localScale.z); if (z == sizeZ * 2 - 1) // last row { obj.layer = LayerMask.NameToLayer("1stRowMazeWalls"); } walls.Add(obj); } } } return walls; }
List<GameObject> drawSmallWalls(Labirynth labirynth, float yOffset) { List<GameObject> walls = new List<GameObject> (); for (int z=2; z<=sizeZ * 2; z+=2) // don't draw edges { for (int x=2; x<=sizeX * 2 - 2; x+=2) // don't draw edges { if (labirynth.getWalls(x, z).Equals(TileType.WALL)) { Vector3 pos = new Vector3 (-planeSizeX/2f + spaceX * x, smallWallPrefab.transform.position.y + yOffset, offsetZ + planeSizeZ/2f - spaceZ * z); int angle = Random.Range(0, 4) * 90; GameObject obj = (GameObject)Instantiate (smallWallPrefab, pos, Quaternion.Euler(0, angle, 0)); obj.name = obj.name + labirynth.getParams(x, z); if (z == sizeZ * 2) // last row { obj.layer = LayerMask.NameToLayer("1stRowMazeWalls"); } walls.Add(obj); } } } return walls; }
public List <Point> FindPath(Labirynth labirynth, Point source, Point destination) { throw new System.NotImplementedException(); }
public static void MainX() { map = Labirynth.CreateLarge(); Go(0, 0); }