From 6953eaf4cacf9951ef9a34a4e21bda936b84fe4c Mon Sep 17 00:00:00 2001 From: didi <2020201387@ruc.edu.cn> Date: Mon, 2 Oct 2023 17:46:18 +0800 Subject: [PATCH] =?UTF-8?q?merge=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/st_game/utils/utils.py | 110 ++++++++++++++++++++++++++------ 1 file changed, 89 insertions(+), 21 deletions(-) diff --git a/examples/st_game/utils/utils.py b/examples/st_game/utils/utils.py index 00c71e424..fb2341e0d 100644 --- a/examples/st_game/utils/utils.py +++ b/examples/st_game/utils/utils.py @@ -3,11 +3,11 @@ # @Desc : utils from typing import Any, Union +import os import json import openai from pathlib import Path import csv -import os from ..prompts.run_gpt_prompts import get_poignancy_action, get_poignancy_chat @@ -30,12 +30,12 @@ def write_json_file(json_file: str, data: list, encoding=None): def read_csv_to_list(curr_file: str, header=False, strip_trail=True): """ - Reads in a csv file to a list of list. If header is True, it returns a + Reads in a csv file to a list of list. If header is True, it returns a tuple with (header row, all rows) ARGS: - curr_file: path to the current csv file. - RETURNS: - List of list where the component lists are the rows of the file. + curr_file: path to the current csv file. + RETURNS: + List of list where the component lists are the rows of the file. """ if not header: analysis_list = [] @@ -94,21 +94,89 @@ def extract_first_json_dict(data_str: str) -> Union[None, dict]: # If parsing fails, return None return None -def check_if_file_exists(curr_file): - """ - Checks if a file exists - ARGS: - curr_file: path to the current csv file. - RETURNS: - True if the file exists - False if the file does not exist - """ - try: - with open(curr_file) as f_analysis_file: - pass - return True - except: - return False + +def path_finder_v2(a, start, end, collision_block_char) -> list[int]: + def make_step(m, k): + for i in range(len(m)): + for j in range(len(m[i])): + if m[i][j] == k: + if i > 0 and m[i - 1][j] == 0 and a[i - 1][j] == 0: + m[i - 1][j] = k + 1 + if j > 0 and m[i][j - 1] == 0 and a[i][j - 1] == 0: + m[i][j - 1] = k + 1 + if i < len(m) - 1 and m[i + 1][j] == 0 and a[i + 1][j] == 0: + m[i + 1][j] = k + 1 + if j < len(m[i]) - 1 and m[i][j + 1] == 0 and a[i][j + 1] == 0: + m[i][j + 1] = k + 1 + + new_maze = [] + for row in a: + new_row = [] + for j in row: + if j == collision_block_char: + new_row += [1] + else: + new_row += [0] + new_maze += [new_row] + a = new_maze + + m = [] + for i in range(len(a)): + m.append([]) + for j in range(len(a[i])): + m[-1].append(0) + i, j = start + m[i][j] = 1 + + k = 0 + except_handle = 150 + while m[end[0]][end[1]] == 0: + k += 1 + make_step(m, k) + + if except_handle == 0: + break + except_handle -= 1 + + i, j = end + k = m[i][j] + the_path = [(i, j)] + while k > 1: + if i > 0 and m[i - 1][j] == k - 1: + i, j = i - 1, j + the_path.append((i, j)) + k -= 1 + elif j > 0 and m[i][j - 1] == k - 1: + i, j = i, j - 1 + the_path.append((i, j)) + k -= 1 + elif i < len(m) - 1 and m[i + 1][j] == k - 1: + i, j = i + 1, j + the_path.append((i, j)) + k -= 1 + elif j < len(m[i]) - 1 and m[i][j + 1] == k - 1: + i, j = i, j + 1 + the_path.append((i, j)) + k -= 1 + + the_path.reverse() + return the_path + + +def path_finder(maze: "Maze", start: list[int], end: list[int], collision_block_char: str) -> list[int]: + # EMERGENCY PATCH + start = (start[1], start[0]) + end = (end[1], end[0]) + # END EMERGENCY PATCH + + path = path_finder_v2(maze, start, end, collision_block_char) + + new_path = [] + for i in path: + new_path += [(i[1], i[0])] + path = new_path + + return path def create_folder_if_not_there(curr_path): """ @@ -151,4 +219,4 @@ def find_filenames(path_to_dir, suffix=".csv"): """ filenames = os.listdir(path_to_dir) return [path_to_dir + "/" + filename - for filename in filenames if filename.endswith(suffix)] + for filename in filenames if filename.endswith(suffix)] \ No newline at end of file