finished bfs;
This commit is contained in:
parent
88297d0b9b
commit
e98a4da2aa
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user