首页 > 开发 > 综合 > 正文

走迷宫C#版(二)

2024-07-21 02:19:30
字体:
来源:转载
供稿:网友
//窗体,调用...

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: [email protected]/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("已到出口");
}

}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表