This commit is contained in:
Tomasz Adamczyk 2021-04-09 22:49:58 +02:00
parent 36be402ae3
commit 32d7317af5
10 changed files with 142 additions and 79 deletions

View File

@ -2,10 +2,13 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="56453584-72bd-49f4-a39c-fccf91ab20c6" name="Default Changelist" comment=""> <list default="true" id="56453584-72bd-49f4-a39c-fccf91ab20c6" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/graph.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/definitions.py" beforeDir="false" afterPath="$PROJECT_DIR$/definitions.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/fringe.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/graph.py" beforeDir="false" afterPath="$PROJECT_DIR$/graph.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/harvest.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/istate.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/py.py" beforeDir="false" afterPath="$PROJECT_DIR$/py.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/py.py" beforeDir="false" afterPath="$PROJECT_DIR$/py.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tractor.py" beforeDir="false" afterPath="$PROJECT_DIR$/tractor.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -105,6 +108,9 @@
<workItem from="1617966682687" duration="4169000" /> <workItem from="1617966682687" duration="4169000" />
<workItem from="1617972419025" duration="598000" /> <workItem from="1617972419025" duration="598000" />
<workItem from="1617973153919" duration="5668000" /> <workItem from="1617973153919" duration="5668000" />
<workItem from="1617982355911" duration="241000" />
<workItem from="1617984929356" duration="612000" />
<workItem from="1617992640419" duration="8178000" />
</task> </task>
<servers /> <servers />
</component> </component>
@ -112,44 +118,48 @@
<option name="version" value="1" /> <option name="version" value="1" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state width="1879" height="295" key="GridCell.Tab.0.bottom" timestamp="1617978874344"> <state x="690" y="287" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1617994393023">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1617978874344" /> <state x="690" y="287" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1617994393023" />
<state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1617978874344"> <state width="1879" height="364" key="GridCell.Tab.0.bottom" timestamp="1618001369741">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1617978874344" /> <state width="1879" height="364" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1618001369741" />
<state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1617978874343"> <state width="1879" height="364" key="GridCell.Tab.0.center" timestamp="1618001369741">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1617978874343" /> <state width="1879" height="364" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1618001369741" />
<state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1617978874344"> <state width="1879" height="364" key="GridCell.Tab.0.left" timestamp="1618001369741">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1617978874344" /> <state width="1879" height="364" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1618001369741" />
<state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1617385468328"> <state width="1879" height="364" key="GridCell.Tab.0.right" timestamp="1618001369741">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="364" key="GridCell.Tab.1.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1617385468328" /> <state width="1879" height="364" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1618001369741" />
<state width="1879" height="364" key="GridCell.Tab.1.center" timestamp="1617385468328"> <state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1618001369741">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="364" key="GridCell.Tab.1.center/0.0.1920.1080@0.0.1920.1080" timestamp="1617385468328" /> <state width="1879" height="364" key="GridCell.Tab.1.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1618001369741" />
<state width="1879" height="364" key="GridCell.Tab.1.left" timestamp="1617385468328"> <state width="1879" height="364" key="GridCell.Tab.1.center" timestamp="1618001369741">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="364" key="GridCell.Tab.1.left/0.0.1920.1080@0.0.1920.1080" timestamp="1617385468328" /> <state width="1879" height="364" key="GridCell.Tab.1.center/0.0.1920.1080@0.0.1920.1080" timestamp="1618001369741" />
<state width="1879" height="364" key="GridCell.Tab.1.right" timestamp="1617385468328"> <state width="1879" height="364" key="GridCell.Tab.1.left" timestamp="1618001369741">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1617385468328" /> <state width="1879" height="364" key="GridCell.Tab.1.left/0.0.1920.1080@0.0.1920.1080" timestamp="1618001369741" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1617973162746"> <state width="1879" height="364" key="GridCell.Tab.1.right" timestamp="1618001369741">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1617973162746" /> <state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1618001369741" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1617992641722">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1617992641722" />
</component> </component>
<component name="com.intellij.coverage.CoverageDataManagerImpl"> <component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1617977810609" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" /> <SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1618001250511" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
</component> </component>
</project> </project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +0,0 @@
class Fringe: #kolejka zawierająca akcje oraz pola do odwiedzenia
def __init__(self, fringe):
self.fringe = fringe
def get_fringe(self):
return self.fringe
def set_fringe(self, fringe):
self.fringe = fringe

View File

