Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
Al and Algorithms Project Toolbox starter code

Full instructions at https://sd17spring.github.io/toolboxes/algorithms-and-ai/

My screenshots and reflection for this toolbox can be found at:

https://docs.google.com/document/d/13Tmkvu1-FT4Bj3LW5vT6KWTqidRys0eqDtDRgo1x6qc/edit?usp=sharing


My reflections on the other 5 toolboxes that I completed this semester can be founf at:

https://docs.google.com/document/d/1DqShx01xs8j0NznuAELx_SODi0nsG1GNsEEfMEXn8KM/edit?usp=sharing
60 changes: 50 additions & 10 deletions astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ def _is_occupied(self, cell_coord):
return False

def _add_swamp(self, mouse_pos):
""" Adds a swamp tile in the cell that mouse_pos indicates """
# insert swamp code here.
pass
swamp_coord = (mouse_pos[0]//50, mouse_pos[1]//50)
if self._is_occupied(swamp_coord):
if self.actors[swamp_coord].removable:
self.actors.pop(swamp_coord, None)
elif swamp_coord != self.cake.cell_coordinates:
swamp = ObstacleTile(swamp_coord, self, './images/swamp.jpg',
is_unpassable=False, terrain_cost=3)
self.actors[swamp_coord] = swamp

def _add_lava(self, mouse_pos):
""" Adds a lava tile in the cell that mouse_pos indicates """
Expand Down Expand Up @@ -108,13 +113,16 @@ def main_loop(self):
elif event.type is pygame.MOUSEBUTTONDOWN:
if self.add_tile_type == 'lava':
self._add_lava(event.pos)
# insert swamp code here
elif self.add_tile_type == 'swamp':
self._add_swamp(event.pos)
elif event.type is pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.paul.run_astar(self.cake.cell_coordinates, self)
self.paul.get_path()
elif event.key == pygame.K_l:
self.add_tile_type = 'lava'
elif event.key == pygame.K_s:
self.add_tile_type = 'swamp'
# insert swamp code here


Expand Down Expand Up @@ -167,10 +175,10 @@ def f_cost(self):
return self.g_cost + self.h_cost

def draw(self):
COST_TO_DRAW = ''
# COST_TO_DRAW = self.g_cost
# COST_TO_DRAW = self.h_cost
# COST_TO_DRAW = self.f_cost
#COST_TO_DRAW = ''
COST_TO_DRAW = self.g_cost
#COST_TO_DRAW = self.h_cost
#COST_TO_DRAW = self.f_cost
line_width = 2
rect = pygame.Rect(self.coordinates, self.dimensions)
pygame.draw.rect(self.draw_screen, self.color, rect, line_width)
Expand All @@ -196,22 +204,54 @@ def get_open_adj_coords(self, coords):
""" returns list of valid coords that are adjacent to the argument,
open, and not in the closed list. """
# modify directions and costs as needed
"""
I updated this section to include the diagonals and jumping over lava.
"""

directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
diagonals = [(1, -1), (1, 1), (-1, 1), (-1, -1)]
jump_overs = [(2, 0),(0, 2),(-2, 0),(0, -2)]
all_adj = [self.world._add_coords(coords, d) for d in directions]
in_bounds = [self.is_valid(c) for c in all_adj]
all_diag = [self.world._add_coords(coords, d) for d in diagonals]
all_jumps = [self.world._add_coords(coords, d) for d in jump_overs]
in_bounds_adj = [self.is_valid(c) for c in all_adj]
in_bounds_diag = [self.is_valid(c) for c in all_diag]
in_bounds_jumps = [self.is_valid(c) for c in all_jumps]
lava_filled = [self.is_filled(d) for d in all_adj]
costs = []
open_adj = []
for i, coord in enumerate(all_adj):
if(in_bounds[i]):
if(in_bounds_adj[i]):
costs.append(1 + self.world.get_terrain_cost(coord))
open_adj.append(coord)
elif(lava_filled[i]):
if(in_bounds_jumps[i]):
costs.append(8 + self.world.get_terrain_cost(jump_overs[i]))
open_adj.append(jump_overs[i])

for i, coord in enumerate(all_diag):
if(in_bounds_diag[i]):
costs.append(3 + self.world.get_terrain_cost(coord))
open_adj.append(coord)

return open_adj, costs

def is_valid(self, coord):
return self.world._is_in_grid(coord) \
and not self.world._is_occupied(coord) \
and coord not in self.closed_list

"""
I added the is_filled function here to tell if the square is valid, but has
lava in it. This lets the get_open_adj_coords function know if it can be jumped
over.
"""

def is_filled(self, coord):
return self.world._is_in_grid(coord) \
and self.world._is_occupied(coord) \
and coord not in self.closed_list

def get_lowest_cost_open_coord(self):
open_cells = self.open_list
sorted_cells = sorted(open_cells, key=lambda s: self.cells[s].f_cost)
Expand Down