扫雷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

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值