如何用 Javascript 生成激光游戏的关卡?
我正在尝试为我的愚蠢游戏生成一个随机级别。该游戏包括在可能的镜子的方形区域周围放置激光/探测器对。像这样:
/*
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保它不会多次尝试多个字段的一种方法是迭代这些字段并根据某种概率放置或不放置镜像。放置镜像的概率应该是#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.