refactored
This commit is contained in:
parent
f868b53e7e
commit
6cfeedcd7e
63
Cucumber.py
63
Cucumber.py
@ -1,55 +1,14 @@
|
||||
from Plant import Plant
|
||||
|
||||
class Cucumber(Plant):
|
||||
def __init__(self):
|
||||
self.__is_alive = True
|
||||
self.__ttl = 40
|
||||
self.__hydration = 40
|
||||
self.__soil_level = 40
|
||||
self.__dehydration_ratio = 0.8
|
||||
self.__desoil_ratio = 0.7
|
||||
self.__max_soil_lvl = 40
|
||||
self.__max_hydration_lvl = 40
|
||||
self.__ready = 11
|
||||
|
||||
def get_symbol(self):
|
||||
if not self.__is_alive:
|
||||
return ('x', None)
|
||||
elif self.__hydration > (self.__max_hydration_lvl / 2) \
|
||||
and self.__soil_level > (self.__max_soil_lvl / 2):
|
||||
return ('o', "green") if (self.__ttl > self.__ready) \
|
||||
else ('O', "green")
|
||||
else:
|
||||
return ('o', "yellow") if (self.__ttl > self.__ready) \
|
||||
else ('O', "yellow")
|
||||
|
||||
def tick(self, n):
|
||||
self.decrease_ttl(n)
|
||||
self.decrase_hydration(n)
|
||||
self.decrease_soillevel(n)
|
||||
|
||||
def decrease_ttl(self, n):
|
||||
self.__ttl -= n
|
||||
if self.__ttl == 0:
|
||||
self.__is_alive == False
|
||||
|
||||
|
||||
def increase_hydration(self, n):
|
||||
self.__hydration += n
|
||||
if self.__hydration > 40:
|
||||
self.__is_alive = False
|
||||
|
||||
def decrase_hydration(self, n):
|
||||
self.__hydration -= n * self.__dehydration_ratio
|
||||
if self.__hydration < 1:
|
||||
self.__is_alive = False
|
||||
|
||||
def increase_soillevel(self, n):
|
||||
self.__soil_level += n
|
||||
if self.__soil_level > 40:
|
||||
self.__is_alive = False
|
||||
|
||||
def decrease_soillevel(self, n):
|
||||
self.__soil_level -= n * self.__desoil_ratio
|
||||
if self.__soil_level < 1:
|
||||
self.__is_alive = False
|
||||
is_alive = True
|
||||
ttl = 40
|
||||
hydration = 40
|
||||
soil_level = 40
|
||||
dehydration_ratio = 0.8
|
||||
desoil_ratio = 0.7
|
||||
max_soil_lvl = 40
|
||||
optimal_soil_ratio = 2
|
||||
max_hydration_lvl = 40
|
||||
optimal_hydration_ratio = 2
|
||||
ready = 11
|
||||
|
7
Graph.py
Normal file
7
Graph.py
Normal file
@ -0,0 +1,7 @@
|
||||
class SimpleGraph:
|
||||
def __init__(self):
|
||||
self.edges = {}
|
||||
|
||||
def neighbors(self, id):
|
||||
return self.edges[id]
|
||||
|
84
Plant.py
84
Plant.py
@ -1,11 +1,87 @@
|
||||
class Plant():
|
||||
def _init_(self):
|
||||
@property
|
||||
def is_alive(self):
|
||||
pass
|
||||
|
||||
def get_symbol(self):
|
||||
raise NotImplementedError("Please Implement this method")
|
||||
@property
|
||||
def ttl(self):
|
||||
pass
|
||||
|
||||
def tick(self, n):
|
||||
raise NotImplementedError("Please Implement this method")
|
||||
@property
|
||||
def hydration(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def soil_level(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def dehydration_ratio(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def max_soil_lvl(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def optimal_soil_ratio(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def max_hydration_lvl(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def optimal_hydration_ratio(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def ready(self):
|
||||
pass
|
||||
|
||||
def get_stats(self):
|
||||
#np. touple'a z info czy żyje, ile ma wody i nawozu
|
||||
raise NotImplementedError("Please Implement this method")
|
||||
|
||||
def get_symbol(self):
|
||||
if not self.is_alive:
|
||||
return ('x', None)
|
||||
elif self.hydration > (self.max_hydration_lvl / self.optimal_hydration_ratio) \
|
||||
and self.soil_level > (self.max_soil_lvl / self.optimal_soil_ratio):
|
||||
return ('p', "green") if (self.ttl > self.ready) \
|
||||
else ('P', "green")
|
||||
else:
|
||||
return ('p', "yellow") if (self.ttl > self.ready) \
|
||||
else ('P', "yellow")
|
||||
|
||||
def tick(self, n):
|
||||
self.decrease_ttl(n)
|
||||
self.decrase_hydration(n)
|
||||
self.decrease_soillevel(n)
|
||||
|
||||
def decrease_ttl(self, n):
|
||||
self.ttl -= n
|
||||
if self.ttl == 0:
|
||||
self.is_alive == False
|
||||
|
||||
|
||||
def increase_hydration(self, n):
|
||||
self.hydration += n
|
||||
if self.hydration > self.max_hydration_lvl:
|
||||
self.is_alive = False
|
||||
|
||||
def decrase_hydration(self, n):
|
||||
self.hydration -= n * self.dehydration_ratio
|
||||
if self.hydration < 1:
|
||||
self.is_alive = False
|
||||
|
||||
def increase_soillevel(self, n):
|
||||
self.soil_level += n
|
||||
if self.soil_level > self.max_soil_lvl:
|
||||
self.is_alive = False
|
||||
|
||||
def decrease_soillevel(self, n):
|
||||
self.soil_level -= n * self.desoil_ratio
|
||||
if self.soil_level < 1:
|
||||
self.is_alive = False
|
||||
|
63
Tomato.py
63
Tomato.py
@ -1,55 +1,14 @@
|
||||
from Plant import Plant
|
||||
|
||||
class Tomato(Plant):
|
||||
def __init__(self):
|
||||
self.__is_alive = True
|
||||
self.__ttl = 38
|
||||
self.__hydration = 41
|
||||
self.__soil_level = 41
|
||||
self.__dehydration_ratio = 0.7
|
||||
self.__desoil_ratio = 0.8
|
||||
self.__max_soil_lvl = 40
|
||||
self.__max_hydration_lvl = 40
|
||||
self.__ready = 10
|
||||
|
||||
def get_symbol(self):
|
||||
if not self.__is_alive:
|
||||
return ('x', None)
|
||||
elif self.__hydration > (self.__max_hydration_lvl / 2) \
|
||||
and self.__soil_level > (self.__max_soil_lvl / 2):
|
||||
return ('p', "green") if (self.__ttl > self.__ready) \
|
||||
else ('P', "green")
|
||||
else:
|
||||
return ('p', "yellow") if (self.__ttl > self.__ready) \
|
||||
else ('P', "yellow")
|
||||
|
||||
def tick(self, n):
|
||||
self.decrease_ttl(n)
|
||||
self.decrase_hydration(n)
|
||||
self.decrease_soillevel(n)
|
||||
|
||||
def decrease_ttl(self, n):
|
||||
self.__ttl -= n
|
||||
if self.__ttl == 0:
|
||||
self.__is_alive == False
|
||||
|
||||
|
||||
def increase_hydration(self, n):
|
||||
self.__hydration += n
|
||||
if self.__hydration > 40:
|
||||
self.__is_alive = False
|
||||
|
||||
def decrase_hydration(self, n):
|
||||
self.__hydration -= n * self.__dehydration_ratio
|
||||
if self.__hydration < 1:
|
||||
self.__is_alive = False
|
||||
|
||||
def increase_soillevel(self, n):
|
||||
self.__soil_level += n
|
||||
if self.__soil_level > 40:
|
||||
self.__is_alive = False
|
||||
|
||||
def decrease_soillevel(self, n):
|
||||
self.__soil_level -= n * self.__desoil_ratio
|
||||
if self.__soil_level < 1:
|
||||
self.__is_alive = False
|
||||
is_alive = True
|
||||
ttl = 38
|
||||
hydration = 41
|
||||
soil_level = 41
|
||||
dehydration_ratio = 0.7
|
||||
desoil_ratio = 0.8
|
||||
max_soil_lvl = 40
|
||||
optimal_soil_ratio = 2
|
||||
max_hydration_lvl = 40
|
||||
optimal_hydration_ratio = 2
|
||||
ready = 10
|
||||
|
19
Trac.py
19
Trac.py
@ -1,8 +1,6 @@
|
||||
from Point import Point
|
||||
|
||||
class Trac:
|
||||
def __init__(self, rotation, position):
|
||||
self.__position = Point(position.get_cord())
|
||||
self.__position = position
|
||||
self.__rotation = rotation
|
||||
|
||||
def get_symbol(self):
|
||||
@ -19,7 +17,8 @@ class Trac:
|
||||
self.__rotation = rotation
|
||||
|
||||
def set_position(self, position):
|
||||
self.__position.set_cord(position)
|
||||
self.__position[0] = position[0]
|
||||
self.__position[1] = position[1]
|
||||
|
||||
def get_position(self):
|
||||
return self.__position
|
||||
@ -28,14 +27,14 @@ class Trac:
|
||||
return self.__rotation
|
||||
|
||||
def move(self):
|
||||
x = self.get_position().get_x()
|
||||
y = self.get_position().get_y()
|
||||
x = self.__position[0]
|
||||
y = self.__position[1]
|
||||
if self.__rotation == 'N':
|
||||
self.set_position((x - 1 ,y))
|
||||
self.__position = (x - 1, y)
|
||||
elif self.__rotation == 'S':
|
||||
self.set_position((x + 1 ,y))
|
||||
self.__position = (x + 1, y)
|
||||
elif self.__rotation == 'W':
|
||||
self.set_position((x, y - 1))
|
||||
self.__position = (x, y - 1)
|
||||
else:
|
||||
self.set_position((x, y + 1))
|
||||
self.__position = (x, y + 1)
|
||||
|
||||
|
15
env.py
15
env.py
@ -6,7 +6,6 @@ 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'
|
||||
@ -46,8 +45,8 @@ def print_field(field, tractor):
|
||||
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:
|
||||
if tractor.get_position()[0] == x \
|
||||
and tractor.get_position()[1] == y:
|
||||
sys.stdout.write(OKBLUE)
|
||||
sys.stdout.write(tractor.get_symbol())
|
||||
sys.stdout.write(" ")
|
||||
@ -73,8 +72,8 @@ def update_state(field):
|
||||
i.tick(1)
|
||||
|
||||
def try_move(field, tractor):
|
||||
x = tractor.get_position().get_x()
|
||||
y = tractor.get_position().get_y()
|
||||
x = tractor.get_position()[0]
|
||||
y = tractor.get_position()[1]
|
||||
rotation = tractor.get_rotation()
|
||||
if rotation == 'N':
|
||||
x -= 1
|
||||
@ -87,7 +86,7 @@ def try_move(field, tractor):
|
||||
if x >= 0 and x < len(field) \
|
||||
and y >= 0 and y < len(field[0]) \
|
||||
and not field[x][y]:
|
||||
print(str(x) + " " + str(y))
|
||||
# print(str(x) + " " + str(y))
|
||||
return True
|
||||
return False
|
||||
|
||||
@ -95,7 +94,7 @@ def try_move(field, tractor):
|
||||
if __name__ == "__main__":
|
||||
|
||||
field = initialize_field()
|
||||
tractor = Trac('N', Point((0,0)))
|
||||
tractor = Trac('N', (0,0))
|
||||
|
||||
async def handle_echo(reader, writer):
|
||||
data = await reader.readline()
|
||||
@ -113,7 +112,7 @@ if __name__ == "__main__":
|
||||
tractor.move()
|
||||
writer.write(("OK\n").encode())
|
||||
|
||||
# print_field(field, tractor)
|
||||
print_field(field, tractor)
|
||||
# print(tractor.get_position().get_x())
|
||||
# print(tractor.get_position().get_y())
|
||||
# print(tractor.get_rotation())
|
||||
|
55
tractor.py
55
tractor.py
@ -31,6 +31,29 @@ def getrotation(a, b):
|
||||
else:
|
||||
return 'W'
|
||||
|
||||
async def rotate(direction):
|
||||
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
||||
writer.write(("rotate " + direction + "\n").encode())
|
||||
await reader.readline()
|
||||
time.sleep(0.7)
|
||||
writer.close()
|
||||
|
||||
async def try_move():
|
||||
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
||||
writer.write("try\n".encode())
|
||||
data = await reader.readline()
|
||||
result = data.decode()
|
||||
time.sleep(0.7)
|
||||
writer.close()
|
||||
return result
|
||||
|
||||
async def move():
|
||||
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
||||
writer.write("move\n".encode())
|
||||
await reader.readline()
|
||||
time.sleep(0.7)
|
||||
writer.close()
|
||||
|
||||
|
||||
async def move_tractor(start_position, goal):
|
||||
directions = ['N', 'W', 'S', 'E']
|
||||
@ -51,24 +74,11 @@ async def move_tractor(start_position, goal):
|
||||
if direction == last_node:
|
||||
continue
|
||||
|
||||
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
||||
writer.write(("rotate " + direction + "\n").encode())
|
||||
await reader.readline()
|
||||
time.sleep(0.7)
|
||||
writer.close()
|
||||
await rotate(direction)
|
||||
|
||||
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
||||
writer.write("try\n".encode())
|
||||
data = await reader.readline()
|
||||
result = data.decode()
|
||||
time.sleep(0.7)
|
||||
writer.close()
|
||||
result = await try_move()
|
||||
|
||||
if result == "OK\n":
|
||||
print("~~~~~~~~~~")
|
||||
print(current_position)
|
||||
print(direction)
|
||||
print("~~~~~~~~~~")
|
||||
local_graph.append(addnode(current_position, direction))
|
||||
|
||||
current_rotation = directions[(directions.index(current_rotation) - 1) % 4]
|
||||
@ -78,19 +88,9 @@ async def move_tractor(start_position, goal):
|
||||
if not start_flag:
|
||||
rotation = getrotation(current_position, current)
|
||||
if rotation != current_rotation:
|
||||
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
||||
writer.write(("rotate " + rotation + "\n").encode())
|
||||
await reader.readline()
|
||||
current_rotation = rotation
|
||||
time.sleep(0.7)
|
||||
writer.close()
|
||||
await rotate(rotation)
|
||||
last_node = directions[(directions.index(rotation) - 2) % 4]
|
||||
|
||||
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
||||
writer.write("move\n".encode())
|
||||
await reader.readline()
|
||||
time.sleep(0.7)
|
||||
writer.close()
|
||||
await move()
|
||||
current_position = current
|
||||
|
||||
if current == goal:
|
||||
@ -104,7 +104,6 @@ async def move_tractor(start_position, goal):
|
||||
frontier.put(next, priority)
|
||||
|
||||
start_flag = False
|
||||
writer.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
start = (0,0)
|
||||
|
Loading…
Reference in New Issue
Block a user