a_star #21
25
algorithms/a_star.py
Normal file
25
algorithms/a_star.py
Normal file
@ -0,0 +1,25 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Tuple, Optional
|
||||
|
||||
from common.constants import Direction
|
||||
|
||||
|
||||
@dataclass
|
||||
class State:
|
||||
position: Tuple[int, int]
|
||||
direction: Direction
|
||||
|
||||
|
||||
@dataclass
|
||||
class Node:
|
||||
state: State
|
||||
parent: Optional[Node]
|
||||
action: str
|
||||
cost: int = field(init=False)
|
||||
depth: int = field(init=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
self.cost = 0 if not self.parent else self.parent.cost + 1
|
||||
self.depth = self.cost
|
@ -114,7 +114,7 @@ def go(row, column, direction):
|
||||
|
||||
|
||||
def is_valid_move(map, target_row, target_column):
|
||||
if 0 <= target_row < ROWS and 0 <= target_column < COLUMNS and map[target_row][target_column] == ' ':
|
||||
if 0 <= target_row < ROWS and 0 <= target_column < COLUMNS and map[target_row][target_column] in ['g', 's', ' ']:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
@ -29,6 +29,7 @@ CASTLE_SPAWN_FIRST_COL = 9
|
||||
NBR_OF_WATER = 16
|
||||
NBR_OF_TREES = 20
|
||||
NBR_OF_MONSTERS = 2
|
||||
NBR_OF_SANDS = 15
|
||||
|
||||
TILES = [
|
||||
'grass1.png',
|
||||
|
@ -8,7 +8,7 @@ class Spawner:
|
||||
self.map = map
|
||||
|
||||
def __is_free_field(self, field):
|
||||
return field == ' '
|
||||
return field in ['g', 's', ' ']
|
||||
|
||||
def spawn_in_area(self, objects: list, spawn_area_pos_row=0, spawn_area_pos_column=0, spawn_area_width=0,
|
||||
spawn_area_height=0, size=1):
|
||||
@ -17,17 +17,17 @@ class Spawner:
|
||||
while spawned_objects_count != len(objects):
|
||||
x = random.randint(0, spawn_area_height) + spawn_area_pos_row
|
||||
y = random.randint(0, spawn_area_width) + spawn_area_pos_column
|
||||
if x < ROWS-1 and y < COLUMNS-1 and self.__is_free_field(self.map[x][y]):
|
||||
if x < ROWS - 1 and y < COLUMNS - 1 and self.__is_free_field(self.map[x][y]):
|
||||
for i in range(size):
|
||||
for j in range(size):
|
||||
self.map[x-i][y-j] = objects[spawned_objects_count]
|
||||
self.map[x - i][y - j] = objects[spawned_objects_count]
|
||||
spawned_objects_count += 1
|
||||
|
||||
def spawn_where_possible(self, objects: list):
|
||||
spawned_objects_count = 0
|
||||
while spawned_objects_count != len(objects):
|
||||
x = random.randint(0, ROWS-1)
|
||||
y = random.randint(0, COLUMNS-1)
|
||||
x = random.randint(0, ROWS - 1)
|
||||
y = random.randint(0, COLUMNS - 1)
|
||||
if self.__is_free_field(self.map[x][y]):
|
||||
self.map[x][y] = objects[spawned_objects_count]
|
||||
spawned_objects_count += 1
|
||||
|
Loading…
Reference in New Issue
Block a user