From b29703ee97dd40d9f9e63b20c9715dd68d706820 Mon Sep 17 00:00:00 2001 From: Lukewh Date: Mon, 17 Jan 2022 23:15:34 +0000 Subject: [PATCH] found the masses --- main.py | 56 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index b86f5a9..b859f47 100644 --- a/main.py +++ b/main.py @@ -23,6 +23,8 @@ grass_high = 0.6 mountains = 1 block = "█" block = block + block +LAND = "land" +WATER = "water" def generate_world(seed): try: @@ -46,8 +48,8 @@ def generate_world(seed): except Exception as e: print(e) -def terrain_matches(cell1, cell2): - return cell1["is_land"] == cell2["is_land"] or cell1["is_water"] == cell2["is_water"] +def terrain_matches(cell1, cell2, key): + return cell1[key] == cell2[key] def get_from_coords(world, x, y): if y >= 0 and x>= 0 and y < len(world) and x < len(world[y]): @@ -76,29 +78,47 @@ def get_adjacent(world, x, y): def group_terrain(world): - groups = [[], []] + terrain = [] + to_visit = [] for y in range(world_h): row = [] for x in range(world_w): cell = world[y][x] - terrain = { + cell_with_details = { "v": cell, "x": x, - "y": y - } - if cell > water: - groups[0].append(terrain) - else: - groups[1].append(terrain) - for group in groups: - sub_group_id = 1 - for i, cell in enumerate(group): - if i == 0: - cell["group_id"] = sub_group_id - elif + "y": y, + "type": LAND if cell > water else WATER + } + row.append(cell_with_details) + to_visit.append(cell_with_details) + terrain.append(row) - print(groups) - return groups + visited = [] + def lets_walk(w, x, y, visited, group_id): + cell = get_from_coords(w, x, y) + cell["group_id"] = group_id + visited.append(cell) + to_visit.remove(cell) + + north, east, south, west = get_adjacent(w, x, y) + if north and not north in visited and terrain_matches(cell, north, "type"): + lets_walk(w, north["x"], north["y"], visited, group_id) + if east and not east in visited and terrain_matches(cell, east, "type"): + lets_walk(w, east["x"], east["y"], visited, group_id) + if south and not south in visited and terrain_matches(cell, south, "type"): + lets_walk(w, south["x"], south["y"], visited, group_id) + if west and not west in visited and terrain_matches(cell, west, "type"): + lets_walk(w, west["x"], west["y"], visited, group_id) + + group_id = 0 + while len(to_visit) > 0: + lets_walk(terrain, to_visit[0]["x"], to_visit[0]["y"], visited, group_id) + group_id += 1 + + print(group_id) + + return terrain def add_home(world): print("Choosing home location")