public OrthogonalGrid On(OrthogonalGrid grid) { foreach (OrthogonalCell cell in grid.Cells()) { List<Cell> neighbours = new List<Cell>(); if (cell.North != null) { neighbours.Add(cell.North); } if (cell.East != null) { neighbours.Add(cell.East); } if (neighbours.Count > 0) { var index = rnd.Next(neighbours.Count); var neighbour = neighbours[index]; cell.Link(neighbour); } } return grid; }
private static void DeadEndCounts() { string Namespace = typeof(BinaryTree).Namespace; var algorithms = new[] { "BinaryTree", "Sidewinder", "AldousBroder", "Wilsons", "HuntAndKill"} ; const int tries = 100; const int size = 20; var averages = new Dictionary<string, int>(); foreach (var algorithm in algorithms) { Console.WriteLine($"running {algorithm}"); var deadEndCounts = new List<int>(); for (int i = 0; i < tries; i++) { var grid = new OrthogonalGrid(size, size); dynamic alg = Activator.CreateInstance(Type.GetType(Namespace + "." + algorithm)); alg.On(grid); deadEndCounts.Add(grid.DeadEnds().Count); } var totalDeadEnds = deadEndCounts.Sum(); averages[algorithm] = totalDeadEnds / deadEndCounts.Count; } const int totalCells = size * size; Console.WriteLine(); Console.WriteLine($"Average dead-ends per {size}x{size} maze ({totalCells} cells):"); Console.WriteLine(); foreach (var alg in averages.OrderByDescending(a => a.Value)) { var percentage = alg.Value / (decimal)totalCells; Console.WriteLine($"{alg.Key,14} : {alg.Value,3}/{totalCells} ({percentage:p})"); } }