微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

河内塔:移动钉从钉到钉

扩大我以前的职位,我仍然在写河内的塔。 在解释了如何在钉上画环之后,我仍然有一个问题,现在我已经摆弄了很长一段时间了。

这是我的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 ("ttnWelcome 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); } } }

这是目前的产出:

如何指导创build一个uPNP iPhone应用程序?

如何获得一个稳定的机器ID **没有**pipe理员权限

在Linux / Ubuntu上大规模部署.net单声道应用程序

C# – 文件path的正则expression式,例如C: test test.exe

计算目录大小

| | | | | | | | | | | | | | -|- | | --|-- | -|- ---|--- -|- --|-- ----|---- ---------------- ---------------- ----------------

我的问题仍然存在,当问及提示时,如何将“ – ”字符从挂钩移到挂钩。 我已经尝试了几个小时,但仍然无法弄清楚。

提前谢谢你,你好

我如何检索显示器信息?

检查CD-ROM托盘状态

在CentOS或Linux上使用Mono执行.Net应用程序

我如何杀死我的移动应用程序?

如何打开Windows证书查看器(不是pipe理器)

你已经将这些戒指表现为“这个钉子上有多少个戒指”,但这还不够。

例如,如果你有8个戒指,你将代表一个宽度为1的戒指,一个宽度为2,一个为3,等等。

在你的图像中,你有三个宽度为1的环(每个钉上的最高一个),2个宽度为2(两个钉上有多个环的第二个环),依此类推。 这是不正确的,为什么你的代码这样做的原因是它没有“这个特定的环应该有多宽”的概念,而是绘制宽度为1的顶圈,宽度为2的顶圈。

相反,这是一组非常简单的对象来表示环和挂钩以及从一个移动到另一个的操作:

public void MoveRing(Peg fromPeg,Peg toPeg) { toPeg.Push(fromPeg.Pop()); } public class Peg : Stack<Ring> { } public struct Ring { public int Width { get; } public Ring(int width) { Width = width; } }

要创建3个挂钩并在第一个挂钩上堆叠8个环,可以使用以下代码

const int pegCount = 3; const int ringCount = 8; var pegs = Enumerable.Range(1,pegCount).Select(_ => new Peg()).ToList(); foreach (var ring in Enumerable.Range(1,ringCount).Select(width => new Ring(ringCount + 1 - width))) pegs[0].Push(ring);

为了绘制它们,我冒充了一个LINQPad程序来绘制它们来展示,但是你可以很容易地将它应用到现在的控制台代码中:

void Main() { const int pegCount = 3; const int ringCount = 8; var pegs = Enumerable.Range(1,ringCount).Select(width => new Ring(ringCount + 1 - width))) pegs[0].Push(ring); DrawPegs(pegs); MoveRing(pegs[0],pegs[1]); DrawPegs(pegs); } public void MoveRing(Peg fromPeg,Peg toPeg) { toPeg.Push(fromPeg.Pop()); } public class Peg : Stack<Ring> { } public struct Ring { public int Width { get; } public Ring(int width) { Width = width; } } public void DrawPegs(IEnumerable<Peg> pegs) { var bitmaps = pegs.Select(peg => DrawPeg(peg)); Util.HorizontalRun(true,bitmaps).Dump(); } public Bitmap DrawPeg(Peg peg) { const int width = 200; const int height = 300; const int pegWidth = 6; const int ringHeight = 20; const int ringWidthFactor = 10; const int ringGapHeight = 3; var result = new Bitmap(width,height); using (var g = Graphics.FromImage(result)) { g.Clear(Color.White); g.FillRectangle(Brushes.Black,width / 2 - pegWidth/2,pegWidth,height); int y = height; foreach (var ring in peg.Reverse()) { y -= ringHeight; g.FillRectangle(Brushes.Blue,width / 2 - ring.Width * ringWidthFactor,y,2 * ring.Width * ringWidthFactor,ringHeight); y -= ringGapHeight; } } return result; }

输出

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