Function succ, graphsearch(not work)

This commit is contained in:
Art-cyber520 2021-04-26 18:21:56 +02:00
parent 60f1e95894
commit 0493082643
3 changed files with 175 additions and 36 deletions

View File

@ -1,11 +1,8 @@
#from bin.Main.main import player
class Node:
def __init__(self):
self.state = State()
self.parent = None
self.action = None
self.action = ""
class State:
@ -16,41 +13,162 @@ class State:
def successor(state):
if state.direction == "east":
node_state_left = Node()
node_state_right = Node()
node_state_forward = Node()
#state_left = state.coord
if state.direction == "east":
node_state_left.state = State()
node_state_left.state.coord = state.coord
node_state_left.state.direction = "north"
node_state_left.parent = state
#node_state_left.parent = state
node_state_left.action = "Left"
#state_right = state.coord
node_state_right.state = State()
node_state_right.state.coord = state.coord
node_state_right.state.direction = "south"
node_state_right.parent = state
#node_state_right.parent = state
node_state_right.action = "Right"
#state_forward = state.coord
#state_forward[0] = 53
node_state_forward.state = State()
node_state_forward.state.coord = [state.coord[0] + 53, state.coord[1]]
node_state_forward.state.direction = state.direction
node_state_forward.parent = state
#node_state_forward.parent = state
node_state_forward.action = "Up"
#return [node_state_left, node_state_right, node_state_forward]
elif state.direction == "west":
node_state_left.state = State()
node_state_left.state.coord = state.coord
node_state_left.state.direction = "south"
#node_state_left.parent = state
node_state_left.action = "Left"
node_state_right.state = State()
node_state_right.state.coord = state.coord
node_state_right.state.direction = "north"
#node_state_right.parent = state
node_state_right.action = "Right"
node_state_forward.state = State()
node_state_forward.state.coord = [state.coord[0] - 53, state.coord[1]]
node_state_forward.state.direction = state.direction
#node_state_forward.parent = state
node_state_forward.action = "Up"
#return [node_state_left, node_state_right, node_state_forward]
elif state.direction == "north":
node_state_left.state = State()
node_state_left.state.coord = state.coord
node_state_left.state.direction = "west"
#node_state_left.parent = state
node_state_left.action = "Left"
node_state_right.state = State()
node_state_right.state.coord = state.coord
node_state_right.state.direction = "east"
#node_state_right.parent = state
node_state_right.action = "Right"
node_state_forward.state = State()
node_state_forward.state.coord = [state.coord[0], state.coord[1] - 53]
node_state_forward.state.direction = state.direction
#node_state_forward.parent = state
node_state_forward.action = "Up"
#return [node_state_left, node_state_right, node_state_forward]
elif state.direction == "south":
node_state_left.state = State()
node_state_left.state.coord = state.coord
node_state_left.state.direction = "east"
#node_state_left.parent = state
node_state_left.action = "Left"
node_state_right.state = State()
node_state_right.state.coord = state.coord
node_state_right.state.direction = "west"
#node_state_right.parent = state
node_state_right.action = "Right"
node_state_forward.state = State()
node_state_forward.state.coord = [state.coord[0], state.coord[1] + 53]
node_state_forward.state.direction = state.direction
#node_state_forward.parent = state
node_state_forward.action = "Up"
#return [node_state_left, node_state_right, node_state_forward]
return [node_state_left, node_state_right, node_state_forward]
#elif state.direction == "west":
#elif state.direction == "north":
def graphsearch(fringe, explored, start_state, end_state_coord):
#elif state.direction == "south":
node = Node()
node.state = start_state
node.parent = node.state
#node.action = "Right"
fringe.append(node)
iter = 0
bool = True
while bool:
if len(fringe) == 0:
bool = False
#return False
def hello():
print("Hello Node!")
elem = fringe[iter]
if elem.state.coord == end_state_coord:
print("Gotowe!")
bool = False
return fringe
explored.append(elem)
another_states = successor(elem.state)
for i in range(0, len(another_states)):
n = len(fringe)
for j in range(0, n):
if another_states[i].state.coord[0] == fringe[j].state.coord[0] and another_states[i].state.coord[1] == fringe[j].state.coord[1]:
if another_states[i].state.direction == fringe[j].state.direction:
break
else:
# states = []
# for k in range(0, len(fringe)):
# new_state = fringe[k].state
# states.append(new_state)
# now_state = another_states[i].state
# if now_state in states:
# break
states = []
for k in range(0, len(fringe)):
new_state = [fringe[k].state.coord, fringe[k].state.direction]
states.append(new_state)
now_state = [another_states[i].state.coord, another_states[i].state.direction]
if now_state in states:
break
# bool_break = False
# for k in range(0, n):
# if another_states[i].state.coord[0] == fringe[k].state.coord[0] and another_states[i].state.coord[1] == fringe[k].state.coord[1]:
# if another_states[i].state.direction == fringe[k].state.direction:
# bool_break = True
# if bool_break:
# break
another_states[i].parent = elem.state
fringe.append(another_states[i])
else:
if another_states[i] in fringe:
break
if another_states[i].state.direction == fringe[j].state.direction:
another_states[i].parent = elem.state
fringe.append(another_states[i])
iter += 1

