# 20183325任廷江实验四《Python程序设计》实验报告

#20183325任廷江 2019-2020-2 《Python程序设计》实验x报告

## 1.实验内容

## 2. 实验分析与设计

class BlockStatus(Enum):    normal = 1  # 未点击    opened = 2  # 已点击    mine = 3    # 地雷    flag = 4    # 标记为地雷    ask = 5   # 标记为问号    bomb = 6    # 踩中地雷    hint = 7    # 被双击的周围    double = 8  # 正被鼠标左右键双击class Mine:    def __init__(self, x, y, value=0):        self._x = x        self._y = y        self._value = 0        self._around_mine_count = -1        self._status = BlockStatus.normal        self.set_value(value)    def __repr__(self):        return str(self._value)        # return f'({self._x},{self._y})={self._value}, status={self.status}'    def get_x(self):        return self._x    def set_x(self, x):        self._x = x    x = property(fget=get_x, fset=set_x)    def get_y(self):        return self._y    def set_y(self, y):        self._y = y    y = property(fget=get_y, fset=set_y)    def get_value(self):        return self._value    def set_value(self, value):        if value:            self._value = 1        else:            self._value = 0    value = property(fget=get_value, fset=set_value, doc='0:非地雷 1:雷')    def get_around_mine_count(self):        return self._around_mine_count    def set_around_mine_count(self, around_mine_count):        self._around_mine_count = around_mine_count    around_mine_count = property(fget=get_around_mine_count, fset=set_around_mine_count, doc='四周地雷数量')    def get_status(self):        return self._status    def set_status(self, value):        self._status = value    status = property(fget=get_status, fset=set_status, doc='BlockStatus')

class MineBlock:  def open_mine(self, x, y):        # 踩到雷了        if self._block[y][x].value:            self._block[y][x].status = BlockStatus.bomb            return False        # 先把状态改为 opened        self._block[y][x].status = BlockStatus.opened        around = _get_around(x, y)        _sum = 0        for i, j in around:            if self._block[j][i].value:                _sum += 1        self._block[y][x].around_mine_count = _sum        # 如果周围没有雷，那么将周围8个未中未点开的递归算一遍        # 这就能实现一点出现一大片打开的效果了        if _sum == 0:            for i, j in around:                if self._block[j][i].around_mine_count == -1:                    self.open_mine(i, j)        return Truedef _get_around(x, y):    """返回(x, y)周围的点的坐标"""    # 这里注意，range 末尾是开区间，所以要加 1    return [(i, j) for i in range(max(0, x - 1), min(BLOCK_WIDTH - 1, x + 1) + 1)            for j in range(max(0, y - 1), min(BLOCK_HEIGHT - 1, y + 1) + 1) if i != x or j != y]

4.运行结果见提交的视频
## 实验过程中遇到的问题和解决过程
- 问题1：课上还有很多东西没会，重新学的，且实验还没完全实现功能
- 问题1解决方案：不断地去练习
5.总结

posted @ 2020-06-14 22:35  20183325任廷江  阅读(123)  评论(0编辑  收藏