//迷宫类相关
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
namespace MazeDemo
{
///
1<summary>
2/// 迷宫类
3/// </summary>
public class CMaze
{
bool[,] mg; //地图格子
Stack stack; //堆栈
Point in_p; //入口点
Point out_p; //出口点
Point start_p; //绘制迷时候的起始点
Size boxsize; //每个格子的大小
int step_count; //共走多少步
public CMaze()
{
stack=new Stack();
this.start_p=new Point(0,0);
this.boxsize=new Size(50,50);
step_count=0;
}
public CMaze(bool[,] _mg):this()
{
this.mg=_mg;
}
public CMaze(bool[,] _mg,Point _in,Point _out):this()
{
this.mg=_mg;
this.in_p=_in;
this.out_p=_out;
Stack way=this.Test(this.in_p,_in);
stack.Push(new CCoor(this.in_p,way));
this.step_count++;
}
///
1<summary>
2/// 绘制迷宫时窗口的起始坐标
3/// </summary>
public Point StartPoint
{
set{this.start_p=value;}
get{return this.start_p;}
}
///
1<summary>
2/// 当前迷宫共走多少步
3/// </summary>
public int StepCount
{
get{return this.step_count;}
}
///
1<summary>
2/// 迷宫格子大小
3/// </summary>
public Size BoxSize
{
set{this.boxsize=value;}
get{return this.boxsize;}
}
///
1<summary>
2/// 堆栈数据个数
3/// </summary>
public int StackCount
{
get{return this.stack.Count;}
}
///
1<summary>
2/// 绘制迷宫
3/// </summary>
///
1<param name="g"/>
public void DrawBox(Graphics g)
{
for(int i=0;i
1<mg.getlength(0);i++) <summary="" brush="new" brush;="" else="" for(int="" g.fillrectangle(brush,rect);="" if(mg[i,j])="" j="0;j<mg.GetLength(1);j++)" point="" point((j*boxsize.width)+startpoint.x,(i*boxsize.height)+startpoint.y);="" pp="new" rect="new" rectangle="" rectangle(pp,boxsize);="" solidbrush="" solidbrush(color.green);="" solidbrush(color.red);="" {="" }="" 位置="">
2/// 绘制所走线路
3///
4/// <param name="g"/>
5public void DrawPath(Graphics g)
6{
7IEnumerator myEnumerator = stack.GetEnumerator();
8while ( myEnumerator.MoveNext() )
9{
10CCoor c=new CCoor();
11c=(CCoor)myEnumerator.Current;
12Point pp=new Point((c.CurrentPoint.Y*BoxSize.Width)+StartPoint.X,(c.CurrentPoint.X*BoxSize.Height)+StartPoint.Y);
13SolidBrush brush=new SolidBrush(Color.Blue);
14Rectangle rect=new Rectangle(pp,BoxSize);
15g.FillRectangle(brush,rect);
16}
17}
18
19/// <summary>
20/// 绘制当前位置的可行路径
21/// </summary>
22/// <param name="g"/>
23public void DrawNextPath(Graphics g)
24{
25CCoor c=(CCoor)this.stack.Peek();
26Stack s=c.WayPath;
27IEnumerator myEnumerator=s.GetEnumerator();
28while(myEnumerator.MoveNext())
29{
30Point p=(Point)myEnumerator.Current;
31Point pp=new Point((p.Y*BoxSize.Width)+StartPoint.X,(p.X*BoxSize.Height)+StartPoint.Y);
32SolidBrush brush=new SolidBrush(Color.Yellow);
33Rectangle rect=new Rectangle(pp,BoxSize);
34g.FillRectangle(brush,rect);
35}
36}
37
38/// <summary>
39/// 判断迷宫是否走完
40/// </summary>
41/// <returns></returns>
42public bool IsEnd()
43{
44CCoor coor=(CCoor)this.stack.Peek(); //当前位置信息
45if( coor.CurrentPoint.X==this.out_p.X && coor.CurrentPoint.Y==this.out_p.Y )
46return true;
47else
48return false;
49}
50
51/// <summary>
52/// 走一迷宫中的一个格子
53/// </summary>
54/// <returns>数字状态</returns>
55public int Step()
56{
57CCoor coor=(CCoor)this.stack.Peek(); //当前位置信息
58//是否到达出口
59if(!(coor.CurrentPoint.X==this.out_p.X&&coor.CurrentPoint.Y==this.out_p.Y))
60{
61Stack ss=coor.WayPath;
62if(ss.Count==0)
63{
64this.stack.Pop();
65return 0;
66}
67Point p=(Point)ss.Pop(); //当前位置可继续移动的下一个位置
68if(p.X==this.out_p.X&&p.Y==this.out_p.Y)
69{
70this.stack.Push(new CCoor(p,new Stack()));
71return 0;
72}
73Stack st=this.Test(p,coor.CurrentPoint); //得到下一个可移动位置的所有可移动位置
74if(st.Count==0)
75{
76return 0;
77}
78CCoor newcoor=new CCoor(p,st); //建立新的位置信息
79this.stack.Push(newcoor); //压入堆栈
80this.step_count++; //所走步骤加1
81return 0;
82}
83else
84return 1;
85}
86
87/// <summary>
88/// 走迷宫
89/// </summary>
90public void Run()
91{
92while(this.Step()!=1);
93}
94
95/// <summary>
96/// 回复到迷宫起点
97/// </summary>
98public void Reset()
99{
100this.stack.Clear();
101Stack way=this.Test(this.in_p,this.in_p);
102stack.Push(new CCoor(this.in_p,way));
103this.step_count=1;
104}
105
106/// <summary>
107/// 探测可行路线
108/// 探测顺序 右->前->左->后
109/// 左
110/// |
111/// 后--+-->前
112/// |
113/// 右
114/// </summary>
115/// <param name="p"/>从当前点查询四周是否有可行路线
116/// <param name="perv_p"/>先前的路线
117/// <returns>可行路线堆栈</returns>
118public Stack Test(Point p,Point perv_p)
119{
120Stack stack_way=new Stack(); //该点可行位置堆栈
121int x,y;
122
123//后
124x=p.X;
125y=p.Y-1;
126this.Signpost(x,y,stack_way,perv_p);
127
128//左
129x=p.X-1;
130y=p.Y;
131this.Signpost(x,y,stack_way,perv_p);
132
133//前
134x=p.X;
135y=p.Y+1;
136this.Signpost(x,y,stack_way,perv_p);
137
138//右
139x=p.X+1;
140y=p.Y;
141this.Signpost(x,y,stack_way,perv_p);
142
143return stack_way;
144}
145
146/// <summary>
147/// 判断该方向是否可行,可行则将信息压入堆栈,只在Test()函数中调用
148/// </summary>
149/// <param name="x"/>x坐标
150/// <param name="y"/>y坐标
151/// <param name="s"/>堆栈
152/// <param name="perv_p"/>来时候的方向
153private void Signpost(int x,int y,Stack s,Point perv_p)
154{
155if( (x>=0 && x<this.mg.getlength(0)) &&="" (y="">=0 && y<this.mg.getlength(1)) )="" <summary="" if(this.mg[x,y]&&!(x="perv_p.X&&y==perv_p.Y))" point(x,y));="" s.push(new="" {="" }="">
156/// 迷宫简图
157///
158/// <returns>字符地图</returns>
159public override string ToString()
160{
161string str="";
162for(int i=0;i<mg.getlength(0);i++) ;="" <summary="" else="" for(int="" if(this.mg[i,j])="" j="0;j<mg.GetLength(1);j++)" return="" str+="\n" str;="" {="" }="">
163/// 当前坐标信息,和可走方向坐标
164///
165public class CCoor
166{
167private Point curr_p; //当前坐标
168private Stack way; //可走方向坐标
169
170public CCoor()
171{
172//...
173}
174
175public CCoor(Point p,Stack w)
176{
177curr_p=p;
178way=w;
179}
180
181public Point CurrentPoint
182{
183get{return this.curr_p;}
184set{this.curr_p=value;}
185}
186
187public Stack WayPath
188{
189set{this.way=value;}
190get{return this.way;}
191}
192}
193}</mg.getlength(0);i++)></this.mg.getlength(1))></this.mg.getlength(0))></mg.getlength(0);i++)>