public void ComputeRoom(Room room, List<Room> visitedRooms, Stack<Room> computationStack, Random rand) { // mark room as visited visitedRooms.Add(room); // has unvisited neighbor? List<Connection> connections = room.GetUnvisitedNeighbors(visitedRooms); if (connections.Count > 0) { // choose random neighbor int randomIndex = rand.Next(connections.Count); Room neighbor = connections[randomIndex].Neighbor; // add room to stack computationStack.Push(room); // remove walls between room and neighbor int direction = connections[randomIndex].Direction; room.Walls[direction] = false; neighbor.Walls[(direction + 2) % 4] = false; // computer neighbor room this.ComputeRoom(neighbor, visitedRooms, computationStack, rand); } else { if (computationStack.Count > 0) { // remove first stack room room = computationStack.Pop(); // backtrack this.ComputeRoom(room, visitedRooms, computationStack, rand); } } }
public static Maze CreateMaze() { Room room1 = new Room(1); Room room2 = new Room(2); Door door = new Door(room1, room2); room1.SetSide(Direction.North, new Wall()); room1.SetSide(Direction.East, door); room1.SetSide(Direction.South, new Wall()); room1.SetSide(Direction.West, new Wall()); room2.SetSide(Direction.North, new Wall()); room2.SetSide(Direction.East, new Wall()); room2.SetSide(Direction.South, new Wall()); room2.SetSide(Direction.West, door); Maze maze = new Maze(); maze.AddRoom(room1); maze.AddRoom(room2); return maze; }
public Connection(Room neighbor, int direction) { this.Neighbor = neighbor; this.Direction = direction; }
private void CreateRooms() { for (int i = 0; i < _width * _height; ++i) { _rooms[i] = new Room(); } }
public static void Generate(int width, int height) { int NumberOfCells, NumberOfWalls, NumberOfSets; int i, j, z, w; Cell[] cells; Wall[] walls; Room[] rooms; NumberOfCells = width * height; NumberOfWalls = ((width * (height - 1)) + (height * (width - 1))); NumberOfSets = NumberOfCells; cells = new Cell[NumberOfCells]; walls = new Wall[NumberOfWalls]; rooms = new Room[NumberOfSets]; // Create each Cell with its default ID in the Cells array for (i = 0; i < NumberOfCells; i++) { cells[i] = new Cell(i); //debug // Console.WriteLine(cells[i]); } // Create each with its default ID in the Sets array for (i = 0; i < NumberOfSets; i++) { rooms[i] = new Room(i, NumberOfCells); rooms[i].Cells[0] = cells[i]; // debug // Console.WriteLine(rooms[i]); } // Create each wall with its default ID in the walls array for (i = 0; i < NumberOfWalls; i++) { walls[i] = new Wall(i); } // Algorithm discovers if a wall is vertical // based on that each row starts by a multiple // of (width * 2) - 1 for (i = 0; i < NumberOfWalls; i++) { if (i == 0 || (i % ((width * 2) - 1) == 0)) { for (j = 0; j < (width - 1); j++) { walls[i + j].isFloor = false; } } // debug // Console.WriteLine(walls[i]); } // Now add two rooms to each wall // based on if it's a floor or not // need refactoring for (i = 0, z = 0, j = 0, w = width; i < NumberOfWalls; i++) { // Checks if it's a floor // and then, floor walls will begin // to increase from 0 and width // by 1 each time (width = 4 -> 0,4 (+1,+1) -> 1,5) if (walls[i].isFloor) { walls[i].Cells[0] = cells[j]; walls[i].Cells[1] = cells[w]; // debug // Console.WriteLine(walls[i] + " received " + j + " and " + w); j++; w++; } // And if it's not // it starts as 0 and 0+1 // but on the beginning of it each new row // (multiple of width*2 -1) // it increases +1 else { if (i != 0 && (i % ((width * 2) - 1) == 0)) { z++; } walls[i].Cells[0] = cells[z]; walls[i].Cells[1] = cells[z + 1]; // debug // Console.WriteLine(walls[i] + " received " + z + " and " + (z + 1)); z++; } } for (i = 0; i < width; i++) { Console.Write(" " + "_"); } foreach (Cell cell in cells) { if (cell.id % width == 0) { Console.WriteLine(" "); Console.Write("|"); } if (cell.id > (width * (height - 1)) || cell.id == NumberOfCells - 1) { Console.Write("_"); } foreach (Wall wall in walls) { if (wall.Contains(cell)) { if (wall.isFloor) { Console.Write("_"); } else { Console.Write("|"); } } } //if (cell.id % width == 1) //{ // Console.Write("|"); //} } }