public Grid<Cell> On(Grid<Cell> grid) { var current = grid.RandomCell(); while (current != null) { var unvisitedNeighbours = current.Neighbours().Where(n => n.Links.Count == 0).ToList(); if (unvisitedNeighbours.Any()) { var neighbour = unvisitedNeighbours[rand.Next(unvisitedNeighbours.Count())]; current.Link(neighbour); current = neighbour; } else { current = null; foreach (var cell in grid.Cells()) { var visitedNeighbours = cell.Neighbours().Where(n => n.Links.Any()).ToList(); if (cell.Links.Count == 0 && visitedNeighbours.Any()) { current = cell; var neighbour = visitedNeighbours[rand.Next(visitedNeighbours.Count())]; current.Link(neighbour); break; } } } } return grid; }
static void Main(string[] args) { Grid test; test = new Grid(); test.setUpGame(); test.startGame(); }
static void Main(string[] args) { Robot robot = new Robot(); Grid grid = new Grid(5, 5, robot); BaseView view = new ConsoleGridView(grid); //Can use one of these controllers // BaseRemoteControl remote = new ConsoleKeyboardController(view); BaseRemoteControl remote = new ConsoleCommandController(view); remote.OnError += step_OnError; remote.TurnOn(); }
public Grid<Cell> On(Grid<Cell> grid) { var unvisited = new List<Cell>(); unvisited.AddRange(grid.Cells()); var first = unvisited[rand.Next(unvisited.Count)]; unvisited.Remove(first); Debug.Print($"{first.Row},{first.Column}"); while (unvisited.Count > 0) { var cell = unvisited[rand.Next(unvisited.Count)]; Debug.Print($"cell:{cell.Row},{cell.Column}"); var path = new List<Cell>() { cell }; while (unvisited.Contains(cell)) { cell = cell.Neighbours()[rand.Next(cell.Neighbours().Count)]; Debug.Print($"neighbour cell:{cell.Row},{cell.Column}"); var position = path.IndexOf(cell); if (position > -1) { //add one to position as zero based. path = path.GetRange(0, position + 1); } else { path.Add(cell); } } for (int i = 0; i <= path.Count - 2; i++) { path[i].Link(path[i + 1]); unvisited.Remove(path[i]); } } return grid; }
public Grid<OrthogonalCell> On(Grid<OrthogonalCell> grid) { for (int x = 0; x < grid.Rows; x++) { var run = new List<OrthogonalCell>(); for (int y = 0; y < grid.Columns; y++) { var cell = grid[x, y]; run.Add(cell); bool at_eastern_boundary = (cell.East == null); bool at_northern_boundary = (cell.North == null); bool should_close_out = at_eastern_boundary || (!at_northern_boundary && rand.Next(2) == 0); if (should_close_out) { var member = run[rand.Next(run.Count)]; run.Remove(member); if (member.North != null) { member.Link(member.North); } run.Clear(); } else { cell.Link(cell.East); } } } return grid; }