2019-05-27 07:40:42 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-05-06 12:51:41 +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 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):
|
|
|
|
if sys.platform == "win32":
|
|
|
|
os.system("cls")
|
|
|
|
else:
|
|
|
|
os.system("clear")
|
2019-05-08 02:31:29 +02:00
|
|
|
print(tractor.get_position())
|
2019-05-06 12:51:41 +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:
|
2019-05-07 15:37:27 +02:00
|
|
|
if tractor.get_position()[0] == x \
|
|
|
|
and tractor.get_position()[1] == y:
|
2019-05-06 12:51:41 +02:00
|
|
|
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):
|
2019-05-07 15:37:27 +02:00
|
|
|
x = tractor.get_position()[0]
|
|
|
|
y = tractor.get_position()[1]
|
2019-05-06 12:51:41 +02:00
|
|
|
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]:
|
2019-05-07 15:37:27 +02:00
|
|
|
# print(str(x) + " " + str(y))
|
2019-05-06 12:51:41 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
field = initialize_field()
|
2019-05-07 15:37:27 +02:00
|
|
|
tractor = Trac('N', (0,0))
|
2019-05-06 12:51:41 +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())
|
|
|
|
elif message[0] == "try":
|
|
|
|
if try_move(field, tractor):
|
|
|
|
writer.write("OK\n".encode())
|
|
|
|
else:
|
|
|
|
writer.write("FAIL\n".encode())
|
|
|
|
else:
|
|
|
|
tractor.move()
|
|
|
|
writer.write(("OK\n").encode())
|
|
|
|
|
2019-05-07 15:37:27 +02:00
|
|
|
print_field(field, tractor)
|
2019-05-06 12:51:41 +02:00
|
|
|
# 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())
|