扩大我之前的帖子,我仍然在写河内塔.在解释了如何在钉子上画出环之后,我有一个很好的解决方案,我仍然有一个问题,我现在已经摆弄了很长一段时间.
这是我的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.我已经尝试了几个小时调整它仍然无法搞清楚.
提前谢谢你,你在外面