# codereview


03/18/2021, 4:15 AM
I created a world generator for a game I'm making: Thoughts on it?
Copy code
import biomes.Biome
import biomes.OakForest
import biomes.Savannah
import kotlin.random.Random

class World {
    private var worldSize: Int = 0
    private var biomes: MutableMap<IntRange, Biome> = mutableMapOf()

    data class PointData(val currentPoint: Int?, val currentBiome: Biome?)

    fun getPointData(point: Int): PointData {
        biomes.keys.forEach {
            if (it.contains(point)) {
                return PointData(point, biomes[it])
        return PointData(null, null)

    companion object {

        fun generateWorld(): World {
            val it = World()
            it.worldSize = (Random.nextInt(100000, 1000000))

            var blocksCovered = 0
            var priorBiomeEndPoint = 0

            while (blocksCovered <= it.worldSize) {
                val possibleBiomes: List<Biome> = listOf(OakForest(), Savannah())
                val rand: Biome = possibleBiomes[Random.nextInt(0, 2)]
                val size = blocksCovered + rand.biomeSize
                it.biomes[priorBiomeEndPoint..size] = rand

                priorBiomeEndPoint = blocksCovered + rand.biomeSize
                blocksCovered += rand.biomeSize

            return it
It will return at particular Int Ranges a certain biome:


03/18/2021, 1:04 PM
could use extensions:
Copy code
fun getPointData(point: Int): PointData {
    return biomes.values.firstOrNull { (range, _) -> point in range }
        ?.let { (_, biome) -> PointData(point, biome) }
        ?: PointData(null, null)

Matteo Mirk

03/25/2021, 9:09 AM
a few advices: • make World immutable; initialize data locally inside generateWorld() and then create a World instance passing constructor parameters • try to avoid nulls in PointData: when is the case where a point can’t be found? Can it happen? If yes, try to model the absence of a value either with a special value, or a sealed class representing NoPointData, or if you can’t avoid it have getPointData return
• don’t know how you modeled biomes, but since they’re a finite set of types, you could use an enum or sealed classes, depending on your need • having a map but iterating its entries every time to find a value, smells like the wrong data structure for the job; probably you need a binary search tree or similar


03/26/2021, 3:35 AM
@Matteo Mirk thanks for this 👍
👌 1