From 33577c1824025741121c2493b5b1f3bc55204684 Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Mon, 9 May 2022 18:22:01 +0200 Subject: [PATCH] hotfix --- common/helpers.py | 4 ++-- learning/decision_tree.py | 8 ++++---- logic/level.py | 5 ++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/common/helpers.py b/common/helpers.py index eef852f..b8d68e9 100644 --- a/common/helpers.py +++ b/common/helpers.py @@ -58,7 +58,7 @@ def castle_neighbors(map, castle_bottom_right_row, castle_bottom_right_col): return neighbors -def find_neighbours(col: int, row: int) -> List[Tuple[int, int]]: +def find_neighbours(grid: List[List[str]], col: int, row: int) -> List[Tuple[int, int]]: dr = [-1, 1, 0, 0] dc = [0, 0, -1, 1] @@ -70,7 +70,7 @@ def find_neighbours(col: int, row: int) -> List[Tuple[int, int]]: if rr < 0 or cc < 0: continue if rr >= ROWS or cc >= COLUMNS: continue - + if grid[rr][cc] not in ['g', 's', '.']: continue neighbours.append((rr, cc)) return neighbours diff --git a/learning/decision_tree.py b/learning/decision_tree.py index ea36d88..18c1c47 100644 --- a/learning/decision_tree.py +++ b/learning/decision_tree.py @@ -34,7 +34,7 @@ class DecisionTree: self.model = DecisionTreeClassifier(criterion='entropy') self.model.fit(self.train_set.values, self.goals) - def predict_move(self, map: List[List[str]], current_knight: Knight, castle: Castle, monsters: List[Monster], + def predict_move(self, grid: List[List[str]], current_knight: Knight, castle: Castle, monsters: List[Monster], opponents: List[Knight]) -> \ List[Tuple[int, int]]: distance_to_castle = manhattan_distance(current_knight.position, castle.position) @@ -59,14 +59,14 @@ class DecisionTree: agent_hp=current_knight.current_hp) print(prediction) if prediction == 'tower': # castle... - return castle_neighbors(map, castle_bottom_right_row=castle.position[0], + return castle_neighbors(grid, castle_bottom_right_row=castle.position[0], castle_bottom_right_col=castle.position[1]) elif prediction.startswith('opp'): idx = parse_idx_of_opp_or_monster(prediction) - return find_neighbours(opponents[idx].position[1], opponents[idx].position[0]) + return find_neighbours(grid, opponents[idx].position[1], opponents[idx].position[0]) else: idx = parse_idx_of_opp_or_monster(prediction) - return find_neighbours(monsters[idx].position[1], monsters[idx].position[0]) + return find_neighbours(grid, monsters[idx].position[1], monsters[idx].position[0]) def get_prediction(self, tower_dist: int, mob1_dist: int, mob2_dist: int, opp1_dist: int, opp2_dist: int, opp3_dist: int, opp4_dist: int, agent_hp: int, tower_hp: int, mob1_hp: int, mob2_hp: int, diff --git a/logic/level.py b/logic/level.py index fe1713f..0491a4d 100644 --- a/logic/level.py +++ b/logic/level.py @@ -113,12 +113,15 @@ class Level: knight_pos_x = current_knight.position[0] knight_pos_y = current_knight.position[1] - goal_list = self.decision_tree.predict_move(map=self.map, current_knight=current_knight, + goal_list = self.decision_tree.predict_move(grid=self.map, current_knight=current_knight, monsters=self.list_monsters, opponents=self.list_knights_red if current_knight.team_alias == 'k_r' else self.list_knights_blue, castle=self.list_castles[0]) + if len(goal_list) == 0: + return + state = State((knight_pos_y, knight_pos_x), current_knight.direction.name) action_list = a_star(state, self.map, goal_list) print(action_list)