From 5e4af5773b07dd563db8a4c5ba0748c8e4c182dd Mon Sep 17 00:00:00 2001 From: secret_dude Date: Tue, 27 Apr 2021 19:36:48 +0200 Subject: [PATCH] field cost --- Board.py | 15 ++++++++++++--- Field.py | 13 ++++++++++++- FindPath.py | 17 ++++++++++------- main.py | 2 +- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/Board.py b/Board.py index f2b6de6..b6639be 100644 --- a/Board.py +++ b/Board.py @@ -11,8 +11,17 @@ def generate(): for i in range(0, int(HORIZONTAL_TILES_NUMBER)): board.append([]) for j in range(0, int(VERTICAL_TILES_NUMBER)): - board[i].append(Field(int(i * TILE_SIZE), int(j * TILE_SIZE), random.choice(states))) + current_state = random.choice(states) + current_cost = getCost(current_state) + board[i].append(Field(int(i * TILE_SIZE), int(j * TILE_SIZE), current_state, current_cost)) - board[0][0] = Field(int(0 * TILE_SIZE), int(0 * TILE_SIZE), "TOOLS_FIELD") - board[1][0] = Field(int(1 * TILE_SIZE), int(0 * TILE_SIZE), "FUEL_FIELD") + board[0][0] = Field(int(0 * TILE_SIZE), int(0 * TILE_SIZE), "TOOLS_FIELD", cost=0) + board[1][0] = Field(int(1 * TILE_SIZE), int(0 * TILE_SIZE), "FUEL_FIELD", cost=0) return board + +def getCost(state): + if state == 'toPlow': return 1 + if state == 'toSeed': return 2 + if state == 'toFertilize': return 3 + if state == 'toWater': return 4 + if state == 'toCut': return 5 diff --git a/Field.py b/Field.py index 7aa8d96..e9f48a9 100644 --- a/Field.py +++ b/Field.py @@ -1,13 +1,18 @@ class Field: - def __init__(self, horizontal_index, vertical_index, state): + def __init__(self, horizontal_index, vertical_index, state, cost): self.__horizontal_index = horizontal_index self.__vertical_index = vertical_index self.__state = state + self.__cost = cost @property def state(self): return self.__state + @property + def cost(self): + return self.__cost + @property def horizontal_index(self): return self.__horizontal_index @@ -21,3 +26,9 @@ class Field: if state == "toPlow" or state == "toWater" or state == "toSeed" or \ state == "toFertilize" or state == "toCut" or state == "TOOLS_FIELD" or state == "FUEL_FIELD": self.__state = state + + @cost.setter + def cost(self, cost): + if cost == 1 or cost == 2 or cost == 3 or \ + cost == 4 or cost == 5 or cost == 0: + self.__cost = cost \ No newline at end of file diff --git a/FindPath.py b/FindPath.py index 510650d..af47d1d 100644 --- a/FindPath.py +++ b/FindPath.py @@ -30,11 +30,16 @@ def whichStateLookingFor(tractor, TillageUnit): def nearestLookingField(board, tractor, TillageUnit): + horizontal_tiles_number = int(HORIZONTAL_TILES_NUMBER) + vertical_tiles_number = int(VERTICAL_TILES_NUMBER) + + print(horizontal_tiles_number, vertical_tiles_number) + a = input() end_horizontal_index = 0 end_vertical_index = 0 searching_field = whichStateLookingFor(tractor, TillageUnit) - for i in range(0, int(HORIZONTAL_TILES_NUMBER)): - for j in range(0, int(VERTICAL_TILES_NUMBER)): + for i in range(horizontal_tiles_number): + for j in range(vertical_tiles_number): field = board[i][j] if searching_field == field.state: end_horizontal_index = field.horizontal_index @@ -62,8 +67,8 @@ def graphsearch(tractor, board, TillageUnit, fringe: Queue, explored): if goaltest(elem, end_state): break - # TODO - # return droga ktora musi pokonac traktor + #TODO + #return droga ktora musi pokonac traktor else: explored.append(elem) elem = succ(start_state, end_state, tractor) @@ -71,10 +76,8 @@ def graphsearch(tractor, board, TillageUnit, fringe: Queue, explored): def goaltest(elem, end_state): - print('element: ',elem,' stan koncowy; ', end_state) + print('element:', elem, 'state:', end_state) if elem == end_state: - print("heeee") - return True else: return False diff --git a/main.py b/main.py index 069ca00..8b4b095 100644 --- a/main.py +++ b/main.py @@ -59,7 +59,7 @@ while working: clock.tick(FPS) - graphsearch(tractor, board, TillageUnit, Queue(), list()) + #graphsearch(tractor, board, TillageUnit, Queue(), list()) pygame.quit() quit()