如何用 Javascript 生成激光游戏的关卡?

发布于 2024-11-25 15:52:34 字数 726 浏览 1 评论 0原文

我正在尝试为我的愚蠢游戏生成一个随机级别。该游戏包括在可能的镜子的方形区域周围放置激光/探测器对。像这样:

/*

LLLLLLLLLL
LmmmmmmmmL
LmmmmmmmmL
LmmmmmmmmL
LLLLLLLLLL

*/

现在,我有一个算法,可以通过依赖随机放置并拒绝不良位置来生成水平,但效果很差。这不是很快,并且没有真正生成我想要的字段。请随时在 http://cmouse.drespect.org/laser/ 尝试一下。

任何建议欢迎。

当前的算法看起来像这样:

function createLevel:
  for i=0 to mirrors:
    mirrorLocation = pickRandomPosition
    mirrorType = pickRandomType

    if (verifyPosition(mirrorLocation, mirrorType)):
       i = i - 1
       next
    else:
       storeMirror(mirrorLocation, mirrorType)

在 verifyPosition 中,我们测试镜子是否在所有四个方向上都到达激光,希望避免无法检测到镜子。这是有点无聊的代码,所以我在这里省略它。

I am trying to generate a random level for my silly game. The game consists of having laser/detector pairs around a square field of possible mirrors. Like this:

/*

LLLLLLLLLL
LmmmmmmmmL
LmmmmmmmmL
LmmmmmmmmL
LLLLLLLLLL

*/

Now, I have an algorithm which generates a level, quite poorly, by relying on random placement, and rejecting bad positions. This is not very fast, and does not really generate the kind of fields I'd like to have. Please feel free to try it out at http://cmouse.desteem.org/laser/

Any suggestions are welcome.

The current algorithm looks something like this:

function createLevel:
  for i=0 to mirrors:
    mirrorLocation = pickRandomPosition
    mirrorType = pickRandomType

    if (verifyPosition(mirrorLocation, mirrorType)):
       i = i - 1
       next
    else:
       storeMirror(mirrorLocation, mirrorType)

In verifyPosition, we test the mirror that it reaches a laser in all four directions, in hope of avoiding undetectedable mirrors. It is somewhat boring code, so I omit it here.

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

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

发布评论

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

评论(1

撩动你心 2024-12-02 15:52:34

确保它不会多次尝试多个字段的一种方法是迭代这些字段并根据某种概率放置或不放置镜像。放置镜像的概率应该是#mirros / #fields,这样期望的镜像数量就是最后的#mirrors。

One way to make sure it's not trying multiple fields more than once is to iterate over the fields and put a mirror or not based on some probability. The probability to put a mirror should be #mirros / #fields, so that the expected number of mirrors is #mirrors at the end.

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