河内的塔:将钉从钉子移动到钉子

时间:2016-01-08 04:42:52

标签: c# .net windows console

扩展我之前的帖子,我仍然在写河内塔。在解释了如何在钉子上画出环之后,我有一个很好的解决方案,我仍然有一个问题,我现在已经摆弄了很长一段时间。

这是我的PegClass:

namespace Towers_Of_Hanoi
{
    class PegClass
    {
        private int pegheight; 
        private int y = 3;
        int[] rings = new int[0];
        public PegClass()
        { 
            //this is the default constructor 
        }
        public PegClass(int height)
        { 
            pegheight = height; 
        }

        // other user defined functions 
        public void AddRing(int size)
        { 
            Array.Resize (ref rings, rings.Length + 2);
            rings[rings.Length - 1] = size;
        }

        public void DrawPeg(int x, int numberOfRings = 0)
        { 
            for (int i = pegheight; i >= 1; i--) 
            {
                string halfRing = new string (' ', i);
                if (numberOfRings > 0) 
                { 
                    if (i <= numberOfRings)
                        halfRing = new string ('-', numberOfRings - i + 1);

                }
                Console.SetCursorPosition(x - halfRing.Length * 2 + i + (halfRing.Contains("-") ? (-i + halfRing.Length) : 0), y);
                Console.WriteLine(halfRing + "|" + halfRing);
                y++;
            }
            if (x < 7) {
                x = 7;
            }
            Console.SetCursorPosition (x - 7, y); //print the base of the peg
            Console.WriteLine("----------------");
        }
    }
}

这是我的主要方法。

namespace Tower_of_hanoi
{
    class Program
    {
        static void Main(string[] args)
        {
            PegClass myPeg = new PegClass(8);
            PegClass myPeg2 = new PegClass(8);
            PegClass myPeg3 = new PegClass(8);
            DrawBoard(myPeg, myPeg2, myPeg3);
            Console.WriteLine ("\t\t\nWelcome to kTowers!");

            while (true) 
            {
                string input = "\nWhat peg do you want to move to commander?";
                Console.WriteLine (input);
                if (input == "2")
                {
                    myPeg.DrawPeg (2);
                }
                Console.ReadLine ();          
            }
        }

        public static void DrawBoard(PegClass peg1,PegClass peg2,PegClass peg3)
        {
            Console.Clear();
            peg1.DrawPeg(20,1);
            peg2.DrawPeg(40,2);
            peg3.DrawPeg(60,4);
        }
    }
}

这是当前的输出:

                |                   |                   |        
                |                   |                   |       
                |                   |                   |      
                |                   |                   |     
                |                   |                  -|-
                |                   |                 --|--
                |                  -|-               ---|---
               -|-                --|--             ----|----
         ----------------    ----------------    ----------------

我的问题仍然是,当被要求提示时,如何将' - '字符从peg移动到peg。我已经尝试了几个小时调整它仍然无法搞清楚。

提前谢谢你,你在外面

1 个答案:

答案 0 :(得分:3)

你已经将这些戒指表现为&#34;这个挂钩上有多少个戒指&#34;但这还不够。

例如,如果你有8个戒指,你将代表一个宽度为1的戒指,一个宽度为2的戒指,一个带有3个等等,最多为一个带8个戒指。

在您的图像中,您有3个宽度为1的环(每个挂钉上的顶部),2个宽度为2的环(两个挂环上的第二个环有多个环),依此类推。这是不正确的,你的代码为什么这样做的原因是它没有&#34;这个特定的环应该有多宽的概念&#34;而是它绘制宽度为1的顶部环,宽度为1的顶部环2,等等。

相反,这里有一组非常简单的对象来表示戒指和钉子以及从一个到另一个的移动操作:

public void MoveRing(Peg fromPeg, Peg toPeg)
{
    toPeg.Push(fromPeg.Pop());
}

public class Peg : Stack<Ring>
{
}

public struct Ring
{
    public int Width { get; }
    public Ring(int width) { Width = width; }
}

要在第一个挂钩上创建3个挂钩并堆叠8个环,您可以使用以下代码:

const int pegCount = 3;
const int ringCount = 8;

var pegs = Enumerable.Range(1, pegCount).Select(_ => new Peg()).ToList();

foreach (var ring in Enumerable.Range(1, ringCount).Select(width => new Ring(ringCount + 1 - width)))
    pegs[0].Push(ring);

为了绘制它们,我冒昧地充实了一个LINQPad程序来绘制它们以进行演示,但是你可以很容易地将它改编为你现在拥有的控制台代码:

void Main()
{
    const int pegCount = 3;
    const int ringCount = 8;

    var pegs = Enumerable.Range(1, pegCount).Select(_ => new Peg()).ToList();

    foreach (var ring in Enumerable.Range(1, ringCount).Select(width => new Ring(ringCount + 1 - width)))
        pegs[0].Push(ring);

    DrawPegs(pegs);
    MoveRing(pegs[0], pegs[1]);
    DrawPegs(pegs);
}

public void MoveRing(Peg fromPeg, Peg toPeg)
{
    toPeg.Push(fromPeg.Pop());
}

public class Peg : Stack<Ring>
{
}

public struct Ring
{
    public int Width { get; }
    public Ring(int width) { Width = width; }
}

public void DrawPegs(IEnumerable<Peg> pegs)
{
    var bitmaps = pegs.Select(peg => DrawPeg(peg));
    Util.HorizontalRun(true, bitmaps).Dump();
}

public Bitmap DrawPeg(Peg peg)
{
    const int width = 200;
    const int height = 300;
    const int pegWidth = 6;
    const int ringHeight = 20;
    const int ringWidthFactor = 10;
    const int ringGapHeight = 3;

    var result = new Bitmap(width, height);
    using (var g = Graphics.FromImage(result))
    {
        g.Clear(Color.White);

        g.FillRectangle(Brushes.Black, width / 2 - pegWidth/2, 0, pegWidth, height);
        int y = height;
        foreach (var ring in peg.Reverse())
        {
            y -= ringHeight;
            g.FillRectangle(Brushes.Blue, width / 2 - ring.Width * ringWidthFactor, y, 2 * ring.Width * ringWidthFactor, ringHeight);
            y -= ringGapHeight;
        }
    }
    return result;
}

输出:

pegs and rings