1 private static int[,] mazeMap = new int[13 + 2, 25 + 2];//0<=x<=12 0<=y<=24 2 private static Random Rd = new Random();
首先声明mazeMap存储数据,声明了一个15*27的迷宫,其中最外面的一圈是用来做墙的。
1 public void GenerateMap() { 3 //初始化 4 for (int i = 0; i <= 13 + 1; i++) { 5 for (int j = 0; j <= 25 + 1; j++) { 6 if (i == 0 || i == 13 + 1 || j == 0 || j == 25 + 1) //初始化迷宫 7 mazeMap[i, j] = 0; 8 else 9 mazeMap[i, j] = 1; 10 } 11 }
这一块是初始化墙和内部的构造。其中我们约定,0是路,1是墙。
接下来贴出之后的算法。
1 public void GenerateMap() { 2 for (int i = 0; i <= 13 + 1; i++) { 3 for (int j = 0; j <= 25 + 1; j++) { 4 if (i == 0 || i == 13 + 1 || j == 0 || j == 25 + 1) //初始化迷宫 5 mazeMap[i, j] = 0; 6 else 7 mazeMap[i, j] = 1; 8 } 9 } 10 11 12 13 Create(2 * (Rd.Next() % (13 / 2) + 1), 2 * (Rd.Next() % (25 / 2) + 1)); //从随机一个点开始生成迷宫,该点行列都为偶数 14 for (int i = 0; i <= 13 + 1; i++) //边界处理 15 { 16 mazeMap[i, 0] = 1; 17 mazeMap[i, 25 + 1] = 1; 18 } 19 20 for (int j = 0; j <= 25 + 1; j++) //边界处理 21 { 22 mazeMap[0, j] = 1; 23 mazeMap[13 + 1, j] = 1; 24 } 25 mazeMap[2, 1] = 2; //给定入口 26 mazeMap[13 - 1, 25] = 2; //给定出口 27 }
1 public void Create(int x, int y) //随机生成迷宫 2 { 3 int[,] c = new int[4, 2] { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; //四个方向 4 for (int i = 0; i < 4; i++) 5 { 6 int j = Rd.Next() % 4; 7 8 int t = c[i, 0]; 9 c[i, 0] = c[j, 0]; 10 c[j, 0] = t; 11 t = c[i, 1]; 12 c[i, 1] = c[j, 1]; 13 c[j, 1] = t; 14 } 15 mazeMap[x, y] = 0; 16 for (int i = 0; i < 4; i++) 17 if (mazeMap[x + 2 * c[i, 0], y + 2 * c[i, 1]] == 1) { 18 mazeMap[x + c[i, 0], y + c[i, 1]] = 0; 19 20 Create(x + 2 * c[i, 0], y + 2 * c[i, 1]); 21 } 22 }
审视这段代码,如果我们使用一些技巧,代码可读性将会更好,其中的13都是Height,迷宫高度;25都是Width,迷宫宽度,当读到13和25时,可以自动代入Height与Width去理解。
算法的原理很简单,就是递归生成迷宫。
1 Create(x + 2 * c[i, 0], y + 2 * c[i, 1]);
这一行就是关键递归代码。
参考文献:http://wenku.baidu.com/view/f22455126edb6f1aff001f13.html
虽说是百度文库的,但是值得一读。
下面贴出完整代码。
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Collections.ObjectModel; 5 using System.IO; 6 using System.Linq; 7 using System.Runtime.CompilerServices; 8 using System.Runtime.InteropServices.WindowsRuntime; 9 using Windows.Devices.Bluetooth.Advertisement; 10 using Windows.Foundation; 11 using Windows.Foundation.Collections; 12 using Windows.Security.Cryptography.Core; 13 using Windows.UI.Xaml; 14 using Windows.UI.Xaml.Controls; 15 using Windows.UI.Xaml.Controls.Primitives; 16 using Windows.UI.Xaml.Data; 17 using Windows.UI.Xaml.Input; 18 using Windows.UI.Xaml.Media; 19 using Windows.UI.Xaml.Navigation; 20 21 // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 22 23 namespace Maze 24 { 25 /// <summary> 26 /// An empty page that can be used on its own or navigated to within a Frame. 27 /// </summary> 28 public sealed partial class MainPage : Page 29 { 30 private static int[,] mazeMap = new int[13 + 2, 25 + 2];//0<=x<=12 0<=y<=24 31 private static Random Rd = new Random(); 32 public static List<Rect> Rects { get; set; } 33 34 public MainPage() { 35 this.InitializeComponent(); 36 37 Rects = new List<Rect>(); 38 39 GenerateMap(); 40 for (int x = 0; x < 325; x++)//25*14 41 { 42 var rect = new Rect() { X = x / 25, Y = x % 25 }; 43 switch (mazeMap[rect.X + 1, rect.Y + 1]) { 44 case 0: 45 rect.Color = "Blue"; 46 break; 47 case 1: 48 rect.Color = "Gray"; 49 break; 50 case 2: 51 rect.Color = "Red"; 52 break; 53 case 3: 54 rect.Color = "Yellow"; 55 break; 56 57 } 58 Rects.Add(rect); 59 } 60 GridView.ItemsSource = Rects; 61 62 63 } 64 65 66 67 68 public class Rect 69 { 70 public int X { get; set; } 71 public int Y { get; set; }//以左上角为1,1 72 public string Color { get; set; } 73 } 74 75 public void GenerateMap() { 76 for (int i = 0; i <= 13 + 1; i++) { 77 for (int j = 0; j <= 25 + 1; j++) { 78 if (i == 0 || i == 13 + 1 || j == 0 || j == 25 + 1) //初始化迷宫 79 mazeMap[i, j] = 0; 80 else 81 mazeMap[i, j] = 1; 82 } 83 } 84 85 86 87 Create(2 * (Rd.Next() % (13 / 2) + 1), 2 * (Rd.Next() % (25 / 2) + 1)); //从随机一个点开始生成迷宫,该点行列都为偶数 88 for (int i = 0; i <= 13 + 1; i++) //边界处理 89 { 90 mazeMap[i, 0] = 1; 91 mazeMap[i, 25 + 1] = 1; 92 } 93 94 for (int j = 0; j <= 25 + 1; j++) //边界处理 95 { 96 mazeMap[0, j] = 1; 97 mazeMap[13 + 1, j] = 1; 98 } 99 mazeMap[2, 1] = 2; //给定入口 100 mazeMap[13 - 1, 25] = 2; //给定出口 101 } 102 103 public void Create(int x, int y) //随机生成迷宫 104 { 105 int[,] c = new int[4, 2] { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } }; //四个方向 106 for (int i = 0; i < 4; i++) 107 { 108 int j = Rd.Next() % 4; 109 110 int t = c[i, 0]; 111 c[i, 0] = c[j, 0]; 112 c[j, 0] = t; 113 t = c[i, 1]; 114 c[i, 1] = c[j, 1]; 115 c[j, 1] = t; 116 } 117 mazeMap[x, y] = 0; 118 for (int i = 0; i < 4; i++) 119 if (mazeMap[x + 2 * c[i, 0], y + 2 * c[i, 1]] == 1) { 120 mazeMap[x + c[i, 0], y + c[i, 1]] = 0; 121 122 Create(x + 2 * c[i, 0], y + 2 * c[i, 1]); 123 } 124 } 125 }