diff --git a/utils.py b/utils.py index c4e8779..6a243ee 100644 --- a/utils.py +++ b/utils.py @@ -36,7 +36,16 @@ def fetch_rooms(path): return assembly,background - +def check_map(carte): + """Function to check if a map is valid i.e. if no two rooms are at the same location + """ + positions = [] + for room in carte: + if room[2] in positions: + return(False) + else: + positions.append(room[2]) + return(True) def map_generator(n): """Map generator generate a map with a main path of n rooms @@ -53,6 +62,7 @@ def map_generator(n): start_label,start_room = random.choice(assembly[start_key]) # Place the first room in the list + positions = [] position = [0,0] asset = pygame.image.load(start_room).convert_alpha() mask = pygame.mask.from_surface(asset) @@ -63,7 +73,9 @@ def map_generator(n): start_pos = start_poss[dir_from] current_label = start_label - for i in range(n): + counter = 0 + while counter < n: + # select the next direction, can't be the direction of arrival dir_next = random.choice(current_label.replace(dir_from,'')) @@ -89,15 +101,25 @@ def map_generator(n): next_position = [position[0]-room_side,position[1]] dir_from = 'R' - # Build the map - carte.append((asset,mask,next_position)) + # Check if we are not overwriting an existing room and store the current one. + if next_position not in positions: + counter += 1 + # Build the map + carte.append((asset,mask,next_position)) + positions.append(position) - # Update the label for next turn - current_label = next_label - position = next_position + # Update the variables for next turn + current_label = next_label + position = next_position + positions.append(position) background = pygame.image.load(back_path).convert() + if not check_map(carte): + print("Invalid Map.") + else: + print("Map checked and valid!") + return(carte,start_pos,background)