程序源码下载链接: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.BackgroundImage = bmp;pictureBox1.BackgroundImageLayout = ImageLayout.None;p.Dispose();g.Dispose();}public Disk getTop(string index){int pw = bmpWidth / 3;int i = index == "A" ? 0 : index == "B" ? 1 : index == "C" ? 2 : -1;int Vline = pw / 2 + pw * i - 1;int topY = 0;Bitmap bmp = new Bitmap(pictureBox1.Image);while (bmp.GetPixel(Vline, bmpHeight - topY - diskHeight - diskVSpace - 1).ToArgb() == Color.Blue.ToArgb()){topY += diskVSpace + diskHeight;}if (topY == 0)return new Disk(0, bmpHeight - 1, 0, 0);int y = bmpHeight - topY - 1;int x = Vline;while (bmp.GetPixel(--x, y).ToArgb() == Color.Blue.ToArgb()) ;x++;int width = (Vline - x + 1) * 2;return new Disk(x, y, width, diskHeight);}Disk GDIMove(Disk d, string direct, int distance){//要移动的圆盘d ,移动的方向 [UP|DOWN|LEFT|RIGHT] ,移动的距离 distance。Bitmap bmp = new Bitmap(pictureBox1.Image);for (int times = 0; times < distance / moveInterval; times++){System.Threading.Thread.Sleep((moveInterval * 1000) / moveSpeed);for (int i = 0; i < d.Width + 1; i++){if (i == 0 || i == d.Width){int liteBug = 0;if (i == d.Width && bmp.GetPixel(i + d.X, d.Y).ToArgb() != Color.Blue.ToArgb()){liteBug = 1;}for (int j = 0; j < d.Height + 1; j++){try{bmp.SetPixel(i + d.X - liteBug, j + d.Y, Color.White);}catch{//}}}else{bmp.SetPixel(i + d.X, d.Y, Color.White);bmp.SetPixel(i + d.X, d.Y + d.Height, Color.White);}}if (direct == "LEFT"){d.X -= moveInterval;}if (direct == "RIGHT"){d.X += moveInterval;}if (direct == "UP"){d.Y -= moveInterval;}if (direct == "DOWN"){d.Y += moveInterval;}for (int i = 0; i < d.Width + 1; i++){if (i == 0 || i == d.Width){for (int j = 0; j < d.Height + 1; j++){if (direct == "LEFT"){bmp.SetPixel(i + d.X, j + d.Y, Color.Blue);}if (direct == "RIGHT"){bmp.SetPixel(i + d.X, j + d.Y, Color.Blue);}if (direct == "UP"){bmp.SetPixel(i + d.X, j + d.Y, Color.Blue);}if (direct == "DOWN"){bmp.SetPixel(i + d.X, j + d.Y, Color.Blue);}}}else{if (direct == "LEFT"){bmp.SetPixel(i + d.X, d.Y, Color.Blue);bmp.SetPixel(i + d.X, d.Y + d.Height, Color.Blue);}if (direct == "RIGHT"){bmp.SetPixel(i + d.X, d.Y, Color.Blue);bmp.SetPixel(i + d.X, d.Y + d.Height, Color.Blue);}if (direct == "UP"){bmp.SetPixel(i + d.X, d.Y, Color.Blue);bmp.SetPixel(i + d.X, d.Y + d.Height, Color.Blue);}if (direct == "DOWN"){bmp.SetPixel(i + d.X, d.Y, Color.Blue);bmp.SetPixel(i + d.X, d.Y + d.Height, Color.Blue);}}}pictureBox1.Image = bmp;pictureBox1.Refresh();}return d;}void hanoi(int n, string a, string b, string c){if (n > 0){hanoi(n - 1, a, c, b);move(a, b);hanoi(n - 1, c, b, a);}}void move(string src, string dst){int distance = bmpWidth / 3;if (src + dst == "AB"){int ay = getTop("A").Y;int by = getTop("B").Y;if (by > ay){Disk d = GDIMove(getTop("A"), "RIGHT", distance);GDIMove(d, "DOWN", by - ay - diskHeight - diskVSpace);}else{Disk d = GDIMove(getTop("A"), "UP", ay - by + diskVSpace + diskHeight);GDIMove(d, "RIGHT", distance);}}if (src + dst == "BA"){int ay = getTop("A").Y;int by = getTop("B").Y;if (ay > by){Disk d = GDIMove(getTop("B"), "LEFT", distance);GDIMove(d, "DOWN", ay - by - diskHeight - diskVSpace);}else{Disk d = GDIMove(getTop("B"), "UP", by - ay + diskVSpace + diskHeight);GDIMove(d, "LEFT", distance);}}if (src + dst == "AC"){int ay = getTop("A").Y;int cy = getTop("C").Y;int by = getTop("B").Y;if (ay >= by && cy >= by)//中间被b上的圆盘阻断,所以要先向上移动在向右在向下{Disk d = GDIMove(getTop("A"), "UP", ay - by + diskVSpace + diskHeight);d = GDIMove(d, "RIGHT", distance * 2);GDIMove(d, "DOWN", cy - by);}else{if (cy > ay){Disk d = GDIMove(getTop("A"), "RIGHT", distance * 2);GDIMove(d, "DOWN", cy - ay - diskVSpace - diskHeight);}else{Disk d = GDIMove(getTop("A"), "UP", ay - cy + diskVSpace + diskHeight);GDIMove(d, "RIGHT", distance * 2);}}}if (src + dst == "CA"){int ay = getTop("A").Y;int cy = getTop("C").Y;int by = getTop("B").Y;if (ay >= by && cy >= by)//中间被b上的圆盘阻断,所以要先向上移动在向右在向下{Disk d = GDIMove(getTop("C"), "UP", cy - by + diskVSpace + diskHeight);d = GDIMove(d, "LEFT", distance * 2);GDIMove(d, "DOWN", ay - by);}else{if (ay > cy){Disk d = GDIMove(getTop("C"), "LEFT", distance * 2);GDIMove(d, "DOWN", ay - cy - diskHeight - diskVSpace);}else{Disk d = GDIMove(getTop("C"), "UP", cy - ay + diskVSpace + diskHeight);GDIMove(d, "LEFT", distance * 2);}}}if (src + dst == "BC"){int by = getTop("B").Y;int cy = getTop("C").Y;if (cy > by){Disk d = GDIMove(getTop("B"), "RIGHT", distance);GDIMove(d, "DOWN", cy - by - diskHeight - diskVSpace);}else{Disk d = GDIMove(getTop("B"), "UP", by - cy + diskVSpace + diskHeight);GDIMove(d, "RIGHT", distance);}}if (src + dst == "CB"){int cy = getTop("C").Y;int by = getTop("B").Y;if (by > cy){Disk d = GDIMove(getTop("C"), "LEFT", distance);GDIMove(d, "DOWN", by - cy - diskVSpace - diskHeight);}else{Disk d = GDIMove(getTop("C"), "UP", cy - by + diskVSpace + diskHeight);GDIMove(d, "LEFT", distance);}}}private void button1_Click(object sender, EventArgs e){moveSpeed = int.Parse(textBox2.Text);foreach (Control c in groupBox1.Controls){c.Enabled = false;}hanoi(count, "A", "C", "B");foreach (Control c in groupBox1.Controls){c.Enabled = true;}}bool isFirst = true;private void comboBox1_SelectedIndexChanged(object sender, EventArgs e){if (!isFirst){switch (comboBox1.SelectedIndex){case 0:MessageBox.Show("方案一:\r\n" + "    圆盘高度:17\r\n    圆盘间距:3\r\n    每次移动像素:20px");diskHeight = 17;diskVSpace = 3;moveInterval = 20;break;case 1:MessageBox.Show("方案二:\r\n" + "    圆盘高度:17\r\n    圆盘间距:3\r\n    每次移动像素:10px");diskHeight = 17;diskVSpace = 3;moveInterval = 10;break;case 2:MessageBox.Show("方案四:\r\n" + "    圆盘高度:16\r\n    圆盘间距:4\r\n    每次移动像素:10px");diskHeight = 16;diskVSpace = 4;moveInterval = 10;break;case 3:MessageBox.Show("方案三:\r\n" + "    圆盘高度:12\r\n    圆盘间距:3\r\n    每次移动像素:5px");diskHeight = 12;diskVSpace = 3;moveInterval = 5;break;}}button2.PerformClick();isFirst = false;}private void button2_Click(object sender, EventArgs e){int num = int.Parse(textBox1.Text);initDisk(num);}}}

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

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

主要使用2D 画图,

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

C#图形界面汉诺塔Hanoi相关推荐

  1. c语言程序设计电子图书 汉诺塔,用C写的汉诺塔(hanoi)程序

    用C写的汉诺塔(hanoi)程序 分类:计算机等级 | 更新时间:2016-07-07| 来源:转载 #include void movedisc(unsigned n,char fromneedle ...

  2. 【头歌】汉诺塔(Hanoi)的递归算法

    任务描述 本关任务:汉诺塔(Hanoi)的递归算法. 相关知识 相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到 ...

  3. 汉诺塔(Hanoi)递归算法

    相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到小按顺序放置64个金盘. 游戏的目标:把A杆上的金盘全部移到C杆 ...

  4. 汉诺塔(hanoi tower)游戏

    问题 递归实现汉诺塔 算法实现 #include <stdio.h> void hanoi( int n, char a, char b, char c) {if( n > 0 ) ...

  5. 汉诺塔(Hanoi)问题归纳总结

    一.汉诺塔问题及其递归算法 1.问题阐述 经典汉诺塔: 外文算法书对汉诺塔问题的描述: 2.算法步骤 三阶汉诺塔问题解题步骤 共需7步. 四阶汉诺塔问题解题步骤 共需15步 五阶汉诺塔问题解题步骤 可 ...

  6. 汉诺塔 hanoi 如此简单

    先贴代码,重点要理解递归函数的作用 #include <stdio.h> int hanoi (int n , char x , char y , char z){//将n个盘子从x到z ...

  7. 汉诺塔(hanoi)

    问题描述: 有三根柱子,第一根柱子上有n个盘子,借助第二根柱子,将第一根的所有盘子搬到第三根,在搬运过程中遵循每次只能搬一个,且大盘子在小盘子之下的原则. 解决方案: 首先把柱子标记为ABC, 由A搬 ...

  8. 室友上了个厕所我就学会了递推算法解决汉诺塔hanoi

    对于游戏的玩法,我们可以简单分解为三个步骤 1)将前63个盘子从X移动到Y上: 2)将最底下的第64个盘子从X移动到Z上: 3)将Y的63个盘子移动到Z上: 第二步是最容易实现的一步,那么第一步和第三 ...

  9. 汉诺塔问题(Hanoi)

    汉诺塔问题,是心理学实验研究常用的任务之一.该问题的主要材料包括三根高度相同的柱子和一些大小及颜色不同的圆盘,三根柱子分别为起始柱A.辅助柱B及目标柱C. 相传在古印度圣庙中,有一种被称为汉诺塔(Ha ...

最新文章

  1. ASP.NET 获取上一个页面的Url链接
  2. python 使用全局变量_如何在Python中的不同模块中使用全局变量
  3. layui table异步调用数据的时候,数据展示不出来现象解决方案
  4. AMQP Connection 127.0.0.1:5672] ERROR [o.s.a.rabbit.connection.CachingConnectionFactory] CachingConn
  5. 【数据结构笔记01】什么是数据结构
  6. 用C#实现图片数据库存储与显示
  7. 机器学习—LightGBM的原理、优化以及优缺点
  8. 苹果x和xs买哪个好_苹果12和苹果11哪个值得买-苹果12和11哪个更值得买
  9. icem合并面网格_ICEM CFD中合并多个网格
  10. bzoj5470 / P4578 [FJOI2018]所罗门王的宝藏//(尚未修正)
  11. 租用游艇,Clear And Present Danger S,Heat Wave G,单源最短路径(弱化版)
  12. VScode前进后退快捷键
  13. 开学季,中学生用什么样护眼台灯好?中学生用护眼台灯排行
  14. 7 进度指示器(LinearProgressIndicator、CircularProgressIndicator)
  15. exec函数族的作用与讲解
  16. Mybatis-Cause:无效的主机/绑定变量名 The error may involve -Inline
  17. STM32 GPIO的配置寄存器(CRL、CRH)快速学习
  18. 2022出海东亚:韩国电商市场现状及网红营销特点
  19. replace()和replaceAll()的使用
  20. layui 合并单元格

热门文章

  1. YOLOv5-优化器和学习率调整策略
  2. 5月14日社区技术直播【Analytics Zoo上的分布式TensorFlow训练AI玩FIFA足球游戏】
  3. 绿米Aara单火开关,ZigBee智能开关和单火线取电技术, 对于单火取电电源和ZIGBEE缩合分析
  4. MAC install MySQL and DBeaver
  5. 【解决方案】windows7无法启动黑屏,报0xc000014c错误解决方案不用重新安系统
  6. U盘文件乱码怎么恢复正常
  7. 【hadoop生态之Flume】概念【笔记+代码】
  8. 服务器监控系统——Cacti
  9. Openstack Zoning – Region/Availability Zone/Host Aggregate
  10. 测试当前访问百度的IP地址(代理IP)