📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace DFSAlgorithmMaze
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem DimensionsMenu;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem8;
private Maze TheMaze = new Maze();
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
private int delayS = 20;//搜索显示延时
private int delayP = 20;//路径显示延时
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
SetBounds(this.Left, this.Top, ( Maze.kDimension + 1 )* Cell.kCellSize + 2 * Cell.kPadding ,( Maze.kDimension + 3) * Cell.kCellSize + 2 * Cell.kPadding);
this.Cursor = Cursors.WaitCursor;
TheMaze.Generate();
this.Cursor = Cursors.Arrow;
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.DimensionsMenu = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.DimensionsMenu,
this.menuItem9,
this.menuItem10});
this.menuItem1.Text = "初始化";
//
// DimensionsMenu
//
this.DimensionsMenu.Index = 0;
this.DimensionsMenu.Text = "生成完美迷宫..";
this.DimensionsMenu.Click += new System.EventHandler(this.DimensionsMenu_Click);
//
// menuItem9
//
this.menuItem9.Index = 1;
this.menuItem9.Text = "生成随机迷宫...";
this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);
//
// menuItem10
//
this.menuItem10.Index = 2;
this.menuItem10.Text = "搜索演示设置.";
this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5,
this.menuItem6,
this.menuItem7});
this.menuItem2.Text = "选择算法";
//
// menuItem5
//
this.menuItem5.Index = 0;
this.menuItem5.Text = "深度搜索";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Index = 1;
this.menuItem6.Text = "广度搜索";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);
//
// menuItem7
//
this.menuItem7.Index = 2;
this.menuItem7.Text = "启发式搜索";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem8});
this.menuItem3.Text = "退出";
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.Text = "离开迷宫";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem8
//
this.menuItem8.Index = 1;
this.menuItem8.Text = "About p_Sa";
this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(350, 294);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "1002016迷宫啊迷宫 v0.1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);
TheMaze.Draw(g);//重绘墙
TheMaze.reDraw(g);//重绘路,解决菜单遮挡迷宫路径等显示问题
}
//完美迷宫
private void DimensionsMenu_Click(object sender, System.EventArgs e)
{
DimensionsDialog theDialog = new DimensionsDialog();
theDialog.numericUpDown1.Value = Maze.kDimension;
theDialog.numericUpDown2.Value = Cell.kCellSize;
if (theDialog.ShowDialog() == DialogResult.OK)
{
Maze.kDimension = (int)theDialog.numericUpDown1.Value;
Cell.kCellSize = (int)theDialog.numericUpDown2.Value;
TheMaze.Initialize();
TheMaze.Generate();
SetBounds(this.Left, this.Top, (Maze.kDimension + 2) * Cell.kCellSize + Cell.kPadding, (Maze.kDimension + 5) * Cell.kCellSize + Cell.kPadding);
Invalidate();//......
}
}
//随机迷宫
private void menuItem9_Click(object sender, System.EventArgs e)
{
RandomMaze theRandom = new RandomMaze();
theRandom.numericUpDown1.Value = 16;
theRandom.numericUpDown2.Value = 30;
theRandom.wallsnumericUpDown.Value = 20;
if(theRandom.ShowDialog() == DialogResult.OK)
{
Maze.kDimension = (int)theRandom.numericUpDown1.Value;
Cell.kCellSize = (int)theRandom.numericUpDown2.Value;
Maze.wallsrate = (int)theRandom.wallsnumericUpDown.Value;
TheMaze.Initialize();
TheMaze.GenerateRandom();
SetBounds(this.Left, this.Top, (Maze.kDimension + 2) * Cell.kCellSize + Cell.kPadding, (Maze.kDimension + 5) * Cell.kCellSize + Cell.kPadding);
Invalidate();//......
}
}
//搜索演示设置
private void menuItem10_Click(object sender, System.EventArgs e)
{
setting theset = new setting();
if(theset.ShowDialog() == DialogResult.OK)
{
if(theset.checkBox1.Checked == true)//注意要用==
delayS = (int)theset.numericUpDown1.Value;
else
delayS = 0;
if(theset.checkBox2.Checked == true)
delayP = (int)theset.numericUpDown2.Value;
else
delayP = 0;
}
}
//copyright...
private void menuItem8_Click(object sender, System.EventArgs e)
{
MessageBox.Show(this,
//"!时间 — summer.2005\n ◎地点 — FuzhouU.W2\n #人物 — 1002016.林锋杰\n ¥事件 — 迷宫搜索v0.1\n",
"\n!时间 — summer.2005\n\n◎地点 — FuzhouU.W2\n\n#人物 — 1002016.林锋杰\n\n¥事件 — 迷宫搜索v0.1\n\n",
"p_Sa.software | A.I.Maze0.1",
MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1);
}
//退出程序
private void menuItem4_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
//深度搜索
Graphics g = this.CreateGraphics();
TheMaze.clearSearch(g);
if(TheMaze.DFSearch())
{
DrawTogether();
}
else
{
MessageBox.Show("搜索失败-深度");
}
//this.Refresh();
//this.Invalidate();
}
private void menuItem6_Click(object sender, System.EventArgs e)
{
//广度搜索
Graphics g = this.CreateGraphics();
TheMaze.clearSearch(g);
if(TheMaze.BFSearch())
{
DrawTogether();
}
else
{
MessageBox.Show("搜索失败-广度");
}
}
private void menuItem7_Click(object sender, System.EventArgs e)
{
//启发搜索(A*)
Graphics g = this.CreateGraphics();
TheMaze.clearSearch(g);
if(TheMaze.AISearch())
{
DrawTogether();
}
else
{
MessageBox.Show("搜索失败-A*");
}
}
//三个搜索的共同代码,提出成函数
private void DrawTogether()
{
if(TheMaze.CLOSED.Count != 0)
{
for(int i=0;i<TheMaze.CLOSED.Count;i++)
{
Cell PathCell =( Cell)TheMaze.CLOSED[i];
Graphics g = this.CreateGraphics();
PathCell.DrawFillSearch(g);
System.Threading.Thread.Sleep(delayS);//画搜索格子,每格都会延时delayS
}
}
while(TheMaze.PathStack.Count != 0)
{
Cell PathCell = (Cell)TheMaze.PathStack.Pop();
Graphics g = this.CreateGraphics();
PathCell.DrawFill(g);
System.Threading.Thread.Sleep(delayP);//画路径格子,每格都会延时delayP
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -