本文整理汇总了Python中terrain.Terrain.get_distance方法的典型用法代码示例。如果您正苦于以下问题:Python Terrain.get_distance方法的具体用法?Python Terrain.get_distance怎么用?Python Terrain.get_distance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类terrain.Terrain
的用法示例。
在下文中一共展示了Terrain.get_distance方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Robot
# 需要导入模块: from terrain import Terrain [as 别名]
# 或者: from terrain.Terrain import get_distance [as 别名]
#.........这里部分代码省略.........
reverse_direction = dir_reverse[heading]
index = self.terrain.get_index_of_wall(reverse_direction)
cell.imaginary_walls[index] = 1
# 4) Change the value of visited to signify dead end
cell.visited = 'x'
cell.distance = WALL_VALUE
# 5) Update imaginary walls and distances
self.terrain.update_imaginary_walls(x, y, cell.imaginary_walls)
return rotation, movement
def should_end_exploring(self, x, y):
"""
The robot should end exploring in all these cases:
- It has already explored more than 90% of the cells
- It has already taken 30 steps (in the exploration phase)
- It has reached the center of the maze (again)
- It has reached the starting location (again)
"""
# Check for % of cells covered
if self.terrain.get_percentage_of_maze_explored() > 80:
return True
if self.consecutive_explored_cells >= 3:
return True
# Check for number of steps
if self.steps_exploring > 15:
return True
# Check for center of the maze:
if self.is_at_center_of_the_maze(x, y):
return True
if self.is_at_starting_position(x, y):
return True
return False
def final_round(self, x, y, heading, sensors):
"""
Returns the correct rotation and maximum numbers of steps that
the robot can take to optimize the score while staying on track
"""
rotation = None
movement = None
current_distance = self.terrain.grid[x][y].distance
adj_distances, adj_visited = \
self.terrain.get_adj_info(
x, y, heading, sensors, False)
# Change sensor info to max allowed moves, when it applies
sensors = [3 if step > 3 else step for step in sensors]
for i, steps in enumerate(sensors):
# If we found a movement, exit and apply it
if movement is not None:
break
# Otherwise, iterate through steps to see if one matches with the
# next correct and logical distance for that number of steps
elif self.is_a_possible_move(adj_distances, adj_visited, i):
for idx in range(steps):
step = steps - idx
rotation = rotations[str(i)]
new_direction = self.get_new_direction(rotation)
furthest_distance = self.terrain.get_distance(
x, y, new_direction, step)
if furthest_distance == current_distance - step:
movement = step
break
return rotation, movement
def is_a_possible_move(self, adj_distances, adj_visited, i):
"""
Distances are valid if they are not walls, unvisited, or dead ends.
"""
return (adj_visited[i] is not ''
and adj_visited[i] is not 'x'
and adj_distances[i] is not WALL_VALUE)
def report_results(self):
distance = self.terrain.grid[0][0].distance
percentage = self.terrain.get_percentage_of_maze_explored()
first_round = self.steps_first_round + self.steps_exploring
final_round = self.steps_final_round
print('ALGORITHM USED: {}'.format(self.algorithm.name.upper()))
print('EXPLORING AFTER CENTER: {}'.format(self.explore_after_center))
print('NUMBER OF MOVES FIRST ROUND: {}'.format(first_round))
print('PERCENTAGE OF MAZE EXPLORED: {}%'.format(percentage))
print('DISTANCE TO CENTER: {}'.format(distance))
print('NUMBER OF MOVES FINAL ROUND: {}'.format(final_round))
print('********************************')