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
11 changes: 11 additions & 0 deletions ToolBoxThoughts.txt
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 16 additions & 11 deletions astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Binary file added diagonalScreenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fScreenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gScreenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hScreenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hopsScreenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hopsScreenshot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added onesScreenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added swampScreenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added swampScreenshot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added swampScreenshot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added swampScreenshot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added wonderingScreenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.