@ -1,5 +1,96 @@
@staticmethod import tractor
class Node:
def __init__(self, action, direction, parent, x, y):
self.action = action
self.direction = direction
self.parent = parent
self.x = x
self.y = y
def get_action(self):
return self.action
def set_action(self, action):
self.action = action
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_parent(self):
return self.parent
def set_parent(self, parent):
self.parent = parent
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
class Istate: #stan początkowy traktora
def __init__(self, direction, x, y):
self.direction = direction
self.x = x
self.y = y
def get_direction(self):
return self.direction
def set_direction(self, direction):
self.direction = direction
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
# class Fringe: #kolejka zawierająca akcje oraz pola do odwiedzenia
# def __init__(self, fringe):
# self.fringe = fringe
# def get_fringe(self):
# return self.fringe
# def set_fringe(self, fringe):
# self.fringe = fringe
# def add_to_fringe(self, value):
# self.fringe.append(value)
# def get_element_from_fringe_pop(self):
# return self.fringe.pop(0)
def goal_test(elem, goaltest):
if elem.get_x() == goaltest.get_x() and elem.get_y() == goaltest.get_y(): #goaltest bez getterów
return True
else:
return False
def succ(elem):
actions_list = []
actions_list.append(("rotate_left", (elem.get_x(), elem.get_y())))
actions_list.append(("rotate_right", (elem.get_x(), elem.get_y())))
if tractor.Tractor.is_move_allowed_succ(elem) == "x + 1":
actions_list.append(("move", (elem.set_x(elem.get_x() + 1), elem.get_y())))
elif tractor.Tractor.is_move_allowed_succ(elem) == "y - 1":
actions_list.append(("move", (elem.get_x(), elem.set_y(elem.get_y() - 1))))
elif tractor.Tractor.is_move_allowed_succ(elem) == "y + 1":
actions_list.append(("move", (elem.set_x(elem.get_x()), elem.set_y(elem.get_y() + 1))))
elif tractor.Tractor.is_move_allowed_succ(elem) == "x - 1":
actions_list.append(("move", (elem.set_x(elem.get_x() - 1), elem.get_y())))
return actions_list
def graphsearch(fringe, explored, istate, succ, goaltest): def graphsearch(fringe, explored, istate, succ, goaltest):
fringe.add_to_fringe(istate) node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y())
#fringe.add_to_fringe(node)
fringe.append(node)
while True: while True:
if not fringe:
return False
#elem = fringe.get_element_from_fringe_pop()
elem = fringe.pop(0)
if goal_test(elem, goaltest) is True:
return explored
explored.append(elem)
for (action, state) in succ(elem):
if state not in fringe and state not in explored:
x = Node(action, elem.get_direction(), elem, state[0], state[1])
print(state[0])
print(state[1])
print(x.get_action())
print(x.get_direction())
print(x.get_parent())
fringe.append(x)

View File

@ -1,28 +0,0 @@
import definitions
class Harvest: #lista dojrzałych roślin
def __init__(self, harvest):
self.harvest = harvest
def get_harvest(self):
return self.harvest
def set_harvest(self, harvest):
self.harvest = harvest
def add_to_harvest(self, value):
self.harvest.append(value)
def remove_element_from_harvest(self, value): #usuwa element z listy o wartości value
self.harvest.remove(value)
def find_grown_plants(self, map1): #szuka, na których polach są dojrzałe rośliny
for i in range(definitions.WIDTH_AMOUNT):
for j in range(definitions.HEIGHT_AMOUNT):
field = map1.get_fields()[i][j]
if (i * definitions.BLOCK_SIZE,
j * definitions.BLOCK_SIZE) not in self.harvest and field.get_plant().get_name() == "beetroot" and field.get_plant().get_state() > 0 and field.get_plant().get_state() == definitions.BEETROOTS_MAXIMUM_STATE:
self.add_to_harvest((i * definitions.BLOCK_SIZE, j * definitions.BLOCK_SIZE))
elif (i * definitions.BLOCK_SIZE,
j * definitions.BLOCK_SIZE) not in self.harvest and field.get_plant().get_name() == "carrot" and field.get_plant().get_state() > 0 and field.get_plant().get_state() == definitions.CARROTS_MAXIMUM_STATE:
self.add_to_harvest((i * definitions.BLOCK_SIZE, j * definitions.BLOCK_SIZE))
elif (i * definitions.BLOCK_SIZE,
j * definitions.BLOCK_SIZE) not in self.harvest and field.get_plant().get_name() == "potato" and field.get_plant().get_state() > 0 and field.get_plant().get_state() == definitions.POTATOES_MAXIMUM_STATE:
self.add_to_harvest((i * definitions.BLOCK_SIZE, j * definitions.BLOCK_SIZE))
elif (i * definitions.BLOCK_SIZE,
j * definitions.BLOCK_SIZE) not in self.harvest and field.get_plant().get_name() == "wheat" and field.get_plant().get_state() > 0 and field.get_plant().get_state() == definitions.WHEAT_MAXIMUM_STATE:
self.add_to_harvest((i * definitions.BLOCK_SIZE, j * definitions.BLOCK_SIZE))

View File

@ -1,17 +0,0 @@
class Istate: #stan początkowy traktora
def __init__(self, direction, x, y):
self.direction = direction
self.x = x
self.y = y
def get_direction(self):
return self.direction
def set_x(self, direction):
self.direction = direction
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y

8
py.py
View File

