SzybciorSmartTraktor/graph.py

152 lines
4.1 KiB
Python
Raw Normal View History

2022-04-28 09:21:32 +02:00
import copy
2022-04-07 18:44:33 +02:00
import settings
2022-04-28 09:21:32 +02:00
class State:
def __init__(self, direction, x, y):
self.direction = direction
self.x = x
self.y = y
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
class Node:
def __init__(self, action, direction, parent, x, y):
self.action = action
self.direction = direction
self.parent = parent
self.x = x
self.y = y
def get_action(self):
return self.action
def set_action(self, action):
self.action = action
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_parent(self):
return self.parent
def set_parent(self, parent):
self.parent = parent
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
2022-05-12 21:49:22 +02:00
def goal_test(goal_test_arr, elem):
if elem.get_x() == goal_test_arr[0] and elem.get_y() == goal_test_arr[1]:
2022-04-28 09:21:32 +02:00
return True
else:
return False
2022-04-28 09:21:32 +02:00
2022-05-12 21:49:22 +02:00
def graph_search(explored, fringe, goal_test_arr, is_state, succ):
node = Node(None, is_state.get_direction(), None, is_state.get_x(),
is_state.get_y())
2022-04-28 09:21:32 +02:00
fringe.append(node)
while True:
if not fringe:
return False
elem = fringe.pop(0)
temp = copy.copy(elem)
2022-05-12 21:49:22 +02:00
if goal_test(goal_test_arr,
elem) is True:
2022-04-28 09:21:32 +02:00
return get_moves_list(elem)
explored.append(elem)
for (action, state) in succ(
temp):
fringe_tuple = []
explored_tuple = []
for x in fringe:
fringe_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
for x in explored:
explored_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
if state not in fringe_tuple and state not in explored_tuple:
x = Node(action, state[0], elem, state[1],
state[2])
fringe.append(x)
def get_moves_list(node):
moves_list = []
while (node.get_parent() != None):
moves_list.append(node.get_action())
node = node.get_parent()
moves_list.reverse()
return moves_list
def succ(node):
actions_list = []
direction = copy.copy(node.get_direction())
if direction == 1:
direction = 4
else:
direction = direction - 1
actions_list.append(("l", (direction, node.get_x(), node.get_y())))
direction = copy.copy(node.get_direction())
if direction == 4:
direction = 1
else:
direction = direction + 1
actions_list.append(("r", (direction, node.get_x(), node.get_y())))
temp_move_south = node.get_y() + 1
temp_move_west = node.get_x() - 1
temp_move_east = node.get_x() + 1
temp_move_north = node.get_y() - 1
if is_move_allowed(node) == "x + 1":
actions_list.append(("f", (node.get_direction(), temp_move_east, node.get_y())))
elif is_move_allowed(node) == "y - 1":
actions_list.append(("f", (node.get_direction(), node.get_x(), temp_move_north)))
elif is_move_allowed(node) == "y + 1":
actions_list.append(("f", (node.get_direction(), node.get_x(), temp_move_south)))
elif is_move_allowed(node) == "x - 1":
actions_list.append(("f", (node.get_direction(), temp_move_west, node.get_y())))
return actions_list
def is_move_allowed(node):
if node.get_direction() == 2 and node.get_x() + 1 < settings.Pygame.width():
return "x + 1"
elif node.get_direction() == 1 and node.get_y() - 1 >= 0:
return "y - 1"
elif node.get_direction() == 3 and node.get_y() + 1 < settings.Pygame.height():
return "y + 1"
elif node.get_direction() == 4 and node.get_x() - 1 >= 0:
return "x - 1"
else:
return False