走迷宫C#版(一)
文章来源:
文章作者: 发布时间:2006-03-28
//迷宫类相关
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
namespace MazeDemo
{
///
/// 迷宫类
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 ;
}
///
/// 绘制迷宫时窗口的起始坐标
///
public Point StartPoint
{
set{this.start_p=value;}
get{return this.start_p;}
}
///
/// 当前迷宫共走多少步
///
public int StepCount
{
get{return this.step_count;}
}
///
/// 迷宫格子大小
///
public Size BoxSize
{
set{this.boxsize=value;}
get{return this.boxsize;}
}
///
/// 堆栈数据个数
///
public int StackCount
{
get{return this.stack.Count;}
}
///
/// 绘制迷宫
///
///
public void DrawBox(Graphics g)
{
for(int i=0;i
for(int j=0;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);
}
}
}
///
/// 绘制所走线路
///
///
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);
}
}
///
/// 绘制当前位置的可行路径
///
///
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);
}
}
///
/// 判断迷宫是否走完
///
///
public bool IsEnd()
{
CCoor coor=(CCoor)this.stack.Peek(); //当前位置信息
if( coor.CurrentPoint.X==this.out_p.X
上一篇:走进C#(我的C#学习之旅) 之四(3)
下一篇:怎样通过Visual C#.net创建一个DTS任务
精彩推荐
[技巧]·诺基亚S60机友必看——蓝牙的应用
[技巧]·用玩转手机实现摩托A1000于电脑同步
[技巧]·关于V902如何使用3GP为来电铃声方法
[新机]·完美结合 索爱滑盖音乐手机W580图赏
[新机]·延续经典 诺基亚神秘翻盖6131i曝光
[新机]·大屏王者 多普达智能新机D805曝光
[新机]·时尚风格 松下P904i精美广告壁纸欣赏
[新机]·苹果iPhone终上市 海量开箱照片抢先欣
[行情]·S型曲线 联想时尚折叠机S9仅售1480
[行情]·镜面美人 联想女性超薄翻盖S9仅1399
[行情]·畅所欲言 飞利浦待机王9@9s仅1398元
[行情]·创意无限 LG滑盖音乐机KE608跌破两千
[行情]·天籁之音 索爱音乐强机W810c只1890元
[技巧]·用玩转手机实现摩托A1000于电脑同步
[技巧]·关于V902如何使用3GP为来电铃声方法
[新机]·完美结合 索爱滑盖音乐手机W580图赏
[新机]·延续经典 诺基亚神秘翻盖6131i曝光
[新机]·大屏王者 多普达智能新机D805曝光
[新机]·时尚风格 松下P904i精美广告壁纸欣赏
[新机]·苹果iPhone终上市 海量开箱照片抢先欣
[行情]·S型曲线 联想时尚折叠机S9仅售1480
[行情]·镜面美人 联想女性超薄翻盖S9仅1399
[行情]·畅所欲言 飞利浦待机王9@9s仅1398元
[行情]·创意无限 LG滑盖音乐机KE608跌破两千
[行情]·天籁之音 索爱音乐强机W810c只1890元
相关新闻
相关链接
