def _find_all_next_locations(self): all_next_locations = [] x = self.start_point.x y = self.start_point.y locations = [ Location(x - 1, y, self.map[y][x - 1]), # 左边的 Location(x + 1, y, self.map[y][x + 1]), # 右边的 Location(x, y - 1, self.map[y - 1][x]), # 上边的 Location(x, y + 1, self.map[y + 1][x]), # 下边的 ] for location in locations: map_type = self.map[location.y][location.x] # 如果没有到达过 if self.arrived_map[location.y][location.x] == 0: if map_type == Map.land: # 如果是在陆地上 self._add_next_location(all_next_locations, location) if map_type == Map.stump: # 如果是在树桩上 self._add_next_location(all_next_locations, location) if map_type == Map.tree_horizontal_1_sea: # 单节长度 横着的 海中的 木棍 self._add_next_location(all_next_locations, location) if map_type == Map.tree_vertical_1_sea: # 单节长度 竖着的 海中的 木棍 self._add_next_location(all_next_locations, location) if map_type == Map.tree_horizontal_2_sea: # 双节长度 横着的 海中的 木棍 self._add_next_location(all_next_locations, location) if map_type == Map.tree_vertical_2_sea: # 双节长度 竖着的 海中的 木棍 self._add_next_location(all_next_locations, location) return all_next_locations
def _get_new_tree_location(self, tree_location, push_enum, move_step): new_tree_location = Location(tree_location.x, tree_location.y) if move_step == 0: return new_tree_location if push_enum == Push.up: new_tree_location.y = new_tree_location.y - move_step if push_enum == Push.down: new_tree_location.y = new_tree_location.y + move_step if push_enum == Push.left: new_tree_location.x = new_tree_location.x - move_step if push_enum == Push.right: new_tree_location.x = new_tree_location.x + move_step if tree_location.location_value == Map.tree1 or tree_location.location_value == Map.tree_stand: if push_enum == Push.up or push_enum == Push.down: if self.map[new_tree_location.y][ new_tree_location.x] == Map.sea: new_tree_location.location_value = Map.tree_vertical_1_sea else: new_tree_location.location_value = Map.tree_vertical_1 else: if self.map[new_tree_location.y][ new_tree_location.x] == Map.sea: new_tree_location.location_value = Map.tree_horizontal_1_sea else: new_tree_location.location_value = Map.tree_horizontal_1 return new_tree_location
def _find_all_monster_locations(self, trees): all_monster_locations = [] for tree in trees: x = tree.x y = tree.y locations = [ Location(x-1, y, self.map[y][x-1], Push.right), # 左边的 Location(x+1, y, self.map[y][x+1], Push.left), # 右边的 Location(x, y-1, self.map[y-1][x], Push.down), # 上边的 Location(x, y+1, self.map[y+1][x], Push.up), # 下边的 ] for location in locations: map_type = self.map[location.y][location.x] if map_type == Map.land: # 如果是在陆地上 all_monster_locations.append(location) if map_type == Map.stump: # 如果是在树桩上 all_monster_locations.append(location) if map_type == Map.tree_horizontal_1_sea: # 单节长度 横着的 海中的 木棍 all_monster_locations.append(location) if map_type == Map.tree_vertical_1_sea: # 单节长度 竖着的 海中的 木棍 all_monster_locations.append(location) if map_type == Map.tree_horizontal_2_sea: # 双节长度 横着的 海中的 木棍 all_monster_locations.append(location) if map_type == Map.tree_vertical_2_sea: # 双节长度 竖着的 海中的 木棍 all_monster_locations.append(location) return all_monster_locations
def _find_first_location(self, enum_map_types, point): y = 0 for line in self.map: x = 0 for value in line: for map_type in enum_map_types: if value == map_type: location = Location(x, y) location.location_value = value point.append(location) x += 1 y += 1
def _find_tree_location(self): x = self.monster_location.x y = self.monster_location.y if self.monster_location.push_location == Push.up: y -= 1 if self.monster_location.push_location == Push.down: y += 1 if self.monster_location.push_location == Push.left: x -= 1 if self.monster_location.push_location == Push.right: x += 1 tree_location = Location(x, y) tree_location.location_value = self.map[tree_location.y][ tree_location.x] return tree_location
def _find_all_tree_locations(self): trees_location = [] y = 0 for line in self.map: x = 0 for value in line: if value == Map.tree1 or \ value == Map.tree2 or \ value == Map.tree_horizontal_1 or \ value == Map.tree_vertical_1: tree = Location(x, y) tree.location_value = value trees_location.append(tree) x += 1 y += 1 return trees_location
def monster_push_tree(self, monster_location): new_monster_location = Location( monster_location.x, monster_location.y, monster_location.location_value, ) able_to_push, new_map = self._push_tree_1(monster_location) return new_map, new_monster_location, able_to_push
class GameController: map = [] deep_count = 5 start_location = Location(0, 0) end_locations = [] def __init__(self, map): self.map = map def play_game(self, deep_count): self.deep_count = deep_count self._find_start_location() self._find_end_location() Log.location(self.start_location) Log.locations(self.end_locations) self._play(self.map, 0, self.start_location) def _play(self, map, play_count, monster_location): play_controller = PlayController(map, self.end_locations, monster_location) # 检查是否已经成功推到位置 if play_controller.is_success(): print(play_controller.success_result()) return # 如果递归超过deep count,则不再进行计算 if play_count > self.deep_count: return # 找到树旁边可以站小怪兽的地方 monster_near_by_trees_locations = play_controller.find_monster_near_by_tree_locations( ) # 如果无路可走 if len(monster_near_by_trees_locations) == 0: return # 循环所有位置 for location in monster_near_by_trees_locations: # 推树,并返回新的地图 new_map, new_monster_location, is_move = play_controller.monster_push_tree( location) if not is_move: # 如果没有任何移动 return Log.map(new_map) # 然后根据新的地图,继续玩,并增加递归次数 self._play(new_map, play_count + 1, new_monster_location) def _find_start_location(self): y = 0 for line in self.map: x = 0 for value in line: if value == Map.start: self.start_location.x = x self.start_location.y = y self.start_location.location_value = value x += 1 y += 1 def _find_end_location(self): self._find_first_location( [Map.end_double, Map.end_horizontal, Map.end_vertical], self.end_locations) def _find_first_location(self, enum_map_types, point): y = 0 for line in self.map: x = 0 for value in line: for map_type in enum_map_types: if value == map_type: location = Location(x, y) location.location_value = value point.append(location) x += 1 y += 1