#扫雷游戏。 m*n 矩阵中,不规律分布有雷"*", 每个没雷的单元格有数字显示,该单元格周围8个单元格的雷的总和。 ''' 逻辑流程: 1) 生成含雷,和数字标识的矩阵 2)进入游戏模式: 请用户输入坐标,以及行为(点开,标雷) 动作如果是点开,返回坐标结果:是雷,则显示是雷,且已爆炸;不是雷,则返回数字结果。返回矩阵 动作如果是标雷,则返回标雷后的矩阵 动作如果是退出,则退出游戏 ----------------------------------------------------------- 如何使用: 在包含本文件的目录下,命令行输入: python3 mindsweeper.py ''' import random def creat2D(row,line,value = 0): assert isinstance(row,int),'row number must be int' assert isinstance(line,int),'line number must be int' array= [] array_line=[] array = [None] * row for index in range(row): array[index] = [value]*line return array class mine_matrix(): def __init__(self,m,n): # print("------this is init-------") self.matrix = creat2D(m,n) self.m = int(m) self.n = int(n) self.mine_location = [] self.mine_number = 0 self.matrix_known = creat2D(m,n,value = 9) self.mark_record = [] def create_new(self): # print('------this is create_new-------') # m,n = input('please input (line,row):').split(',') mine_number = int(self.m*self.n/6) self.mine_number = mine_number location = [(x,y) for y in range(self.n)for x in range(self.m)] location1 = location for i in range(mine_number): location_number = random.randrange(len(location1)) mine_location=location1[location_number] self.matrix[mine_location[0]][mine_location[1]] = 'mine' location1.pop(location_number) self.mine_location.append(mine_location) for i in range(self.m): for j in range(self.n): i1_list = [i-1,i,i+1 ] if (i>0 and i 0 and j