From 4642d1d86edfed2b129000296137bc2312f00652 Mon Sep 17 00:00:00 2001 From: marcinljablonski Date: Mon, 29 Apr 2019 10:49:03 +0200 Subject: [PATCH] . --- Cucumber.py | 10 +-- Tomato.py | 10 +-- ] | 111 +++++++++++++++++++++++++++ __pycache__/Cucumber.cpython-37.pyc | Bin 2154 -> 2139 bytes __pycache__/Tomato.cpython-37.pyc | Bin 2124 -> 2111 bytes env.py | 4 +- tractor.py | 114 +++++++++++++++++++++++----- 7 files changed, 216 insertions(+), 33 deletions(-) create mode 100644 ] diff --git a/Cucumber.py b/Cucumber.py index c9b32ec..b29a15e 100644 --- a/Cucumber.py +++ b/Cucumber.py @@ -31,25 +31,25 @@ class Cucumber(Plant): def decrease_ttl(self, n): self.__ttl -= n if self.__ttl == 0: - self.__is_life == False + self.__is_alive == False def increase_hydration(self, n): self.__hydration += n if self.__hydration > 40: - self.__is_life = False + self.__is_alive = False def decrase_hydration(self, n): self.__hydration -= n * self.__dehydration_ratio if self.__hydration < 1: - self.__is_life = False + self.__is_alive = False def increase_soillevel(self, n): self.__soil_level += n if self.__soil_level > 40: - self.__is_life = False + self.__is_alive = False def decrease_soillevel(self, n): self.__soil_level -= n * self.__desoil_ratio if self.__soil_level < 1: - self.__is_life = False + self.__is_alive = False diff --git a/Tomato.py b/Tomato.py index 66674ef..4fa2891 100644 --- a/Tomato.py +++ b/Tomato.py @@ -31,25 +31,25 @@ class Tomato(Plant): def decrease_ttl(self, n): self.__ttl -= n if self.__ttl == 0: - self.__is_life == False + self.__is_alive == False def increase_hydration(self, n): self.__hydration += n if self.__hydration > 40: - self.__is_life = False + self.__is_alive = False def decrase_hydration(self, n): self.__hydration -= n * self.__dehydration_ratio if self.__hydration < 1: - self.__is_life = False + self.__is_alive = False def increase_soillevel(self, n): self.__soil_level += n if self.__soil_level > 40: - self.__is_life = False + self.__is_alive = False def decrease_soillevel(self, n): self.__soil_level -= n * self.__desoil_ratio if self.__soil_level < 1: - self.__is_life = False + self.__is_alive = False diff --git a/] b/] new file mode 100644 index 0000000..727d92f --- /dev/null +++ b/] @@ -0,0 +1,111 @@ +import asyncio +import sys +import time +from queue import PriorityQueue + +def heuristic(a, b): + (x1, y1) = a + (x2, y2) = b + return abs(x1 - x2) + abs(y1 - y2) + + +async def tractor(): + directions = ['N', 'W', 'S', 'E'] + current_rotation = 'S' + start = (0,0) + current_position = start + goal = (10,6) + + frontier = PriorityQueue() + frontier.put(start, 0) + came_from = {} + cost_so_far = {} + came_from[start] = None + cost_so_far[start] = 0 + start_flag = True + + while not frontier.empty(): + local_graph = [] + is_already_moved = False + for i, direction in enumerate(directions): + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write("move\n".encode()) + data = await reader.readline() + result = data.decode() + writer.close() + time.sleep(0.7) + if result == "OK": + print("OK") + if direction == 'N': + local_graph.append((current_position[0] - 1, current_position[1])) + elif direction == 'S': + local_graph.append((current_position[0] + 1, current_position[1])) + elif direction == 'W': + local_graph.append((current_position[0], current_position[1] - 1 )) + else: + local_graph.append((current_position[0], current_position[1] + 1 )) + + if i == 3 and len(local_graph) == 1: + is_already_moved = True + else: + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write(("rotate " + directions[((i + 2) % 4)] + "\n").encode()) + data = await reader.readline() + writer.close() + time.sleep(0.7) + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write("move\n".encode()) + data = await reader.readline() + writer.close() + time.sleep(0.7) + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + current_rotation = directions[((i - 1) % 4)] + writer.write(("rotate " + current_rotation + "\n").encode()) + data = await reader.readline() + writer.close() + time.sleep(0.7) + + current = frontier.get() + if not start_flag and not is_already_moved: + if current[0] == current_position[0] and current[1] > current_position[1]: + current_rotation = 'E' + elif current[0] == current_position[0] and current[1] < current_position[1]: + current_rotation = 'W' + elif current[0] > current_position[0]: + current_rotation = 'S' + elif current[0] < current_position[0]: + current_rotation = 'N' + + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write(("rotate " + current_rotation + "\n").encode()) + data = await reader.readline() + writer.close() + time.sleep(0.7) + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write("move\n".encode()) + data = await reader.readline() + writer.close() + time.sleep(0.7) + + if current == goal: + break + + for next in local_graph: + new_cost = cost_so_far[current] + 1 + if next not in cost_so_far or new_cost < cost_so_far[next]: + cost_so_far[next] = new_cost + priority = new_cost + heuristic(goal, next) + frontier.put(next, priority) + came_from[next] = current + + start_flag = False + # reader, writer = await asyncio.open_connection( + # '127.0.0.1', 8888) + + # time.sleep(0.7) + + # writer.write("rotate E\n".encode()) + # data = await reader.readline() + # sys.stdout.write(data.decode()) + # time.sleep(0.7) +asyncio.run(tractor()) diff --git a/__pycache__/Cucumber.cpython-37.pyc b/__pycache__/Cucumber.cpython-37.pyc index f8912969d6afacab8ddd449a884b39c8c713f427..b81ac71bf7938ab85a9c656fa379687732a64da3 100644 GIT binary patch delta 112 zcmaDQa9e=aiI9| rF}5~g1)w%JP39s_AYH@_B0%c91>`|YX%L|ZB4j4pafnYAjuwQ`JiIgx036?TO5NivVl$$KXRt;upPTtN| qDXaw4;-<-5#0{j2ctHe2S0a11fFg)13nG+3gxq8W4)MvK*&_kV7#O1f delta 115 zcmdlla7KXFiI9g9iZ$lJ#vbW0#UBtJK?BtJervp7B{Gc9%U0+xx4ijy1J zG$%*1c7s`RlUdoS!K_8X%0TUIn#@JqK)Q$*M1a)FO%7qt=2il6WkH0> last_position[1]: + current_rotation = 'E' + elif current[0] == last_position[0] and current[1] < last_position[1]: + current_rotation = 'W' + elif current[0] > last_position[0]: + current_rotation = 'S' + elif current[0] < last_position[0]: + current_rotation = 'N' + last_rotation = current_rotation + + for i, direction in enumerate(directions): + if direction == last_rotation: + continue + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + # current_rotation = directions[((i - 1) % 4)] + writer.write(("rotate " + direction + "\n").encode()) + data = await reader.readline() + writer.close() + time.sleep(0.7) + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write("move\n".encode()) + data = await reader.readline() + result = data.decode().split() + writer.close() + time.sleep(0.7) + if result[0] == "OK": + print("Jade na "+result[1]) + # print("ok"+result[1]) + if direction == 'N': + local_graph.append((current_position[0] - 1, current_position[1])) + elif direction == 'S': + local_graph.append((current_position[0] + 1, current_position[1])) + elif direction == 'W': + local_graph.append((current_position[0], current_position[1] - 1 )) + else: + local_graph.append((current_position[0], current_position[1] + 1 )) + print(local_graph) + + # print("nawrót") + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write(("rotate " + directions[((i + 2) % 4)] + "\n").encode()) + data = await reader.readline() + # print(current_rotation) + writer.close() + time.sleep(0.7) + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write("move\n".encode()) + print("hop -nawrót") + data = await reader.readline() + writer.close() + time.sleep(0.7) + # reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + # current_rotation = directions[((i - 1) % 4)] + # writer.write(("rotate " + current_rotation + "\n").encode()) + # data = await reader.readline() + # writer.close() + # time.sleep(0.7) + # else: + # reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + # current_rotation = directions[((i + 1) % 4)] + # writer.write(("rotate " + current_rotation + "\n").encode()) + # data = await reader.readline() + # writer.close() + # time.sleep(0.7) + # print(":(") + + + if not start_flag: + print("jaaazda") + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write(("rotate " + current_rotation + "\n").encode()) + data = await reader.readline() + writer.close() + time.sleep(0.7) + reader, writer = await asyncio.open_connection('127.0.0.1', 8888) + writer.write("move\n".encode()) + data = await reader.readline() + writer.close() + time.sleep(0.7) + if current == goal: break - for next in + for next in local_graph: new_cost = cost_so_far[current] + 1 if next not in cost_so_far or new_cost < cost_so_far[next]: cost_so_far[next] = new_cost priority = new_cost + heuristic(goal, next) frontier.put(next, priority) came_from[next] = current - reader, writer = await asyncio.open_connection( - '127.0.0.1', 8888) - writer.write("move XD\n".encode()) - data = await reader.readline() - sys.stdout.write(data.decode()) - time.sleep(0.7) - - writer.write("rotate E\n".encode()) - data = await reader.readline() - sys.stdout.write(data.decode()) - time.sleep(0.7) - writer.close() + start_flag = False asyncio.run(tractor())