From 26a183d5f38e7dd29709d2b972c65d450686804d Mon Sep 17 00:00:00 2001 From: s464852 Date: Thu, 12 May 2022 21:49:22 +0200 Subject: [PATCH] Code refactor --- agent.py | 2 - astar.py | 4 +- field.py | 108 +++++++++--------------------------------------------- field1.py | 12 +++--- graph.py | 14 +++---- main.py | 90 ++++++++++++++++++++++----------------------- 6 files changed, 77 insertions(+), 153 deletions(-) diff --git a/agent.py b/agent.py index 7035918..176f34a 100644 --- a/agent.py +++ b/agent.py @@ -121,5 +121,3 @@ class Instance: elif key_pressed[pygame.K_RIGHT] and self.direction == 2 or action == 'r' and self.direction == 2: self.direction = 3 return 'none' - - diff --git a/astar.py b/astar.py index ea7a44e..9ac52b9 100644 --- a/astar.py +++ b/astar.py @@ -28,9 +28,9 @@ class State: class Node: def __init__(self, action, direction, parent, x, y): - self.action = action + self.action = action self.direction = direction - self.parent = parent + self.parent = parent self.x = x self.y = y diff --git a/field.py b/field.py index 2aa2ef7..187e675 100644 --- a/field.py +++ b/field.py @@ -80,105 +80,31 @@ class Dirt(Field): cost = 5 def __init__(self): - super(Dirt, self).__init__() + super().__init__() -class Carrot_empty(Field): +class Carrot(Field): type = Type.PLANT - name = 'carrot_empty' cost = 5 - def __init__(self): - super(Carrot_empty, self).__init__() + def __init__(self, name): + self.name = name + super().__init__() -class Carrot_sow(Field): +class Potato(Field): type = Type.PLANT - name = 'carrot_sow' cost = 5 - def __init__(self): - super(Carrot_sow, self).__init__() + def __init__(self, name): + self.name = name + super().__init__() -class Carrot_watered(Field): +class Wheat(Field): type = Type.PLANT - name = 'carrot_watered' cost = 5 - def __init__(self): - super(Carrot_watered, self).__init__() - -class Carrot_feritized(Field): - type = Type.PLANT - name = 'carrot_feritized' - cost = 5 - - def __init__(self): - super(Carrot_feritized, self).__init__() - - -class Potato_empty(Field): - type = Type.PLANT - name = 'potato_empty' - cost = 5 - - def __init__(self): - super(Potato_empty, self).__init__() - -class Potato_sow(Field): - type = Type.PLANT - name = 'potato_sow' - cost = 5 - - def __init__(self): - super(Potato_sow, self).__init__() - -class Potato_watered(Field): - type = Type.PLANT - name = 'potato_watered' - cost = 5 - - def __init__(self): - super(Potato_watered, self).__init__() - - -class Potato_feritized(Field): - type = Type.PLANT - name = 'potato_feritized' - cost = 5 - - def __init__(self): - super(Potato_feritized, self).__init__() - -class Wheat_empty(Field): - type = Type.PLANT - name = 'wheat_empty' - cost = 5 - - def __init__(self): - super(Wheat_empty, self).__init__() - -class Wheat_sow(Field): - type = Type.PLANT - name = 'wheat_sow' - cost = 5 - - def __init__(self): - super(Wheat_sow, self).__init__() - -class Wheat_watered(Field): - type = Type.PLANT - name = 'wheat_watered' - cost = 5 - - def __init__(self): - super(Wheat_watered, self).__init__() - -class Wheat_feritized(Field): - type = Type.PLANT - name = 'wheat_feritized' - cost = 5 - - def __init__(self): - super(Wheat_feritized, self).__init__() + def __init__(self, name): + self.name = name + super().__init__() class Cobble(Field): type = Type.SPECIAL @@ -186,7 +112,7 @@ class Cobble(Field): cost = 1 def __init__(self): - super(Cobble, self).__init__() + super().__init__() class Grass(Field): @@ -195,7 +121,7 @@ class Grass(Field): cost = 3 def __init__(self): - super(Grass, self).__init__() + super().__init__() class Sand(Field): @@ -204,7 +130,7 @@ class Sand(Field): cost = 10 def __init__(self): - super(Sand, self).__init__() + super().__init__() class Station(Field): @@ -213,5 +139,5 @@ class Station(Field): cost = 1 def __init__(self): - super(Station, self).__init__() + super().__init__() # Tile Types END diff --git a/field1.py b/field1.py index 6e3fdb8..3f01c71 100644 --- a/field1.py +++ b/field1.py @@ -1,13 +1,13 @@ class Tile: - def init(self, type, rowPosition, colPosition, status, isChecked): + def init(self, type, row_position, col_position, status, is_checked): self.type = type - self.rowPosition = rowPosition - self.colPosition = colPosition + self.row_position = row_position + self.col_position = col_position self.status = status - self.isChecked = False + self.is_checked = False - def isAvaibleForHarvest(self, type): + def is_avaible_for_harvest(self, type): self.type = type - def checkTile(self): + def check_tile(self): self.isChecked = True diff --git a/graph.py b/graph.py index 8eb67fb..e6bf57a 100644 --- a/graph.py +++ b/graph.py @@ -66,24 +66,24 @@ class Node: self.y = y -def goal_test(goaltest,elem): - if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]: +def goal_test(goal_test_arr, elem): + if elem.get_x() == goal_test_arr[0] and elem.get_y() == goal_test_arr[1]: return True else: return False -def graphsearch(explored, fringe, goaltest, istate, succ): - node = Node(None, istate.get_direction(), None, istate.get_x(), - istate.get_y()) +def graph_search(explored, fringe, goal_test_arr, is_state, succ): + node = Node(None, is_state.get_direction(), None, is_state.get_x(), + is_state.get_y()) fringe.append(node) while True: if not fringe: return False elem = fringe.pop(0) temp = copy.copy(elem) - if goal_test(goaltest, - elem) is True: + if goal_test(goal_test_arr, + elem) is True: return get_moves_list(elem) explored.append(elem) for (action, state) in succ( diff --git a/main.py b/main.py index 556285f..2bdae14 100644 --- a/main.py +++ b/main.py @@ -8,27 +8,27 @@ import common import field import settings -possibleFields = { +possible_fields = { 'dirt': field.Dirt(), 'grass': field.Grass(), 'cobble': field.Cobble(), 'sand': field.Sand(), 'station': field.Station(), - 'carrot_empty': field.Carrot_empty(), - 'carrot_sow': field.Carrot_sow(), - 'carrot_watered': field.Carrot_watered(), - 'carrot_feritized': field.Carrot_feritized(), - 'potato_empty': field.Potato_empty(), - 'potato_sow': field.Potato_sow(), - 'potato_watered': field.Potato_watered(), - 'potato_feritized': field.Potato_feritized(), - 'wheat_empty': field.Wheat_empty(), - 'wheat_sow': field.Wheat_sow(), - 'wheat_watered': field.Wheat_watered(), - 'wheat_feritized': field.Wheat_feritized() + 'carrot_empty': field.Carrot('carrot_empty'), + 'carrot_sow': field.Carrot('carrot_sow'), + 'carrot_watered': field.Carrot('carrot_watered'), + 'carrot_feritized': field.Carrot('carrot_feritized'), + 'potato_empty': field.Potato('potato_empty'), + 'potato_sow': field.Potato('potato_sow'), + 'potato_watered': field.Potato('potato_watered'), + 'potato_feritized': field.Potato('potato_feritized'), + 'wheat_empty': field.Wheat('wheat_empty'), + 'wheat_sow': field.Wheat('wheat_sow'), + 'wheat_watered': field.Wheat('wheat_watered'), + 'wheat_feritized': field.Wheat('wheat_feritized') } -possibleFieldsWithPlants = [ +possible_fields_with_plants = [ 'carrot_empty', 'carrot_sow', 'carrot_watered', @@ -46,8 +46,8 @@ possibleFieldsWithPlants = [ def randomize_map(): fields_array = [] - for i in possibleFields: - fields_array.append(possibleFields[i].tile.object) + for i in possible_fields: + fields_array.append(possible_fields[i].tile.object) field_array_big = [] field_array_small = [] field_array_big_2 = [] @@ -61,16 +61,16 @@ def randomize_map(): # k = random.choice(list(possibleFields.keys())) x = random.uniform(0, 100) if x <= 80: - plant = random.choice(possibleFieldsWithPlants) - field_array_small.append(possibleFields[plant].tile.object) + plant = random.choice(possible_fields_with_plants) + field_array_small.append(possible_fields[plant].tile.object) field_array_small_2.append('dirt') field_array_small_3.append(plant) elif 80 < x <= 90: - field_array_small.append(possibleFields['sand'].tile.object) + field_array_small.append(possible_fields['sand'].tile.object) field_array_small_2.append('sand') field_array_small_3.append('sand') elif 90 < x <= 100: - field_array_small.append(possibleFields['grass'].tile.object) + field_array_small.append(possible_fields['grass'].tile.object) field_array_small_2.append('grass') field_array_small_3.append('grass') field_array_big.append(field_array_small) @@ -81,14 +81,14 @@ def randomize_map(): field_array_small_3 = [] for i in range(height): - field_array_big[math.floor(width / 2)][i] = possibleFields['cobble'].tile.object + field_array_big[math.floor(width / 2)][i] = possible_fields['cobble'].tile.object field_array_big_2[math.floor(width / 2)][i] = 'cobble' field_array_big_3[math.floor(width / 2)][i] = 'cobble' for i in range(width): - field_array_big[i][math.floor(height / 2)] = possibleFields['cobble'].tile.object + field_array_big[i][math.floor(height / 2)] = possible_fields['cobble'].tile.object field_array_big_2[i][math.floor(height / 2)] = 'cobble' field_array_big_3[i][math.floor(height / 2)] = 'cobble' - field_array_big[0][0] = possibleFields['station'].tile.object + field_array_big[0][0] = possible_fields['station'].tile.object field_array_big_2[0][0] = 'station' field_array_big_3[0][0] = 'station' return field_array_big, field_array_big_2, field_array_big_3 @@ -97,16 +97,16 @@ def randomize_map(): def read_img(agent, fields): window = common.get('window') current_field = fields[agent.x()][agent.y()] - if current_field == possibleFields['grass'].tile.object: - window.blit(possibleFields['grass'].block.object, (0, 0)) - elif current_field == possibleFields['dirt'].tile.object: - window.blit(possibleFields['dirt'].block.object, (0, 0)) - elif current_field == possibleFields['sand'].tile.object: - window.blit(possibleFields['sand'].block.object, (0, 0)) - elif current_field == possibleFields['cobble'].tile.object: - window.blit(possibleFields['cobble'].block.object, (0, 0)) - elif current_field == possibleFields['station'].tile.object: - window.blit(possibleFields['station'].block.object, (0, 0)) + if current_field == possible_fields['grass'].tile.object: + window.blit(possible_fields['grass'].block.object, (0, 0)) + elif current_field == possible_fields['dirt'].tile.object: + window.blit(possible_fields['dirt'].block.object, (0, 0)) + elif current_field == possible_fields['sand'].tile.object: + window.blit(possible_fields['sand'].block.object, (0, 0)) + elif current_field == possible_fields['cobble'].tile.object: + window.blit(possible_fields['cobble'].block.object, (0, 0)) + elif current_field == possible_fields['station'].tile.object: + window.blit(possible_fields['station'].block.object, (0, 0)) pygame.display.update() pygame.time.delay(2000) @@ -187,7 +187,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_sow.append(field_to_visit) fields_with_plants[x1][y1] = 'wheat_empty' - fields_for_movement[x1][y1] = possibleFields['wheat_empty'].tile.object + fields_for_movement[x1][y1] = possible_fields['wheat_empty'].tile.object draw_window(agent, fields_for_movement) elif fields_with_plants[x1][y1] == 'carrot_feritized': state = astar.State(dir, x, y) @@ -197,7 +197,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_sow.append(field_to_visit) fields_with_plants[x1][y1] = 'carrot_empty' - fields_for_movement[x1][y1] = possibleFields['carrot_empty'].tile.object + fields_for_movement[x1][y1] = possible_fields['carrot_empty'].tile.object draw_window(agent, fields_for_movement) elif fields_with_plants[x1][y1] == 'potato_feritized': state = astar.State(dir, x, y) @@ -207,7 +207,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_sow.append(field_to_visit) fields_with_plants[x1][y1] = 'potato_empty' - fields_for_movement[x1][y1] = possibleFields['potato_empty'].tile.object + fields_for_movement[x1][y1] = possible_fields['potato_empty'].tile.object draw_window(agent, fields_for_movement) if len(fields_to_harvest) == 0: k += 1 @@ -223,7 +223,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_feritize.append(field_to_visit) fields_with_plants[x1][y1] = 'wheat_watered' - fields_for_movement[x1][y1] = possibleFields['wheat_watered'].tile.object + fields_for_movement[x1][y1] = possible_fields['wheat_watered'].tile.object draw_window(agent, fields_for_movement) elif fields_with_plants[x1][y1] == 'carrot_sow': state = astar.State(dir, x, y) @@ -233,7 +233,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_feritize.append(field_to_visit) fields_with_plants[x1][y1] = 'carrot_watered' - fields_for_movement[x1][y1] = possibleFields['carrot_watered'].tile.object + fields_for_movement[x1][y1] = possible_fields['carrot_watered'].tile.object draw_window(agent, fields_for_movement) elif fields_with_plants[x1][y1] == 'potato_sow': state = astar.State(dir, x, y) @@ -243,7 +243,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_feritize.append(field_to_visit) fields_with_plants[x1][y1] = 'potato_watered' - fields_for_movement[x1][y1] = possibleFields['potato_watered'].tile.object + fields_for_movement[x1][y1] = possible_fields['potato_watered'].tile.object draw_window(agent, fields_for_movement) if len(fields_to_water) == 0: k += 1 @@ -259,7 +259,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_harvest.append(field_to_visit) fields_with_plants[x1][y1] = 'wheat_feritized' - fields_for_movement[x1][y1] = possibleFields['wheat_feritized'].tile.object + fields_for_movement[x1][y1] = possible_fields['wheat_feritized'].tile.object draw_window(agent, fields_for_movement) elif fields_with_plants[x1][y1] == 'carrot_watered': state = astar.State(dir, x, y) @@ -269,7 +269,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_harvest.append(field_to_visit) fields_with_plants[x1][y1] = 'carrot_feritized' - fields_for_movement[x1][y1] = possibleFields['carrot_feritized'].tile.object + fields_for_movement[x1][y1] = possible_fields['carrot_feritized'].tile.object draw_window(agent, fields_for_movement) elif fields_with_plants[x1][y1] == 'potato_watered': state = astar.State(dir, x, y) @@ -279,7 +279,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_harvest.append(field_to_visit) fields_with_plants[x1][y1] = 'potato_feritized' - fields_for_movement[x1][y1] = possibleFields['potato_feritized'].tile.object + fields_for_movement[x1][y1] = possible_fields['potato_feritized'].tile.object draw_window(agent, fields_for_movement) if len(fields_to_feritize) == 0: k += 1 @@ -295,7 +295,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_water.append(field_to_visit) fields_with_plants[x1][y1] = 'wheat_sow' - fields_for_movement[x1][y1] = possibleFields['wheat_sow'].tile.object + fields_for_movement[x1][y1] = possible_fields['wheat_sow'].tile.object draw_window(agent, fields_for_movement) elif fields_with_plants[x1][y1] == 'carrot_empty': state = astar.State(dir, x, y) @@ -305,7 +305,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_water.append(field_to_visit) fields_with_plants[x1][y1] = 'carrot_sow' - fields_for_movement[x1][y1] = possibleFields['carrot_sow'].tile.object + fields_for_movement[x1][y1] = possible_fields['carrot_sow'].tile.object draw_window(agent, fields_for_movement) elif fields_with_plants[x1][y1] == 'potato_empty': state = astar.State(dir, x, y) @@ -315,7 +315,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent_movement(move_list, agent, fields_for_movement, fields_for_astar) fields_to_water.append(field_to_visit) fields_with_plants[x1][y1] = 'potato_sow' - fields_for_movement[x1][y1] = possibleFields['potato_sow'].tile.object + fields_for_movement[x1][y1] = possible_fields['potato_sow'].tile.object draw_window(agent, fields_for_movement) if len(fields_to_sow) == 0: k = 0 -- 2.20.1