Skip to content

Commit

Permalink
Placed first utility. Fixed bug with spot content
Browse files Browse the repository at this point in the history
  • Loading branch information
kostyaHrytsyuk committed Dec 9, 2018
1 parent 1578573 commit 4a910c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
50 changes: 27 additions & 23 deletions City.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def build(self):
for i in range(self.rows):
self.city.append([])
for j in range(self.columns):
point = CityCoordinate(i, j, '.', None)
point = CityCoordinate(i, j, 0, None)
self.city[i].append(point)

found = False
Expand Down Expand Up @@ -72,23 +72,27 @@ def __make_square(self, residential, top_left_corner):
column += residential.columns
row += residential.rows
counter += 1

top_left_corner[0] = residential.rows
top_left_corner[1] = residential.columns
self.__fill_square_with_utilities(top_left_corner)
print()

def __fill_square_with_utilities(self, top_left_corner):
best_utility = self.__find_best_utility(top_left_corner)
self.__build_house(best_utility, top_left_corner)
pass

def __find_best_utility(self, top_left_corner):
neighbour_point = self.city[top_left_corner[0]][top_left_corner[1]]
neighbour_point = self.city[top_left_corner[0]-1][top_left_corner[1]-1]
neighbour = self.residentials[neighbour_point.id]
u = 0
best_utility = self.possible_utilities[u]
while not neighbour.is_utility_around(self.possible_utilities[u]):
u = list(self.possible_utilities.keys())[0]
while u <= len(self.possible_utilities.keys()):
if not neighbour.is_utility_around(u):
for utility in self.possible_utilities[u]:
if self.__check_building_possibility(utility, top_left_corner[0], top_left_corner[1]):
return utility
u += 1

return []

def __build_house(self, house, top_left_corner):
project = copy.deepcopy(house)
project.left_top_corner = top_left_corner
Expand Down Expand Up @@ -152,11 +156,11 @@ def look_up(self, coord, project):
for column in range(cur_step):
if cur_point[1] < self.columns:
spot = self.city[cur_point[0]][cur_point[1]]
if coord.build_type != spot.build_type and coord.id != spot.id and spot.content != '.':
if coord.content == 'R' and not project.is_utility_around(spot.service_type):
if coord.build_type != spot.build_type and coord.id != spot.id and spot.content != 0:
if coord.build_type == 'R' and not project.is_utility_around(spot.service_type):
project.utilities_around.append(spot.service_type)
elif coord.content == 'U':
neighbour = self.residentials[coord.id]
elif coord.build_type == 'U':
neighbour = self.residentials[spot.id]
if not neighbour.is_utility_around(project.service_type):
neighbour.utilities_around.append(project)
cur_point[1] += 1
Expand All @@ -173,11 +177,11 @@ def look_right(self, coord, project):
for row in range(cur_step):
if cur_point[0] < self.rows:
spot = self.city[cur_point[0]][cur_point[1]]
if coord.build_type != spot.build_type and coord.id != spot.id and spot.content != '.':
if coord.content == 'R' and not project.is_utility_around(spot.service_type):
if coord.build_type != spot.build_type and coord.id != spot.id and spot.content != 0:
if coord.build_type == 'R' and not project.is_utility_around(spot.service_type):
project.utilities_around.append(spot.service_type)
elif coord.content == 'U':
neighbour = self.residentials[coord.id]
elif coord.build_type == 'U':
neighbour = self.residentials[spot.id]
if not neighbour.is_utility_around(project.service_type):
neighbour.utilities_around.append(project)
cur_point[0] += 1
Expand All @@ -194,11 +198,11 @@ def look_down(self, coord, project):
for column in range(cur_step):
if cur_point[1] >= 0:
spot = self.city[cur_point[0]][cur_point[1]]
if coord.build_type != spot.build_type and coord.id != spot.id and spot.content != '.':
if coord.build_type != spot.build_type and coord.id != spot.id and spot.content != 0:
if coord.content == 'R' and not project.is_utility_around(spot.service_type):
project.utilities_around.append(spot.service_type)
elif coord.content == 'U':
neighbour = self.residentials[coord.id]
neighbour = self.residentials[spot.id]
if not neighbour.is_utility_around(project.service_type):
neighbour.utilities_around.append(project)
cur_point[1] -= 1
Expand All @@ -213,13 +217,13 @@ def look_left(self, coord, project):
while cur_point[1] != coord.point[1]:
if cur_point[1] >= 0:
for rows in range(cur_step):
if cur_point[0] <= 0:
if cur_point[0] >= 0:
spot = self.city[cur_point[0]][cur_point[1]]
if coord.build_type != spot.build_type and coord.id != spot.id and spot.content != '.':
if coord.content == 'R' and not project.is_utility_around(spot.service_type):
if coord.build_type != spot.build_type and coord.id != spot.id and spot.content != 0:
if coord.build_type == 'R' and not project.is_utility_around(spot.service_type):
project.utilities_around.append(spot.service_type)
elif coord.content == 'U':
neighbour = self.residentials[coord.id]
elif coord.build_type == 'U':
neighbour = self.residentials[spot.id]
if not neighbour.is_utility_around(project.service_type):
neighbour.utilities_around.append(project)
cur_point[0] += 1
Expand Down
2 changes: 1 addition & 1 deletion visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def read_input_file(file):


if __name__ == '__main__':
information = read_input_file("c_going_green.in")
information = read_input_file("b_short_walk.in")

new_city = City(information)
new_city.build()
Expand Down

0 comments on commit 4a910c7

Please sign in to comment.