diff --git a/main.py b/main.py index 7cd3b1d..b86f5a9 100644 --- a/main.py +++ b/main.py @@ -49,9 +49,34 @@ def generate_world(seed): def terrain_matches(cell1, cell2): return cell1["is_land"] == cell2["is_land"] or cell1["is_water"] == cell2["is_water"] +def get_from_coords(world, x, y): + if y >= 0 and x>= 0 and y < len(world) and x < len(world[y]): + return world[y][x] + return None + +def get_adjacent(world, x, y): + north = None + east = None + south = None + west = None + + if y - 1 >= 0: + north = get_from_coords(world, x, y - 1) + + if x + 1 < world_w: + east = get_from_coords(world, x + 1, y) + + if y + 1 < world_h: + south = get_from_coords(world, x, y + 1) + + if x - 1 >= 0: + west = get_from_coords(world, x - 1, y) + + return north, east, south, west + + def group_terrain(world): - world_terrain_without_group = [] - group_id = 0 + groups = [[], []] for y in range(world_h): row = [] for x in range(world_w): @@ -59,40 +84,21 @@ def group_terrain(world): terrain = { "v": cell, "x": x, - "y": y, - "is_land": cell > water, - "is_water": cell <= water + "y": y } - row.append(terrain) - world_terrain_without_group.append(row) + 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 - world_terrain = [] - for y in range(world_h): - row = [] - for x in range(world_w): - cell = world_terrain_without_group[y][x] - north = world_terrain_without_group[y - 1][x] if y > 0 else None - south = world_terrain_without_group[y][x] if y < world_h else None - east = world_terrain_without_group[y][x] if x < world_w else None - west = world_terrain_without_group[y][x - 1] if x > 0 else None - if north and terrain_matches(cell, north): - print(f"{x}:{y} - north") - elif east and terrain_matches(cell, east): - print(f"{x}:{y} - east") - elif south and terrain_matches(cell, south): - print(f"{x}:{y} - south") - elif west and terrain_matches(cell, west): - print(f"{x}:{y} - west") - # else: - # print("new group") - # cell["group_id"] = group_id - # group_id += 1 - #print(cell) - row.append(cell) - world_terrain.append(row) - - print(f"Number of groups: {group_id}") - return world_terrain + print(groups) + return groups def add_home(world): print("Choosing home location") @@ -159,6 +165,7 @@ def print_map(world, home): def print_terrain(world): colours = ["grey", "red", "green", "yellow", "blue", "magenta", "cyan", "white"] + colours = colours + colours + colours + colours print(" ", end="", flush=True) for x in range(world_w): to_print = str(x) + " " if x < 10 else x