private void Start() { SlidingPuzzleMap map = new SlidingPuzzleMap( "11111111111111111111;" + "11111111110000011111;" + "11000000000111011111;" + "10000010000111010001;" + "10010010000000010001;" + "10111010110000000001;" + "10010000110101000001;" + "1100000000000101W101;" + "10000001100111011101;" + "10000001100000010001;" + "11110000000001100001;" + "11111000100001100001;" + "11111000100000000001;" + "11111000000000000001;" + "11110000000111111111;" + "11111100001111111111;" + "11111111111111111111" ); currentState = new SlidingPuzzleState(new Vector2Int(7, 9)); CreateMap(map); _display.ShowState(currentState); movementQueue = new Queue <Vector2Int>(); _pathfinder = new PuzzlePathfinder <Vector2Int, SlidingPuzzleState>() .RecalculateGrah(_processor, currentState, new Vector2Int[] { Vector2Int.up, Vector2Int.down, Vector2Int.left, Vector2Int.right }); }
public void _02_Pathfinder_Solves_The_Puzzle() { SlidingPuzzleMap map = new SlidingPuzzleMap( "11111111;" + "11000101;" + "11000101;" + "10000101;" + "10000001;" + "10001111;" + "11000001;" + "10000001;" + "11W11111;" + "11111111"); SlidingPuzzleState startingState = new SlidingPuzzleState(new Vector2Int(1, 6)); var processor = new SlidingPuzzleProcessor(map); var pathfinder = new Pathfinder.PuzzlePathfinder <Vector2Int, SlidingPuzzleState>().RecalculateGrah(processor, startingState, new Vector2Int[] { _up, _down, _left, _right }); var stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start(); var path = pathfinder.GetClosestWinPath(startingState); stopwatch.Stop(); Debug.Log($"Pathfinding had taken: {stopwatch.ElapsedMilliseconds} ms [{stopwatch.ElapsedTicks} ticks]"); Assert.That(path.Succesfull, Is.EqualTo(true), $"Path is not marked as succesful"); Assert.That(path.Path, Is.EqualTo(new Vector2Int[] { _right, _up, _left, _down }), $"Path is incorrect"); }
private void CreateMap(SlidingPuzzleMap map) { if (_processor != null) { _processor.Reconstruct(map); } else { _processor = new SlidingPuzzleProcessor(map); } _display.SetMap(map); }
public void SetMap(SlidingPuzzleMap map) { #if UNITY_EDITOR _editor_map = map; #endif var coords = new List <Vector3Int>(); var tiles = new List <TileBase>(); _tilemap.ClearAllTiles(); var walls = map.Walls; Vector3 offset = new Vector3(-(walls.GetLength(0) * .5f), -(walls.GetLength(1) * .5f), 0); _grid.transform.position = offset; for (int x = -_border; x < walls.GetLength(0) + _border; x++) { for (int y = -_border; y < walls.GetLength(1) + _border; y++) { if (x < 0 || x >= walls.GetLength(0) || y < 0 || y >= walls.GetLength(1) || walls[x, y]) { coords.Add(new Vector3Int(x, y, 0)); tiles.Add(_tile); } } } if (_winPrefab_instance == null) { _winPrefab_instance = Instantiate(_winPrefab, transform); } _winPrefab_instance.transform.position = _grid.GetCellCenterWorld(new Vector3Int(map.WinCoords.x, map.WinCoords.y, 0)); _tilemap.SetTiles(coords.ToArray(), tiles.ToArray()); }