走迷宫C#版(一)
http://www.webstudy8.com/2006/12/net16073.html
//迷宫类相关
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
namespace MazeDemo
{
/// <summary>
/// 迷宫类
/// </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++;
}
/// <summary>
/// 绘制迷宫时窗口的起始坐标
/// </summary>
public Point StartPoint
{
set{this.start_p=value;}
get{return this.start_p;}
}
/// <summary>
/// 当前迷宫共走多少步
/// </summary>
public int StepCount
{
get{return this.step_count;}
}
/// <summary>
/// 迷宫格子大小
/// </summary>
public Size BoxSize
{
set{this.boxsize=value;}
get{return this.boxsize;}
}
/// <summary>
/// 堆栈数据个数
/// </summary>
public int StackCount
{
get{return this.stack.Count;}
}
/// <summary>
/// 绘制迷宫
/// </summary>
/// <param name="g"></param>
public void DrawBox(Graphics g)
{
for(int i=0;i<mg.GetLength(0);i++)
{
for(int j=0;j<mg.GetLength(1);j++)
{
Point pp=new Point((j*BoxSize.Width)+StartPoint.X,(i*BoxSize.Height)+StartPoint.Y); //位置
SolidBrush brush;
Rectangle rect=new Rectangle(pp,BoxSize);
if(mg[i,j])
brush=new SolidBrush(Color.Green);
else
brush=new SolidBrush(Color.Red);
g.FillRectangle(brush,rect);
}
}
}
/// <summary>
/// 绘制所走线路
/// </summary>
/// <param name="g"></param>
public void DrawPath(Graphics g)
{
IEnumerator myEnumerator = stack.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
CCoor c=new CCoor();
c=(CCoor)myEnumerator.Current;
Point pp=new Point((c.CurrentPoint.Y*BoxSize.Width)+StartPoint.X,(c.CurrentPoint.X*BoxSize.Height)+StartPoint.Y);
SolidBrush brush=new SolidBrush(Color.Blue);
Rectangle rect=new Rectangle(pp,BoxSize);
g.FillRectangle(brush,rect);
}
}
/// <summary>
/// 绘制当前位置的可行路径
/// </summary>
/// <param name="g"></param>
public void DrawNextPath(Graphics g)
{
CCoor c=(CCoor)this.stack.Peek();
Stack s=c.WayPath;
IEnumerator myEnumerator=s.GetEnumerator();
while(myEnumerator.MoveNext())
{
Point p=(Point)myEnumerator.Current;
Point pp=new Point((p.Y*BoxSize.Width)+StartPoint.X,(p.X*BoxSize.Height)+StartPoint.Y);
SolidBrush brush=new SolidBrush(Color.Yellow);
Rectangle rect=new Rectangle(pp,BoxSize);
g.FillRectangle(brush,rect);
}
}
/// <summary>
/// 判断迷宫是否走完
/// </summary>
/// <returns></returns>
public bool IsEnd()
{
CCoor coor=(CCoor)this.stack.Peek(); //当前位置信息
if( coor.CurrentPoint.X==this.out_p.X && coor.CurrentPoint.Y==this.out_p.Y )
return true;
else
return false;
}
/// <summary>
/// 走一迷宫中的一个格子
/// </summary>
/// <returns>数字状态</returns>
public int Step()
{
CCoor coor=(CCoor)this.stack.Peek(); //当前位置信息
//是否到达出口
if(!(coor.CurrentPoint.X==this.out_p.X&&coor.CurrentPoint.Y==this.out_p.Y))
{
Stack ss=coor.WayPath;
if(ss.Count==0)
{
this.stack.Pop();
return 0;
}
Point p=(Point)ss.Pop(); //当前位置可继续移动的下一个位置
if(p.X==this.out_p.X&&p.Y==this.out_p.Y)
{
this.stack.Push(new CCoor(p,new Stack()));
return 0;
}
Stack st=this.Test(p,coor.CurrentPoint); //得到下一个可移动位置的所有可移动位置
if(st.Count==0)
{
return 0;
}
CCoor newcoor=new CCoor(p,st); //建立新的位置信息
this.stack.Push(newcoor); //压入堆栈
this.step_count++; //所走步骤加1
return 0;
}
else
return 1;
}
/// <summary>
/// 走迷宫
/// </summary>
public void Run()
{
while(this.Step()!=1);
}
/// <summary>
/// 回复到迷宫起点
/// </summary>
public void Reset()
{
this.stack.Clear();
Stack way=this.Test(this.in_p,this.in_p);
stack.Push(new CCoor(this.in_p,way));
this.step_count=1;
}
/// <summary>
/// 探测可行路线
/// 探测顺序 右->前->左->后
/// 左
/// |
/// 后--+-->前
/// |
/// 右
/// </summary>
/// <param name="p">从当前点查询四周是否有可行路线</param>
/// <param name="perv_p">先前的路线</param>
/// <returns>可行路线堆栈</returns>
public Stack Test(Point p,Point perv_p)
{
Stack stack_way=new Stack(); //该点可行位置堆栈
int x,y;
//后
x=p.X;
y=p.Y-1;
this.Signpost(x,y,stack_way,perv_p);
//左
x=p.X-1;
y=p.Y;
this.Signpost(x,y,stack_way,perv_p);
//前
x=p.X;
y=p.Y+1;
this.Signpost(x,y,stack_way,perv_p);
//右
x=p.X+1;
y=p.Y;
this.Signpost(x,y,stack_way,perv_p);
return stack_way;
}
/// <summary>
/// 判断该方向是否可行,可行则将信息压入堆栈,只在Test()函数中调用
/// </summary>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
/// <param name="s">堆栈</param>
/// <param name="perv_p">来时候的方向</param>
private void Signpost(int x,int y,Stack s,Point perv_p)
{
if( (x>=0 && x<this.mg.GetLength(0)) && (y>=0 && y<this.mg.GetLength(1)) )
{
if(this.mg[x,y]&&!(x==perv_p.X&&y==perv_p.Y))
s.Push(new Point(x,y));
}
}
/// <summary>
/// 迷宫简图
/// </summary>
/// <returns>字符地图</returns>
public override string ToString()
{
string str="";
for(int i=0;i<mg.GetLength(0);i++)
{
for(int j=0;j<mg.GetLength(1);j++)
{
if(this.mg[i,j])
str+="□";
else
str+="■";
}
str+="/n";
}
return str;
}
}
/// <summary>
/// 当前坐标信息,和可走方向坐标
/// </summary>
public class CCoor
{
private Point curr_p; //当前坐标
private Stack way; //可走方向坐标
public CCoor()
{
//...
}
public CCoor(Point p,Stack w)
{
curr_p=p;
way=w;
}
public Point CurrentPoint
{
get{return this.curr_p;}
set{this.curr_p=value;}
}
public Stack WayPath
{
set{this.way=value;}
get{return this.way;}
}
}
}
走迷宫C#版(二)
//窗体,调用...
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace MazeDemo
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
///
private CMaze mymaze;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button btnAbout;
private System.Windows.Forms.Button btnRun;
private System.Windows.Forms.Button btnReset;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Button button3;
private System.ComponentModel.IContainer components=null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
bool[,] _mg=new bool[,]{{true,false,false,true,false,false,false},
{true,true,true,true,true,true,true},
{false,false,true,false,true,false,true},
{false,false,true,false,true,false,false},
{false,true,true,false,true,false,false},
{true,true,false,false,true,true,false},
{false,false,false,false,false,true,true}
};
mymaze=new CMaze(_mg,new Point(0,0),new Point(6,6));
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.btnAbout = new System.Windows.Forms.Button();
this.btnRun = new System.Windows.Forms.Button();
this.btnReset = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.label8 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.button3 = new System.Windows.Forms.Button();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(360, 288);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Info";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(360, 192);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "Step";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// btnAbout
//
this.btnAbout.Location = new System.Drawing.Point(360, 320);
this.btnAbout.Name = "btnAbout";
this.btnAbout.TabIndex = 2;
this.btnAbout.Text = "About";
this.btnAbout.Click += new System.EventHandler(this.btnAbout_Click);
//
// btnRun
//
this.btnRun.Location = new System.Drawing.Point(360, 160);
this.btnRun.Name = "btnRun";
this.btnRun.TabIndex = 3;
this.btnRun.Text = "Run";
this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
//
// btnReset
//
this.btnReset.Location = new System.Drawing.Point(360, 256);
this.btnReset.Name = "btnReset";
this.btnReset.TabIndex = 4;
this.btnReset.Text = "Reset";
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// panel1
//
this.panel1.Controls.Add(this.label8);
this.panel1.Controls.Add(this.label7);
this.panel1.Controls.Add(this.label6);
this.panel1.Controls.Add(this.label5);
this.panel1.Controls.Add(this.label4);
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(360, 8);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(72, 144);
this.panel1.TabIndex = 5;
//
// label8
//
this.label8.Location = new System.Drawing.Point(32, 120);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(32, 16);
this.label8.TabIndex = 7;
this.label8.Text = "墙壁";
//
// label7
//
this.label7.BackColor = System.Drawing.Color.Red;
this.label7.Location = new System.Drawing.Point(8, 112);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(20, 20);
this.label7.TabIndex = 6;
//
// label6
//
this.label6.Location = new System.Drawing.Point(32, 88);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(32, 16);
this.label6.TabIndex = 5;
this.label6.Text = "已走";
//
// label5
//
this.label5.BackColor = System.Drawing.Color.Blue;
this.label5.Location = new System.Drawing.Point(8, 80);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(20, 20);
this.label5.TabIndex = 4;
//
// label4
//
this.label4.Location = new System.Drawing.Point(32, 56);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(32, 16);
this.label4.TabIndex = 3;
this.label4.Text = "可选方向";
//
// label3
//
this.label3.BackColor = System.Drawing.Color.Yellow;
this.label3.Location = new System.Drawing.Point(8, 48);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(20, 20);
this.label3.TabIndex = 2;
//
// label2
//
this.label2.Location = new System.Drawing.Point(32, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(32, 16);
this.label2.TabIndex = 1;
this.label2.Text = "通路";
//
// label1
//
this.label1.BackColor = System.Drawing.Color.Green;
this.label1.Location = new System.Drawing.Point(8, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(20, 20);
this.label1.TabIndex = 0;
//
// button3
//
this.button3.Location = new System.Drawing.Point(360, 224);
this.button3.Name = "button3";
this.button3.TabIndex = 6;
this.button3.Text = "Auto";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(442, 348);
this.Controls.Add(this.button3);
this.Controls.Add(this.panel1);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.btnRun);
this.Controls.Add(this.btnAbout);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Maze";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
string msgstr="";
msgstr+="迷宫简图:/n";
msgstr+=mymaze.ToString();
msgstr+="/n";
msgstr+="堆栈信息:/n";
msgstr+="当前数据总和:"+mymaze.StackCount.ToString()+"/n";
msgstr+="当前所走步数:"+mymaze.StepCount.ToString()+"/n";
MessageBox.Show(msgstr);
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//e.Graphics.DrawRectangle(new Pen(Color.Red),0,0,10,10);
mymaze.DrawBox(e.Graphics);
mymaze.DrawPath(e.Graphics);
mymaze.DrawNextPath(e.Graphics);
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button2_Click(object sender, System.EventArgs e)
{
if(mymaze.Step()==1)
MessageBox.Show("已到出口");
this.Refresh();
}
private void btnRun_Click(object sender, System.EventArgs e)
{
mymaze.Run();
this.Refresh();
}
private void btnReset_Click(object sender, System.EventArgs e)
{
mymaze.Reset();
this.Refresh();
}
private void btnAbout_Click(object sender, System.EventArgs e)
{
MessageBox.Show("作 者: lovered (GV)/nM S N: lovered_GV@hotmail.com/n完成日期:
04
年
04
月
04
日
");
}
private void button3_Click(object sender, System.EventArgs e)
{
mymaze.Reset();
while(mymaze.Step()!=1)
{
this.Refresh();
Thread.Sleep(500);
}
MessageBox.Show("已到出口");
}
}
}