View File

@ -20,6 +20,8 @@ AMOUNT_OF_MINES = 10
# Creating objects
player = Player()
field = Field()
fringe = []
explored = []
def Arrow(direction):
@ -161,36 +163,55 @@ def Action(event):
# Modified by Artem to search in the status area
def MouseClickEvent(event):
print(len(field.canvas_small_images), field.canvas_small_images)
global fringe
#print(len(field.canvas_small_images), field.canvas_small_images)
for i in range(0, len(field.canvas_small_images)):
print(field.small_field_canvas.coords(field.canvas_small_images[i]))
print("Lewy przycisk myszy zostal nacisniety!")
node = nd.Node()
print(node.state.coord, node.state.direction, node.action, node.parent)
node.state = nd.State()
node.state.coord = field.small_field_canvas.coords(player.image_canvas_id)
node.state.direction = "east"
#print("Lewy przycisk myszy zostal nacisniety!")
#node = nd.Node()
#print(node.state.coord, node.state.direction, node.action, node.parent)
#node.state = nd.State()
#node.state.coord = field.small_field_canvas.coords(player.image_canvas_id)
#node.state.direction = "east"
#node.state.coord = field.small_field_canvas.coords(field.canvas_small_images[5])
start_position = field.small_field_canvas.coords(player.image_canvas_id)
end_position = []
end_state_coord = []
print("Pierwsza pozycja: {} {}".format(start_position[0], start_position[1]))
#print(node.state.coord, node.state.direction, node.parent, node.action)
print("Pozycje myszy: {} {}".format(event.x, event.y))
#print("Pozycje myszy: {} {}".format(event.x, event.y))
for i in range(0, len(field.canvas_small_images)):
img_coords = field.small_field_canvas.coords(field.canvas_small_images[i])
if (img_coords[0] <= event.x and event.x <= img_coords[0] + IMAGE_SIZE) and (img_coords[1] <= event.y and event.y <= img_coords[1] + IMAGE_SIZE):
end_position = img_coords
if len(end_position) == 2:
print("Koncowa pozycja: {} {}".format(end_position[0], end_position[1]))
end_state_coord = img_coords
if len(end_state_coord) == 2:
print("Koncowa pozycja: {} {}".format(end_state_coord[0], end_state_coord[1]))
node = nd.Node()
if len(fringe) == 0:
node.state.coord = field.small_field_canvas.coords(player.image_canvas_id)
node.state.direction = "east"
print("Pierwszy state - OK")
else:
node = fringe[len(fringe) - 1]
print("Pozostale states - OK")
fringe.clear()
print("\nLIST IS EMPTY: {}\n".format(fringe))
explored.clear()
print("Czyszczenie list - OK")
# Successor - only east
list_node_state = nd.successor(node.state)
for i in range(0, len(list_node_state)):
print('Node{} = State: {} {}, Parent: {} {}, Action: {}'.format(i + 1, list_node_state[i].state.coord, list_node_state[i].state.direction, list_node_state[i].parent.coord, list_node_state[i].parent.direction, list_node_state[i].action))
fringe = nd.graphsearch(fringe, explored, node.state, end_state_coord)
print("Fringe - OK")
#print(fringe)
print("{}".format(fringe))
for i in range(0, len(fringe)):
print('Node{} = State: {} {}, Parent: {} {}, Action: {}'.format(i + 1, fringe[i].state.coord, fringe[i].state.direction, fringe[i].parent.coord, fringe[i].parent.direction, fringe[i].action))
def PutMines(mines_array):