重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

利用c#编写一个汉诺塔小游戏-创新互联

利用c# 编写一个汉诺塔小游戏?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站设计、网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的西夏网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

汉诺塔游戏一旦掌握了规律,其实是有点单调和无聊的,不过却是学习递归的一个绝佳例子,想当初学习老谭C的时候,就卡在这儿好长时间。

对初学编程的人来说,递归本身就不易理解,如果使用C语言没有好的调试环境就更难去理解了。在这方面,VS调试输出是真方便,一目了然。

但要想理解递归求解汉诺塔,还是得先玩一玩游戏本身,有点感受。另外,设计一下这个游戏也可以加深对它的理解,同时还可以对数据结构中的栈有一个简单的认识。

因为汉诺塔的游戏规则本身就是一个栈,只能从平台最上面取走碟子,然后放在另一个平台最上面,这明显是个栈。下面是我设计的游戏界面:

利用c# 编写一个汉诺塔小游戏

游戏的玩法关键:奇数个盘子,第一个先移到C上;偶数个盘子,第一个先移到B上

平台和背景容器是事先画好的,碟子是动态生成的,使用了容器控件panel的拖动事件来实现拖动碟子(就是个label控件而已)的功能。

主要代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
 
namespace 汉诺塔
{
    public partial class Form1 : Form
    {
        int dishNum = 3;
        int dishThick = 20;
        int count;            // 记录步数
        Stack pileA;     // 存放平台A的数据
        Stack pileB;     // 存放平台B的数据
        Stack pileC;     // 存放平台C的数据
 
        public Form1()
        {
            InitializeComponent();
            InitGame();
            Result.ShowAnswer(dishNum);
        }
 
        private void InitGame()
        {
            pileA = new Stack();
            pileB = new Stack();
            pileC = new Stack();
            panelA.Tag = pileA;
            panelB.Tag = pileB;
            panelC.Tag = pileC;
            for (int i = 0; i < dishNum; i++)
            {
                Label label = new Label();
                label.BackColor = Color.Gold;
                label.BorderStyle = BorderStyle.FixedSingle;
                label.Parent = panelA;
                label.Size = new Size(panelA.Width - (i + 1) * 12, dishThick);    // 从下往上每层宽度减少12
                label.Location = new Point(panelA.Width / 2 - label.Width / 2, panelA.Height - dishThick * (i + 1));
                label.BringToFront();
                label.MouseMove += new MouseEventHandler(Dish_MouseMove);
                label.Text = (dishNum - i).ToString();
                label.TextAlign = ContentAlignment.MiddleCenter;
                label.Name = "dish" + i;
                label.Tag = i;    // 设置碟子的编号
                pileA.Push(i);    // 存储碟子的编号
            }
            count = 0;
            lblCount.Text = "已走步数:" + count;
            lblLevel.Text = "最少步数:" + (Math.Pow(2, dishNum) - 1);
        }
 
        private void Dish_MouseMove(object sender, MouseEventArgs e)
        {
            Label label = sender as Label;
            if (e.Button == MouseButtons.Left)
            {
                label.DoDragDrop(label, DragDropEffects.Move);
            }
        }
 
        private void panelBox_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Label)))
            {
                //Console.WriteLine("DragEnter: {0}", (sender as Panel).Name);
                Label label = e.Data.GetData(typeof(Label)) as Label;
                Panel panelFrom = label.Parent as Panel;
                Stack pileFrom = panelFrom.Tag as Stack;
                int index = (int)label.Tag;
                if (pileFrom.Peek() == index) e.Effect = DragDropEffects.Move;    // 只可以拖走最顶层的碟子
                else e.Effect = DragDropEffects.None;
            }
        }
 
        private void panelBox_DragDrop(object sender, DragEventArgs e)
        {
            Panel panelTo = sender as Panel;
            //Console.WriteLine("DragDrop: {0}", panelTo.Name);
            //Point newPoint = panel.PointToClient(new Point(e.X, e.Y));
            Stack pileTo = panelTo.Tag as Stack;
            Label label = e.Data.GetData(typeof(Label)) as Label;
            Panel panelFrom = label.Parent as Panel;
            Stack pileFrom = panelFrom.Tag as Stack;
            int index = (int)label.Tag;
            if (pileTo.Count == 0 || index > pileTo.Peek())    // 只可以放置小的碟子到大的碟子上(栈为空时使用Peek会引发异常)
            {
                label.Parent = panelTo;
                label.Location = new Point(panelTo.Width / 2 - label.Width / 2, panelTo.Height - dishThick * (pileTo.Count + 1));
                label.BringToFront();
                pileFrom.Pop();
                pileTo.Push(index);
                count++;
                lblCount.Text = "已走步数:" + count;
                if (IsWin())
                {
                    if (dishNum < 9)    // 9层够玩了
                    {
                        MessageBox.Show("OK, be going to next...", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        dishNum++;
                        Reset();
                        InitGame();
                    }
                    else
                    {
                        MessageBox.Show("You Win!", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else e.Effect = DragDropEffects.None;
        }
 
        private bool IsWin()
        {
            if (pileC.Count == dishNum) return true;
            else return false;
        }
 
        private void Reset()    // 删除现在所有的碟子
        {
            //panelC.Controls.Clear();    // 会把柱子也删除
            //Console.WriteLine("panelC: {0}", panelC.Controls.Count);
            for (int i = panelC.Controls.Count - 1; i >= 0; i--)    // 注意从前往后移除控件时控件的索引会跟着变化导致难以跟踪
            {
                //Console.WriteLine(" {0} {1}", i, panelC.Controls[i].Name);
                if (panelC.Controls[i].Name.StartsWith("dish")) panelC.Controls[i].Dispose();
            }
        }
    }
}

文章名称:利用c#编写一个汉诺塔小游戏-创新互联
标题URL:http://cqcxhl.cn/article/csghej.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP