diff --git a/main.py b/main.py index be45ce3..b44d245 100644 --- a/main.py +++ b/main.py @@ -1,301 +1,141 @@ -import pygame as pg +import time +MAP_SIZE = 9 +start = (0, 0) +neo = start +observer = neo +keymaster = () -class Cell: - coordinates = (0, 0) - position = (0, 0) - size = 50 - perceptor = None - content = None - rect = pg.Rect(0, 0, size, size) - border_width = 2 - closed = False - g = 1 - h = 2 - f = g + h - def __init__(self, position): - self.set_position(position) - self.rect = pg.Rect(self.coordinates[0], self.coordinates[1], - self.size - self.border_width, self.size - self.border_width) - def set_position(self, position): - self.position = position - self.coordinates = (self.position[0] * self.size, - self.position[1] * self.size) - def set_content(self, content): - self.content = content - self.content - def set_perceptor(self, perceptor): - self.perceptor = perceptor - def clear_perceptor(self): - self.perceptor = None - def draw(self): - pg.draw.rect(screen, "white", self.rect) - - if self.perceptor == None: - pg.draw.rect(screen, "white", self.rect) - else: - pg.draw.rect(screen, self.perceptor.color, self.rect) - - g_font = pg.font.Font(None, 14) - text = g_font.render((str)(self.g), True, "black") - screen.blit(text, ((self.coordinates[0] + self.size * 0.05, self.coordinates[1] + self.size * 0.75))) - - h_font = pg.font.Font(None, 14) - text = h_font.render((str)(self.h), True, "black") - screen.blit(text, ((self.coordinates[0] + self.size * 0.8, self.coordinates[1] + self.size * 0.75))) - - f_font = pg.font.Font(None, 16) - text = f_font.render((str)(self.f), True, "maroon") - screen.blit(text, ((self.coordinates[0] + self.size * 0.425, self.coordinates[1] + self.size * 0.7))) - - def calculate_cost(self): - self.g = abs(self.position[0] - neo.position[0]) + abs(self.position[1] - neo.position[1]) - def calculate_heuristic(self): - self.h = abs(self.position[0] - keymaker.position[0]) + abs(self.position[1] - keymaker.position[1]) - def calculate_estimated(self): - self.f = self.g + self.h +green_cells = [] #open cells + +red_cells = [] #closed cells - +black_cells = [] #blocked cells = +blue_cells = [] #traversed cells # -class Map: - cell_matrix = [] - dimensions = (8, 8) - def __init__(self, dimensions:tuple): - self.dimensions = dimensions - for x in range(dimensions[0]): - column = [] - for y in range(dimensions[1]): - column.append(Cell((x,y))) - self.cell_matrix.append(column) - def get_cell(self, x, y): - if (x < self.dimensions[0] and y < self.dimensions[1]): - return self.cell_matrix[x][y] - def calculate_costs(self): - for x in range(self.dimensions[0]): - for y in range(self.dimensions[1]): - self.get_cell(x, y).calculate_cost() - self.get_cell(x, y).calculate_heuristic() - self.get_cell(x, y).calculate_estimated() - def draw(self): - for x in range(self.dimensions[0]): - for y in range(self.dimensions[1]): - self.get_cell(x, y).draw() - +steps_count = 0 +#accumulated_g = 0 +map_dict = dict() + +def initialize_weighted_map_dict(): + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): + map_dict[(x, y)] = [float("inf"), float("inf"), float("inf"), '.'] # (x,y) : (h, g, f, status) -class Entity: - coordinates = (0, 0) - position = (0, 0) - offset = (0, 0) - cell = None - name = "" - color = "black" - font_size = 32 - def __init__(self, cell_position): - self.set_cell(map.get_cell(cell_position[0], cell_position[1])) - def set_cell(self, cell): - self.cell = cell - self.position = cell.position - self.coordinates = (self.cell.coordinates[0] + self.offset[0], - self.cell.coordinates[1] + self.offset[1]) - self.cell.set_content(self) - def set_position(self, x, y): - self.set_cell(map.get_cell(x, y)) - def draw(self): - font = pg.font.Font(None, self.font_size) - text = font.render(self.name, True, self.color) - screen.blit(text, ((self.cell.coordinates[0] + self.offset[0], self.cell.coordinates[1] + self.offset[1]))) +def print_map(): + map_str = "" - + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): + if map_dict[(x,y)][3] in "kon": + set_status((x,y), '.') - pass -class Actor(Entity): - perception_radius = 1 - percepted_cells = [] - pass - -class Neo(Actor): - open_set = [] - def __init__(self, cell_position): - super().__init__(cell_position) - self.color = (100, 100, 255) - self.name = "neo" - def set_position(self, x, y): - map.get_cell(x, y).closed = True - self.update_open_set() - return super().set_position(x, y) - def update_open_set(self): - - self.open_set = [] - - estimated_cells = [] - estimated_cells.append((self.position[0] + 1, self.position[1])) - estimated_cells.append((self.position[0] - 1, self.position[1])) - estimated_cells.append((self.position[0], self.position[1] + 1)) - estimated_cells.append((self.position[0], self.position[1] - 1)) - for i in estimated_cells: - if 0 <= i[0] < map.dimensions[0] and 0 <= i[1] < map.dimensions[1]: - if (map.get_cell(i[0], i[0]).closed == False): - self.open_set.append(map.get_cell(i[0], i[1])) - - - def percept(self): - #clear percepted celles - for i in self.percepted_cells: - if (i != None): - i.clear_perceptor() - self.percepted_cells = [] - - self.open_set = [] + for x in range(MAP_SIZE): + for y in range(MAP_SIZE): - for x in range(self.position[0] - self.perception_radius, - self.position[0] + self.perception_radius + 1): - for y in range(self.position[1] - self.perception_radius, - self.position[1] + self.perception_radius + 1): - if 0 <= x < map.dimensions[0] and 0 <= y < map.dimensions[1]: - if (x != self.position[0] or y != self.position[1]): - self.percepted_cells.append(map.get_cell(x, y)) #set current percieved cells - + set_status(keymaster, 'k') + set_status(observer, 'o') + set_status(neo, 'n') - #set perceptors - for i in self.percepted_cells: - if (i != None): - i.set_perceptor(self) - map.draw() - pg.display.update() + map_str += " " + map_dict[(x, y)][3] + " " + map_str += "\n" + print(map_str) + +def get_g(cell, accumulated_g): # TODO MAYBE FIX NEEDED + return accumulated_g + 1 + +def get_h(cell): + return abs(keymaster[0] - cell[0]) + abs(keymaster[1] - cell[1]) + +def get_f(cell, accumulated_g): + if map_dict[cell][3] == '=': + return float("inf") + return get_g(cell, accumulated_g) + get_h(cell) + +def get_walkable_cells(actor:tuple): + potential_positions = [ + (actor[0], actor[1] + 1), (actor[0], actor[1] - 1), + (actor[0] - 1, actor[1]), (actor[0] + 1, actor[1]) + ] + return [pos for pos in potential_positions if pos[0] in range(MAP_SIZE) and pos[1] in range(MAP_SIZE)] + +def get_position_input(): + position_input_list = input().split(" ") + return int(position_input_list[0]), int(position_input_list[1]) + +def set_status(position:tuple, status:str): + map_dict[position][3] = status + +def print_cells_paremeters(cells:list): + str = "" + for cell in cells: + str += f"({cell[0]},{cell[1]}): {map_dict[cell][0]} + {map_dict[cell][1]} = {map_dict[cell][2]} ({map_dict[cell][3]}) | " + print(str) +def calculate_next_cell(actor, accumulated_g): + + fs = [] + for cell in get_walkable_cells(actor): + fs.append(get_f(cell, accumulated_g)) + min_f = min(fs) + + min_cells_by_f = [] + for cell in get_walkable_cells(actor): + if get_f(cell, accumulated_g) == min_f: + min_cells_by_f.append(cell) + + hs = [] + for cell in min_cells_by_f: + hs.append(get_h(cell)) + min_h = min(hs) + + min_cells_by_h = [] + for cell in min_cells_by_f: + if get_h(cell) == min_h: + min_cells_by_h.append(cell) + + next_cell = min_cells_by_h[0] + return next_cell + +def regenerate_route(): + global green_cells, red_cells, black_cells, neo, observer + accumulated_g = 0 + observer = neo + previous_cell = () + finish = False + while not finish: - pass -class Smith(Actor): - def __init__(self, cell_position): - super().__init__(cell_position) - self.color = "red" - self.name = "smith" - self.font_size = 26 - def percept(self): - #clear percepted celles - for i in self.percepted_cells: - i.clear_perceptor() - self.percepted_cells = [] - - for x in range(self.position[0] - self.perception_radius, - self.position[0] + self.perception_radius + 1): - for y in range(self.position[1] - self.perception_radius, - self.position[1] + self.perception_radius + 1): - if (x != self.position[0] or y != self.position[1]): - self.percepted_cells.append(map.get_cell(x, y)) #set current percieved cells - - #set perceptors - for i in self.percepted_cells: - i.set_perceptor(self) - pass -class Sentinel(Actor): - def __init__(self, cell_position): - super().__init__(cell_position) - self.color = "orange" - self.name = "sentinel" - self.font_size = 18 - def percept(self): - #clear percepted celles - for i in self.percepted_cells: - i.clear_perceptor() - self.percepted_cells = [] - - stencil_cells_positions = [] - stencil_cells_positions.append((self.position[0], self.position[1] + 1)) - stencil_cells_positions.append((self.position[0] - 1, self.position[1])) - stencil_cells_positions.append((self.position[0], self.position[1])) - stencil_cells_positions.append((self.position[0] + 1, self.position[1])) - stencil_cells_positions.append((self.position[0], self.position[1] - 1)) - for i in stencil_cells_positions: - if (i[0] != self.position[0] or i[1] != self.position[1]): - self.percepted_cells.append(map.get_cell(i[0], i[1])) #set current percieved cells - - #set perceptors - for i in self.percepted_cells: - i.set_perceptor(self) - pass -class Keymaker(Entity): - def __init__(self, cell_position): - super().__init__(cell_position) - self.color = "green" - self.name = "key maker" - self.font_size = 14 - pass -class Backdoor_key(Entity): - pass + # setting green cells + for cell in get_walkable_cells(observer): + if map_dict[cell][3] not in "kon-#=": + set_status(cell, '+') + + for cell in get_walkable_cells(observer): + map_dict[cell][0] = get_h(cell) + map_dict[cell][1] = get_g(cell, accumulated_g) + map_dict[cell][2] = get_f(cell, accumulated_g) + + + next_cell = calculate_next_cell(observer, accumulated_g) + + print_cells_paremeters(get_walkable_cells(observer)) + print_map() + + previous_cell = observer + observer = next_cell + + set_status(previous_cell, '-') + + accumulated_g += 1 + red_cells.append(next_cell) + + time.sleep(0.5) + if observer == keymaster: + finish = True +perception_radius = 2 #input() +keymaster = (6,4) #get_position_input() -def read_initial_inputs(): - neo.perception_radius = (int)(input()) - keymaker_position = input().split(" ") - keymaker.set_position((int)(keymaker_position[0]), (int)(keymaker_position[1])) - -def read_input(): - inpt = input().split(" ") - return inpt - -def move(x, y): - for i in neo.open_set: - print(i.position) - if map.get_cell(x, y) in neo.open_set: #TODO: FIX CHECK - neo.set_position(x, y) - print(f"m {x} {y}") - else: - print("ERROR: CAN'T MOVE HERE") - neo.update_open_set() - - -map = Map((8, 8)) -neo = Neo((3, 3)) -neo.perception_radius = 2 -smith = Smith((1,1)) -sentinel = Sentinel((3,4)) -keymaker = Keymaker((5,5)) -pg.init() -screen = pg.display.set_mode([1124,1124]) -pg.display.set_caption("Matrix Universe") -running = True -#pg.time.delay(7000) -neo.set_position(0, 0) #TODO: ITS TEMPORARY -neo.update_open_set() -move(0, 0) -read_initial_inputs() -pg.time.delay(1500) -while running: - for event in pg.event.get(): - if event.type == pg.QUIT: - running = False - screen.fill("black") - map.calculate_costs() - map.draw() - neo.percept() - neo.draw() - - keymaker.draw() - pg.display.update() - - move_input = read_input() - pg.time.delay(1500) - - move((int)(move_input[0]), (int)(move_input[1])) - - - pg.display.update() - ''' - - - - neo.set_position(5,6) - - smith.draw() - smith.percept() - - sentinel.draw() - sentinel.percept() - - keymaker.draw() - - ''' - - - #pg.time.delay(1000) - +initialize_weighted_map_dict() +set_status((0,4),'=') +set_status((1,3),'=') # TODO FIX STUCK IN A DEAD END ISSUE +regenerate_route() diff --git a/main2.py b/main2.py index 76d5479..9c746d8 100644 --- a/main2.py +++ b/main2.py @@ -125,7 +125,7 @@ while not finish: next_cell = min(min_f_cell_list, key=lambda cell: get_h(cell)) neo = next_cell accumulated_g += 1 # TODO ENSURE THAT g WORKS PROPERLY - closed_cells.append(next_cell) + closed_cells.append(next_cell) # TODO DELETE passed_cells.append(next_cell) print(f"m {next_cell[1]} {next_cell[0]}") # TODO FIX OR ENSURE THAT x,y OR y,x DOES NOT MAKE ANY DIFFERENCE steps_count += 1