2019-04-29 06:52:57 +02:00
|
|
|
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(None)
|
|
|
|
elif j < 3:
|
|
|
|
row.append(Cucumber())
|
|
|
|
else:
|
|
|
|
row.append(Tomato())
|
|
|
|
field.append(row)
|
|
|
|
return field
|
|
|
|
|
|
|
|
|
|
|
|
def print_field(field, tractor):
|
2019-04-30 00:08:38 +02:00
|
|
|
if sys.platform == "win32":
|
|
|
|
os.system("cls")
|
|
|
|
else:
|
|
|
|
os.system("clear")
|
2019-04-29 06:52:57 +02:00
|
|
|
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()
|
2019-04-29 10:49:03 +02:00
|
|
|
tractor = Trac('N', Point((0,0)))
|
2019-04-29 06:52:57 +02:00
|
|
|
|
|
|
|
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())
|
2019-04-30 00:08:38 +02:00
|
|
|
elif message[0] == "try":
|
|
|
|
if try_move(field, tractor):
|
|
|
|
writer.write("OK\n".encode())
|
2019-04-29 06:52:57 +02:00
|
|
|
else:
|
2019-04-30 00:08:38 +02:00
|
|
|
writer.write("FAIL\n".encode())
|
2019-04-29 06:52:57 +02:00
|
|
|
else:
|
2019-04-30 00:08:38 +02:00
|
|
|
tractor.move()
|
|
|
|
writer.write(("OK\n").encode())
|
|
|
|
|
2019-04-29 06:52:57 +02:00
|
|
|
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())
|