@ -1,5 +1,5 @@
import definitions import definitions
import fringe import graph
import map import map
import plant import plant
import pygame import pygame
@ -8,14 +8,13 @@ import tractor
pygame.display.set_caption("Smart Tractor") pygame.display.set_caption("Smart Tractor")
def main(): def main():
#tworzenie podstawowych obiektów #tworzenie podstawowych obiektów
fringe1 = fringe.Fringe([])
map1 = map.Map([]) map1 = map.Map([])
map1.create_base_map() map1.create_base_map()
amount_of_seeds_dict = {"beetroot": definitions.TRACTOR_AMOUNT_OF_SEEDS_EACH_TYPE, "carrot": definitions.TRACTOR_AMOUNT_OF_SEEDS_EACH_TYPE, "potato": definitions.TRACTOR_AMOUNT_OF_SEEDS_EACH_TYPE, "wheat": definitions.TRACTOR_AMOUNT_OF_SEEDS_EACH_TYPE} amount_of_seeds_dict = {"beetroot": definitions.TRACTOR_AMOUNT_OF_SEEDS_EACH_TYPE, "carrot": definitions.TRACTOR_AMOUNT_OF_SEEDS_EACH_TYPE, "potato": definitions.TRACTOR_AMOUNT_OF_SEEDS_EACH_TYPE, "wheat": definitions.TRACTOR_AMOUNT_OF_SEEDS_EACH_TYPE}
collected_plants_dict = {"beetroot": 0, "carrot": 0, "potato": 0, "wheat": 0} collected_plants_dict = {"beetroot": 0, "carrot": 0, "potato": 0, "wheat": 0}
fertilizer_dict = {"beetroot": definitions.TRACTOR_FERTILIZER, "carrot": definitions.TRACTOR_FERTILIZER, "potato": definitions.TRACTOR_FERTILIZER, "wheat": definitions.TRACTOR_FERTILIZER} fertilizer_dict = {"beetroot": definitions.TRACTOR_FERTILIZER, "carrot": definitions.TRACTOR_FERTILIZER, "potato": definitions.TRACTOR_FERTILIZER, "wheat": definitions.TRACTOR_FERTILIZER}
station1 = station.Station(collected_plants_dict) station1 = station.Station(collected_plants_dict)
tractor1 = tractor.Tractor(amount_of_seeds_dict, collected_plants_dict, definitions.TRACTOR_DIRECTION_NORTH, fertilizer_dict, definitions.TRACTOR_FUEL, definitions.TRACTOR_WATER_LEVEL, 0, 0) tractor1 = tractor.Tractor(amount_of_seeds_dict, collected_plants_dict, definitions.TRACTOR_DIRECTION_SOUTH, fertilizer_dict, definitions.TRACTOR_FUEL, definitions.TRACTOR_WATER_LEVEL, 0, 0)
tractor1_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE) tractor1_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE)
clock = pygame.time.Clock() clock = pygame.time.Clock()
run = True run = True
@ -24,6 +23,9 @@ def main():
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
run = False run = False
istate = graph.Istate(definitions.TRACTOR_DIRECTION_SOUTH, 0, 0)
istate1 = graph.Istate(None, 4, 4)
print(graph.graphsearch([], [], istate, graph.succ, istate1))
map1.draw_window(tractor1, tractor1_rect) map1.draw_window(tractor1, tractor1_rect)
tractor1.tractor1_handle_movement(tractor1_rect) tractor1.tractor1_handle_movement(tractor1_rect)
tractor1.do_work(map1, station1, tractor1_rect) tractor1.do_work(map1, station1, tractor1_rect)

View File

@ -172,6 +172,18 @@ class Tractor:
return True return True
else: else:
return False return False
@staticmethod
def is_move_allowed_succ(node): #sprawdza czy dany ruch, który chce wykonać traktor jest możliwy, zwraca pozycje po wykonaniu ruchu, wersja node
if node.get_direction() == definitions.TRACTOR_DIRECTION_EAST and node.get_x() + definitions.BLOCK_SIZE < definitions.WIDTH:
return "x + 1"
elif node.get_direction() == definitions.TRACTOR_DIRECTION_NORTH and node.get_y() - definitions.BLOCK_SIZE >= 0:
return "y - 1"
elif node.get_direction() == definitions.TRACTOR_DIRECTION_SOUTH and node.get_y() + definitions.BLOCK_SIZE < definitions.HEIGHT:
return "y + 1"
elif node.get_direction() == definitions.TRACTOR_DIRECTION_WEST and node.get_x() - definitions.BLOCK_SIZE >= 0:
return "x - 1"
else:
return False
def tractor1_handle_movement(self, tractor1_rect): #odpowiada za poruszanie się traktora po mapie def tractor1_handle_movement(self, tractor1_rect): #odpowiada za poruszanie się traktora po mapie
loop = True loop = True
while loop and self.get_fuel() > 0: while loop and self.get_fuel() > 0: