1
0

minesweeper.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #扫雷游戏。 m*n 矩阵中,不规律分布有雷"*", 每个没雷的单元格有数字显示,该单元格周围8个单元格的雷的总和。
  2. '''
  3. 逻辑流程:
  4. 1) 生成含雷,和数字标识的矩阵
  5. 2)进入游戏模式:
  6. 请用户输入坐标,以及行为(点开,标雷)
  7. 动作如果是点开,返回坐标结果:是雷,则显示是雷,且已爆炸;不是雷,则返回数字结果。返回矩阵
  8. 动作如果是标雷,则返回标雷后的矩阵
  9. 动作如果是退出,则退出游戏
  10. -----------------------------------------------------------
  11. 如何使用:
  12. 在包含本文件的目录下,命令行输入: python3 mindsweeper.py
  13. '''
  14. import random
  15. def creat2D(row,line,value = 0):
  16. assert isinstance(row,int),'row number must be int'
  17. assert isinstance(line,int),'line number must be int'
  18. array= []
  19. array_line=[]
  20. array = [None] * row
  21. for index in range(row):
  22. array[index] = [value]*line
  23. return array
  24. class mine_matrix():
  25. def __init__(self,m,n):
  26. # print("------this is init-------")
  27. self.matrix = creat2D(m,n)
  28. self.m = int(m)
  29. self.n = int(n)
  30. self.mine_location = []
  31. self.mine_number = 0
  32. self.matrix_known = creat2D(m,n,value = 9)
  33. self.mark_record = []
  34. def create_new(self):
  35. # print('------this is create_new-------')
  36. # m,n = input('please input (line,row):').split(',')
  37. mine_number = int(self.m*self.n/6)
  38. self.mine_number = mine_number
  39. location = [(x,y) for y in range(self.n)for x in range(self.m)]
  40. location1 = location
  41. for i in range(mine_number):
  42. location_number = random.randrange(len(location1))
  43. mine_location=location1[location_number]
  44. self.matrix[mine_location[0]][mine_location[1]] = 'mine'
  45. location1.pop(location_number)
  46. self.mine_location.append(mine_location)
  47. for i in range(self.m):
  48. for j in range(self.n):
  49. i1_list = [i-1,i,i+1 ] if (i>0 and i <self.m-1) else ([i,i+1] if i == 0 else [i-1,i])
  50. j1_list = [j-1,j,j+1 ] if (j>0 and j <self.n-1) else ([j,j+1] if j == 0 else [j-1,j])
  51. if self.matrix[i][j]!='mine':
  52. for i1 in i1_list:
  53. for j1 in j1_list:
  54. if i1 == i and j1 == j:
  55. pass
  56. else:
  57. self.matrix[i][j] +=1 if self.matrix[i1][j1]=='mine' else 0
  58. return self.matrix
  59. def check(self,i,j):
  60. # print('-------this is check--------')
  61. if self.matrix[i][j] == 'mine':
  62. for line in self.matrix:
  63. print(line)
  64. print('you got bombed!')
  65. return False
  66. else:
  67. return True
  68. def mark(self,i,j):
  69. return 'm'
  70. def win(self):
  71. # print('--------this is win----------')
  72. if len(self.mark_record) == self.mine_number:
  73. self.mine_location.sort(key = lambda x:x[0])
  74. self.mark_record.sort(key = lambda x:x[0])
  75. if self.mine_location == self.mark_record:
  76. return True
  77. else:
  78. return False
  79. else:
  80. return False
  81. def print_matrix(self):
  82. # print('------this is print_matrix--------')
  83. for line in self.matrix:
  84. print(line)
  85. def print_known(self):
  86. # print('------this is print_known-------')
  87. for line in self.matrix_known:
  88. print(line)
  89. def operation(self,m,n,i,j,operation):
  90. # print('------operation-------')
  91. if operation == 'c':
  92. if not self.check(i,j):
  93. if not self.choice(m,n):
  94. return False
  95. else:
  96. return True
  97. else:
  98. self.matrixknown(i,j)
  99. return True
  100. if operation == 'm':
  101. self.matrix_known[i][j] = 'm'
  102. self.mark_record.append((i,j))
  103. return True
  104. def matrixknown(self,i,j):
  105. # print('-----matrixknown------')
  106. self.matrix_known[i][j] =self.matrix[i][j]
  107. # print('matrix_know[{0}][{1}] is {2}'.format(i,j,self.matrix_known[i][j]))
  108. # print('matrix[{0}][{1}] is {2}'.format(i,j,self.matrix[i][j]))
  109. def choice(self,m,n):
  110. # print('-----choice------')
  111. new_choice = input('would you want to start a new game(n) or to quit(q)?')
  112. if new_choice == 'n':
  113. print('start a new game with the same scale')
  114. self.__init__(m,n)
  115. self.create_new()
  116. return True
  117. else:
  118. return False
  119. def new_choice(m,n):
  120. new_choice = input('would you want to start a new game(n) or to quit(q)?')
  121. if new_choice == 'n':
  122. print('start a new game with the same scale')
  123. mine1 = mine_matrix(m,n)
  124. return mine1
  125. else:
  126. return False
  127. def main():
  128. m = int(input('please input m:'))
  129. n = int(input('please input n:'))
  130. mine1 = mine_matrix(m,n)
  131. mine1.create_new()
  132. while True:
  133. if mine1.win():
  134. print('you win!')
  135. for line in mine1.matrix:
  136. print (line)
  137. mine1 = new_choice(m,n)
  138. mine1.create_new()
  139. if not mine1:
  140. break
  141. mine1.print_known()
  142. choice= input('please input(i,j,[c]heck/[m]ark),or input "q" to quit:')
  143. if choice == 'q':
  144. print('you chose to quit the game')
  145. break
  146. else:
  147. i,j,operation = choice.split(',')
  148. i = int(i)
  149. j = int(j)
  150. op_result = mine1.operation(m,n,i,j,operation)
  151. # print('op_result is {0}'.format(op_result))
  152. if not op_result:
  153. break
  154. main()