feat: movement
This commit is contained in:
parent
6d4891fab8
commit
7131d1f905
@ -29,6 +29,9 @@ class State:
|
|||||||
position: Tuple[int, int]
|
position: Tuple[int, int]
|
||||||
direction: str
|
direction: str
|
||||||
|
|
||||||
|
def __eq__(self, other: State) -> bool:
|
||||||
|
return other.position == self.position and self.direction == other.direction
|
||||||
|
|
||||||
def __lt__(self, state):
|
def __lt__(self, state):
|
||||||
return self.position < state.position
|
return self.position < state.position
|
||||||
|
|
||||||
@ -44,16 +47,13 @@ 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):
|
def __lt__(self, node) -> None:
|
||||||
return self.state < node.state
|
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
|
||||||
|
|
||||||
def __eq__(self, other: Node) -> bool:
|
|
||||||
return self.state == other.state
|
|
||||||
|
|
||||||
def __hash__(self) -> int:
|
def __hash__(self) -> int:
|
||||||
return hash(self.state)
|
return hash(self.state)
|
||||||
|
|
||||||
@ -158,11 +158,12 @@ def f(current_node: Node, goal: Tuple[int, int]) -> int:
|
|||||||
|
|
||||||
|
|
||||||
def get_path_from_start(node: Node) -> List[str]:
|
def get_path_from_start(node: Node) -> List[str]:
|
||||||
path = [node]
|
path = [node.action]
|
||||||
|
|
||||||
while node.parent is not None:
|
while node.parent is not None:
|
||||||
node = node.parent
|
node = node.parent
|
||||||
path.append(node.action)
|
if node.action:
|
||||||
|
path.append(node.action)
|
||||||
|
|
||||||
path.reverse()
|
path.reverse()
|
||||||
return path
|
return path
|
||||||
|
@ -2,7 +2,7 @@ import random
|
|||||||
|
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
from algorithms.a_star import a_star, State
|
from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD, UP, DOWN, LEFT, RIGHT
|
||||||
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
|
||||||
@ -112,32 +112,29 @@ class Level:
|
|||||||
|
|
||||||
state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
|
state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
|
||||||
action_list = a_star(state, self.map, goal_list)
|
action_list = a_star(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 == TURN_LEFT:
|
||||||
# current_knight.rotate_left()
|
current_knight.rotate_left()
|
||||||
# elif next_action == ACTION.get("rotate_right"):
|
elif next_action == TURN_RIGHT:
|
||||||
# current_knight.rotate_right()
|
current_knight.rotate_right()
|
||||||
# elif next_action == ACTION.get("go"):
|
elif next_action == FORWARD:
|
||||||
# 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
|
||||||
|
Loading…
Reference in New Issue
Block a user