a_star #21

Merged
s464961 merged 15 commits from a_star into master 2022-04-27 19:50:51 +02:00
2 changed files with 35 additions and 26 deletions
Showing only changes of commit 6d4891fab8 - Show all commits

View File

@ -29,6 +29,12 @@ class State:
position: Tuple[int, int] position: Tuple[int, int]
direction: str direction: str
def __lt__(self, state):
return self.position < state.position
def __hash__(self) -> int:
return hash(self.position)
@dataclass @dataclass
class Node: class Node:
@ -38,6 +44,9 @@ class Node:
cost: int = field(init=False) cost: int = field(init=False)
depth: int = field(init=False) depth: int = field(init=False)
def __lt__(self, node):
return self.state < node.state
def __post_init__(self) -> None: def __post_init__(self) -> None:
self.cost = 0 if not self.parent else self.parent.cost + 1 self.cost = 0 if not self.parent else self.parent.cost + 1
self.depth = self.cost self.depth = self.cost

View File

@ -2,8 +2,7 @@ import random
import pygame import pygame
import algorithms.a_star as a_s from algorithms.a_star import a_star, State
from algorithms.bfs import graphsearch, State
from common.constants import * from common.constants import *
from common.helpers import castle_neighbors from common.helpers import castle_neighbors
from logic.knights_queue import KnightsQueue from logic.knights_queue import KnightsQueue
@ -107,37 +106,38 @@ class Level:
current_knight = self.knights_queue.dequeue_knight() current_knight = self.knights_queue.dequeue_knight()
knight_pos_x = current_knight.position[0] knight_pos_x = current_knight.position[0]
knight_pos_y = current_knight.position[1] knight_pos_y = current_knight.position[1]
state = State(knight_pos_y, knight_pos_x, current_knight.direction)
castle_cords = (self.list_castles[0].position[0], self.list_castles[0].position[1]) castle_cords = (self.list_castles[0].position[0], self.list_castles[0].position[1])
goal_list = castle_neighbors(self.map, castle_cords[0], castle_cords[1]) # list of castle neighbors goal_list = castle_neighbors(self.map, castle_cords[0], castle_cords[1]) # list of castle neighbors
print(knight_pos_x, knight_pos_y)
st = a_s.State((knight_pos_x, knight_pos_y), a_s.UP) state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
print(f'Actions: {a_s.actions(st, self.map)}') action_list = a_star(state, self.map, goal_list)
action_list = graphsearch(state, self.map, goal_list)
print(action_list) for action in action_list:
print(action)
print()
if len(action_list) == 0: if len(action_list) == 0:
return return
next_action = action_list.pop(0) # next_action = action_list.pop(0)
if next_action == ACTION.get("rotate_left"): # if next_action == ACTION.get("rotate_left"):
current_knight.rotate_left() # current_knight.rotate_left()
elif next_action == ACTION.get("rotate_right"): # elif next_action == ACTION.get("rotate_right"):
current_knight.rotate_right() # current_knight.rotate_right()
elif next_action == ACTION.get("go"): # elif next_action == ACTION.get("go"):
current_knight.step_forward() # current_knight.step_forward()
self.map[knight_pos_y][knight_pos_x] = ' ' # self.map[knight_pos_y][knight_pos_x] = ' '
#
# update knight on map # # update knight on map
if current_knight.direction.name == 'UP': # if current_knight.direction.name == 'UP':
self.map[knight_pos_y - 1][knight_pos_x] = current_knight # self.map[knight_pos_y - 1][knight_pos_x] = current_knight
elif current_knight.direction.name == 'RIGHT': # elif current_knight.direction.name == 'RIGHT':
self.map[knight_pos_y][knight_pos_x + 1] = current_knight # self.map[knight_pos_y][knight_pos_x + 1] = current_knight
elif current_knight.direction.name == 'DOWN': # elif current_knight.direction.name == 'DOWN':
self.map[knight_pos_y + 1][knight_pos_x] = current_knight # self.map[knight_pos_y + 1][knight_pos_x] = current_knight
elif current_knight.direction.name == 'LEFT': # elif current_knight.direction.name == 'LEFT':
self.map[knight_pos_y][knight_pos_x - 1] = current_knight # self.map[knight_pos_y][knight_pos_x - 1] = current_knight
def update(self): def update(self):
bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH