plane of states #2

Merged
s473603 merged 1 commits from plaszczyzna_stanow into master 2023-04-27 13:22:54 +02:00
3 changed files with 18 additions and 14 deletions

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project> </project>

View File

@ -37,7 +37,7 @@ def get_click_mouse_pos():
grid_x, grid_y = x // BLOCK_SIZE, y // BLOCK_SIZE grid_x, grid_y = x // BLOCK_SIZE, y // BLOCK_SIZE
pygame.draw.rect(sc, BLUE, (grid_x * BLOCK_SIZE, grid_y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1) pygame.draw.rect(sc, BLUE, (grid_x * BLOCK_SIZE, grid_y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
click = pygame.mouse.get_pressed() click = pygame.mouse.get_pressed()
return (grid_x, grid_y) if click[0] else False return (grid_x, grid_y, Direction.RIGHT) if click[0] else False
def draw_interface(): def draw_interface():
@ -98,7 +98,7 @@ def draw_interface():
tractor.water = WATER_TANK_CAPACITY tractor.water = WATER_TANK_CAPACITY
tractor.gas = GAS_TANK_CAPACITY tractor.gas = GAS_TANK_CAPACITY
elif event.type == pygame.MOUSEBUTTONDOWN: elif event.type == pygame.MOUSEBUTTONDOWN:
startpoint = (tractor.x, tractor.y) startpoint = (tractor.x, tractor.y, tractor.direction)
endpoint = get_click_mouse_pos() endpoint = get_click_mouse_pos()
print(endpoint) print(endpoint)
a = graph1.dijkstra(startpoint, endpoint) a = graph1.dijkstra(startpoint, endpoint)
@ -174,7 +174,6 @@ class Grid:
self.height = height self.height = height
self.block_size = block_size self.block_size = block_size
self.grid = [[types.EMPTY for col in range(BOARD_SIZE)] for row in range(BOARD_SIZE)] self.grid = [[types.EMPTY for col in range(BOARD_SIZE)] for row in range(BOARD_SIZE)]
self.graph = {}
self.initialize_grid() self.initialize_grid()
def add_object(self, x, y, type_of_object: types): def add_object(self, x, y, type_of_object: types):
@ -205,13 +204,6 @@ class Grid:
else: else:
i -= 1 i -= 1
def get_next_nodes(self, x, y):
check_next_node = lambda x, y: True if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and (
self.grid[x][y] != types.ROCK) else False
ways = [-1, 0], [1, 0], [0, -1], [0, 1]
return [(1 if self.grid[x][y] != types.ROCK else 5, (x + dx, y + dy)) for dx, dy in ways if
check_next_node(x + dx, y + dy)]
class Graph: class Graph:
def __init__(self, grid: Grid): def __init__(self, grid: Grid):
@ -221,7 +213,8 @@ class Graph:
def initialize_graph(self, grid: Grid): def initialize_graph(self, grid: Grid):
for y, row in enumerate(grid.grid): for y, row in enumerate(grid.grid):
for x, col in enumerate(row): for x, col in enumerate(row):
self.graph[(x, y)] = self.graph.get((x, y), []) + grid.get_next_nodes(x, y) for direction in Direction:
self.graph[(x, y, direction)] = get_next_nodes(x, y, direction, grid)
def dijkstra(self, start, goal): def dijkstra(self, start, goal):
@ -287,10 +280,21 @@ class Tractor:
return return
def get_next_nodes(x, y, direction: Direction, grid: Grid):
check_next_node = lambda x, y: True if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE else False
way = [0, -1] if direction == Direction.UP else [1, 0] if direction == Direction.RIGHT else [0, 1] if direction == Direction.DOWN else [-1, 0]
next_nodes = []
for new_direction in Direction:
if new_direction != direction:
next_nodes.append((1, (x, y, new_direction)))
else:
if check_next_node(x + way[0], y + way[1]):
next_nodes.append((grid.grid[x + way[0]][y + way[1]].value, (x + way[0], y + way[1], new_direction)))
return next_nodes
def movement(traktor: Tractor, grid: Grid, road): def movement(traktor: Tractor, grid: Grid, road):
n = len(road) n = len(road)
for i in range(n - 1): for i in range(n - 1):
time.sleep(0.3)
if road[i][0] != road[i + 1][0]: if road[i][0] != road[i + 1][0]:
if road[i][0] > road[i + 1][0]: if road[i][0] > road[i + 1][0]:
if traktor.direction != 3: if traktor.direction != 3: