Code refactor #22

Merged
s459307 merged 1 commits from code-refactor into master 2022-05-13 08:08:48 +02:00
6 changed files with 77 additions and 153 deletions

View File

@ -121,5 +121,3 @@ class Instance:
elif key_pressed[pygame.K_RIGHT] and self.direction == 2 or action == 'r' and self.direction == 2: elif key_pressed[pygame.K_RIGHT] and self.direction == 2 or action == 'r' and self.direction == 2:
self.direction = 3 self.direction = 3
return 'none' return 'none'

108
field.py
View File

@ -80,105 +80,31 @@ class Dirt(Field):
cost = 5 cost = 5
def __init__(self): def __init__(self):
super(Dirt, self).__init__() super().__init__()
class Carrot_empty(Field): class Carrot(Field):
type = Type.PLANT type = Type.PLANT
name = 'carrot_empty'
cost = 5 cost = 5
def __init__(self): def __init__(self, name):
super(Carrot_empty, self).__init__() self.name = name
super().__init__()
class Carrot_sow(Field): class Potato(Field):
type = Type.PLANT type = Type.PLANT
name = 'carrot_sow'
cost = 5 cost = 5
def __init__(self): def __init__(self, name):
super(Carrot_sow, self).__init__() self.name = name
super().__init__()
class Carrot_watered(Field): class Wheat(Field):
type = Type.PLANT type = Type.PLANT
name = 'carrot_watered'
cost = 5 cost = 5
def __init__(self): def __init__(self, name):
super(Carrot_watered, self).__init__() self.name = name
super().__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__()
class Cobble(Field): class Cobble(Field):
type = Type.SPECIAL type = Type.SPECIAL
@ -186,7 +112,7 @@ class Cobble(Field):
cost = 1 cost = 1
def __init__(self): def __init__(self):
super(Cobble, self).__init__() super().__init__()
class Grass(Field): class Grass(Field):
@ -195,7 +121,7 @@ class Grass(Field):
cost = 3 cost = 3
def __init__(self): def __init__(self):
super(Grass, self).__init__() super().__init__()
class Sand(Field): class Sand(Field):
@ -204,7 +130,7 @@ class Sand(Field):
cost = 10 cost = 10
def __init__(self): def __init__(self):
super(Sand, self).__init__() super().__init__()
class Station(Field): class Station(Field):
@ -213,5 +139,5 @@ class Station(Field):
cost = 1 cost = 1
def __init__(self): def __init__(self):
super(Station, self).__init__() super().__init__()
# Tile Types END # Tile Types END

View File

@ -1,13 +1,13 @@
class Tile: 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.type = type
self.rowPosition = rowPosition self.row_position = row_position
self.colPosition = colPosition self.col_position = col_position
self.status = status self.status = status
self.isChecked = False self.is_checked = False
def isAvaibleForHarvest(self, type): def is_avaible_for_harvest(self, type):
self.type = type self.type = type
def checkTile(self): def check_tile(self):
self.isChecked = True self.isChecked = True

View File

