diff --git a/.idea/misc.xml b/.idea/misc.xml index 5ea737b..d1e22ec 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/sapper.iml b/.idea/sapper.iml index c19edc0..d0876a7 100644 --- a/.idea/sapper.iml +++ b/.idea/sapper.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/bin/Classess/Field.py b/bin/Classess/Field.py index b8cdcd3..65e254f 100644 --- a/bin/Classess/Field.py +++ b/bin/Classess/Field.py @@ -74,4 +74,4 @@ class Field(object): def PuttingLargeImage(self, large_img_name): self.large_image_canvas.image = large_img_name - self.large_image_canvas.create_image(0, 0, anchor=NW, image=large_img_name) + self.large_image_canvas.create_image(0, 0, anchor=NW, image=large_img_name) \ No newline at end of file diff --git a/bin/Classess/Node.py b/bin/Classess/Node.py index 2a2cd01..fa2506e 100644 --- a/bin/Classess/Node.py +++ b/bin/Classess/Node.py @@ -1,11 +1,15 @@ from resources.Globals import * +images_coord_list = [] +cell_expense_list = [] + class Node: def __init__(self): self.state = State() - self.parent = None + self.parent = [] self.action = "" + self.priority = 0 class State: @@ -14,6 +18,14 @@ class State: self.direction = "" +def init_data(coord_list, expense_list): + global images_coord_list + global cell_expense_list + + images_coord_list = coord_list + cell_expense_list = expense_list + + def successor(state): node_state_left = Node() @@ -98,11 +110,86 @@ def successor(state): return [node_state_left, node_state_right] -def graph_search(fringe, explored, start_state, end_state_coord): +def get_cell_expense(node): + global images_coord_list + global cell_expense_list + for i in range(0, len(images_coord_list)): + if (images_coord_list[i][0] <= node.state.coord[0] and node.state.coord[0] <= images_coord_list[i][0] + IMAGE_SIZE) and (images_coord_list[i][1] <= node.state.coord[1] and node.state.coord[1] <= images_coord_list[i][1] + IMAGE_SIZE): + return cell_expense_list[i] + + +def heurystyka(node_now): + + if node_now.action == "Left" or node_now.action == "Right": + return node_now.parent[2] + (get_cell_expense(node_now) / 2) + elif node_now.action == "Up": + return node_now.parent[2] + (get_cell_expense(node_now) * 2) + elif node_now.action == "": + return get_cell_expense(node_now) + + +# def graph_search(fringe, explored, start_state, end_state_coord): +# +# node = Node() +# node.state = start_state +# node.parent = node.state +# fringe.append(node) +# iterator = 0 +# +# end_loop = True +# while end_loop: +# if len(fringe) == 0: +# end_loop = False +# #return False +# +# elem = fringe[iterator] +# +# if elem.state.coord == end_state_coord: +# 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.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 +# +# another_states[i].parent = elem.state +# fringe.append(another_states[i]) +# else: +# 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 +# +# if another_states[i].state.direction == fringe[j].state.direction: +# another_states[i].parent = elem.state +# fringe.append(another_states[i]) +# iterator += 1 + + +def graph_search_A(fringe, explored, start_state, end_state_coord): node = Node() node.state = start_state - node.parent = node.state + node.priority = heurystyka(node) + node.parent = [node.state.coord, node.state.direction, node.priority] + fringe.append(node) iterator = 0 @@ -110,7 +197,7 @@ def graph_search(fringe, explored, start_state, end_state_coord): while end_loop: if len(fringe) == 0: end_loop = False - #return False + # return False elem = fringe[iterator] @@ -121,10 +208,15 @@ def graph_search(fringe, explored, start_state, end_state_coord): another_states = successor(elem.state) for i in range(0, len(another_states)): + another_states[i].parent = [elem.state.coord, elem.state.direction, elem.priority] + p = heurystyka(another_states[i]) + 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: + if another_states[i].state.direction == fringe[j].state.direction and p < fringe[j].priority: + another_states[i].priority = p + fringe[j] = another_states[i] break else: states = [] @@ -133,10 +225,30 @@ def graph_search(fringe, explored, start_state, end_state_coord): states.append(new_state) now_state = [another_states[i].state.coord, another_states[i].state.direction] if now_state in states: - break + index = states.index(now_state) + if p < fringe[index].priority: + another_states[i].priority = p + fringe[index] = another_states[i] + break + else: + break - another_states[i].parent = elem.state + another_states[i].priority = p fringe.append(another_states[i]) + + n1 = len(fringe) + + while n1 > 1: + change = False + for l in range(0, n1 - 1): + if fringe[l].priority > fringe[l + 1].priority: + fringe[l], fringe[l + 1] = fringe[l + 1], fringe[l] + change = True + + n1 -= 1 + + if not change: + break else: states = [] for k in range(0, len(fringe)): @@ -145,9 +257,29 @@ def graph_search(fringe, explored, start_state, end_state_coord): now_state = [another_states[i].state.coord, another_states[i].state.direction] if now_state in states: - break + index = states.index(now_state) + if p < fringe[index].priority: + another_states[i].priority = p + fringe[index] = another_states[i] + break + else: + break if another_states[i].state.direction == fringe[j].state.direction: - another_states[i].parent = elem.state + another_states[i].priority = p fringe.append(another_states[i]) + + n2 = len(fringe) + + while n2 > 1: + change = False + for h in range(0, n2 - 1): + if fringe[h].priority > fringe[h + 1].priority: + fringe[h], fringe[h + 1] = fringe[h + 1], fringe[h] + change = True + + n2 -= 1 + + if not change: + break iterator += 1 diff --git a/bin/Classess/__pycache__/Field.cpython-38.pyc b/bin/Classess/__pycache__/Field.cpython-38.pyc index b7ba1f2..8cf8c4b 100644 Binary files a/bin/Classess/__pycache__/Field.cpython-38.pyc and b/bin/Classess/__pycache__/Field.cpython-38.pyc differ diff --git a/bin/Classess/__pycache__/Mine.cpython-38.pyc b/bin/Classess/__pycache__/Mine.cpython-38.pyc index 3767a9c..7144571 100644 Binary files a/bin/Classess/__pycache__/Mine.cpython-38.pyc and b/bin/Classess/__pycache__/Mine.cpython-38.pyc differ diff --git a/bin/Classess/__pycache__/Node.cpython-38.pyc b/bin/Classess/__pycache__/Node.cpython-38.pyc index d1caddc..fe72028 100644 Binary files a/bin/Classess/__pycache__/Node.cpython-38.pyc and b/bin/Classess/__pycache__/Node.cpython-38.pyc differ diff --git a/bin/Classess/__pycache__/Player.cpython-38.pyc b/bin/Classess/__pycache__/Player.cpython-38.pyc index 98cddaa..da481ba 100644 Binary files a/bin/Classess/__pycache__/Player.cpython-38.pyc and b/bin/Classess/__pycache__/Player.cpython-38.pyc differ diff --git a/bin/Classess/__pycache__/__init__.cpython-38.pyc b/bin/Classess/__pycache__/__init__.cpython-38.pyc index 173e7c2..4f22f99 100644 Binary files a/bin/Classess/__pycache__/__init__.cpython-38.pyc and b/bin/Classess/__pycache__/__init__.cpython-38.pyc differ diff --git a/bin/Main/__pycache__/main.cpython-38.pyc b/bin/Main/__pycache__/main.cpython-38.pyc index af7cdba..1e5d3a0 100644 Binary files a/bin/Main/__pycache__/main.cpython-38.pyc and b/bin/Main/__pycache__/main.cpython-38.pyc differ diff --git a/bin/main/main.py b/bin/main/main.py index 13e9c92..d318f41 100644 --- a/bin/main/main.py +++ b/bin/main/main.py @@ -28,6 +28,7 @@ field = Field() fringe = [] explored = [] action_list = [] +images_coord = [] def Arrow(direction): @@ -46,9 +47,16 @@ def Arrow(direction): # Putting images def Fill(bool): + global images_coord if bool: field.PuttingSmallImages() + for i in range(0, len(field.canvas_small_images)): + images_coord.append(field.small_field_canvas.coords(field.canvas_small_images[i])) + print("Coords List: ", images_coord) + + nd.init_data(images_coord, field.cell_expense) + # Drawing red/green rectangles for el in field.state_of_cell_array: if el[0] != 0: @@ -171,7 +179,7 @@ def create_action_list(states, index): action_list.reverse() return True action_list.append(fringe[index].action) - state_parent = [fringe[index].parent.coord, fringe[index].parent.direction] + state_parent = [fringe[index].parent[0], fringe[index].parent[1]] create_action_list(states, states.index(state_parent)) @@ -189,6 +197,7 @@ def MouseClickEvent(event): 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 + print("Color cost: ", field.cell_expense[i]) # if len(end_position) == 2: # print("Koncowa pozycja: {} {}".format(end_position[0], end_position[1])) @@ -202,8 +211,7 @@ def MouseClickEvent(event): for k in range(0, len(fringe)): new_state = fringe[k].state.coord states.append(new_state) - index = states.index(start_position) - start_node = fringe[index] + start_node = fringe[-1] node.state.coord = start_node.state.coord node.state.direction = start_node.state.direction @@ -211,21 +219,28 @@ def MouseClickEvent(event): fringe.clear() explored.clear() action_list.clear() - - fringe = nd.graph_search(fringe, explored, node.state, end_position) + fringe = nd.graph_search_A(fringe, explored, node.state, end_position) + # fringe = nd.graph_search(fringe, explored, node.state, end_position) states = [] + goal_all = [] for i in range(0, len(fringe)): new_state = [fringe[i].state.coord, fringe[i].state.direction] states.append(new_state) if end_position[0] == fringe[i].state.coord[0] and end_position[1] == fringe[i].state.coord[1]: - fringe = fringe[:i + 1] - break + goal_all.append(fringe[i]) + + elem_min = goal_all[0] + for i in range(1, len(goal_all)): + if elem_min.priority > goal_all[i].priority: + elem_min = goal_all[i] + index = fringe.index(elem_min) + fringe = fringe[:index + 1] create_action_list(states, -1) # 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)) + # print('Node{} = State: {} {}, Parent: {} {} {}, Action: {}'.format(i + 1, fringe[i].state.coord, fringe[i].state.direction, fringe[i].parent[0], fringe[i].parent[1], fringe[i].parent[2], fringe[i].action)) print(action_list) @@ -310,25 +325,26 @@ def DrawRectangle(): y = 4 color = None - # Chose color for rectangle for i in range(len(field.cell_expense)): if field.cell_expense[i] == 10: color = "None" elif field.cell_expense[i] == 20: color = "yellow" - elif field.cell_expense[i] == 30: - color = "dodger blue" elif field.cell_expense[i] == 40: + color = "dodger blue" + elif field.cell_expense[i] == 80: color = "green4" if color != "None": field.small_field_canvas.create_rectangle(x, y, x + IMAGE_SIZE + 2, y + IMAGE_SIZE + 2, width=2, outline=color) x += player.step - if i > 0 and i % 10 == 0: + if x + IMAGE_SIZE + 2 > field.width: x = 4 y += player.step + + def AddCostCellsToArray(amount, cost): counter = 0 while counter < amount: diff --git a/resources/Globals.py b/resources/Globals.py index f8cc7ff..f3167a8 100644 --- a/resources/Globals.py +++ b/resources/Globals.py @@ -19,12 +19,10 @@ amount_of_sand_cells = 10 sand_cell_cost = 20 amount_of_water_cells = 10 -water_cell_cost = 30 +water_cell_cost = 40 amount_of_swamp_cells = 10 -swamp_cell_cost = 40 - - +swamp_cell_cost = 80 x_start = 5 y_start = 5 diff --git a/resources/__pycache__/Globals.cpython-38.pyc b/resources/__pycache__/Globals.cpython-38.pyc new file mode 100644 index 0000000..e0de283 Binary files /dev/null and b/resources/__pycache__/Globals.cpython-38.pyc differ diff --git a/resources/__pycache__/__init__.cpython-38.pyc b/resources/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 0000000..42e2115 Binary files /dev/null and b/resources/__pycache__/__init__.cpython-38.pyc differ