SzybciorSmartTraktor/graph.py
2022-05-12 21:49:22 +02:00

152 lines
4.1 KiB
Python

import copy
import settings
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
def goal_test(goal_test_arr, elem):
if elem.get_x() == goal_test_arr[0] and elem.get_y() == goal_test_arr[1]:
return True
else:
return False
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())
fringe.append(node)
while True:
if not fringe:
return False
elem = fringe.pop(0)
temp = copy.copy(elem)
if goal_test(goal_test_arr,
elem) is True:
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