diff --git a/bfs.py b/bfs.py deleted file mode 100644 index bd42ea7..0000000 --- a/bfs.py +++ /dev/null @@ -1,51 +0,0 @@ -import settings -import math - - -def generate_neighbours_list(): - width = settings.Field.horizontal_count() - height = settings.Field.vertical_count() - keys = [width for width in range(height * width)] - neighbours_list = {key: [] for key in keys} - for i in range(height): - for j in range(width): - point = i * width + j - # point_row = math.floor(point/width) - # point_column = point % width - point_left = i * width + j - 1 - point_left_column = point_left - width * i - point_right = i * width + j + 1 - point_right_column = point_right % width - point_up = (i - 1) * width + j - point_up_row = math.floor(point_up / width) - point_down = (i + 1) * width + j - point_down_row = math.floor(point_down / width) - if point_left_column >= 0: - neighbours_list[point].append(point_left) - if point_right_column > 0: - neighbours_list[point].append(point_right) - if point_up_row >= 0: - neighbours_list[point].append(point_up) - if point_down_row < height: - neighbours_list[point].append(point_down) - return neighbours_list - - -def generate_fields_lists(fields): - width = settings.Field.horizontal_count() - height = settings.Field.vertical_count() - fields_to_visit = [] - for i in range(height): - for j in range(width): - point = i * width + j - if fields[j][i] == 'dirt': - fields_to_visit.append(point) - return fields_to_visit - - -def generate_all(fields): - neighbours_list = generate_neighbours_list() - fields_to_visit = generate_fields_lists(fields) - print(neighbours_list) - print(fields_to_visit) - diff --git a/field.py b/field.py index 93333d8..7121b36 100644 --- a/field.py +++ b/field.py @@ -105,4 +105,4 @@ class Sand(Field): def __init__(self): super(Sand, self).__init__() -# Tile Types END \ No newline at end of file +# Tile Types END diff --git a/graph.py b/graph.py new file mode 100644 index 0000000..7292ac1 --- /dev/null +++ b/graph.py @@ -0,0 +1,58 @@ +import settings +import math +from collections import defaultdict + + +class Instance: + def __init__(self): + self.graph = defaultdict(list) + + def config(self, fields): + adjacency_list = self.generate_graph() + fields_to_visit = self.generate_target_fields(fields) + print("Adjacency list: " + str(dict(adjacency_list))) + print("List of targets: " + str(fields_to_visit)) + + def add_edge(self, u, v): + self.graph[u].append(v) + + def generate_graph(self): + width = settings.Field.horizontal_count() + height = settings.Field.vertical_count() + + for i in range(height): + for j in range(width): + point = i * width + j + # point_row = math.floor(point/width) + # point_column = point % width + point_left = i * width + j - 1 + point_left_column = point_left - width * i + point_right = i * width + j + 1 + point_right_column = point_right % width + point_up = (i - 1) * width + j + point_up_row = math.floor(point_up / width) + point_down = (i + 1) * width + j + point_down_row = math.floor(point_down / width) + + if point_left_column >= 0: + self.add_edge(point, point_left) + if point_right_column > 0: + self.add_edge(point, point_right) + if point_up_row >= 0: + self.add_edge(point, point_up) + if point_down_row < height: + self.add_edge(point, point_down) + + return self.graph + + @staticmethod + def generate_target_fields(fields): + width = settings.Field.horizontal_count() + height = settings.Field.vertical_count() + fields_to_visit = [] + for i in range(height): + for j in range(width): + point = i * width + j + if fields[j][i] == 'dirt': + fields_to_visit.append(point) + return fields_to_visit diff --git a/main.py b/main.py index a7834e9..d1ca567 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import pygame import random -import bfs +import graph import field import settings import common @@ -16,7 +16,6 @@ possibleFields = { 'sand': field.Sand(), } - def randomize_map(): fields_array = [] for i in possibleFields: @@ -36,7 +35,6 @@ def randomize_map(): field_array_small_2 = [] return field_array_big, field_array_big_2 - def read_img(agent, fields): window = common.get('window') current_field = fields[agent.x()][agent.y()] @@ -53,12 +51,10 @@ def read_img(agent, fields): pygame.time.delay(2000) common.set('state_imgShown', False) - def agent_action(action: str): if action == 'open_window': common.set('state_imgShown', True) - def draw_window(agent, fields): window = common.get('window') rect = agent.rect @@ -68,10 +64,9 @@ def draw_window(agent, fields): window.blit(AGENT, (rect.x, rect.y)) pygame.display.update() - common = common.Instance() agent = agent.Instance(1000) - +graph = graph.Instance() def main(): common.set('game_running', True) @@ -85,7 +80,7 @@ def main(): pygame.display.set_caption(settings.Pygame.display_name()) fields, fields_2 = randomize_map() - bfs.generate_all(fields_2) + graph.config(fields_2) while common.get('game_running'): pygame.time.Clock().tick(settings.Pygame.fps()) @@ -102,6 +97,5 @@ def main(): pygame.quit() - if __name__ == "__main__": main()