From e98a4da2aa9c2fa0edf24cbe58663da07cb361a5 Mon Sep 17 00:00:00 2001 From: Angelika Iskra Date: Mon, 11 Apr 2022 12:56:22 +0200 Subject: [PATCH] finished bfs; --- algorithms/bfs.py | 16 +++++++--------- common/helpers.py | 17 +++++++++++++++++ logic/level.py | 5 +++-- models/castle.py | 1 + 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/algorithms/bfs.py b/algorithms/bfs.py index 2e76d45..316ecf1 100644 --- a/algorithms/bfs.py +++ b/algorithms/bfs.py @@ -3,8 +3,9 @@ from typing import List # temporary goal for testing from common.constants import ACTION, Direction, ROWS, COLUMNS +from common.helpers import castle_neighbors -GOAL = (9, 9) +GOAL_LIST = [] class State: @@ -21,8 +22,8 @@ class Node: self.action = action -def goal_test(state: State): - if (state.row, state.column) == GOAL: +def goal_test(goal_list, state: State): + if (state.row, state.column) in goal_list: return True return False @@ -45,16 +46,13 @@ def get_successors(state: State, map): return successors -def graphsearch(initial_state: State, map, fringe: List[Node] = None, explored: List[Node] = None, - tox: int = None, toy: int = None): +def graphsearch(initial_state: State, map, goal_cords, fringe: List[Node] = None, explored: List[Node] = None): # fringe and explored initialization - global GOAL if fringe is None: fringe = list() if explored is None: explored = list() - if tox is not None and toy is not None: - GOAL = (tox, toy) + goal_list = castle_neighbors(map, goal_cords[0], goal_cords[1]) explored_states = set() fringe_states = set() @@ -73,7 +71,7 @@ def graphsearch(initial_state: State, map, fringe: List[Node] = None, explored: fringe_states.remove((element.state.row, element.state.column, element.state.direction)) # if solution was found, prepare and return actions sequence - if goal_test(element.state): + if goal_test(goal_list, element.state): actions_sequence = [element.action] parent = element.parent diff --git a/common/helpers.py b/common/helpers.py index b3d51c5..dd13e31 100644 --- a/common/helpers.py +++ b/common/helpers.py @@ -28,3 +28,20 @@ def print_numbers(): # parse array index to screen x or y coordinate def parse_cord(cord): return (GRID_CELL_PADDING + GRID_CELL_SIZE) * cord + GRID_CELL_PADDING + 7 + + +def castle_neighbors(map, castle_bottom_right_row, castle_bottom_right_col): + neighbors = [] + for row_add in range(-2, 2): + new_row = castle_bottom_right_row + row_add + if 0 <= new_row <= len(map) - 1: + for col_add in range(-2, 2): + new_col = castle_bottom_right_col + col_add + if 0 <= new_col <= len(map) - 1: + if (new_col == castle_bottom_right_col - 1 and new_row == castle_bottom_right_row - 1) \ + or (new_col == castle_bottom_right_col and new_row == castle_bottom_right_row - 1) \ + or (new_col == castle_bottom_right_col - 1 and new_row == castle_bottom_right_row) \ + or (new_col == castle_bottom_right_col and new_row == castle_bottom_right_row): + continue + neighbors.append((new_col, new_row)) + return neighbors diff --git a/logic/level.py b/logic/level.py index dbfb807..6c5d012 100644 --- a/logic/level.py +++ b/logic/level.py @@ -102,12 +102,13 @@ class Level: def handle_turn(self): print("next turn") - # current_knight = self.list_knights_red[0] current_knight = self.knights_queue.dequeue_knight() knight_pos_x = current_knight.position[0] knight_pos_y = current_knight.position[1] state = State(knight_pos_y, knight_pos_x, current_knight.direction) - action_list = graphsearch(state, self.map) + + castle_cords = (self.list_castles[0].position[0], self.list_castles[0].position[1]) + action_list = graphsearch(state, self.map, castle_cords) print(action_list) if len(action_list) == 0: diff --git a/models/castle.py b/models/castle.py index d558d6e..629f524 100644 --- a/models/castle.py +++ b/models/castle.py @@ -8,6 +8,7 @@ class Castle(pygame.sprite.Sprite): super().__init__(group) self.image = pygame.image.load("./resources/textures/castle.png").convert_alpha() self.image = pygame.transform.scale(self.image, (78, 78)) + self.position = position position_in_px = (parse_cord(position[0]), parse_cord(position[1])) self.rect = self.image.get_rect(center=position_in_px) self.health = 80