private Maze(int width, int height, BitList northWalls, BitList westWalls, long startKey, long endKey) { Width = width; Height = height; _northWalls = northWalls; _westWalls = westWalls; Start = new Cell(this, startKey); End = new Cell(this, endKey); }
/// <summary> /// Create a copy of another maze. /// </summary> /// <param name="other">The other maze</param> public Maze(Maze other) { Width = other.Width; Height = other.Height; Start = new Cell(this, other.Start.Key); End = new Cell(this, other.End.Key); _northWalls = new BitList(other._northWalls); _westWalls = new BitList(other._westWalls); }
private static bool TryGetTailDir(Cell cell, out Direction tailDir) { var missing = 0; tailDir = default(Direction); foreach (var dir in Utils.Dirs) { if (!cell.HasWall(dir)) { missing++; tailDir = dir; } } return missing == 1; }
void CreateGrid(int size) { if (size >= 10) grid = new Cell[size, size]; else grid = new Cell[10, 10]; for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { grid[i, j] = new Cell(i, j); } } grid[0, 0].CType = CellTypes.Start; grid[0, 0].Visited = true; int size1 = grid.GetLength(0); int size2 = grid.GetLength(1); grid[size1 - 1, size2 - 1].CType = CellTypes.End; }
/// <summary> /// Set the state of the wall separating this cell and the other cell. /// </summary> /// <param name="other">The other cell</param> /// <param name="state">The state of the wall</param> public void SetMutualWall(Cell other, bool state) { Direction dir; if (TryGetAdjacentDirection(other, out dir)) { SetWall(dir, state); } else { throw new ArgumentException(); } }
/// <summary> /// Try to determine the direction an adjacent cell is relative to us. /// </summary> /// <param name="other">The other cell</param> /// <param name="dir">The direction</param> /// <returns>If the cell was really adjacent and so the direction is valid</returns> public bool TryGetAdjacentDirection(Cell other, out Direction dir) { if (_maze != other._maze) { throw new ArgumentException(); } return _maze.TryGetAdjacentDirection(_key, other._key, out dir); }
private static bool IsTail(Cell cell) { Direction tmp; return TryGetTailDir(cell, out tmp); }
private bool Visited(Cell current, Dir dir) { Cell neighbor = Neighbor(current, dir); if (neighbor == null) { return true; } else { return neighbor.Visited; } }
private void Remove(Cell current, Dir dir, Cell neighbor) { switch (dir) { case Dir.Right: neighbor.Left = false; break; case Dir.Left: current.Left = false; break; case Dir.Top: neighbor.Bottom = false; break; case Dir.Bottom: current.Bottom = false; break; default: throw new InvalidOperationException(); } }
private static bool IsSpecial(Cell cell) { return cell.Maze.Start == cell || cell.Maze.End == cell; }
private bool VisitedAllNeighbors(Cell c) { return (Visited(c, Dir.Right) && Visited(c, Dir.Top) && Visited(c, Dir.Left) && Visited(c, Dir.Bottom)); }
public bool Generate() { while (true) { if (_stack.Count == 0) { return false; } var cell = new Cell(_maze, _stack.Peek()); var offset = _rnd.Next(4); for (var i = 0; i < 4; i++) { Cell tmp; if (cell.TryMove(Utils.Dirs[(offset + i) % 4], out tmp)) { if (InBounds(tmp) && tmp.IsInactive()) { cell.ClearMutualWall(tmp); _mapping[(int)tmp.Key] = _id; _stack.Push(tmp.Key); return true; } } } _stack.Pop(); } }
/// <summary> /// Try to move the cell one unit in the given direction. /// </summary> /// <param name="dir">The direction</param> /// <param name="newCell">The moved cell</param> /// <returns>If the move was successful</returns> public bool TryMove(Direction dir, out Cell newCell) { long sKey; if (_maze.TryRelativeKey(_key, dir, out sKey)) { newCell = new Cell(_maze, sKey); return true; } else { newCell = default(Cell); return false; } }
public bool Equals(Cell other) { return object.Equals(this._maze, other._maze) && this._key == other._key; }
bool CheckMove(int x, int y, Cell[,] grind) { if (x >= 0 && y >= 0 && x < grid.GetLength(0) && y < grid.GetLength(1) && !grid[x, y].Visited) return true; else return false; }
void RemoveWalls(ref Cell c1, ref Cell c2) { if (c1.X == c2.X) { if (c1.Y < c2.Y) { c1.wS.Right = false; c2.wS.Left = false; } else { c1.wS.Left = false; c2.wS.Right = false; } } else if (c1. Y == c2.Y) { if (c1.X < c2.X) { c1.wS.Down = false; c2.wS.Up = false; } else { c1.wS.Up = false; c2.wS.Down = false; } } }
public bool Contains(Cell cell) { if (this.isFloor) { if (Cells[0] == cell) { return true; } else { return false; } } else { if (Cells[1] == cell) { return true; } else { return false; } } }
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("|"); //} } }
public Maze(int width, int height) { Width = width; Height = height; cell = new Cell[Width + 1, Height + 1]; for (int i = 0; i <= Width; i++) for (int j = 0; j <= Height; j++) cell[i, j] = new Cell(i, j); for (int i = 0; i <= Width; i++) { cell[i, Height].Visited = true; cell[i, Height].Left = false; } for (int j = 0; j <= Height; j++) { cell[Width, j].Visited = true; cell[Width, j].Bottom = false; } }
/// <summary> /// Cell the wall separating this cell and the other cell. /// </summary> /// <param name="other">The other cell</param> public void ClearMutualWall(Cell other) { SetMutualWall(other, false); }
public PathFinder(Cell start, Cell destination, BlockType[][] blocks) { reachable = new List<Node> {new Node(start)}; explored = new List<Node>(); this.start = start; this.destination = destination; this.blocks = blocks; }
private bool IsInBound(Cell cell) { return cell.Col >= 0 && cell.Row >= 0 && cell.Col < blocks[0].Length && cell.Row < blocks.Length; }
private bool IsLand(Cell cell) { return blocks[cell.Row][cell.Col] == BlockType.Land; }
public DFSGenerator(int id, Maze maze, int[] mapping, Random rnd, Cell cell, int x1, int y1, int x2, int y2) { _id = id; _rnd = rnd; _maze = maze; _mapping = mapping; _stack = new Stack<long>(); _stack.Push(cell.Key); cell.GetPosition(out _cx, out _cy); _mapping[(int)cell.Key] = _id; _x1 = x1; _y1 = y1; _x2 = x2; _y2 = y2; }
public Node(Cell cell) { this.Cell = cell; this.Previous = null; }
private bool InBounds(Cell cell) { int x, y; cell.GetPosition(out x, out y); return _x1 < x && x < _x2 && _y1 < y && y < _y2; }
private Cell Neighbor(Cell current, Dir dir) { switch (dir) { case Dir.Right: if (0 <= current.X && current.X < Width) return cell[current.X + 1, current.Y]; break; case Dir.Left: if (0 < current.X && current.X <= Width) return cell[current.X - 1, current.Y]; break; case Dir.Top: if (0 <= current.Y && current.Y < Height) return cell[current.X, current.Y + 1]; break; case Dir.Bottom: if (0 < current.Y && current.Y <= Height) return cell[current.X, current.Y - 1]; break; } return null; }