简体   繁体   English

C#中的河内塔可视化问题

[英]Towers of Hanoi Visualisation Problems in C#

I've got a Problem with my Console Output I receive always exceptions and can't fix the bug the solution already worked but the stack grows from top to bottom, so I tried to sort my Elements by Whitespace.Count to reverse that order hope you can help me, here is my code: 我的控制台输出出现问题,我总是收到异常并且无法解决该解决方案已经解决的错误,但是堆栈从上到下增长,因此我尝试按Whitespace排序我的Elements.Count以颠倒该顺序的希望您可以帮我,这是我的代码:

public class TvH
{
    #region
    IDictionary<string, int> towerMapping = new Dictionary<string, int>()
    {
        {"Start", 0}, {"Mitte", 1}, {"Ziel", 2}
    };

    private string emptyTower = "     |     ";
    private int turns = 0;
    Stack<string>[] towers;
    Dictionary<int, string> discs = new Dictionary<int, string>() 
        {
            {1, "-----|-----"},
            {2, " ----|---- "},
            {3, "  ---|---  "},
            {4, "   --|--   "},
            {5, "    -|-    "}
        };
    #endregion

    public TvH(int towerCount)
    {
        towers = new Stack<string>[towerCount];
        initializeTowers();
    }

    private void initializeTowers()
    {
        for (int i = 0; i < towers.Length; i++)
        {
            towers[i] = new Stack<string>();
        }
        for (int i = 1; i <= discs.Count; i++)
        {
            towers[0].Push(discs[i]);
            towers[1].Push(emptyTower);
            towers[2].Push(emptyTower);
        }
    }

    public void bewegeScheibe(int n, string a, string b, string c)
    {  
        if (n > 0)
        {
            bewegeScheibe(n - 1, a, c, b);   
            turns++;
            Console.Write("\nZug # ");
            if (turns < 10)
            {
                Console.Write("0");
            }
            Console.WriteLine("{0}      Disc # {1}      {2} --> {3}\n", turns, n, a, c);
            move(a, c);
            bewegeScheibe(n - 1, b, a, c);                
        }
    }

    private void move(string start, string target)
    {
        var element = towers[towerMapping[start]].Pop();
        towers[towerMapping[target]].Push(element);

        printContent();
    }

    private void printContent()
    {
        IList<string> t1 = prepairTowerForPrint(towers[0].GetEnumerator());
        IList<string> t2 = prepairTowerForPrint(towers[1].GetEnumerator());
        IList<string> t3 = prepairTowerForPrint(towers[2].GetEnumerator());

        int i = 0;
        while (i < discs.Count)
        {                
            object ob1 = t1[i];
            object ob2 = t2[i];
            object ob3 = t3[i];

            Console.WriteLine("\t{0}\t{1}\t{2}", ob1, ob2, ob3);
            i++;
        }
    }

    private IList<string> prepairTowerForPrint(Stack<string>.Enumerator enumerator)
    {
        IList<string> towerList = new List<string>();
        while (enumerator.MoveNext())
        {
            towerList.Add(TryReadNext(enumerator));
        }
        towerList = towerList.OrderByDescending(scheiben => scheiben.Count(Char.IsWhiteSpace)).ToList();
        return towerList;
    }

    private string TryReadNext(IEnumerator ce)
    {
        if (ce.MoveNext())
        {
            return (string)ce.Current;
        }
        else
        {
            return emptyTower;
        }
    }     
}

thank you very much 非常感谢你

The problem is that you assume that each stack always has five discs, yet in the method move you pop an element from one and push it on another. 问题是,你假设每个堆栈始终有五盘,但在方法movepop从一个元素,并push它在另一个。 Hence the error. 因此,错误。

So you'd need to remove one element from towers[towerMapping[target]] and add one to towers[towerMapping[start]]. 因此,您需要从towers [towerMapping [target]]中删除一个元素,然后将其添加到towers [towerMapping [start]]中。

Can I also do a little code review? 我还能做一点代码审查吗?

Why is discs a dictionary? 为什么discs是字典? It would make for more sense to make it a List() and simply loop through it: 将其设置为List()并简单地循环遍历它会更有意义:

List<string> discs = new List<string>() 
{
    "-----|-----",
    " ----|---- ",
    "  ---|---  ",
    "   --|--   ",
    "    -|-    "
};

