generate classic water pour puzzle level: all tubes are of the same volume each color…
- User
generate classic water pour puzzle level:
all tubes are of the same volume
each color has the same volume randomly portioned and poured in all tubes
except two which are empty
so the number of colors is equal to number of tubes minus two
func make_random_classic_level(colors: int, volume: int) -> bool: var MIN_COLORS := 3 var MIN_VOLUME := 2 if colors < MIN_COLORS: print_debug("Wrong 'colors' number, setting to MIN") colors = MIN_COLORS if colors > Globals.MAX_COLORS || colors > Globals.MAX_TUBES - 2: print_debug("Wrong 'colors' number, setting to MAX") colors = Globals.MAX_COLORS if volume < MIN_VOLUME: print_debug("Wrong 'volume', setting to MIN") colors = MIN_VOLUME if volume > Globals.MAX_TUBE_VOLUME: print_debug("Wrong 'volume', setting to MAX") colors = Globals.MAX_TUBE_VOLUME
# make empty tubes var tube: Array var tubes: Array = [] for i in colors + 2: tube = [] tube.resize(volume) tube.fill(0) tubes.append(tube) # make random palette var palette: Array = [] for i in Globals.MAX_COLORS + 1: if i == 0: continue palette.append(i) randomize() palette.shuffle() palette.resize(colors) var portions: Array = [] portions.resize(colors) portions.fill(volume)print(palette, portions)
# fill tubes with colors from palette var iterations := colors * volume var tube_por: int var tube_num: int var random_color_index: int for i in iterations: tube_por = i % volume tube_num = int((i - tube_por) / volume)print("%s / %s" % [tube_num, tube_por])
random_color_index = randi() % palette.size() portions[random_color_index] -= 1 tubes[tube_num][tube_por] = palette[random_color_index] if portions[random_color_index] == 0: palette.remove(random_color_index) portions.remove(random_color_index)print_debug(tubes)
# init level if !set_tubes(tubes): print_debug("Error while setting random tubes, classic level generation aborted") return falseif data.has("drains") && !set_drains(data.drains):
print_debug("Error while setting drains, import aborted")
return false
make_reset_copy() return truegenerate classic water pour puzzle level
where every tube has only faucets
func make_random_classic_faucet_level(colors: int, volume: int) -> bool: if make_random_classic_level(colors, volume): var drains: Array = [] drains.resize(get_tubes_number()) drains.fill(2) # set two tubes to '1', but not last two empty tubes var t1 = randi() % (drains.size() - 3) drains[t1] = 1 var t2 = randi() % (drains.size() - 3) if t2 == t1: if t2 % 2 == 0 && t2 > 0: t2 -= 1 else: t2 += 1 drains[t2] = 1
if set_drains(drains): return true else: print_debug("Error while setting drains for random classic faucet level") return false else: return false - Assistant
This code generates a classic water pour puzzle level, where all tubes have the same volume and each color has the same volume randomly portioned and poured into all tubes, except for two that are empty. The number of colors is equal to the number of tubes minus two. There is also a function to generate a classic water pour puzzle level where every tube has only faucets. Both functions check for errors and return false if there is an issue.