@ -66,24 +66,24 @@ class Node:
self.y = y self.y = y
def goal_test(goaltest,elem): def goal_test(goal_test_arr, elem):
if elem.get_x() == goaltest[0] and elem.get_y() == goaltest[1]: if elem.get_x() == goal_test_arr[0] and elem.get_y() == goal_test_arr[1]:
return True return True
else: else:
return False return False
def graphsearch(explored, fringe, goaltest, istate, succ): def graph_search(explored, fringe, goal_test_arr, is_state, succ):
node = Node(None, istate.get_direction(), None, istate.get_x(), node = Node(None, is_state.get_direction(), None, is_state.get_x(),
istate.get_y()) is_state.get_y())
fringe.append(node) fringe.append(node)
while True: while True:
if not fringe: if not fringe:
return False return False
elem = fringe.pop(0) elem = fringe.pop(0)
temp = copy.copy(elem) temp = copy.copy(elem)
if goal_test(goaltest, if goal_test(goal_test_arr,
elem) is True: elem) is True:
return get_moves_list(elem) return get_moves_list(elem)
explored.append(elem) explored.append(elem)
for (action, state) in succ( for (action, state) in succ(

90
main.py
View File

@ -8,27 +8,27 @@ import common
import field import field
import settings import settings
possibleFields = { possible_fields = {
'dirt': field.Dirt(), 'dirt': field.Dirt(),
'grass': field.Grass(), 'grass': field.Grass(),
'cobble': field.Cobble(), 'cobble': field.Cobble(),
'sand': field.Sand(), 'sand': field.Sand(),
'station': field.Station(), 'station': field.Station(),
'carrot_empty': field.Carrot_empty(), 'carrot_empty': field.Carrot('carrot_empty'),
'carrot_sow': field.Carrot_sow(), 'carrot_sow': field.Carrot('carrot_sow'),
'carrot_watered': field.Carrot_watered(), 'carrot_watered': field.Carrot('carrot_watered'),
'carrot_feritized': field.Carrot_feritized(), 'carrot_feritized': field.Carrot('carrot_feritized'),
'potato_empty': field.Potato_empty(), 'potato_empty': field.Potato('potato_empty'),
'potato_sow': field.Potato_sow(), 'potato_sow': field.Potato('potato_sow'),
'potato_watered': field.Potato_watered(), 'potato_watered': field.Potato('potato_watered'),
'potato_feritized': field.Potato_feritized(), 'potato_feritized': field.Potato('potato_feritized'),
'wheat_empty': field.Wheat_empty(), 'wheat_empty': field.Wheat('wheat_empty'),
'wheat_sow': field.Wheat_sow(), 'wheat_sow': field.Wheat('wheat_sow'),
'wheat_watered': field.Wheat_watered(), 'wheat_watered': field.Wheat('wheat_watered'),
'wheat_feritized': field.Wheat_feritized() 'wheat_feritized': field.Wheat('wheat_feritized')
} }
possibleFieldsWithPlants = [ possible_fields_with_plants = [
'carrot_empty', 'carrot_empty',
'carrot_sow', 'carrot_sow',
'carrot_watered', 'carrot_watered',
@ -46,8 +46,8 @@ possibleFieldsWithPlants = [
def randomize_map(): def randomize_map():
fields_array = [] fields_array = []
for i in possibleFields: for i in possible_fields:
fields_array.append(possibleFields[i].tile.object) fields_array.append(possible_fields[i].tile.object)
field_array_big = [] field_array_big = []
field_array_small = [] field_array_small = []
field_array_big_2 = [] field_array_big_2 = []
@ -61,16 +61,16 @@ def randomize_map():
# k = random.choice(list(possibleFields.keys())) # k = random.choice(list(possibleFields.keys()))
x = random.uniform(0, 100) x = random.uniform(0, 100)
if x <= 80: if x <= 80:
plant = random.choice(possibleFieldsWithPlants) plant = random.choice(possible_fields_with_plants)
field_array_small.append(possibleFields[plant].tile.object) field_array_small.append(possible_fields[plant].tile.object)
field_array_small_2.append('dirt') field_array_small_2.append('dirt')
field_array_small_3.append(plant) field_array_small_3.append(plant)
elif 80 < x <= 90: 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_2.append('sand')
field_array_small_3.append('sand') field_array_small_3.append('sand')
elif 90 < x <= 100: 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_2.append('grass')
field_array_small_3.append('grass') field_array_small_3.append('grass')
field_array_big.append(field_array_small) field_array_big.append(field_array_small)
@ -81,14 +81,14 @@ def randomize_map():
field_array_small_3 = [] field_array_small_3 = []
for i in range(height): 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_2[math.floor(width / 2)][i] = 'cobble'
field_array_big_3[math.floor(width / 2)][i] = 'cobble' field_array_big_3[math.floor(width / 2)][i] = 'cobble'
for i in range(width): 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_2[i][math.floor(height / 2)] = 'cobble'
field_array_big_3[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_2[0][0] = 'station'
field_array_big_3[0][0] = 'station' field_array_big_3[0][0] = 'station'
return field_array_big, field_array_big_2, field_array_big_3 return field_array_big, field_array_big_2, field_array_big_3
@ -97,16 +97,16 @@ def randomize_map():
def read_img(agent, fields): def read_img(agent, fields):
window = common.get('window') window = common.get('window')
current_field = fields[agent.x()][agent.y()] current_field = fields[agent.x()][agent.y()]
if current_field == possibleFields['grass'].tile.object: if current_field == possible_fields['grass'].tile.object:
window.blit(possibleFields['grass'].block.object, (0, 0)) window.blit(possible_fields['grass'].block.object, (0, 0))
elif current_field == possibleFields['dirt'].tile.object: elif current_field == possible_fields['dirt'].tile.object:
window.blit(possibleFields['dirt'].block.object, (0, 0)) window.blit(possible_fields['dirt'].block.object, (0, 0))
elif current_field == possibleFields['sand'].tile.object: elif current_field == possible_fields['sand'].tile.object:
window.blit(possibleFields['sand'].block.object, (0, 0)) window.blit(possible_fields['sand'].block.object, (0, 0))
elif current_field == possibleFields['cobble'].tile.object: elif current_field == possible_fields['cobble'].tile.object:
window.blit(possibleFields['cobble'].block.object, (0, 0)) window.blit(possible_fields['cobble'].block.object, (0, 0))
elif current_field == possibleFields['station'].tile.object: elif current_field == possible_fields['station'].tile.object:
window.blit(possibleFields['station'].block.object, (0, 0)) window.blit(possible_fields['station'].block.object, (0, 0))
pygame.display.update() pygame.display.update()
pygame.time.delay(2000) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit) fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_empty' 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) draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'carrot_feritized': elif fields_with_plants[x1][y1] == 'carrot_feritized':
state = astar.State(dir, x, y) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit) fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_empty' 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) draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'potato_feritized': elif fields_with_plants[x1][y1] == 'potato_feritized':
state = astar.State(dir, x, y) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit) fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_empty' 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) draw_window(agent, fields_for_movement)
if len(fields_to_harvest) == 0: if len(fields_to_harvest) == 0:
k += 1 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit) fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_watered' 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) draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'carrot_sow': elif fields_with_plants[x1][y1] == 'carrot_sow':
state = astar.State(dir, x, y) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit) fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_watered' 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) draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'potato_sow': elif fields_with_plants[x1][y1] == 'potato_sow':
state = astar.State(dir, x, y) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit) fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_watered' 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) draw_window(agent, fields_for_movement)
if len(fields_to_water) == 0: if len(fields_to_water) == 0:
k += 1 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit) fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_feritized' 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) draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'carrot_watered': elif fields_with_plants[x1][y1] == 'carrot_watered':
state = astar.State(dir, x, y) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit) fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_feritized' 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) draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'potato_watered': elif fields_with_plants[x1][y1] == 'potato_watered':
state = astar.State(dir, x, y) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit) fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_feritized' 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) draw_window(agent, fields_for_movement)
if len(fields_to_feritize) == 0: if len(fields_to_feritize) == 0:
k += 1 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit) fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_sow' 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) draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'carrot_empty': elif fields_with_plants[x1][y1] == 'carrot_empty':
state = astar.State(dir, x, y) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit) fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_sow' 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) draw_window(agent, fields_for_movement)
elif fields_with_plants[x1][y1] == 'potato_empty': elif fields_with_plants[x1][y1] == 'potato_empty':
state = astar.State(dir, x, y) 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) agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit) fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_sow' 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) draw_window(agent, fields_for_movement)
if len(fields_to_sow) == 0: if len(fields_to_sow) == 0:
k = 0 k = 0