.
This commit is contained in:
parent
b7198cb910
commit
bf2ecb7de5
11
Trac.py
11
Trac.py
@ -15,12 +15,15 @@ class Trac:
|
|||||||
else:
|
else:
|
||||||
return '<'
|
return '<'
|
||||||
|
|
||||||
# def set_rotation(self, rotation):
|
def set_rotation(self, rotation):
|
||||||
# self.__rotation = rotation
|
self.__rotation = rotation
|
||||||
|
|
||||||
# def set_position(self, position):
|
def set_position(self, position):
|
||||||
# self.__position.set_cord(position)
|
self.__position.set_cord(position)
|
||||||
|
|
||||||
def get_position(self):
|
def get_position(self):
|
||||||
return self.__position
|
return self.__position
|
||||||
|
|
||||||
|
def get_rotation(self):
|
||||||
|
return self.__rotation
|
||||||
|
|
||||||
|
BIN
__pycache__/Cucumber.cpython-37.pyc
Normal file
BIN
__pycache__/Cucumber.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/Plant.cpython-37.pyc
Normal file
BIN
__pycache__/Plant.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/Point.cpython-37.pyc
Normal file
BIN
__pycache__/Point.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/Tomato.cpython-37.pyc
Normal file
BIN
__pycache__/Tomato.cpython-37.pyc
Normal file
Binary file not shown.
BIN
__pycache__/Trac.cpython-37.pyc
Normal file
BIN
__pycache__/Trac.cpython-37.pyc
Normal file
Binary file not shown.
143
env.py
Normal file
143
env.py
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
import string
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import asyncio
|
||||||
|
from Tomato import Tomato
|
||||||
|
from Cucumber import Cucumber
|
||||||
|
from Plant import Plant
|
||||||
|
from Point import Point
|
||||||
|
from Trac import Trac
|
||||||
|
|
||||||
|
string.ascii_letters = 'oOpPx'
|
||||||
|
string.ascii_numbers = '01'
|
||||||
|
OKGREEN = '\033[92m'
|
||||||
|
OKBLUE = '\033[94m'
|
||||||
|
OKRED = '\033[91m'
|
||||||
|
ENDC = '\033[0m'
|
||||||
|
|
||||||
|
|
||||||
|
def initialize_field():
|
||||||
|
field = []
|
||||||
|
for i in range(11):
|
||||||
|
row = []
|
||||||
|
for j in range(7):
|
||||||
|
if i == 0 or i == 10 or j == 0 \
|
||||||
|
or j == 3 or j == 6:
|
||||||
|
# row.append((Point((i, j)), None))
|
||||||
|
row.append(None)
|
||||||
|
elif j < 3:
|
||||||
|
# row.append((Point((i, j)), Cucumber()))
|
||||||
|
row.append(Cucumber())
|
||||||
|
else:
|
||||||
|
# row.append((Point((i, j)), Tomato()))
|
||||||
|
row.append(Tomato())
|
||||||
|
field.append(row)
|
||||||
|
return field
|
||||||
|
|
||||||
|
|
||||||
|
def print_field(field, tractor):
|
||||||
|
sys.stdout.write(OKBLUE)
|
||||||
|
sys.stdout.write("$")
|
||||||
|
sys.stdout.write(" ")
|
||||||
|
sys.stdout.write("\n")
|
||||||
|
|
||||||
|
for x, row in enumerate(field):
|
||||||
|
for y, i in enumerate(row):
|
||||||
|
if not i:
|
||||||
|
if tractor.get_position().get_x() == x \
|
||||||
|
and tractor.get_position().get_y() == y:
|
||||||
|
sys.stdout.write(OKBLUE)
|
||||||
|
sys.stdout.write(tractor.get_symbol())
|
||||||
|
sys.stdout.write(" ")
|
||||||
|
else:
|
||||||
|
sys.stdout.write(" ")
|
||||||
|
else:
|
||||||
|
symbol = i.get_symbol()
|
||||||
|
if symbol[1] == "green":
|
||||||
|
sys.stdout.write(OKGREEN)
|
||||||
|
sys.stdout.write(symbol[0])
|
||||||
|
sys.stdout.write(" ")
|
||||||
|
else:
|
||||||
|
sys.stdout.write(OKRED)
|
||||||
|
sys.stdout.write(symbol[0])
|
||||||
|
sys.stdout.write(" ")
|
||||||
|
|
||||||
|
sys.stdout.write("\n")
|
||||||
|
|
||||||
|
def update_state(field):
|
||||||
|
for row in field:
|
||||||
|
for i in row:
|
||||||
|
if i:
|
||||||
|
i.tick(1)
|
||||||
|
|
||||||
|
def try_move(field, tractor):
|
||||||
|
x = tractor.get_position().get_x()
|
||||||
|
y = tractor.get_position().get_y()
|
||||||
|
rotation = tractor.get_rotation()
|
||||||
|
if rotation == 'N':
|
||||||
|
x -= 1
|
||||||
|
elif rotation == 'S':
|
||||||
|
x += 1
|
||||||
|
elif rotation == 'W':
|
||||||
|
y -= 1
|
||||||
|
else:
|
||||||
|
y += 1
|
||||||
|
if x >= 0 and x < len(field) \
|
||||||
|
and y >= 0 and y < len(field[0]) \
|
||||||
|
and not field[x][y]:
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
field = initialize_field()
|
||||||
|
tractor = Trac('S', Point((0,0)))
|
||||||
|
|
||||||
|
async def handle_echo(reader, writer):
|
||||||
|
data = await reader.readline()
|
||||||
|
message = data.decode().split()
|
||||||
|
|
||||||
|
if message[0] == "rotate":
|
||||||
|
tractor.set_rotation(message[1])
|
||||||
|
writer.write("OK\n".encode())
|
||||||
|
elif try_move(field, tractor):
|
||||||
|
x = tractor.get_position().get_x()
|
||||||
|
y = tractor.get_position().get_y()
|
||||||
|
rotation = tractor.get_rotation()
|
||||||
|
if rotation == 'N':
|
||||||
|
x -= 1
|
||||||
|
elif rotation == 'S':
|
||||||
|
x += 1
|
||||||
|
elif rotation == 'W':
|
||||||
|
y -= 1
|
||||||
|
else:
|
||||||
|
y += 1
|
||||||
|
tractor.set_position((x,y))
|
||||||
|
writer.write("OK\n".encode())
|
||||||
|
else:
|
||||||
|
writer.write("FAIL\n".encode())
|
||||||
|
if sys.platform == "win32":
|
||||||
|
os.system("cls")
|
||||||
|
else:
|
||||||
|
os.system("clear")
|
||||||
|
print_field(field, tractor)
|
||||||
|
# print(tractor.get_position().get_x())
|
||||||
|
# print(tractor.get_position().get_y())
|
||||||
|
# print(tractor.get_rotation())
|
||||||
|
update_state(field)
|
||||||
|
await writer.drain()
|
||||||
|
|
||||||
|
writer.close()
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
server = await asyncio.start_server(
|
||||||
|
handle_echo, '127.0.0.1', 8888)
|
||||||
|
|
||||||
|
addr = server.sockets[0].getsockname()
|
||||||
|
|
||||||
|
async with server:
|
||||||
|
await server.serve_forever()
|
||||||
|
|
||||||
|
asyncio.run(main())
|
55
tractor.py
Normal file
55
tractor.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
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())
|
Loading…
Reference in New Issue
Block a user