国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

走迷宮C#版(一)

2024-07-21 02:19:31
字體:
來源:轉載
供稿:網友
//迷宮類相關

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;}
}
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 四子王旗| 通道| 金湖县| 抚远县| 高邑县| 广河县| 荥阳市| 庆安县| 衡南县| 澄城县| 平塘县| 富源县| 历史| 浪卡子县| 南陵县| 萝北县| 英德市| 宾阳县| 方城县| 淮北市| 新郑市| 伊宁县| 南和县| 启东市| 西乌| 湘乡市| 都昌县| 邯郸县| 蓬安县| 彭泽县| 外汇| 鄂尔多斯市| 咸阳市| 分宜县| 太仓市| 长治县| 汉阴县| 贺州市| 怀仁县| 错那县| 淮阳县|