我的控制台输出出现问题我总是遇到异常并且无法修复解决方案已经解决的错误但是堆栈从上到下增长,所以我尝试按以下方式对我的元素进行排序 Whitespace.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;
}
}
}
非常感谢
答案 0 :(得分:0)
问题在于你假设每个堆栈总是有五张光盘,但在move
方法中你pop
一个元素来自一个而push
它在另一个上。因此错误。
所以你需要从塔中删除一个元素[towerMapping [target]]并将一个元素添加到塔[towerMapping [start]]。
我还可以进行一些代码审查吗?
为什么discs
是一本字典?将它作为List()并简单地遍历它会更有意义:
List<string> discs = new List<string>()
{
"-----|-----",
" ----|---- ",
" ---|--- ",
" --|-- ",
" -|- "
};
foreach(var dics in discs)
{
towers[0].Push(disc);
towers[1].Push(emptyTower);
towers[2].Push(emptyTower);
}
为什么在TvH构造函数中而不是在名为towers = new Stack<string>[towerCount];
的方法中执行initializeTowers()
?
方法prepairTowerForPrint
包含拼写错误,动词是准备。
德语中的方法名称是个坏主意,恕我直言。另外,method names should be PascalCase。
唯一的公共方法是bewegeScheibe()
,但它的参数实际上是TvH类的一部分:&#34;开始&#34;,&#34; Mitte&#34;,&#34; ZIEL&#34 ;.它的第一个参数是int n
,它绝对没有说明;我只是通过阅读代码找出了string a, string b, string c
预期的内容。
答案 1 :(得分:0)
好的,我解决了另一种方式,如果有人需要,这里是代码
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;
}
}
}
而不是初始化所有3个堆栈,我们计算每个堆栈必须打印多少个空引脚元素