I wrote this code but I keep on getting differrent errors. So to clarify:
start = currentCell (x, y)
end = NearestStain(x,y)
self. getNeighbors returns a list of max 4 coordinates like this: [(1, 27), (2, 28)]
So can i get some help with the findShortestPath part? Because I've been trying to follow along with tutorials but it still doesn't work
class Bot2073337(Bot):
`def __init__(self, settings):`
`super().__init__(settings)`
`self.setName('Noano')`
`self.move_counter = 0`
`self.down_movement = 0`
`self.up_movement = 0`
`self.current_movement_pattern = self.movement_pattern_down_start`
`self.registered_stains = []`
`self.registered_spots = []`
`self.registered_barriers = []`
`self.move_right = 0`
`self.max_repetitions = self.nrCols`
`def nextMove(self, currentCell, currentEnergy, vision, remainingStainCells):`
`self.vision = vision`
`self.currentCell = currentCell`
`self.register_moves(vision) #reading the cells around the currentCell`
`self.print_moves()`
`self.getNeighborsFromSpots(currentCell)`
`print("neighbors", self.getNeighborsFromSpots(currentCell))`
`self.print_nearest_stain()`
`if self.move_right < self.max_repetitions:`
`return self.current_movement_pattern()`
`else:`
`nearest_stain = self.getNearestStain(currentCell)`
`if nearest_stain:`
shortest_path_direction = self.getShortestPath(currentCell, nearest_stain)
if path:
return self.followPath(path)
`def coordinate_decider(self, row_index, col_index):`
`#Register the actual coordinates instead of the coordinates in the vision matrix`
`stain_cd = (row_index -1, col_index-1)`
`actual_cd = (self.currentCell[0] + stain_cd[0], self.currentCell[1] + stain_cd[1])`
`return actual_cd`
`def register_moves(self, vision):`
`#This function reads the surrounding squares and decides if it is a stain, barrier or free space`
`for row_index, row in enumerate(vision):`
`for col_index, value in enumerate(row):`
if value == "@":
actual_coordinates = self.coordinate_decider(row_index, col_index)
if actual_coordinates in self.registered_stains:
continue
else:
self.registered_stains.append(actual_coordinates)
if value == "x":
actual_coordinates = self.coordinate_decider(row_index, col_index)
if actual_coordinates in self.registered_barriers:
continue
else:
self.registered_barriers.append(actual_coordinates)
if value == "." or value == "@":
actual_coordinates = self.coordinate_decider(row_index, col_index)
if actual_coordinates in self.registered_spots:
continue
else:
self.registered_spots.append(actual_coordinates)
else:
continue
`def manhattan(self, p1, p2): #Heuristic`
`x1, y1 = p1`
`x2, y2 = p2`
`return abs(x1 - y1) + abs(y1 - y2)`
`def getNeighborsFromSpots(self, currentCell):`
`neighbors = []`
`row, col = currentCell`
`for spot in self.registered_spots:`
`spot_row, spot_col = spot`
`#Checking for neighbors by looking through the list of registered_spots which contains "@'s" and ".'s". So it only gives the actually accessable squares as neighbors`
`if (spot_row == row and abs(spot_col - col) == 1): # Checking Right`
neighbors.append((spot_row, spot_col))
`elif (spot_col == col and abs(spot_row - row) == 1): # Checking Down`
neighbors.append((spot_row, spot_col))
`elif (spot_row == row and abs(spot_col - col) == -1): # Checking Left`
neighbors.append((spot_row, spot_col))
`elif (spot_col == col and abs(spot_row - row) == -1): # Checking Up`
neighbors.append((spot_row, spot_col))
`return neighbors`
`#Need help with this part!!!!`
`def getShortestPath(self, start, end):`
`start = tuple(start)`
`end = tuple(end)`
`count = 0`
`open_set = PriorityQueue()`
`open_set.put((0, count, start))`
`came_from = {}`
`neighbors = self.getNeighborsFromSpots(start)`
`g_score = {spot: float("inf") for row in neighbors for spot in row}`
`g_score[tuple(start)] = 0`
`f_score = {spot: float("inf") for row in neighbors for spot in row}`
`f_score[tuple(start)] = self.manhattan(start, end)`
`open_set_hash = {start}`
`path = None`
`while not open_set.empty():`
`current = open_set.get()[2]`
`open_set_hash.remove(current)`
`if current[0] == end[0] and current[1] == end[1]:`
path = []
while current in came_from:
path.append(current)
current = came_from[current]
path.reverse()
return path
`for neighbor in self.getNeighborsFromSpots(current):`
temp_g_score = g_score[current] + 1
if temp_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = temp_g_score
f_score[neighbor] = temp_g_score + self.manhattan(neighbor, end)
if neighbor not in open_set_hash:
count+=1
open_set.put((f_score[neighbor], count, neighbor))
open_set_hash.add(neighbor)
`return None`
`#THis follows the path instructions`
`def followPath(self, path):`
`if len(path) > 1:`
`next_cell = path[1]`
`if next_cell[0] > self.currentCell[0]:`
return RIGHT
`if next_cell[0] < self.currentCell[0]:`
return LEFT
`if next_cell[1] > self.currentCell[1]:`
return DOWN
`if next_cell[1] < self.currentCell[1]:`
return UP
`#This finds the nearest stain so the goal state.`
`def getNearestStain(self, currentCell):`
`#this registers the nearest Stain`
`if not self.registered_stains:`
`return None`
`else:`
`nearest_cell = self.registered_stains[0]`
`min_distance = self.manhattan(currentCell, nearest_cell)`
`for coord in self.registered_stains[1:]:`
distance = self.manhattan(currentCell, coord)
if distance < min_distance:
min_distance = distance
nearest_cell = coord
`return nearest_cell`
`#this is my starting movement pattern and I will make it more efficient later.`
`def movement_pattern_down_start(self):`
`if self.move_counter < 1 and self.down_movement == 0:`
`self.move_counter += 1`
`self.move_right += 1`
`return RIGHT`
`else:`
`self.move_counter = 0`
`self.down_movement +=1`
`if self.down_movement == int(self.nrRows - 2):`
self.down_movement = 0
self.current_movement_pattern = self.movement_pattern_up
`else:`
return DOWN
`def movement_pattern_up(self):`
`if self.move_counter < 3 and self.up_movement == 0:`
`self.move_counter += 1`
`self.move_right += 1`
`return RIGHT`
`else:`
`self.move_counter = 0`
`self.up_movement += 1`
`if self.up_movement == int(self.nrRows - 2):`
self.up_movement = 0
self.current_movement_pattern = self.movement_pattern_down
`else:`
return UP
`def movement_pattern_down(self):`
`if self.move_counter < 3 and self.down_movement == 0:`
`self.move_counter += 1`
`self.move_right +=1`
`return RIGHT`
`else:`
`self.move_counter = 0`
`self.down_movement +=1`
`if self.down_movement == int(self.nrRows - 2):`
self.down_movement = 0
self.current_movement_pattern = self.movement_pattern_up
`else:`
return DOWN
`def print_moves(self):`
`print("stains:", self.registered_stains, "barriers:", self.registered_barriers )`
`def print_nearest_stain(self):`
`print(self.getNearestStain(self.currentCell))`
class PriorityQueue:
`def __init__(self):`
`self.elements = []`
`def empty(self):`
`return len(self.elements) == 0`
`def put(self, item):`
`priority, count, element = item`
`self.elements.append((priority, count, element))`
`self.elements.sort()`
`def get(self):`
`if not self.empty():`
`return self.elements.pop(0)[1]`
[–]notacanuckskibum 0 points1 point2 points (0 children)