如何实现迷宫人?

发布于 2024-12-11 02:11:59 字数 510 浏览 0 评论 0原文

我正在考虑设计像《迷宫中的人》这样简单的练习。

我想制作 Maze 对象,在其中放入一些 Man 对象,然后为每个对象调用solve() 以离开迷宫。 人类不应该对迷宫一无所知。它只能尝试去寻找出路。

我是这样看的:

    maze = Maze.new

    man1 = Man.new
    man2 = Man.new
    man3 = Man.new

    maze.put(man1,man2,man3)

    maze.men.each do |man| { man.solve }

但是如何实现这些类的主体呢? 除非我给一个迷宫实例,否则人怎么知道如何在迷宫中行走? 但如果我这样做:

    maze.put(man1(maze),man3(maze),man2(maze))

如果我可以给他们提供迷宫实例,那么为什么要把男人放进迷宫呢?

这是我不明白的地方,也找不到优雅的解决方案。

i am thinking about design for such simple exercise as Man in Maze.

I would like to make Maze object, put some Man objects inside it and call solve() for each to get out of Maze.
Man should know nothing about Maze. It just can try to go and find way out.

Here is how i see it:

    maze = Maze.new

    man1 = Man.new
    man2 = Man.new
    man3 = Man.new

    maze.put(man1,man2,man3)

    maze.men.each do |man| { man.solve }

But how to implement body of these classes?
How can man know how to walk inside Maze unless I give Maze instance to him?
But if i do that:

    maze.put(man1(maze),man3(maze),man2(maze))

What the reason to put men inside Maze if I can just give Maze instance to them?

This is what i don't understand and can't find elegant solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有 2024-12-18 02:11:59

这样想:每个类属于哪些数据?它有哪些方法可以处理这些数据?这就是面向对象:数据和相关代码的捆绑。

迷宫类不需要了解任何人。这是一个迷宫。它只是一些墙壁,一些结构。就这样构建它,就好像它之外什么都不存在一样。制定一些方法来获取有关迷宫的信息,例如某个点是否有一堵墙、一个角落、一个交叉点……

就您而言,Man 类的设计目的只有一个:找到走出迷宫的路。因此,一个人应该在迷宫中拥有一个位置(他的数据的一部分)以及一些帮助他寻找出口的方法。例如,一些深度优先搜索算法。

当然,您可以将当前位于迷宫中的所有男人的一些集合嵌入迷宫本身。但这是没有必要的。您可以使用任何其他集合来保存人员并迭代它们。

maze = Maze.new

man1 = Man.new(maze)
...
men = Array.new
men[0] = man1
...

men.each do |man| {man.solve}

Think of it this way: what data belongs with each class? What methods belong with it to work with that data? This is object-orientation: the bundling of data and relevant code.

The Maze class doesn't need to know about any men. It's a maze. All it is are some walls, some structure. Build it as such, as if nothing outside of it exists. Make some methods to obtain information about the maze, such as whether there's a wall at some point, a corner, a junction...

The Man class is, in your case, conceived for a single purpose: find his way out of the maze. A Man therefore should have a position in the maze (part of his data) and some methods that help him look for the exit. Some depth-first search algorithm, for example.

Of course, you could have some collection of all men currently in a Maze embedded in the maze itself. But that's not necessary. You could use any other collection to hold the men and iterate over them.

maze = Maze.new

man1 = Man.new(maze)
...
men = Array.new
men[0] = man1
...

men.each do |man| {man.solve}
初相遇 2024-12-18 02:11:59

您可以添加一个名为 Map 的新类。迷宫可以有一个名为 GetMap 的方法,它返回迷宫的地图。这张地图可以用来检查人类是否采取了正确的行动。
因此,地图将成为迷宫和人类之间的共同点。但如果您想跟踪迷宫内人的当前位置,这可能对您没有帮助。

You can add a new class called Map. The maze can have a method called GetMap which returns the Map of the maze. This map can be used to check whether man is making the right moves.
So, map would be the common ground between maze and man. But this may not help you if you want to track the current location of the man inside the maze.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文