namespace 汉诺塔
{
public delegate void MessageHandler();
public class Hanoi
{
public event MessageHandler CheckList;
private bool IsStop=false;
public Hanoi()
{ } List<Panel> Xcol = new List<Panel>();//add自动将元素添加到末尾
List<Panel> Ycol = new List<Panel>();
List<Panel> Zcol = new List<Panel>(); public List<Panel> X
{
get
{
return Xcol;
}
} public List<Panel> Y
{
get
{
return Ycol;
}
} public List<Panel> Z
{
get
{
return Zcol;
}
} private void InitXYZcol(int n)//初始化
{
Xcol.Clear();
Ycol.Clear();
Zcol.Clear();
for (int i = 0; i<n; i++)
{
Panel zipanel = new Panel();
zipanel.BorderStyle = BorderStyle.FixedSingle;
zipanel.Height = 30;
zipanel.Width = 60;
Label text = new Label();
text.Text = i.ToString();
zipanel.Controls.Add(text);
zipanel.Width = (zipanel.Width / n) + ((zipanel.Width / n) * i);
text.AutoSize = true;
text.Left = zipanel.Width / 2 - text.Width / 2;
Xcol.Add(zipanel); }
}
private void move(int n, List<Panel> go, List<Panel> end)//移动
{
end.Insert(0,go[0]);
go.RemoveAt(0);
Display();
}
void Display() //显示
{
CheckList();
//System.Threading.Thread.Sleep(1000);
} /// <summary>
/// 开始运行
/// </summary>
/// <param name="n">多少个盘子</param>
public void Bigin(int n)
{
InitXYZcol(n);
Strat(n, Xcol, Ycol, Zcol);
}
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
IsStop = true;
} void Strat(int n, List<Panel> x, List<Panel> y, List<Panel> z)
{
if (IsStop)
return;
if (n == 1)
{
move(n, x, z); System.Threading.Thread.Sleep(1000);
}
else
{
Strat(n - 1, x, z, y);
move(n, x, z); System.Threading.Thread.Sleep(1000);
Strat(n - 1, y, x, z);
}
} }
class Program
{
static List<Panel> Xcol, Ycol, Zcol;
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 kk = new Form1();
//Hanoi pp=new Hanoi();
//Xcol = pp.X;
//Ycol = pp.Y;
//Zcol = pp.Z;
//pp.CheckList += new MessageHandler(pp_CheckList);//事件订阅必须在类运行前
//pp.Bigin(8);
//Console.ReadLine();
Application.Run(kk);
} static void pp_CheckList()
{
Console.Write("X:");
foreach (Panel kk in Xcol)
Console.Write("{0},", kk.Controls[0].Text);
Console.WriteLine();
Console.Write("Y:");
foreach (Panel kk in Ycol)
Console.Write("{0},", kk.Controls[0].Text);
Console.WriteLine();
Console.Write("Z:");
foreach (Panel kk in Zcol)
Console.Write("{0},", kk.Controls[0].Text);
Console.WriteLine();
Console.WriteLine("----------------{0}-------------------", "ll");
} }
}//------------------------------------------------------------------------------------------------------
namespace 汉诺塔
{
public partial class Form1 : Form
{
List<Panel> x, y, z;
Hanoi newhanoi;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
newhanoi = new Hanoi();
x= newhanoi.X;
y = newhanoi.Y;
z = newhanoi.Z;
newhanoi.CheckList+=new MessageHandler(newhanoi_CheckList);
newhanoi.Bigin(8); } void newhanoi_CheckList()
{ panel1.Controls.Clear();
int xsize = x.Count - 1, ysize = y.Count - 1, zsize = z.Count - 1;
for (int i = xsize; i >=0; i--)
{
x[i].Left = 30 - x[i].Width / 2;
x[i].Top = (panel1.Height - x[i].Height) - (x[i].Height * ( xsize-i ));
panel1.Controls.Add(x[i]);
//MessageBox.Show(panel1.Controls.Count.ToString());
}
for (int i = ysize; i >= 0; i--)
{
y[i].Left = 120-y[i].Width/2;
y[i].Top = (panel1.Height - y[i].Height) - (y[i].Height * (ysize - i));
panel1.Controls.Add(y[i]);
//MessageBox.Show(panel1.Controls.Count.ToString());
}
for (int i = zsize; i >= 0; i--)
{
z[i].Left = 240 - z[i].Width / 2;
z[i].Top = (panel1.Height - z[i].Height) - (z[i].Height * (zsize - i));
panel1.Controls.Add(z[i]);
}
//this.Refresh();
panel1.Refresh(); } private void button2_Click(object sender, EventArgs e)
{
newhanoi.Stop();
}
}
}
多线程操作