扫雷python空白延伸算法。
正确的扫雷空白延伸算法是:点击空白,周围8个点没有雷的情况下排空,然后依次进行延伸,从这8个点的每一个再进行周围8点的计算,如果满足排空条件就排空。依次排空这样下去。
算法代码如下:
def set_pos(self, x, y): map_info = self.get_x_y_info(x,y)# 获取x,y点的周围8个点信息 if map_info: if self.check_pos(map_info): # 判断是否可延伸 self.extend_mark(map_info) # 延伸标记 idx = self.get_index(x,y) self.map_list_show[idx] = self.map_list[idx] # 点击点的标记 def extend_mark(self, map_info): while map_info: pos = map_info.pop() # 每次从所有可排空的点中拿出一个 x, y = int(pos[0]), int(pos[1]) idx = self.get_index(x,y) self.map_list_show[idx] = self.map_list[idx] # 对可以排空点进行排空 info_bak = self.get_x_y_info(x,y) # 延伸处理,从排空点获取周围8个点的信息 if self.check_pos(info_bak): # 检测是否排空 for i in info_bak: index = self.get_index(int(i[0]), int(i[1])) if i not in map_info and self.map_list_show[index] != self.map_list[index]: map_info.append(i) # 对未排空和满足排空的点进行追加到map_info,等待后续的排空处理 # 获得 x 和 y 坐标周围的8个点的坐标 def get_x_y_info(self, x, y): map_info = [] if x in range(1, self.row+1) and y in range(1, self.column+1):# x,y 需要满足在地图内部 for i in range(x-1, x+2): for j in range(y-1, y+2): if i in range(1, self.row + 1) and j in range(1, self.column + 1) and (i != x or j != y): # 进行加减法运算后的i和j需要在地图内部,排除非法的点 map_info.append((i,j)) else: return map_info return map_info def check_pos(self, map_info):# 检测是否满排空要求,周围8点是空的,可以排空,否则不可以 if map_info is None: return False for val in map_info: idx = self.get_index(val[0], val[1]) if self.map_list[idx] == MAP_MINE: return False return True