foreach(var dics in discs)
{
    towers[0].Push(disc);
    towers[1].Push(emptyTower);
    towers[2].Push(emptyTower);
}

Why do you do towers = new Stack<string>[towerCount]; 你为什么要做towers = new Stack<string>[towerCount]; in the TvH constructor instead of in the method called initializeTowers() ? 在TvH构造函数中而不是在称为initializeTowers()的方法中?

The method prepairTowerForPrint contains a typo, the verb is to prepare . prepairTowerForPrint方法包含一个错字,动词是prepare

Method names in German are a bad idea, IMHO. 德语中的方法名称是个坏主意,恕我直言。 Also, method names should be PascalCase . 同样, 方法名称应为PascalCase

The only public method is bewegeScheibe() but it's parameters are actually part of the TvH class: "Start", "Mitte", "Ziel". 唯一的公共方法是bewegeScheibe()但是它的参数实际上是TvH类的一部分:“开始”,“ Mitte”,“ Ziel”。 It's first parameter is int n , which says absolutely nothing; 它的第一个参数是int n ,它什么也没说。 I only figured out what string a, string b, string c were expected to be by reading the code. 通过阅读代码string a, string b, string c我只知道应该使用什么string a, string b, string c

Ok I solved another way, here's the code if anybody needs 好吧,我解决了另一种方法,如果有人需要,这是代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TuermeVonHanoi
{
public class TvH
{
    #region
    IDictionary<string, int> towerMapping = new Dictionary<string, int>()
    {
        {"start", 0}, {"middle", 1}, {"target", 2}
    };

    private string emptyTower = "     |     ";
    private int turns = 0;
    Stack<string>[] towers;

    IList<string> discs = new List<string>()
    {
        {"-----|-----"},
        {" ----|---- "},
        {"  ---|---  "},
        {"   --|--   "},
        {"    -|-    "}
    };
    #endregion

    public TvH(int towerCount)
    {
        towers = new Stack<string>[towerCount];
        initializeTowers();
    }

    private void initializeTowers()
    {
        for (int i = 0; i < towers.Length; i++)
        {
            towers[i] = new Stack<string>();
        }

        foreach (var d in discs)
        {
            towers[0].Push(d);
        }
    }

    public void moveAlgorithmic(int n, string a, string b, string c)
    {  
        if (n > 0)
        {
            moveAlgorithmic(n - 1, a, c, b);   
            ++turns;
            Console.Write("\nturn # ");
            if (turns < 10)
            {
                Console.Write("0");
            }
            Console.WriteLine("{0}      disc # {1}      {2} --> {3}\n", turns, n, a, c);
            moveVisual(a, c);
            moveAlgorithmic(n - 1, b, a, c);                
        }
    }

    private void moveVisual(string start, string target)
    {   
        var element = towers[towerMapping[start]].Pop();
        towers[towerMapping[target]].Push(element);                        
        printContent();
    }

    private void printContent()
    {
        IList<string> t1 = prepareTowerForPrint(towers[0].GetEnumerator());
        IList<string> t2 = prepareTowerForPrint(towers[1].GetEnumerator());
        IList<string> t3 = prepareTowerForPrint(towers[2].GetEnumerator());
        int i = 0;
        while (i < discs.Count)
        {
            object ob1 = t1[i];
            object ob2 = t2[i];
            object ob3 = t3[i];           
            Console.WriteLine("\t{0}\t{1}\t{2}", ob1, ob2, ob3);
            ++i;
        }
    }

    private IList<string> prepareTowerForPrint(Stack<string>.Enumerator enumerator)
    {
        IList<string> towerList = new List<string>();
        int count = 0;
        while (enumerator.MoveNext())
        {
            ++count;
            towerList.Add(enumerator.Current);
        }

        int toPush = discs.Count - count;
        for (int i = 0; i < toPush; i++ )
        {
            towerList.Add(emptyTower);
        }
        if (toPush != 0 || toPush != 1)
        {
            towerList = towerList.OrderByDescending(d => d.Count(Char.IsWhiteSpace)).ToList();
        }
        return towerList;
    }
}

} }

instead of initializing all 3 stacks we calculate how many empty pin elements have to be printed out for every stack 而不是初始化所有3个堆栈,我们计算每个堆栈必须打印多少个空别针元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM