bug ruchu

This commit is contained in:
Tomasz Adamczyk 2021-04-10 18:24:13 +02:00
parent 58949ff985
commit 2836a0771d
9 changed files with 120 additions and 102 deletions

View File

@ -3,9 +3,10 @@
<component name="ChangeListManager">
<list default="true" id="56453584-72bd-49f4-a39c-fccf91ab20c6" name="Default Changelist" comment="">
<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$/graph.py" beforeDir="false" afterPath="$PROJECT_DIR$/graph.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/plant.py" beforeDir="false" afterPath="$PROJECT_DIR$/plant.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>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -112,6 +113,7 @@
<workItem from="1618042095757" duration="3541000" />
<workItem from="1618045709925" duration="3464000" />
<workItem from="1618049213609" duration="5378000" />
<workItem from="1618060647009" duration="10450000" />
</task>
<servers />
</component>
@ -123,42 +125,42 @@
<screen x="0" y="0" width="1920" height="1080" />
</state>
<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="364" key="GridCell.Tab.0.bottom" timestamp="1618054918620">
<state width="1879" height="364" key="GridCell.Tab.0.bottom" timestamp="1618071825769">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1618054918620" />
<state width="1879" height="364" key="GridCell.Tab.0.center" timestamp="1618054918620">
<state width="1879" height="364" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1618071825769" />
<state width="1879" height="364" key="GridCell.Tab.0.center" timestamp="1618071825769">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1618054918620" />
<state width="1879" height="364" key="GridCell.Tab.0.left" timestamp="1618054918620">
<state width="1879" height="364" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1618071825769" />
<state width="1879" height="364" key="GridCell.Tab.0.left" timestamp="1618071825769">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1618054918620" />
<state width="1879" height="364" key="GridCell.Tab.0.right" timestamp="1618054918620">
<state width="1879" height="364" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1618071825769" />
<state width="1879" height="364" key="GridCell.Tab.0.right" timestamp="1618071825769">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1618054918620" />
<state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1618054918620">
<state width="1879" height="364" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1618071825769" />
<state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1618071825769">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.1.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1618054918620" />
<state width="1879" height="364" key="GridCell.Tab.1.center" timestamp="1618054918620">
<state width="1879" height="364" key="GridCell.Tab.1.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1618071825769" />
<state width="1879" height="364" key="GridCell.Tab.1.center" timestamp="1618071825769">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.1.center/0.0.1920.1080@0.0.1920.1080" timestamp="1618054918620" />
<state width="1879" height="364" key="GridCell.Tab.1.left" timestamp="1618054918620">
<state width="1879" height="364" key="GridCell.Tab.1.center/0.0.1920.1080@0.0.1920.1080" timestamp="1618071825769" />
<state width="1879" height="364" key="GridCell.Tab.1.left" timestamp="1618071825769">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.1.left/0.0.1920.1080@0.0.1920.1080" timestamp="1618054918620" />
<state width="1879" height="364" key="GridCell.Tab.1.right" timestamp="1618054918620">
<state width="1879" height="364" key="GridCell.Tab.1.left/0.0.1920.1080@0.0.1920.1080" timestamp="1618071825769" />
<state width="1879" height="364" key="GridCell.Tab.1.right" timestamp="1618071825769">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1618054918620" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1618042189818">
<state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1618071825769" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1618060648138">
<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="1618042189818" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1618060648138" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
@ -172,6 +174,6 @@
</breakpoint-manager>
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1618054873527" 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="1618071647071" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
</component>
</project>

Binary file not shown.

Binary file not shown.

Binary file not shown.

107
graph.py
View File

@ -1,10 +1,27 @@
import copy
import tractor
class Node:
def __init__(self, action, direction, parent, x, y):
self.action = action
class Istate: #stan początkowy traktora (strona, w którą patrzy, miejsce, w którym się on znajduje)
def __init__(self, direction, x, y):
self.direction = direction
self.parent = parent
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 Node: #wierzchołek grafu
def __init__(self, action, direction, parent, x, y):
self.action = action #akcja jaką ma wykonać (obróc się w lewo, obróć się w prawo, ruch do przodu)
self.direction = direction
self.parent = parent #ojciec wierzchołka
self.x = x
self.y = y
def get_action(self):
@ -27,47 +44,19 @@ class Node:
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
def goal_test(elem, goaltest): #funkcja sprawdzająca czy położenie traktora równa się położeniu punktu docelowego, jeśli tak zwraca prawdę, w przeciwnym wypadku fałsz
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]:
return True
else:
return False
def print_moves(elem, explored):
def print_moves(elem): #zwraca listę ruchów jakie należy wykonać by dotrzeć do punktu docelowego
moves_list = []
while (elem.get_parent() != None):
moves_list.append(elem.get_action())
elem = elem.get_parent()
moves_list.reverse()
return moves_list
def succ(elem):
def succ(elem): #funkcja następnika, przypisuje jakie akcje są możliwe do wykonania na danym polu oraz jaki będzie stan (kierunek, położenie) po wykonaniu tej akcji
actions_list = []
temp = copy.copy(elem.get_direction())
if temp == 1:
@ -94,46 +83,24 @@ def succ(elem):
elif tractor.Tractor.is_move_allowed_succ(elem) == "x - 1":
actions_list.append(("move", (elem.get_direction(), temp_move_west, elem.get_y())))
return actions_list
def graphsearch(fringe, explored, istate, succ, goaltest):
node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y()) #może None coś nie gra
#fringe.add_to_fringe(node)
fringe.append(node)
def graphsearch(fringe, explored, istate, succ, goaltest): #przeszukiwanie grafu wszerz
node = Node(None, istate.get_direction(), None, istate.get_x(), istate.get_y()) #wierzchołek początkowy, stworzony ze stanu początkowego traktora
fringe.append(node) #wierzchołki do odwiedzenia
while True:
if not fringe:
return False
#elem = fringe.get_element_from_fringe_pop()
elem = fringe.pop(0)
temp = copy.copy(elem) #żeby explored w for succ() nie zmieniało
if goal_test(elem, goaltest) is True:
# for x in fringe:
# print("action: " + str(x.get_action()))
# print("direction: " + str(x.get_direction()))
# print("parent: " + str(x.get_parent()))
# print("node: " + str(x))
# print("x: " + str(x.get_x()))
# print("y: " + str(x.get_y()))
# for x in explored:
# print("action ex: " + str(x.get_action()))
# print("direction ex: " + str(x.get_direction()))
# print("parent ex: " + str(x.get_parent()))
# print("node ex: " + str(x))
# print("x ex: " + str(x.get_x()))
# print("y ex: " + str(x.get_y()))
return print_moves(elem, explored)
explored.append(elem)
for (action, state) in succ(temp):
elem = fringe.pop(0) #zdejmujemy wierzchołek z kolejki fringe i rozpatrujemy go
temp = copy.copy(elem)
if goal_test(elem, goaltest) is True: #jeżeli osiągniemy cel w trakcie przeszukiwania grafu wszerz (wjedziemy na pole docelowe) : zwracamy listę ruchów, po których wykonaniu dotrzemy na miejsce
return print_moves(elem)
explored.append(elem) #dodajemy wierzchołek do listy wierzchołków odwiedzonych
for (action, state) in succ(temp): #iterujemy po wszystkich możliwych akcjach i stanach otrzymanych dla danego wierzchołka grafu
fringe_tuple = []
explored_tuple = []
for x in fringe:
fringe_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
for x in explored:
explored_tuple.append((x.get_direction(), x.get_x(), x.get_y()))
if state not in fringe_tuple and state not in explored_tuple:
x = Node(action, state[0], elem, state[1], state[2])
# print(x.get_action())
# print(state[0])
# print(state[1])
# print(state[2])
# print(x.get_direction())
# print(x.get_parent())
fringe.append(x)
if state not in fringe_tuple and state not in explored_tuple: #jeżeli stan nie znajduje się na fringe oraz nie znajduje się w liście wierzchołków odwiedzonych
x = Node(action, state[0], elem, state[1], state[2]) #stworzenie nowego wierzchołka, którego rodzicem jest elem
fringe.append(x) #dodanie wierzchołka na fringe

View File

@ -11,6 +11,34 @@ class Plant:
return self.state
def set_state(self, state):
self.state = state
# @staticmethod
# def get_closest_mature_plant(map1, tractor1):
# x = -1
# y = -1
# min = definitions.WIDTH_AMOUNT + definitions.HEIGHT_AMOUNT + 1
# for i in range(definitions.WIDTH_AMOUNT):
# for j in range(definitions.HEIGHT_AMOUNT):
# field = map1.get_fields()[i][j]
# if field.get_plant().get_name() == "beetroot" and field.get_plant().get_state() == definitions.BEETROOTS_MAXIMUM_STATE: #and abs(tractor1.get_x() - i) + abs(tractor1.get_y() - j) < min:
# x = i
# y = j
# min = abs(tractor1.get_x() - i) + abs(tractor1.get_y() - j)
# elif field.get_plant().get_name() == "carrot" and field.get_plant().get_state() == definitions.CARROTS_MAXIMUM_STATE: #and abs(tractor1.get_x() - i) + abs(tractor1.get_y() - j) < min:
# x = i
# y = j
# min = abs(tractor1.get_x() - i) + abs(tractor1.get_y() - j)
# elif field.get_plant().get_name() == "potato" and field.get_plant().get_state() == definitions.POTATOES_MAXIMUM_STATE: #and abs(tractor1.get_x() - i) + abs(tractor1.get_y() - j) < min:
# x = i
# y = j
# min = abs(tractor1.get_x() - i) + abs(tractor1.get_y() - j)
# elif field.get_plant().get_name() == "wheat" and field.get_plant().get_state() == definitions.WHEAT_MAXIMUM_STATE: #and abs(tractor1.get_x() - i) + abs(tractor1.get_y() - j) < min:
# x = i
# y = j
# min = abs(tractor1.get_x() - i) + abs(tractor1.get_y() - j)
# if x == -1 and y == -1:
# return False
# else:
# return x, y
@staticmethod
def grow_plants(map1): #metoda statyczna, która zwiększa pole state (etap rozwoju rośliny) dla danej rośliny na danym polu o 1
for i in range(definitions.WIDTH_AMOUNT):

18
py.py
View File

@ -3,6 +3,7 @@ import graph
import map
import plant
import pygame
import random
import station
import tractor
pygame.display.set_caption("Smart Tractor")
@ -10,11 +11,12 @@ def main():
#tworzenie podstawowych obiektów
map1 = map.Map([])
map1.create_base_map()
move_list = []
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}
fertilizer_dict = {"beetroot": definitions.TRACTOR_FERTILIZER, "carrot": definitions.TRACTOR_FERTILIZER, "potato": definitions.TRACTOR_FERTILIZER, "wheat": definitions.TRACTOR_FERTILIZER}
station1 = station.Station(collected_plants_dict)
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 = 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_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE)
clock = pygame.time.Clock()
run = True
@ -23,11 +25,17 @@ def main():
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
istate = graph.Istate(definitions.TRACTOR_DIRECTION_SOUTH, 0, 0)
istate1 = graph.Istate(None, 4, 6)
print(graph.graphsearch([], [], istate, graph.succ, istate1))
map1.draw_window(tractor1, tractor1_rect)
tractor1.tractor1_handle_movement(tractor1_rect)
if not move_list:
istate = graph.Istate(tractor1.get_direction(), 0, 0)
move_list = (graph.graphsearch([], [], istate, graph.succ, (4, 4)))
print(move_list)
elif move_list:
temp = move_list.pop(0)
print(temp)
tractor1.handle_movement(temp, tractor1_rect)
else:
tractor1.handle_random_movement(tractor1_rect)
tractor1.do_work(map1, station1, tractor1_rect)
plant.Plant.grow_plants(map1)
pygame.quit()

View File

@ -184,17 +184,30 @@ class Tractor:
return "x - 1"
else:
return False
def tractor1_handle_movement(self, tractor1_rect): #odpowiada za poruszanie się traktora po mapie
def handle_movement(self, move, tractor1_rect): #odpowiada za poruszanie się traktora po mapie
if self.get_fuel() > 0:
if move == "move":
self.move()
elif move == "rotate_left":
self.rotate_left()
elif move == "rotate_right":
self.rotate_right()
self.set_fuel(self.get_fuel() - 1)
tractor1_rect.x = self.get_x()
tractor1_rect.y = self.get_y()
def handle_random_movement(self, tractor1_rect): #odpowiada za losowe poruszanie się traktora po mapie
loop = True
while loop and self.get_fuel() > 0:
if self.is_move_allowed(tractor1_rect) is True:
random1 = random.randint(1, 3)
if random1 == 1 and self.is_move_allowed(tractor1_rect) is True:
self.move()
tractor1_rect.x = self.get_x()
tractor1_rect.y = self.get_y()
loop = False
else:
elif random1 == 2:
self.rotate_left()
loop = False
elif random1 == 3:
self.rotate_right()
self.set_fuel(self.get_fuel() - 1)
if tractor1_rect.x == 0 and tractor1_rect.y == 0:
self.set_fuel(definitions.TRACTOR_FUEL)
self.set_water_level(definitions.TRACTOR_WATER_LEVEL)
loop = False
self.set_fuel(self.get_fuel() - 1)