traktor/tractor.py

56 lines
1.3 KiB
Python
Raw Normal View History

2019-04-29 06:52:57 +02:00
import asyncio
import sys
import time
def heuristic(a, b):
(x1, y1) = a
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
async def tractor():
graph = ['N', 'W', 'S', 'E']
graph_index = 0
current_rotation = 'S'
start = (0,0)
goal = (10,6)
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
is_possible = False
while not is_possible:
graph_index = (graph_index + 1) % 4
current = frontier.get()
if current == goal:
break
for next in
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()
asyncio.run(tractor())