finding neighbour tiles

This commit is contained in:
korzepadawid 2022-05-08 19:26:09 +02:00
parent 7030488a8a
commit 8b76d3635b
4 changed files with 41 additions and 16 deletions

View File

@ -4,25 +4,15 @@ import heapq
from dataclasses import dataclass, field
from typing import Tuple, Optional, List
from common.constants import ROWS, COLUMNS
from common.constants import ROWS, COLUMNS, LEFT, RIGHT, UP, DOWN
from common.helpers import directions
EMPTY_FIELDS = ['s', 'g', ' ']
LEFT = 'LEFT'
RIGHT = 'RIGHT'
UP = 'UP'
DOWN = 'DOWN'
TURN_LEFT = 'TURN_LEFT'
TURN_RIGHT = 'TURN_RIGHT'
FORWARD = 'FORWARD'
directions = {
LEFT: (0, -1),
RIGHT: (0, 1),
UP: (-1, 0),
DOWN: (1, 0)
}
@dataclass
class State:

View File

@ -67,3 +67,8 @@ ACTION = {
BAR_ANIMATION_SPEED = 1
BAR_WIDTH_MULTIPLIER = 0.9 # (0;1>
BAR_HEIGHT_MULTIPLIER = 0.1
LEFT = 'LEFT'
RIGHT = 'RIGHT'
UP = 'UP'
DOWN = 'DOWN'

View File

@ -1,5 +1,16 @@
from typing import Tuple, List
import pygame
from common.constants import GRID_CELL_PADDING, GRID_CELL_SIZE, COLUMNS, ROWS
from common.constants import GRID_CELL_PADDING, GRID_CELL_SIZE
from common.constants import ROWS, COLUMNS, LEFT, RIGHT, UP, DOWN
directions = {
LEFT: (0, -1),
RIGHT: (0, 1),
UP: (-1, 0),
DOWN: (1, 0)
}
def draw_text(text, color, surface, x, y, text_size=30, is_bold=False):
@ -45,3 +56,21 @@ def castle_neighbors(map, castle_bottom_right_row, castle_bottom_right_col):
continue
neighbors.append((new_col, new_row))
return neighbors
def find_neighbours(col: int, row: int) -> List[Tuple[int, int]]:
dr = [-1, 1, 0, 0]
dc = [0, 0, -1, 1]
neighbours = []
for i in range(4):
rr = row + dr[i]
cc = col + dc[i]
if rr < 0 or cc < 0: continue
if rr >= ROWS or cc >= COLUMNS: continue
neighbours.append((rr, cc))
return neighbours

View File

@ -2,9 +2,9 @@ import random
import pygame
from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD, UP, DOWN, LEFT, RIGHT
from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD
from common.constants import *
from common.helpers import castle_neighbors
from common.helpers import find_neighbours
from logic.knights_queue import KnightsQueue
from logic.spawner import Spawner
from models.castle import Castle
@ -113,11 +113,12 @@ class Level:
knight_pos_y = current_knight.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 = find_neighbours(12, 8)
state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
action_list = a_star(state, self.map, goal_list)
print(action_list)
print(goal_list)
if len(action_list) == 0:
return