public string generate() { lsymbols.Append("++G++"); int x = Random.Range(0, gridx - 1), y = Random.Range(0, gridy - 1); insertIntoBuffer('F'); CaretTree.Node current = new CaretTree.Node(lsymbols.ToString(), x, y); tree.AddNode(null, null, current); int total = gridx * gridy; while (visitedCount < total) { ClearBuffer(); x = current.x; y = current.y; current = walk(current.x, current.y); if (current == null) { Debug.Log(x + " " + y); ClearBuffer(); current = hunt(); } } return(tree.Combine()); }
private CaretTree.Node hunt() { int [] indices = permutation(4); int nx, ny; for (int j = 0; j < gridy; ++j) { for (int i = 0; i < gridx; ++i) { if (!visited[i, j]) { for (int d = 0; d < 4; ++d) { nx = i + dx[(int)directions[indices[d]]]; ny = j + dy[(int)directions[indices[d]]]; if (nx > -1 && nx < gridx && ny > -1 && ny < gridy && visited[nx, ny]) { direction = directionMap[nx, ny]; correctDirection(opposite[(int)directions[indices[d]]]); insertIntoBuffer('F'); CaretTree.Node newNode = new CaretTree.Node(lsymbols.ToString(), i, j); prev = parent = tree.GetNode(nx, ny); head = newNode; tree.AddNode(parent, null, newNode); return(newNode); } } } } } return(null); }
private CaretTree.Node walk(int x, int y) { if (visited[x, y]) { Debug.Assert(false); return(null); } int [] indices = permutation(4); CaretTree.Node newNode = null; bool found = false; directionMap[x, y] = direction; visited[x, y] = true; ++visitedCount; setWalls(x, y, indices); int nx = 0, ny = 0; //Find neighboring point in grid that hasn't been visited for (int i = 0; i < 4; ++i) { nx = x + dx[(int)directions[indices[i]]]; ny = y + dy[(int)directions[indices[i]]]; if (nx > -1 && nx < gridx && ny > -1 && ny < gridy && !visited[nx, ny]) { correctDirection(directions[indices[i]]); found = true; break; } } if (found) { insertIntoBuffer('F'); newNode = new CaretTree.Node(lsymbols.ToString(), nx, ny); tree.AddNode(parent, head, newNode); prev = tree.GetNode(x, y); } else { CaretTree.Node leftovers = new CaretTree.Node(lsymbols.ToString(), -1, -1); tree.AddNode(parent, head, leftovers); } return(newNode); }
public void Awake() { direction = Direction.N; visited = new bool[gridx, gridy]; dx = new int[4] { 0, -1, 1, 0 }; dy = new int[4] { 1, 0, 0, -1 }; directions = new Direction[4] { Direction.N, Direction.W, Direction.E, Direction.S }; opposite = new Direction[4] { Direction.S, Direction.E, Direction.W, Direction.N }; tree = new CaretTree(); prev = head = parent = null; lsymbols = new StringBuilder(); directionMap = new Direction[gridx, gridy]; }