fix: integration with level

This commit is contained in:
korzepadawid 2022-04-12 00:35:01 +02:00
parent 286164c1dd
commit 6d4891fab8
2 changed files with 35 additions and 26 deletions

View File

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

View File

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