1 分钟阅读

2024-11-06

在上周我已经开始尝试第一部分的内容,即生成一个符合要求的地图,要求如下:

  • The world must be a 2D grid, drawn using our tile engine. The tile engine is described in lab5.
  • The world must be pseudorandomly generated. Pseudorandomness is discussed in lab 5.
  • The generated world must include rooms and hallways, though it may also include outdoor spaces.
  • At least some rooms should be rectangular, though you may support other shapes as well.
  • Your game must be capable of generating hallways that include turns (or equivalently, straight hallways that intersect).
  • The world should contain a random number of rooms and hallways.
  • The locations of the rooms and hallways should be random.
  • The width and height of rooms should be random.
  • The length of hallways should be random.
  • Rooms and hallways must have walls that are visually distinct from floors. Walls and floors should be visually distinct from unused spaces.
  • Rooms and hallways should be connected, i.e. there should not be gaps in the floor between adjacent rooms or hallways.
  • The world should be substantially different each time, i.e. you should not have the same basic layout with easily predictable features.

难点就在于要在随机性的基础上生成一个可以自由移动、含有要求的基本元素的地图。
我的思路是先生成随机数量,随机大小的方形房间;然后在房间的随机方向生成连接其与其他房间的出入口;再随机生成一定数量和方向的路径,最后的路径根据计算得到,以达到连接各个房间的目的。
这其中需要注意的是:

  • x,y 坐标的含义,本次我一开始就是搞错了 x,y 坐标的指代,耽误了一些时间;
  • 房间可能会有相互覆盖,目前我的解决方案是让 FLOOR 元素的优先级高于 WALL,即有 WALL 的地方可以覆盖为 FLOOR,但有 FLOOR 的地方不可被 WALL 覆盖;
  • 使用 RANDOM.nextInt()随机数生成时会遇到阻塞,我按照这篇博客修改了”F:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.1\jbr\conf\security\java.security”文件,将 securerandom.source=file:/dev/random 替换成:securerandom.source=file:/dev/./urandom,但是问题似乎没有得到解决。这个针对的是 SecureRandom,我使用的是普通 Random。

留下评论