static public void initializeVariables(MazeGraph <T> G) { counter = G.numVert(); g = new MazeGraph <T>(counter, G.rows, G.cols); visited = new bool[counter]; rand = new System.Random(); }
public static MazeGraph <T> Execute(MazeGraph <T> G) { InitializeVariables(G); for (int i = 0; i < g.numVert(); ++i) { List <MazeGraph <T> .VertexCost> neighbors = new List <MazeGraph <T> .VertexCost>(); List <MazeGraph <T> .VertexCost> adj = G[i]; foreach (MazeGraph <T> .VertexCost v in adj) { if (v.vertex > i) { neighbors.Add(v); } } if (neighbors.Count > 0) { int idx = rand.Next(0, neighbors.Count); MazeGraph <T> .VertexCost neighbor = neighbors[idx]; g.addEdge(i, neighbor.vertex, neighbor.cost); g.addEdge(neighbor.vertex, i, neighbor.cost); } } return(g); }
void Start() { //mazeGraph = new MazeGraph(mazeRows, mazeColumns); //mazeGraph = new MazeGraphWithUnionSet(mazeRows, mazeColumns); //mazeGraph=new MazeGraphByBacktracking(mazeRows,mazeColumns); if (GameData.instance.mazeDifficulty == MazeDifficulty.EASY) { mazeGraph = new MazeGraphWithUnionSet(mazeRows, mazeColumns); } else//hard { mazeGraph = new MazeGraphByBacktracking(mazeRows, mazeColumns); } mazeGraph.Generate(); //一个mesh装不下 // MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>(); //meshRenderer.material = mazeMtrl; //MeshCollider meshCollider = gameObject.AddComponent<MeshCollider>(); //MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>(); //mcterrain = new MCTerrain(meshRenderer, meshCollider, meshFilter, blockLength, mazeColumns, wallHeight, mazeRows); int chunkSizeX = mazeColumns * (wallSize + roomSize) + wallSize; int chunkSizeZ = mazeRows * (wallSize + roomSize) + wallSize; mcterrain = new MCTerrain(parentGO, concreteMtrl, blockLength, chunkSizeX, wallHeight, chunkSizeZ); FillChunk(); mcterrain.BuildMesh(); lineRenderer = GetComponent <LineRenderer>(); CalPathRenderer(); }
public void Enumeration_ReturnsNothing_ForEmptyMatrix() { var m = new bool[0, 0]; var graph = new MazeGraph(m); TestHelper.AssertSequence(graph); }
public MazeGraph GenerateGraphPathFromMaze(Int32Point point) { MazeGraph pathGraph = new MazeGraph(); Stack <Int32Point> cells = new Stack <Int32Point> (); List <Int32Point> processedCells = new List <Int32Point> (); cells.Push(point); while (cells.Count > 0) { Int32Point cell = cells.Pop(); if (processedCells.Contains(cell)) { continue; } processedCells.Add(cell); List <Int32Point> neighbours = GetCellNeighbours(cell); foreach (Int32Point neighbour in neighbours) { if (!HasWallBetween(cell, neighbour)) { pathGraph.AddEdge(cell, neighbour); cells.Push(neighbour); } } } //Debug.Log (pathGraph); return(pathGraph); }
public static MazeGraph <T> Execute(MazeGraph <T> G) { InitializeVariables(G); for (int i = 0; i < g.rows; ++i) { List <int> group = new List <int>(); for (int j = 0; j < g.cols; ++j) { group.Add(j); if (!(j < g.cols - 1) || ((i < g.rows - 1) && rand.Next(2) == 0)) { int connectAt = group[rand.Next(0, group.Count)]; g.addEdge(g.GetNode(i, connectAt), g.GetNode(i + 1, connectAt), default(T)); g.addEdge(g.GetNode(i + 1, connectAt), g.GetNode(i, connectAt), default(T)); group.Clear(); } else { g.addEdge(g.GetNode(i, j), g.GetEast(i, j), default(T)); g.addEdge(g.GetEast(i, j), g.GetNode(i, j), default(T)); } } } return(g); }
public void GetPotentialWeight_ReturnsCellDistance(int sx, int sy, int ex, int ey, double expected) { var graph = new MazeGraph(_m3X3); var p = graph.GetPotentialWeight(new Cell(sx, sy), new Cell(ex, ey)); Assert.AreEqual(expected, p); }
public void GetEdges_ThrowsException_ForInvalidCell(int x, int y) { var graph = new MazeGraph(new[, ] { { true } }); Assert.Throws <ArgumentException>(() => graph.GetEdges(new Cell(x, y))); }
public void IsReadOnly_ReturnsFalse() { var graph = new MazeGraph(new[, ] { { true } }); Assert.IsFalse(graph.IsReadOnly); }
public void GetEdges_ThrowsException_ForFalseCell() { var graph = new MazeGraph(new[, ] { { false } }); Assert.Throws <ArgumentException>(() => graph.GetEdges(new Cell(0, 0))); }
static public void initializeVariables(MazeGraph <T> G) { N = G.numVert(); g = new MazeGraph <T>(N, G.rows, G.cols); added = new bool[N]; added[0] = true; //first node; edge = new Edge <T>(0, 0, default(T)); queue = new PartialOrderedTree <Edge <T> >(N * ((N - 1) / 2) - N + 2); }
public static void InitializeVariables(MazeGraph <T> G) { unvisited = new List <int>(G.numVert()); g = new MazeGraph <T>(G.numVert(), G.rows, G.cols); for (int i = 0; i < G.numVert(); ++i) { unvisited.Add(i); } }
public void IsDirected_ReturnsFalse() { var graph = new MazeGraph(new[, ] { { true, false }, { true, true } }); Assert.IsFalse(graph.IsDirected); }
public void GenerateMaze() { DestroyMaze(); G = MazeGraph <int> .CreateNoWallsGraph4(rows, cols); executeAlgorithm(); createOBJ(); AssetDatabase.Refresh(); Generate3dMaze(); }
static bool hasWestconnection(int i, int j, MazeGraph <T> G) { if (j == 0) { return(true); } else { return(G.hasEdge(G.GetNode(i, j), G.GetNode(i, j - 1))); } }
void Awake() { mazeGraph = new MazeGraph(); mazeGenerator = GetComponent <MazeGenerator>(); mazeGenerator.Initialize(tilePrefab, mazeRoot); graphView = GetComponent <GraphView>(); mazePathfinder = new MazePathfinder(this); mazePathfinder.OnSolutionFinished += DisplaySolution; }
static bool hasNorthconnection(int i, int j, MazeGraph <T> G) { if (i == G.rows - 1) { return(true); } else { return(G.hasEdge(G.GetNode(i, j), G.GetNode(i + 1, j))); } }
static List <Edge <T> > adjEdges(MazeGraph <T> G, int i) { List <MazeGraph <T> .VertexCost> adj = G.Adjacents(i); List <Edge <T> > adjEdges_ = new List <Edge <T> >(adj.Count); for (int j = 0; j < adj.Count; ++j) { adjEdges_.Add(new Edge <T>(i, adj[j].vertex, adj[j].cost)); } return(adjEdges_); }
static bool hasSouthconnection(int i, int j, MazeGraph <T> G) { if (i == 0) { return(true); } else { return(G.hasEdge(G.GetNode(i, j), G.GetNode(i - 1, j))); } }
static void enqueueAdjEdges(MazeGraph <T> G, int i) { List <Edge <T> > adj = adjEdges(G, i); for (int j = 0; j < adj.Count; ++j) { if (!added[adj[j].dest]) { queue.insert(adj[j]); } } }
public void executeAlgorithm() { switch (generationAlgorithm) { case Algorithm.AldousBroder: G = Algorithms.AldousBroder <int> .Execute(G); break; case Algorithm.BinaryTree: G = Algorithms.BinaryTree <int> .Execute(G); break; case Algorithm.Ellers: G = Algorithms.Ellers <int> .Execute(G); break; case Algorithm.HuntAndKill: G = Algorithms.HuntAndKill <int> .Execute(G); break; case Algorithm.Kruskall: G = Algorithms.Kruskall <int> .Execute(G); break; case Algorithm.Prim: G = Algorithms.Prim <int> .Execute(G); break; case Algorithm.RecursiveDivision: G = Algorithms.RecursiveDivision <int> .Execute(G); break; case Algorithm.Sidewinder: G = Algorithms.Sidewinder <int> .Execute(G); break; case Algorithm.Wilson: G = Algorithms.Wilson <int> .Execute(G); break; } }
static public bool TestConnectedGridGraph(MazeGraph <T> G) { for (int i = 0; i < G.rows; ++i) { for (int j = 0; j < G.cols; ++j) { if (!(hasEastconnection(i, j, G) && hasNorthconnection(i, j, G) && hasSouthconnection(i, j, G) && hasWestconnection(i, j, G))) { return(false); } } } return(true); }
static void initializeVariables(MazeGraph <T> G) { n = G.numVert(); g = new MazeGraph <T>(n, G.rows, G.cols); P = new Partition(n); queue = new PartialOrderedTree <Edge <T> >(n * n); for (int i = 0; i < n; ++i) { List <MazeGraph <T> .VertexCost> adj = G.Adjacents(i); for (int j = 0; j < adj.Count; ++j) { queue.insert(new Edge <T>(i, adj[j].vertex, adj[j].cost)); } } }
//interSections private int interSections(MazeGraph <T> g) { int intersections = 0; for (int i = 0; i < g.rows; ++i) { for (int j = 0; j < g.cols - 1; ++j) { if (2 < g.ConnectedNeighbors(i, j).Count) { ++intersections; } } } return(intersections); }
//Deadends private int deadEnds(MazeGraph <T> g) { int deadends = 0; for (int i = 0; i < g.rows; ++i) { for (int j = 0; j < g.cols - 1; ++j) { if (g.ConnectedNeighbors(i, j).Count == 1) { ++deadends; } } } return(deadends); }
static public bool TestGraphIsTree(MazeGraph <T> G) { bool[] visited = new bool[G.NumVert]; if (IsCycle(0, visited, -1, G)) { return(false); } for (int i = 0; i < visited.Length; ++i) { if (!visited[i]) { return(true); } } return(true); }
static public MazeGraph <T> Execute(MazeGraph <T> G) { initializeVariables(G); int current = rand.Next(g.numVert()); visited[current] = true; do { current = Kill(current); if (current == -1) { current = Hunt(); } } while (current != -1); return(g); }
public bool Validate() { Coord dim = m.getDimensions(); if (dim.x < 0 || dim.x > max_width || dim.y < 0 || dim.y > max_height) { Console.WriteLine("Invalid maze dimensions"); return(false); } Coord start = m.getStart(); Coord finish = m.getFinish(); if (start.equals(finish)) { Console.WriteLine("Start can't be the same as finish"); return(false); } if (!PointInMaze(start) || !PointInMaze(finish)) { Console.WriteLine("Start and finish have to be within maze"); return(false); } if (!ValidatePaths()) { return(false); } mg = new MazeGraph(m); if (!ValidateItems()) { return(false); } if (!ValidateEnemies()) { return(false); } if (!SolutionExists()) { return(false); } return(true); }
public void GenerateObj(MazeGraph <T> G, bool ceil = false) { xsize = G.rows; ysize = G.cols; using (System.IO.StreamWriter file = new System.IO.StreamWriter(@Application.dataPath + "/Resources/objeto1.obj")) { for (int i = 0; i < G.rows; ++i) { for (int j = 0; j < G.cols; ++j) { sb.Clear(); V = new System.Numerics.Vector3[] { new System.Numerics.Vector3(i, j, 0), new System.Numerics.Vector3(i, j + 1, 0), new System.Numerics.Vector3(i + 1, j, 0), new System.Numerics.Vector3(i + 1, j + 1, 0), new System.Numerics.Vector3(i, j, 1), new System.Numerics.Vector3(i, j + 1, 1), new System.Numerics.Vector3(i + 1, j, 1), new System.Numerics.Vector3(i + 1, j + 1, 1) }; WriteWall(V, 0); if (!G.hasEdge(G.GetNode(i, j), G.GetNode(i + 1, j)) || i == G.rows - 1) { WriteWall(V, 1); } if (!G.hasEdge(G.GetNode(i, j), G.GetNode(i - 1, j)) || i == 0) { WriteWall(V, 2); } if (!G.hasEdge(G.GetNode(i, j), G.GetNode(i, j + 1)) || j == G.cols - 1) { WriteWall(V, 3); } if (!G.hasEdge(G.GetNode(i, j), G.GetNode(i, j - 1)) || j == 0) { WriteWall(V, 4); } if (ceil) { WriteWall(V, 5); } file.Write(sb.ToString()); } } } }
static public MazeGraph <T> Execute(MazeGraph <T> G) { initializeVariables(G); enqueueAdjEdges(G, 0); for (int i = 1; i < N; i++) { do { edge = queue.top(); queue.delete(); } while (added[edge.dest]); g.addEdge(edge.orig, edge.dest, edge.cost); g.addEdge(edge.dest, edge.orig, edge.cost); added[edge.dest] = true; enqueueAdjEdges(G, edge.dest); } return(g); }