扩大我以前的职位,我仍然在写河内塔。在解释了如何在钉上画环之后,我仍然有一个问题,现在我已经摆弄了很长一段时间了。河内塔:从钉挂环到挂钩
这里是我的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);
}
}
}
这是电流输出:
| | |
| | |
| | |
| | |
| | -|-
| | --|--
| -|- ---|---
-|- --|-- ----|----
---------------- ---------------- ----------------
我的问题是,一个人如何在移动“ - ”字符从钉住钉住当被问及对于一个提示。我已经尝试了几个小时的调整,仍然无法弄清楚。
谢谢你在前进,youmeoutside
欢迎堆栈溢出!请在下次发布问题时,能否正确缩进?它不仅可以帮助人们阅读和理解你的代码,它还可以让你阅读并理解你的代码。 – Rob
为了更好地理解您的问题,您是否想要在控制台的原始输出上移动挂钩?或者你想在每次输入后重新绘制一个新的挂钩系统? – Ruskin
您必须创建戒指作为单独的对象。正如它现在显示你有三个相同宽度的戒指,这不是河内的塔,这是不同的。所以钉的高度是由多少个环给出的,但你需要将环实例化为具有宽度的具体物体。 –