From 58949ff9853fa4b702d0b034b5a0f423c1787307 Mon Sep 17 00:00:00 2001 From: Tomasz Adamczyk Date: Sat, 10 Apr 2021 13:42:32 +0200 Subject: [PATCH] zwraca ruchy --- .idea/workspace.xml | 44 +++++++++----------- __pycache__/definitions.cpython-37.pyc | Bin 3400 -> 3400 bytes __pycache__/graph.cpython-37.pyc | Bin 4050 -> 4389 bytes definitions.py | 2 +- graph.py | 55 +++++++++++++++++++------ py.py | 6 +-- 6 files changed, 67 insertions(+), 40 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 525805a..986edd4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,6 +3,7 @@ + @@ -109,6 +110,8 @@ + + @@ -120,38 +123,38 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + @@ -159,13 +162,6 @@ - - - file://$PROJECT_DIR$/graph.py - 75 - - @@ -176,6 +172,6 @@ - + \ No newline at end of file diff --git a/__pycache__/definitions.cpython-37.pyc b/__pycache__/definitions.cpython-37.pyc index ce470302e9848e7ca174f6c932767176f0aa5b85..8c27e97852429b7d55c2c98c20c826e2c1a004c0 100644 GIT binary patch delta 194 zcmX>hbwY~QiI;fdECg%W2>B&=o zq|D?!%#xC_QJN{NQCcZ1QQ9eTQ93EIQMxJeQFhbwY~QiI(Xj?Smf%G^>>l)N5B?+G+>78%{{=68lhnE`IN`l{@6Gon^L~Au`8aDYS(ZVtzU_6M z&Q9&y)AZ5)`~%DMte3>qJo=b8_Cfs4=t}~HC4&^uT~KrrY>o7!?i?orG9(^p$?OW3 z1{At9BtuF`cG4pqb(!Eq9un9j+)E!i3Y)+UBefoee%oWFP&DT6_$=}rnVl25?svn@ zV1@afYitEOepK7sh+>whmE_ZI`Z8t0y=1C1j_FV~XT4^9V?rvgETgdZZC za#IpHXzz`owU+djveGd;8PxDYt{yo&8FSE6hout^0xuuo7sv4V6dxYLE6E#k^^`tR z8bSDi*$AJV2TOjN*ZYo^X4*CDjd&wyX9*rDvA-3%MZwdw)}mlLUU%((hUb11kL!LN z`j|sIVDb9NLMhR9qj4XM z7t5i)lJAz&Ux{_Da=MGgLfi^hMcTWcxXiU(dXKEWUy*uxFOOz5K!xrCC$Ls~CO3dK z)1sN8Wg#Q8OM)YSAY_*yv5H_-FvoJ$Cc1jfXERVoD%!pt`)Nj4Sy+^I;Q49o z*gSM5W^fgc5Z&=tY$tJ}Qb#kWvTNQkPHBB~O_4&slT=mcc z2QE|=XW|1t03jhb!EfN`D_oHH2^^SlP*nB6TJz?ucb{kccHcL?FI#uZWmC|2{e1NF zbC zznmMG$ybk+Hgffg6Y5#xTsC=B-dZp-rQ-)L(W0Nvw001gpCb8U+3-2}VcFL6wvjv6=1eqN80iGA6#5&K_0DE49CE|9mrFCic%x&rJUNH30bux|YYe#PG1=d)h zc)sK2CgVk#Bj3nOM1G~@*Vb;$$?6i_IeU=cIP+zQm8G<{@j(muPu4GC;8 oxsx}52&JHE3!bZ_$ShTO3ZI#@NUuB&?WFa5%`r~m)} diff --git a/definitions.py b/definitions.py index 5fcd617..2ee2ec4 100644 --- a/definitions.py +++ b/definitions.py @@ -30,7 +30,7 @@ FARMLAND_DRY = pygame.image.load(os.path.join('resources', 'farmland_dry.png')) FARMLAND_DRY = pygame.transform.scale(FARMLAND_DRY, (BLOCK_SIZE, BLOCK_SIZE)) FARMLAND_WET = pygame.image.load(os.path.join('resources', 'farmland_wet.png')) FARMLAND_WET = pygame.transform.scale(FARMLAND_WET, (BLOCK_SIZE, BLOCK_SIZE)) -FPS = 20 +FPS = 1 POTATOES_GROW_TIME = 5 POTATOES_MAXIMUM_STATE = POTATOES_GROW_TIME * 3 + 1 POTATOES_STAGE_0 = pygame.image.load(os.path.join('resources', 'potatoes_stage_0.png')) diff --git a/graph.py b/graph.py index f13e119..927c5a4 100644 --- a/graph.py +++ b/graph.py @@ -60,6 +60,13 @@ def goal_test(elem, goaltest): return True else: return False +def print_moves(elem, explored): + moves_list = [] + while (elem.get_parent() != None): + moves_list.append(elem.get_action()) + elem = elem.get_parent() + moves_list.reverse() + return moves_list def succ(elem): actions_list = [] temp = copy.copy(elem.get_direction()) @@ -74,14 +81,18 @@ def succ(elem): else: temp = temp + 1 actions_list.append(("rotate_right", (temp, elem.get_x(), elem.get_y()))) + temp_move_south = elem.get_y() + 1 + temp_move_west = elem.get_x() - 1 + temp_move_east = elem.get_x() + 1 + temp_move_north = elem.get_y() - 1 if tractor.Tractor.is_move_allowed_succ(elem) == "x + 1": - actions_list.append(("move", (elem.get_direction(), elem.set_x(elem.get_x() + 1), elem.get_y()))) + actions_list.append(("move", (elem.get_direction(), temp_move_east, elem.get_y()))) elif tractor.Tractor.is_move_allowed_succ(elem) == "y - 1": - actions_list.append(("move", (elem.get_direction(), elem.get_x(), elem.set_y(elem.get_y() - 1)))) + actions_list.append(("move", (elem.get_direction(), elem.get_x(), temp_move_north))) elif tractor.Tractor.is_move_allowed_succ(elem) == "y + 1": - actions_list.append(("move", (elem.get_direction(), elem.get_x(), elem.set_y(elem.get_y() + 1)))) + actions_list.append(("move", (elem.get_direction(), elem.get_x(), temp_move_south))) elif tractor.Tractor.is_move_allowed_succ(elem) == "x - 1": - actions_list.append(("move", (elem.get_direction(), elem.set_x(elem.get_x() - 1), elem.get_y()))) + actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y()))) return actions_list def graphsearch(fringe, explored, istate, succ, goaltest): node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y()) #może None coś nie gra @@ -94,15 +105,35 @@ def graphsearch(fringe, explored, istate, succ, goaltest): elem = fringe.pop(0) temp = copy.copy(elem) #żeby explored w for succ() nie zmieniało if goal_test(elem, goaltest) is True: - return explored + # for x in fringe: + # print("action: " + str(x.get_action())) + # print("direction: " + str(x.get_direction())) + # print("parent: " + str(x.get_parent())) + # print("node: " + str(x)) + # print("x: " + str(x.get_x())) + # print("y: " + str(x.get_y())) + # for x in explored: + # print("action ex: " + str(x.get_action())) + # print("direction ex: " + str(x.get_direction())) + # print("parent ex: " + str(x.get_parent())) + # print("node ex: " + str(x)) + # print("x ex: " + str(x.get_x())) + # print("y ex: " + str(x.get_y())) + return print_moves(elem, explored) explored.append(elem) for (action, state) in succ(temp): - if state not in fringe and state not in explored: + fringe_tuple = [] + explored_tuple = [] + for x in fringe: + fringe_tuple.append((x.get_direction(), x.get_x(), x.get_y())) + for x in explored: + explored_tuple.append((x.get_direction(), x.get_x(), x.get_y())) + if state not in fringe_tuple and state not in explored_tuple: x = Node(action, state[0], elem, state[1], state[2]) - print(state[0]) - print(state[1]) - print(state[2]) - print(x.get_action()) - #print(x.get_direction()) - #print(x.get_parent()) + # print(x.get_action()) + # print(state[0]) + # print(state[1]) + # print(state[2]) + # print(x.get_direction()) + # print(x.get_parent()) fringe.append(x) \ No newline at end of file diff --git a/py.py b/py.py index e794c69..eadede4 100644 --- a/py.py +++ b/py.py @@ -14,7 +14,7 @@ def main(): collected_plants_dict = {"beetroot": 0, "carrot": 0, "potato": 0, "wheat": 0} fertilizer_dict = {"beetroot": definitions.TRACTOR_FERTILIZER, "carrot": definitions.TRACTOR_FERTILIZER, "potato": definitions.TRACTOR_FERTILIZER, "wheat": definitions.TRACTOR_FERTILIZER} station1 = station.Station(collected_plants_dict) - tractor1 = tractor.Tractor(amount_of_seeds_dict, collected_plants_dict, definitions.TRACTOR_DIRECTION_EAST, fertilizer_dict, definitions.TRACTOR_FUEL, definitions.TRACTOR_WATER_LEVEL, 0, 0) + tractor1 = tractor.Tractor(amount_of_seeds_dict, collected_plants_dict, definitions.TRACTOR_DIRECTION_SOUTH, fertilizer_dict, definitions.TRACTOR_FUEL, definitions.TRACTOR_WATER_LEVEL, 0, 0) tractor1_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE) clock = pygame.time.Clock() run = True @@ -23,8 +23,8 @@ def main(): for event in pygame.event.get(): if event.type == pygame.QUIT: run = False - istate = graph.Istate(definitions.TRACTOR_DIRECTION_EAST, 0, 0) - istate1 = graph.Istate(None, 4, 4) + istate = graph.Istate(definitions.TRACTOR_DIRECTION_SOUTH, 0, 0) + istate1 = graph.Istate(None, 4, 6) print(graph.graphsearch([], [], istate, graph.succ, istate1)) map1.draw_window(tractor1, tractor1_rect) tractor1.tractor1_handle_movement(tractor1_rect)