擴大我以前的職位,我仍然在寫河內塔。在解釋瞭如何在釘上畫環之後,我仍然有一個問題,現在我已經擺弄了很長一段時間了。河內塔:從釘掛環到掛鉤
這裏是我的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
您必須創建戒指作爲單獨的對象。正如它現在顯示你有三個相同寬度的戒指,這不是河內的塔,這是不同的。所以釘的高度是由多少個環給出的,但你需要將環實例化爲具有寬度的具體物體。 –