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