From f3d0f9ffaee21e9235b43583fb2eeef75e0e179a Mon Sep 17 00:00:00 2001 From: elenameyerson Date: Mon, 24 Apr 2017 02:22:49 -0400 Subject: [PATCH 1/3] AI toolbox --- astar.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/astar.py b/astar.py index f017985..2d78abe 100644 --- a/astar.py +++ b/astar.py @@ -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 """ @@ -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 @@ -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) @@ -196,15 +204,36 @@ 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): @@ -212,6 +241,17 @@ def is_valid(self, 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) From 92fceb6a6372990cc879b1efa0775cb2e3c26de3 Mon Sep 17 00:00:00 2001 From: elenameyerson Date: Mon, 24 Apr 2017 02:25:08 -0400 Subject: [PATCH 2/3] Add reflection and documentation to README --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index eefdc1a..9867bdb 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,7 @@ 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 From 82c9a3250eddae07c0551114bdaf16f6fa92babe Mon Sep 17 00:00:00 2001 From: elenameyerson Date: Mon, 24 Apr 2017 02:51:15 -0400 Subject: [PATCH 3/3] added all reflections --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 9867bdb..189b946 100644 --- a/README.md +++ b/README.md @@ -6,3 +6,8 @@ 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