diff --git a/ToolBoxThoughts.txt b/ToolBoxThoughts.txt new file mode 100644 index 0000000..a39d8b0 --- /dev/null +++ b/ToolBoxThoughts.txt @@ -0,0 +1,11 @@ +COST_TO_DRAW + I've commented the code in lines 146-148 to explain the way each of these lines works. self.g_cost is additive; it starts at 0 and on each tile shows the total cost of the path at that point. Thus, the starting tile has a 0 on it and the tiles immediately surrounding it all have 1s on them,so on and so forth. self.h_cost does the same, just subtracting rather than adding. h_cost calculates the total cost, puts that on the starting tile, and counts down to 0 from there. self.f_cost shows the total cost on each tile. If the most direct path including a given tile has a total cost of 18, that tile will have an 18 on it. If a tile is out of the way and moving to it would result in a higher total cost, such as 20, that higher total cost is what you see on the tile. + +Paul gets Diagonals + At the two points where a diagonal move is made, it is only made because it is necessary. Making two moves, one along the x axis and one along the y axis, to get to the same point is less costly than to move diagonally. This is why the rest of the path is made up of horizontal and vertical moves rather than diagonal moves. However, at the start and at the end both other options are blocked off by lava - therefore in both cases Paul moves diagonally and adds 3 points to the total cost of the path. + +Paul gets Hops + Paul can jump over a block of lava, but will only do so if it is absolutely necessary. The cost of jumping over lava is much higher than moving around it in any direction, so the only case in which Paul will jump over lava is the case in which no other moves are possible. In the screen I took a screenshot of, I placed the lava blocks such that there was no option but to jump at multiple points. This forced a jump move; however, had I not done that, Paul would not have jumped at all. I took a second screenshot to demonstrate this. I also took a screenshot titled onesScreenshot.png of what would happen if all moves had the same value. That would result in a path which gets Paul to the cake in the fewest moves possible. + +Paul gets Swamped + I took mutliple screeenshots of the pygame window including swamp tiles to demonstrate that Paul will move through them in the same way that he will move through empty tiles. These tiles, if I had placed them anywhere else, would have been avoided just as moving diagonally and jumping are avoided because the cost of moving through them is higher than the cost of moving around them. I do wonder why, as shown in wonderingScreenshot.png, Paul's path moves around the last lava block and down, rather than diagonally through the swamp. This would result in the same total cost for the path, and would take fewer moves. However, I believe this is caused by a move-by-move evaluation of the cost: rather than evaluating the total cost for the entire path, we evaluate the total cost as it would be after moving to the next square. Therefore the immediate move with the lowest cost is the one chosen. If we evaluated the total cost for the whole path, we could have Paul follow the path that costs the least AND requires the fewest moves. \ No newline at end of file diff --git a/astar.py b/astar.py index 802abe5..1d41e0d 100644 --- a/astar.py +++ b/astar.py @@ -57,8 +57,12 @@ def _is_occupied(self,cell_coord): return False def _add_swamp(self, mouse_pos): - #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].unremovable == False: + self.actors.pop(swamp_coord, None) + else: + self.actors[swamp_coord] = ObstacleTile( swamp_coord, self, './images/swamp.jpg', is_unpassable = False, terrain_cost = 3) def _add_lava(self, mouse_pos): lava_coord = (mouse_pos[0]/50, mouse_pos[1]/50) @@ -91,14 +95,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' - #insert swamp code here + elif event.key == pygame.K_s: + self.add_tile_type = 'swamp' class Actor(object): def __init__(self, cell_coordinates, world, image_loc, unremovable = False, is_obstacle = True): @@ -142,10 +148,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 #Displays the total cost of the path at that point. Adds to the total as the path is generated. + #COST_TO_DRAW = self.h_cost #Same as g, but in the opposite order. The path counts down from total cost to 0. + #COST_TO_DRAW = self.f_cost #Displays the total cost of the most direct path including that tile. If a tile is out of the way, moving to it will result in a greater total cost, and that total cost is displayed on the tile. line_width = 2 rect = pygame.Rect((self.coordinates[0],self.coordinates[1]),(self.dimensions[0],self.dimensions[1])) pygame.draw.rect(self.draw_screen, self.color, rect, line_width) @@ -166,9 +172,8 @@ def get_h_cost(self, coord_a,coord_b): 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 - directions = [(1,0),(0,1),(-1,0),(0,-1)] - costs = [1,1,1,1] + directions = [(1,0),(0,1),(-1,0),(0,-1), (1,1),(-1,1),(-1,-1),(1,-1), (2,0),(0,2),(-2,0),(0,-2)] + costs = [1,1,1,1, 3,3,3,3, 8,8,8,8] adj_coords = map(lambda d: self.world._add_coords(coords,d), directions) for i, coord in enumerate(adj_coords): costs[i] += self.world.get_terrain_cost(coord) diff --git a/diagonalScreenshot.png b/diagonalScreenshot.png new file mode 100644 index 0000000..f676f4b Binary files /dev/null and b/diagonalScreenshot.png differ diff --git a/fScreenshot.png b/fScreenshot.png new file mode 100644 index 0000000..8a99438 Binary files /dev/null and b/fScreenshot.png differ diff --git a/gScreenshot.png b/gScreenshot.png new file mode 100644 index 0000000..7536e38 Binary files /dev/null and b/gScreenshot.png differ diff --git a/hScreenshot.png b/hScreenshot.png new file mode 100644 index 0000000..c6c3df8 Binary files /dev/null and b/hScreenshot.png differ diff --git a/hopsScreenshot.png b/hopsScreenshot.png new file mode 100644 index 0000000..af750c2 Binary files /dev/null and b/hopsScreenshot.png differ diff --git a/hopsScreenshot2.png b/hopsScreenshot2.png new file mode 100644 index 0000000..f497a51 Binary files /dev/null and b/hopsScreenshot2.png differ diff --git a/onesScreenshot.png b/onesScreenshot.png new file mode 100644 index 0000000..8510f85 Binary files /dev/null and b/onesScreenshot.png differ diff --git a/swampScreenshot1.png b/swampScreenshot1.png new file mode 100644 index 0000000..1dad3e2 Binary files /dev/null and b/swampScreenshot1.png differ diff --git a/swampScreenshot2.png b/swampScreenshot2.png new file mode 100644 index 0000000..beca160 Binary files /dev/null and b/swampScreenshot2.png differ diff --git a/swampScreenshot3.png b/swampScreenshot3.png new file mode 100644 index 0000000..2861634 Binary files /dev/null and b/swampScreenshot3.png differ diff --git a/swampScreenshot4.png b/swampScreenshot4.png new file mode 100644 index 0000000..4dbfdd8 Binary files /dev/null and b/swampScreenshot4.png differ diff --git a/wonderingScreenshot.png b/wonderingScreenshot.png new file mode 100644 index 0000000..f76509f Binary files /dev/null and b/wonderingScreenshot.png differ