所需图片:
设计游戏窗体(form1.cs)
创建新Windows应用程序项目,在新窗体上添加1个图片框picturebox1(显示ABC三个柱子背景)、1个文本框textbox1(用来设置盘子的数量)、1个标签label(text设置为盘子数)
添加两个按钮(button) text属性为“开始游戏”和自动演化
实现代码
//代码测试都可以使用 不懂联系邮箱:1278263100@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace 汉诺塔递归
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int i; //移动的次数
public PictureBox[] Plate = new PictureBox[11];
const int PlateHeight = 17; //盘片厚度
private bool isDragging = false;
private int x1, y1;
private ArrayList A = new ArrayList();
private ArrayList B = new ArrayList();
private ArrayList C = new ArrayList();
//ArrayList 就是数组列表,它位于System.Collections名称空间下,是集合类型。
private int oldx, oldy;
private void load_plate(int n)
{
//加载盘片
//盘片编号从上往下1,2,...n
for (i = 1; i <= n; i++)
{
Plate[i] = new PictureBox();
this.Controls.Add(Plate[i]);
Plate[i].Left = 48 + (n - i) * 5;
Plate[i].Top = 167 - (n - i + 1) * PlateHeight;
Plate[i].Width = 100 - (n - i) * 10;
Plate[i].Height = PlateHeight;
Plate[i].Tag = i; //设置盘片编号
Plate[i].Image = new Bitmap("Plate.bmp"); //盘子图片
Plate[i].SizeMode = PictureBoxSizeMode.StretchImage;
Plate[i].Parent = pictureBox1;
Plate[i].MouseMove += new MouseEventHandler(plate_MouseMove);
Plate[i].MouseUp += plate_MouseUp;
Plate[i].MouseDown += plate_MouseDown;
}
for (i = n; i >= 1; i += -1)
{
A.Add(i); //A数组列表添加条目
}
}