Action-list

This commit is contained in:
Art-cyber520 2021-04-29 16:28:43 +02:00
parent 3f96e3279a
commit cf6bcebf1f
4 changed files with 51 additions and 45 deletions

View File

@ -1,3 +1,6 @@
from bin.Classess.Player import FRAME_WIDTH, FRAME_HEIGHT
class Node: class Node:
def __init__(self): def __init__(self):
self.state = State() self.state = State()
@ -29,7 +32,7 @@ def successor(state):
node_state_right.state.direction = "south" node_state_right.state.direction = "south"
node_state_right.action = "Right" node_state_right.action = "Right"
if state.coord[0] + 53 < 533: if state.coord[0] + 53 < FRAME_WIDTH:
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
@ -83,7 +86,7 @@ def successor(state):
node_state_right.state.direction = "west" node_state_right.state.direction = "west"
node_state_right.action = "Right" node_state_right.action = "Right"
if state.coord[1] + 53 < 533: if state.coord[1] + 53 < FRAME_HEIGHT:
node_state_forward.state = State() node_state_forward.state = State()
node_state_forward.state.coord = [state.coord[0], state.coord[1] + 53] node_state_forward.state.coord = [state.coord[0], state.coord[1] + 53]
node_state_forward.state.direction = state.direction node_state_forward.state.direction = state.direction
@ -95,25 +98,23 @@ def successor(state):
return [node_state_left, node_state_right] return [node_state_left, node_state_right]
def graphsearch(fringe, explored, start_state, end_state_coord): def graph_search(fringe, explored, start_state, end_state_coord):
node = Node() node = Node()
node.state = start_state node.state = start_state
node.parent = node.state node.parent = node.state
#node.action = "Right"
fringe.append(node) fringe.append(node)
iter = 0 iterator = 0
bool = True end_loop = True
while bool: while end_loop:
if len(fringe) == 0: if len(fringe) == 0:
bool = False end_loop = False
#return False #return False
elem = fringe[iter] elem = fringe[iterator]
if elem.state.coord == end_state_coord: if elem.state.coord == end_state_coord:
bool = False
return fringe return fringe
explored.append(elem) explored.append(elem)
@ -126,14 +127,6 @@ def graphsearch(fringe, explored, start_state, end_state_coord):
if another_states[i].state.direction == fringe[j].state.direction: if another_states[i].state.direction == fringe[j].state.direction:
break break
else: 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 = [] states = []
for k in range(0, len(fringe)): for k in range(0, len(fringe)):
new_state = [fringe[k].state.coord, fringe[k].state.direction] new_state = [fringe[k].state.coord, fringe[k].state.direction]
@ -142,13 +135,6 @@ def graphsearch(fringe, explored, start_state, end_state_coord):
if now_state in states: if now_state in states:
break 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 another_states[i].parent = elem.state
fringe.append(another_states[i]) fringe.append(another_states[i])
else: else:
@ -157,11 +143,11 @@ def graphsearch(fringe, explored, start_state, end_state_coord):
new_state = [fringe[k].state.coord, fringe[k].state.direction] new_state = [fringe[k].state.coord, fringe[k].state.direction]
states.append(new_state) states.append(new_state)
now_state = [another_states[i].state.coord, another_states[i].state.direction] now_state = [another_states[i].state.coord, another_states[i].state.direction]
if now_state in states: if now_state in states:
break break
# if another_states[i] in fringe:
# break
if another_states[i].state.direction == fringe[j].state.direction: if another_states[i].state.direction == fringe[j].state.direction:
another_states[i].parent = elem.state another_states[i].parent = elem.state
fringe.append(another_states[i]) fringe.append(another_states[i])
iter += 1 iterator += 1

View File

@ -20,8 +20,10 @@ AMOUNT_OF_MINES = 10
# Creating objects # Creating objects
player = Player() player = Player()
field = Field() field = Field()
fringe = [] fringe = []
explored = [] explored = []
action_list = []
def Arrow(direction): def Arrow(direction):
@ -162,28 +164,41 @@ def Action(event):
# Modified by Artem to search in the status area # Modified by Artem to search in the status area
def create_action_list(states, index):
global fringe
global action_list
if index == 0:
action_list.reverse()
return True
action_list.append(fringe[index].action)
state_parent = [fringe[index].parent.coord, fringe[index].parent.direction]
create_action_list(states, states.index(state_parent))
def MouseClickEvent(event): def MouseClickEvent(event):
global fringe global fringe
for i in range(0, len(field.canvas_small_images)): global explored
print(field.small_field_canvas.coords(field.canvas_small_images[i])) global action_list
start_position = field.small_field_canvas.coords(player.image_canvas_id) start_position = field.small_field_canvas.coords(player.image_canvas_id)
end_state_coord = [] end_position = []
print("Pierwsza pozycja: {} {}".format(start_position[0], start_position[1])) print("Pierwsza pozycja: {} {}".format(start_position[0], start_position[1]))
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_state_coord = img_coords end_position = img_coords
if len(end_state_coord) == 2:
print("Koncowa pozycja: {} {}".format(end_state_coord[0], end_state_coord[1])) if len(end_position) == 2:
print("Koncowa pozycja: {} {}".format(end_position[0], end_position[1]))
node = nd.Node() node = nd.Node()
if len(fringe) == 0: if len(fringe) == 0:
node.state.coord = start_position node.state.coord = start_position
node.state.direction = "east" node.state.direction = "east"
else: else:
# ZLE - NAPRAWIC
states = [] states = []
for k in range(0, len(fringe)): for k in range(0, len(fringe)):
new_state = fringe[k].state.coord new_state = fringe[k].state.coord
@ -196,20 +211,25 @@ def MouseClickEvent(event):
fringe.clear() fringe.clear()
explored.clear() explored.clear()
action_list.clear()
fringe = nd.graphsearch(fringe, explored, node.state, end_state_coord) fringe = nd.graph_search(fringe, explored, node.state, end_position)
states = [] states = []
for k in range(0, len(fringe)):
new_state = [fringe[k].state.coord, fringe[k].state.direction]
states.append(new_state)
for i in range(0, len(states)):
if states[i] in states[i + 1:]:
print("\nDooble element: {}\n".format(states[i]))
print("\nLista fringe nie zawiera powtorzen!\n")
for i in range(0, len(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)) 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
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(action_list)
def PutMines(mines_array): def PutMines(mines_array):