From ac08e70afa5f737ae7ff8151ec7875c3e035f90c Mon Sep 17 00:00:00 2001 From: emil Date: Tue, 29 Oct 2024 23:31:42 +0300 Subject: [PATCH] implement initial A* base --- main2.py | 84 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 65 insertions(+), 19 deletions(-) diff --git a/main2.py b/main2.py index a6b3765..88f1e24 100644 --- a/main2.py +++ b/main2.py @@ -1,11 +1,13 @@ +MAP_SIZE = 8 +map = [] +start = (0, 0) #initial position of Neo +neo = start #position of Neo +keymaster = () #position of Keymaster def get_position_input(): position_input_list = (input().split(" ")) - position_input_tuple = () - for i in position_input_list: - position_input_tuple += (i,) - return position_input_tuple + return ((int)(position_input_list[0]), (int)(position_input_list[1])) def get_walkable_cells(): walkable_cells = [] @@ -20,7 +22,7 @@ def get_walkable_cells(): return walkable_cells def get_g(cell:tuple): - return abs((neo[0] - cell[0]) + (neo[1] - cell[1])) + return abs((start[0] - cell[0]) + (start[1] - cell[1])) #TODO FROM START OR NEO? def get_h(cell:tuple): return abs((keymaster[0] - cell[0]) + (keymaster[1] - cell[1])) @@ -28,19 +30,14 @@ def get_h(cell:tuple): def get_f(cell:tuple): return get_g(cell) + get_h(cell) -def set_position(new_postion:tuple): - neo = new_postion +def get_verified_move_position(new_postion:tuple): + if new_postion in get_walkable_cells(): + return new_postion + else: + print("CAN'T MOVE HERE!") #TEMP + return neo - - - -MAP_SIZE = 8 -map = [] -start = (0, 0) #initial position of Neo -neo = start #position of Neo -keymaster = () #position of Keymaster - perception_radius = input() position_input = get_position_input() @@ -48,11 +45,60 @@ keymaster = (position_input[0], position_input[1]) +print("neo:") print(neo) -print(get_walkable_cells()) +print("keymaster:") +print(keymaster) +walkable_cells_and_f = dict() +for i in get_walkable_cells(): + walkable_cells_and_f[i] = get_f(i) +min_f_cell = min(walkable_cells_and_f, key=walkable_cells_and_f.get) +print("min_f:") +print(min_f_cell) +#neo = get_verified_move_position(position_input) +print(walkable_cells_and_f) while (True): position_input = get_position_input() - set_position(position_input) + print(position_input) + neo = get_verified_move_position(position_input) + print("\nneo:") print(neo) - print(get_walkable_cells()) \ No newline at end of file + print("\nkeymaster:") + print(keymaster) + print("\nwalkabe cells") + #A* + walkable_cells_and_f = dict() + for i in get_walkable_cells(): + walkable_cells_and_f[i] = get_f(i) + min_f_cell = min(walkable_cells_and_f, key=walkable_cells_and_f.get) + min_f_value = walkable_cells_and_f[min_f_cell] + + # Находим минимальное значение f + min_f_value = min(walkable_cells_and_f.values()) + + # Собираем все ячейки с минимальным значением f + min_f_cell_list = [cell for cell, f_value in walkable_cells_and_f.items() if f_value == min_f_value] + + # Если только одна ячейка с минимальным f, выбираем её + if len(min_f_cell_list) == 1: + next_cell = min_f_cell_list[0] + else: + # Если таких ячеек несколько, выбираем ту, у которой минимальный h + next_cell = min(min_f_cell_list, key=lambda cell: get_h(cell)) + + + + + print("min_f:") + print(min_f_cell) + print("min_f_count:") + print(len(min_f_cell_list)) + #neo = get_verified_move_position(position_input) + for i in walkable_cells_and_f.items(): + cell = i[0] + print(cell) + print(f"{get_g(cell)} + {get_h(cell)} = {get_f(cell)}") + print(walkable_cells_and_f) + print("next_cell:") + print(next_cell) \ No newline at end of file