From 0493082643a9805b63cee160e1442c6013987f93 Mon Sep 17 00:00:00 2001 From: Art-cyber520 Date: Mon, 26 Apr 2021 18:21:56 +0200 Subject: [PATCH] Function succ, graphsearch(not work) --- bin/Classess/Node.py | 160 ++++++++++++++++--- bin/Classess/__pycache__/Node.cpython-38.pyc | Bin 1311 -> 2481 bytes bin/main/main.py | 51 ++++-- 3 files changed, 175 insertions(+), 36 deletions(-) diff --git a/bin/Classess/Node.py b/bin/Classess/Node.py index b636e9d..2382c11 100644 --- a/bin/Classess/Node.py +++ b/bin/Classess/Node.py @@ -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() + node_state_left = Node() + node_state_right = Node() + node_state_forward = Node() + + if state.direction == "east": - #state_left = state.coord 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] + #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(): - print("Hello Node!") +def graphsearch(fringe, explored, start_state, end_state_coord): + + 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 diff --git a/bin/Classess/__pycache__/Node.cpython-38.pyc b/bin/Classess/__pycache__/Node.cpython-38.pyc index 49a3ddc47d1a5ab5c9e473b01b5170e7f4d69110..dc862e5548ac9614ecb7c291c6769c09efab2fb6 100644 GIT binary patch literal 2481 zcmbVO&1>976rUN5w32qcc1>t@O?xPm(k(Q!lu{_61Z)~8WgEKnW!a!8n(=;kEv-i~ zZp;WuHUxU}A(#5p9{vCHQ0CfG{)L|U-e}j^xV952n5TJPGrxJ7-;8$G)j)HF5G9N&x9vx@OFgXBb^|Dtk)g} z+C6CbEfA7G9_CQNY>opt-0*HDWI`ce`CuP2x1r@N5CxeNO(Mt!3Hcx$L3-qzbqx;T z_KLWW#?9@^o5f0|h4JE~7-#7@H9VKoy>p^)@&Ej={qk2TzhwtPUQi zY$B5)AM{R&qvRxwqj6fu>3As9WD-Y16@C82;PE&eY)|7nmw7%w+;?VAj2}VhxQHUe zoO>V$^{Gcs)@;Pm+dbzPn#Kn*icAkGaC-XRtmK73X59#J@DhZ#4?G9^qP#tLc$#Z^8yE(66C1kiGvh+rj} z_58!N#Ex zTDYbYC*p$HHx&-pUF&BHKeK+W@lA!R*~0Hse&Mx=sNIIjOt|pdWXQCG891BmQ4we- za!+ac+#(3~*(p`6D=<`8uE)S`#?X2j49sF+7Q@*U7{WKyf!>5+Sp!jPU0a9r7mI)c z0;aaEKtSJsAbdLnx35K@VG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 diff --git a/bin/main/main.py b/bin/main/main.py index cf92c34..d954bec 100644 --- a/bin/main/main.py +++ b/bin/main/main.py @@ -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):