基于c#编写汉诺塔游戏讲解_第1页
基于c#编写汉诺塔游戏讲解_第2页
基于c#编写汉诺塔游戏讲解_第3页
基于c#编写汉诺塔游戏讲解_第4页
基于c#编写汉诺塔游戏讲解_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、所需图片:设计游戏窗体(form1.cs)创建新 Windows应用程序项目,在新窗体上添加 1个图片框pictureboxl (显示ABC三个柱 子背景)、1个文本框textbox1 (用来设置盘子的数量)、1个标签label( text设置为盘子数)添加两个按钮(button) text属性为开始游戏”和自动演化实现代码代码测试都可以使用不懂联系邮箱:1278263100using System;using System.Collect ion s.Ge neric;using System.Comp onen tModel;using System.Data;using Sy

2、stem.Draw ing;using System.Text;using System.Windows.Forms;using System.Collections;namespace 汉诺塔递归public partial class Form1 : Formpublic Form1()InitializeComponent();private int i; / 移动的次数public PictureBox Plate = new PictureBox11; const int PlateHeight = 17; / 盘片厚度 private bool isDragging = false

3、;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 = 1; i += -1)A.Add(i)

4、;/A 数组列表添加条目 private void plate_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e) /处理盘子移动的公共事件 PictureBox p1; p1 =(PictureBox )sender;/ 将被点击的 PictureBox 赋给定义的 p1 变量if (isDragging)p1.Left = p1.Left - x1 + e.X;p1.Top = p1.Top - y1 + e.Y; private void plate_MouseDown(object sender,System.W

5、indows.Forms.MouseEventArgs e)/ 处理盘子 MouseDown 的公共事件 PictureBox p1;p1 = (PictureBox)sender;/ 将被点击的 PictureBox 赋给定义的 p1 变量 int sText;sText = Convert.ToInt16(p1.Tag);/获取盘片编号/ 首先判断是否不是最上面的盘子if (A.Contains(sText)foreach (int i in A) if (i sText) MessageBox.Show( 请选择最上面的盘子 ); return; if (B.Contains(sText

6、)foreach (int i in B) if (i sText) MessageBox.Show( 请选择最上面的盘子 ); return;if (C.Contains(sText) foreach (int i in C) if (i = 150 + 80 & (p1.Left + p1.Width / 2) 320 + 80)if (B.Count = 0 | sText = 320 + 80 & (p1.Left + p1.Width / 2) 490 + 80) if (C.Count = 0 | sText = 100 - 80 & (p1.Left + p1.Width / 2

7、) 150 + 80) if (A.Count = 0 | sText = newtop; j -= 1)Platen.Top = Platen.Top -1; System.Windows.Forms.Application.DoEvents();/其次水平方向移动step1 = (z - x) / Math.Abs( z- x);for (j = 0; j = Math.Abs(z - x) * 170; j += 1) / 柱子之间间隔 170 像素 Platen.Left = Platen.Left + step1;System.Windows.Forms.Application.Do

8、Events(); /再垂直方向向下移动 oldtop = 0;newtop = pictureBox1.Height - (t+1) * PlateHeight;/167 - t * PlateHeight;step1 = (newtop - oldtop) / Math.Abs(newtop - oldtop);for (j = oldtop; j = newtop; j += step1)Platen.Top = Platen.Top + step1;System.Windows.Forms.Application.DoEvents();private void button1_Clic

9、k(object sender, System.EventArgs e)/开始游戏int n = Convert.ToInt16(TextBox1.Text); load_plate(n);private void button2_Click(object sender, System.EventArgs e)/ 汉诺塔演示动画 int n = Convert.ToInt16(TextBox1.Text);load_plate(n);int Num;char x = A;char y = B;char z = C;i = 0;tryNum = n;Hanoi(Num, x, y, z);catch (Exception ex)Me

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论