- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
扩展我之前的帖子,我还在写汉诺塔。在解释了如何在钉子上画环的绝妙解决方案之后,我仍然有一个问题,我已经摆弄了很长一段时间了。
这是我的 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。我已经尝试调整它几个小时,但仍然无法弄清楚。
提前谢谢你,youmeoutside
最佳答案
您已经将环显化为“这个钉子上有多少个环”,但这还不够。
例如,如果您有 8 个环,您将代表一个宽度为 1 的环、一个宽度为 2 的环、一个宽度为 3 的环,等等,直到一个宽度为 8。
在您的图像中,您有 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, pegCount).Select(_ => new Peg()).ToList();
foreach (var ring in 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, 0, 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;
}
输出:
关于c# - 汉诺塔 : Moving Rings from Peg to Peg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34669652/
C++ 中的标准“映射”容器允许您插入右值: T x; std::map m; // m[1]; // populate "1" auto it = m.insert(std::make_pair(
我们知道 std::move does not actually move anything .它只是将左值引用 (&) 转换为右值引用 (&&)。 那么在下面的例子中,拷贝构造函数是如何被调用的呢?
http://en.cppreference.com/w/cpp/language/rule_of_three 几个月前我开始使用 c++11并观看了五人规则。 所以..我开始将复制构造函数/复制赋值
这个问题在这里已经有了答案: In what scenarios should I expect to explicitly need to implement a move constructor
我有一个类似于下面的对象,我正在尝试为它实现一个 move 构造函数,这样你就可以为 std::vector 插入一个. struct Mesh { std::vector vPoint
这个问题在这里已经有了答案: How to create an std::function from a move-capturing lambda expression? (3 个回答) 关闭7年前
我有一个源文件,我正在使用它 move 到一个存档目录 关闭。 move (srcfile,dstdir) 但是当存档目标目录中已经存在相同的文件时,它会抛出一个错误,指出无法 move 文件已经存在
这应该有效,但无效并给出以下错误(如下)。 我读过几篇关于 stackoverflow 的帖子 here和 here但在这种情况下似乎没有一个好的答案。我真的希望我只是错过了一些愚蠢的东西,我已经在这
我似乎无法弄清楚为什么会这样。当我运行以下代码时: $uref = APACHE_ROOT . UPLOAD_PATH . $applicant . "_ref_{$email}_{$year}";
我似乎无法弄清楚为什么会这样。当我运行以下代码时: $uref = APACHE_ROOT . UPLOAD_PATH . $applicant . "_ref_{$email}_{$year}";
我的表格行可以上下 move ,但我的问题是数据表行取代了表格标题(第一行)。 我想要一个固定的第一行,这样当您单击向上箭头时,您就不会向上 move 该行来替换标题。 我尝试了一些条件逻辑来检查当前
正如我在Move constructor/operator=中询问的那样,过了一段时间,我同意并接受了这个问题的正确答案,我只是在想,是否有类似“移动析构函数” 这样的东西会在每次移动的对象上调用会有
如果我有一个像这样的 C 类: class C { std::string s; public: C(std::string& s) : s(s) {} C(std::str
我是 C++11 的新手,发现 move 语义和复制省略非常适合编写优雅高效的代码。不过我有一些问题想请教。这里我写了一个模板类 matrix.hpp 并用它来测试 move 语义的行为。 #incl
我在我们的项目中遇到了这样的代码: class A { public: A(A&& obj): valid_(false), data_(obj.data_) {} //... void
move 语义在这个例子中是如何工作的: struct test { int ii[10]; int i; }; test f() { test a; std::cou
假设我有一个类型为 A 的对象 a。 如果我想将其 move 到函数foo(A)。 一个选择是执行 foo(std::move(a)),这将调用 move 构造函数。 但是,假设我正在使用一个我无法控
我用 move 复制构造函数和 move 复制赋值运算符创建了一个简单的应用程序,并且在它们中的每一个上我都做了一个 cout 语句来告诉我,它们正在执行。但是在执行过程中,我没有看到 move 复制
相关问题: Why this move constructor is not called wtih rvalue temporary? [duplicate] Move Constructor vs
我正在努力研究 move 构造函数,并希望通过这个问题获得更多见解。这是一个简单的类。 class A { private: vector Bs; public: /* ..
我是一名优秀的程序员,十分优秀!