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: class Node:
def __init__(self): def __init__(self):
self.state = State() self.state = State()
self.parent = None self.parent = None
self.action = None self.action = ""
class State: class State:
@ -16,41 +13,162 @@ class State:
def successor(state): def successor(state):
if state.direction == "east": node_state_left = Node()
node_state_left = Node() node_state_right = Node()
node_state_right = Node() node_state_forward = Node()
node_state_forward = Node()
if state.direction == "east":
#state_left = state.coord
node_state_left.state = State() node_state_left.state = State()
node_state_left.state.coord = state.coord node_state_left.state.coord = state.coord
node_state_left.state.direction = "north" node_state_left.state.direction = "north"
node_state_left.parent = state #node_state_left.parent = state
node_state_left.action = "Left" node_state_left.action = "Left"
#state_right = state.coord
node_state_right.state = State() node_state_right.state = State()
node_state_right.state.coord = state.coord node_state_right.state.coord = state.coord
node_state_right.state.direction = "south" node_state_right.state.direction = "south"
node_state_right.parent = state #node_state_right.parent = state
node_state_right.action = "Right" node_state_right.action = "Right"
#state_forward = state.coord
#state_forward[0] = 53
node_state_forward.state = State() node_state_forward.state = State()
node_state_forward.state.coord = [state.coord[0] + 53, state.coord[1]] node_state_forward.state.coord = [state.coord[0] + 53, state.coord[1]]
node_state_forward.state.direction = state.direction node_state_forward.state.direction = state.direction
node_state_forward.parent = state #node_state_forward.parent = state
node_state_forward.action = "Up" 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 == "west":
#elif state.direction == "north": 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"
#elif state.direction == "south": 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]
def hello(): def graphsearch(fringe, explored, start_state, end_state_coord):
print("Hello Node!")
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
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 # Creating objects
player = Player() player = Player()
field = Field() field = Field()
fringe = []
explored = []
def Arrow(direction): def Arrow(direction):
@ -161,36 +163,55 @@ def Action(event):
# Modified by Artem to search in the status area # Modified by Artem to search in the status area
def MouseClickEvent(event): 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)): for i in range(0, len(field.canvas_small_images)):
print(field.small_field_canvas.coords(field.canvas_small_images[i])) print(field.small_field_canvas.coords(field.canvas_small_images[i]))
print("Lewy przycisk myszy zostal nacisniety!") #print("Lewy przycisk myszy zostal nacisniety!")
node = nd.Node() #node = nd.Node()
print(node.state.coord, node.state.direction, node.action, node.parent) #print(node.state.coord, node.state.direction, node.action, node.parent)
node.state = nd.State() #node.state = nd.State()
node.state.coord = field.small_field_canvas.coords(player.image_canvas_id) #node.state.coord = field.small_field_canvas.coords(player.image_canvas_id)
node.state.direction = "east" #node.state.direction = "east"
#node.state.coord = field.small_field_canvas.coords(field.canvas_small_images[5]) #node.state.coord = field.small_field_canvas.coords(field.canvas_small_images[5])
start_position = field.small_field_canvas.coords(player.image_canvas_id) 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("Pierwsza pozycja: {} {}".format(start_position[0], start_position[1]))
#print(node.state.coord, node.state.direction, node.parent, node.action) #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)): for i in range(0, len(field.canvas_small_images)):
img_coords = field.small_field_canvas.coords(field.canvas_small_images[i]) 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): 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 end_state_coord = img_coords
if len(end_position) == 2: if len(end_state_coord) == 2:
print("Koncowa pozycja: {} {}".format(end_position[0], end_position[1])) 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 # Successor - only east
list_node_state = nd.successor(node.state) fringe = nd.graphsearch(fringe, explored, node.state, end_state_coord)
for i in range(0, len(list_node_state)): print("Fringe - OK")
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)) #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): def PutMines(mines_array):