/minesweeper_dependencies.py

https://gitlab.com/Nunuvin/minesweeper · Python · 198 lines · 173 code · 15 blank · 10 comment · 83 complexity · 74a85b901b994ca9dd8cd878277ab5dd MD5 · raw file

  1. # Do not remove the header from this file or other files created from it
  2. # LICENSE General Public License Version 3
  3. # However please give credit to the creator of the original code
  4. # Made by Vlad C.
  5. # Provided on AS IS basis without any warranty of any kind, either expressed or implied
  6. # Use it at your own risk
  7. # Minesweeper dependencies
  8. from __future__ import print_function
  9. import os
  10. def cls():
  11. '''clear console window'''
  12. os.system( 'cls' if os.name=='nt' else 'clear')
  13. def show(grid,mnumber):
  14. '''prints out grid as player knows it'''
  15. end=9 #last character of the line
  16. new=0
  17. numbers=['0','1','2','3','4','5','6','7','8','9']
  18. l=0
  19. j=0
  20. print(' ',end=' ')
  21. for i in numbers: #prints numbers as a top line
  22. print (i, end='')
  23. print('\n')#empty line
  24. for i in range(0,100,1):
  25. j+=1
  26. if end==i:
  27. print (grid[i]) #creates new line
  28. end+=10
  29. elif i==new:
  30. print (numbers[l], '', grid[i], end='') #letter at the beginning of the line
  31. if l!=9:
  32. l+=1
  33. new+=10
  34. else:
  35. print (grid[i], end='') #prevents from printing next time on a new line
  36. print ('\n','Mines left:',mnumber)
  37. def selfncheck(grid,matrix,ident): #shenanigans are present
  38. '''check self and neighbours for being a mine. If not mined open neighbours'''
  39. identifier=[]
  40. identifier.append(ident)
  41. for ident in identifier:
  42. cellvalue=0
  43. if ident==0: #top left corner +1 +11 +10
  44. if matrix[ident+1]=='x':
  45. cellvalue+=1
  46. if matrix[ident+10]=='x':
  47. cellvalue+=1
  48. if matrix[ident+11]=='x':
  49. cellvalue+=1
  50. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  51. identifier.append(ident+1)
  52. identifier.append(ident+11)
  53. identifier.append(ident+10)
  54. if ident==9: #Top right corner -1 +10 +9
  55. if matrix[ident-1]=='x':
  56. cellvalue+=1
  57. if matrix[ident+10]=='x':
  58. cellvalue+=1
  59. if matrix[ident+9]=='x':
  60. cellvalue+=1
  61. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  62. identifier.append(ident-1)
  63. identifier.append(ident+10)
  64. identifier.append(ident+9)
  65. if ident==90: #bot left corner +1 -10 -9
  66. if matrix[ident+1]=='x':
  67. cellvalue+=1
  68. if matrix[ident-10]=='x':
  69. cellvalue+=1
  70. if matrix[ident-9]=='x':
  71. cellvalue+=1
  72. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  73. identifier.append(ident+1)
  74. identifier.append(ident-9)
  75. identifier.append(ident+-10)
  76. if ident==99: #bot right corenr -10 -11 -1
  77. if matrix[ident-1]=='x':
  78. cellvalue+=1
  79. if matrix[ident-10]=='x':
  80. cellvalue+=1
  81. if matrix[ident-11]=='x':
  82. cellvalue+=1
  83. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  84. identifier.append(ident-1)
  85. identifier.append(ident-10)
  86. identifier.append(ident-11)
  87. if ident>0 and ident<9: #checking top row -1 +1 +10 +9 +11 cells only
  88. if matrix[ident-1]=='x':
  89. cellvalue+=1
  90. if matrix[ident+1]=='x':
  91. cellvalue+=1
  92. if matrix[ident+10]=='x':
  93. cellvalue+=1
  94. if matrix[ident+9]=='x':
  95. cellvalue+=1
  96. if matrix[ident+11]=='x':
  97. cellvalue+=1
  98. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  99. identifier.append(ident+1)
  100. identifier.append(ident-1)
  101. identifier.append(ident+10)
  102. identifier.append(ident+9)
  103. identifier.append(ident+11)
  104. if ident>90 and ident<99: #checking bot row +1 -1 -10 -9 -11
  105. if matrix[ident-1]=='x':
  106. cellvalue+=1
  107. if matrix[ident+1]=='x':
  108. cellvalue+=1
  109. if matrix[ident-10]=='x':
  110. cellvalue+=1
  111. if matrix[ident-9]=='x':
  112. cellvalue+=1
  113. if matrix[ident-11]=='x':
  114. cellvalue+=1
  115. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  116. identifier.append(ident+1)
  117. identifier.append(ident-1)
  118. identifier.append(ident-10)
  119. identifier.append(ident-9)
  120. identifier.append(ident-11)
  121. for i in range(1,10,1):
  122. if ident==i*10 and ident<90: #checking left side +1 -10 +10 -9 +11
  123. if matrix[ident-1]=='x':
  124. cellvalue+=1
  125. if matrix[ident+10]=='x':
  126. cellvalue+=1
  127. if matrix[ident-10]=='x':
  128. cellvalue+=1
  129. if matrix[ident-9]=='x':
  130. cellvalue+=1
  131. if matrix[ident+11]=='x':
  132. cellvalue+=1
  133. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  134. identifier.append(ident+1)
  135. identifier.append(ident-10)
  136. identifier.append(ident-9)
  137. identifier.append(ident+10)
  138. identifier.append(ident+11)
  139. if ident==i*10+9 and ident<90: # checking right side -1 -10 +10 +9 -11
  140. if matrix[ident-1]=='x':
  141. cellvalue+=1
  142. if matrix[ident-10]=='x':
  143. cellvalue+=1
  144. if matrix[ident+10]=='x':
  145. cellvalue+=1
  146. if matrix[ident+9]=='x':
  147. cellvalue+=1
  148. if matrix[ident-11]=='x':
  149. cellvalue+=1
  150. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  151. identifier.append(ident-1)
  152. identifier.append(ident-10)
  153. identifier.append(ident+10)
  154. identifier.append(ident+9)
  155. identifier.append(ident-11)
  156. if ident>i*10 and ident<i*10+9 and ident<90: #checking +1 -1 -9 -11 -10 +9 +10 +11
  157. if matrix[ident+1]=='x':
  158. cellvalue+=1
  159. if matrix[ident-1]=='x':
  160. cellvalue+=1
  161. if matrix[ident-9]=='x':
  162. cellvalue+=1
  163. if matrix[ident-11]=='x':
  164. cellvalue+=1
  165. if matrix[ident-10]=='x':
  166. cellvalue+=1
  167. if matrix[ident+9]=='x':
  168. cellvalue+=1
  169. if matrix[ident+10]=='x':
  170. cellvalue+=1
  171. if matrix[ident+11]=='x':
  172. cellvalue+=1
  173. if cellvalue==0 and grid[ident]=='o': #if neighbours are not mines then check them too and count them as explored
  174. identifier.append(ident+1)
  175. identifier.append(ident-1)
  176. identifier.append(ident-9)
  177. identifier.append(ident-11)
  178. identifier.append(ident-10)
  179. identifier.append(ident+9)
  180. identifier.append(ident+10)
  181. identifier.append(ident+11)
  182. grid[ident]=str(cellvalue)
  183. return grid