C#图形界面汉诺塔Hanoi

程序源码下载链接:http://download.csdn.net/detail/y85171642/5361537

该程序未使用多线程技术,主要是楼主对多线程不是很了解,求大神改进!

主要使用2D 画图,

求好的思路想法!(因为本程序运行有时候会卡死,原因不明,然后直接跳出移动的过程得到结果)


using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hanoi_02
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int penWidth = 1;//画笔的粗细
        int diskHCut = 15; //圆盘的宽度差,单一一侧的差值。(宽度差要*2)H 横向
        int diskVSpace = 3;//圆盘之间高度上下的距离。V纵向
        int diskWidth = 200; //最大号圆盘的宽度
        int diskHeight = 17;//圆盘的高度
        int moveSpeed = 500;//每秒移动的像素点速度
        int moveInterval = 20;//每次移动的像素点个数
        int bmpWidth = 660;
        int bmpHeight = 420;
        int dPillar = 10;//柱子间间距
        int pillarHeight = 300;//柱子高度
        int count = 3;//圆盘的个数

        private void Form1_Load(object sender, EventArgs e)
        {
            drawPicBoxBackground();
            initDisk(count);
            comboBox1.SelectedIndex = 0;
        }

        void initDisk(int num)
        {
            count = num;
            Bitmap bmp = new Bitmap(bmpWidth, bmpHeight);
            Graphics g = Graphics.FromImage(bmp);
            Pen p = new Pen(Color.Blue, penWidth);//背景粗细
            int pw = bmpWidth / 3;
            for (int i = 0; i < num; i++)
            {
                int nowDiskWidth = diskWidth - i * diskHCut;
                int x0 = (pw - nowDiskWidth) / 2;
                int y0 = (bmpHeight - diskVSpace * i - diskHeight * i - 1 - diskVSpace - diskHeight);
                int x1 = nowDiskWidth;
                int y1 = diskHeight;
                g.DrawRectangle(p, x0, y0, x1, y1);
            }
            pictureBox1.Image = bmp;
            p.Dispose();
            g.Dispose();
        }

        public void drawPicBoxBackground()
        {
            pictureBox1.Width = bmpWidth;
            pictureBox1.Height = bmpHeight;
            Bitmap bmp = new Bitmap(bmpWidth, bmpHeight);
            Graphics g = Graphics.FromImage(bmp);
            Pen p = new Pen(Color.Black, 1);//背景粗细
            int pw = bmpWidth / 3;
            for (int i = 0; i < 3; i++)
            {
                int Vline = pw / 2 + pw * i - 1;
                g.DrawLine(p, Vline, bmpHeight - pillarHeight, Vline, bmpHeight - 1);
                g.DrawLine(p, dPillar + pw * i, bmpHeight - 1, pw * i + pw - dPillar - 1, bmpHeight - 1);
            }
            pictureBox1.B
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值