Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.81 KB | None | 0 0
  1. """Minesweeper has to swipe the mines."""
  2. import copy
  3.  
  4.  
  5. def prettyprint(arr):
  6. for i in arr:
  7. print(i)
  8.  
  9. def create_minefield(height: int, width: int) -> list:
  10. """
  11. Create and return minefield.
  12.  
  13. Minefield must be height high and width wide. Each position must contain single dot (`.`).
  14. :param height: int
  15. :param width: int
  16. :return: list
  17. """
  18. table = []
  19.  
  20. for i in range(height):
  21. table.append([])
  22. for j in range(width):
  23. table[i].append(".")
  24. return table
  25.  
  26.  
  27. def add_mines(minefield: list, mines: list) -> list:
  28. """
  29. Add mines to a minefield and return minefield.
  30.  
  31. This function cannot modify the original minefield list.
  32. Minefield must be length long and width wide. Each non-mine position must contain single dot.
  33. If a position is empty ("."), then a small mine is added ("x").
  34. If a position contains small mine ("x"), a large mine is added ("X").
  35. Mines are in a list.
  36. Mine is a list. Each mine has 4 integer parameters in the format [N, S, E, W].
  37. - N is the distance between area of mines and top of the minefield.
  38. - S ... area of mines and bottom of the minefield.
  39. - E ... area of mines and right of the minefield.
  40. - W ... area of mines and left of the minefield.
  41. :param minefield: list
  42. :param mines: list
  43. :return: list
  44. """
  45. withmines = copy.deepcopy(minefield)
  46. for mine in mines:
  47. for y in range(mine[0], len(withmines) - mine[1]):
  48. for x in range(mine[3], len(withmines[0]) - mine[2]):
  49. if withmines[y][x] == '.':
  50. withmines[y][x] = 'x'
  51. elif withmines[y][x] == 'x':
  52. withmines[y][x] = 'X'
  53. return withmines
  54.  
  55.  
  56. def get_minefield_string(minefield: list) -> str:
  57. """
  58. Return minefield's string representation.
  59.  
  60. .....
  61. .....
  62. x....
  63. Xx...
  64.  
  65. :param minefield:
  66. :return:
  67. """
  68.  
  69.  
  70. def calculate_mine_count(minefield: list) -> list:
  71. """
  72. For each cell in minefield, calculate how many mines are nearby.
  73.  
  74. This function cannot modify the original list.
  75. So, the result should be a new list (or copy of original).
  76.  
  77. ....
  78. ..x.
  79. X.X.
  80. x..X
  81.  
  82. =>
  83.  
  84. 0111
  85. 13x2
  86. X4X3
  87. x32X
  88.  
  89. :param minefield:
  90. :return:
  91. """
  92. pass
  93.  
  94.  
  95. def walk(minefield, moves, lives) -> list:
  96. """
  97. Make moves on the minefield.
  98.  
  99. This function cannot modify the original minefield list.
  100. Starting position is marked by #.
  101. There is always exactly one # on the field.
  102. The position you start is an empty cell (".").
  103.  
  104. Moves is a list of move "orders":
  105. N - up,
  106. S - down,
  107. E - right,
  108. W - left.
  109.  
  110. Example: "NWWES"
  111.  
  112. If the position you have to move contains "x" (small mine),
  113. then the mine is cleared (position becomes "."),
  114. but you cannot move there.
  115. In case of clearing a small mine, ff the position where the minesweeper is, has 5 or more mines nearby
  116. (see the previous function), minesweeper also loses a life.
  117. If it has 0 lives left, then clearing is not done and moving stops.
  118.  
  119. Example:
  120. #x
  121. ..
  122. moves: ESS
  123.  
  124. =>
  125.  
  126. 1st step ("E"):
  127. #.
  128. ..
  129.  
  130. 2nd step ("S"):
  131. ..
  132. #.
  133.  
  134. 3rd step ("S"):
  135. ..
  136. #.
  137.  
  138. Example #2
  139. XXX
  140. x.x
  141. .#X
  142. moves: NWES, lives = 1
  143.  
  144. 1) "N"
  145. XXX
  146. x#x
  147. ..X
  148.  
  149. 2) "W". the small mine is cleared, but with the cost of one life :'(
  150. XXX
  151. .#x
  152. ..X
  153. lives = 0
  154.  
  155. 3) "E"
  156. XXX
  157. .#x
  158. ..X
  159. As clearing the mine on the right, he would lose a life (because minesweeper has 5 or more mines nearby).
  160. But as he has no lives left, he stops there. No more moves will be carried out.
  161.  
  162. If the position you have to move contains "X" (huge mine),
  163. then you move there and lose a life.
  164.  
  165. #X
  166. ..
  167. moves: ESS
  168.  
  169. 1) (lives = lives - 1)
  170. .#
  171. ..
  172. 2)
  173. ..
  174. .#
  175. 3)
  176. ..
  177. .#
  178.  
  179. If you have to move into a position with a huge mine "X"
  180. but you don't have any more lives, then you finish your moves.
  181.  
  182. lives: 2
  183.  
  184. #XXXX
  185. .....
  186. moves: EEES
  187.  
  188. 1) lives = 1
  189. .#XXX
  190. .....
  191. 2) lives = 0
  192. ..#XX
  193. .....
  194. 3) stop, because you would die
  195. final result:
  196. ..#XX
  197. .....
  198.  
  199. :param minefield:
  200. :param moves:
  201. :param lives:
  202. :return:
  203. """
  204. pass
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. if __name__ == '__main__':
  213. minefield_a = create_minefield(4, 3)
  214. print(minefield_a) # ->
  215. """
  216. [
  217. ['.', '.', '.'],
  218. ['.', '.', '.'],
  219. ['.', '.', '.'],
  220. ['.', '.', '.']
  221. ]
  222. """
  223.  
  224. minefield_a = add_mines(minefield_a, [[0, 3, 2, 0], [2, 1, 0, 1]])
  225. print(minefield_a) # ->
  226. """
  227. [
  228. ['x', '.', '.'],
  229. ['.', '.', '.'],
  230. ['.', 'x', 'x'],
  231. ['.', '.', '.']
  232. ]
  233. """
  234.  
  235. print(get_minefield_string(minefield_a))
  236. minefield_ac = calculate_mine_count(minefield_a)
  237. print(get_minefield_string(minefield_ac))
  238.  
  239. minefield_b = create_minefield(8, 7)
  240. minefield_b = add_mines(minefield_b, [[2, 1, 3, 2], [0, 5, 3, 0]])
  241.  
  242. print(minefield_b) # ->
  243. """
  244. [
  245. ['x', 'x', 'x', 'x', '.', '.', '.'],
  246. ['x', 'x', 'x', 'x', '.', '.', '.'],
  247. ['x', 'x', 'X', 'X', '.', '.', '.'],
  248. ['.', '.', 'x', 'x', '.', '.', '.'],
  249. ['.', '.', 'x', 'x', '.', '.', '.'],
  250. ['.', '.', 'x', 'x', '.', '.', '.'],
  251. ['.', '.', 'x', 'x', '.', '.', '.'],
  252. ['.', '.', '.', '.', '.', '.', '.']
  253. ]
  254. """
  255.  
  256. minefield_c = create_minefield(5, 5)
  257. minefield_c = add_mines(minefield_c, [[0, 0, 2, 2]])
  258. print(minefield_c) # ->
  259. """
  260. [
  261. ['.', '.', 'x', '.', '.'],
  262. ['.', '.', 'x', '.', '.'],
  263. ['.', '.', 'x', '.', '.'],
  264. ['.', '.', 'x', '.', '.'],
  265. ['.', '.', 'x', '.', '.']
  266. ]
  267. """
  268.  
  269. mf = [['.', '.', '.', '.'], ['.', '.', 'x', '.'], ['X', '.', 'X', '.'], ['x', '.', '.', 'X']]
  270. print(calculate_mine_count(mf))
  271.  
  272. """
  273. [
  274. ['0', '1', '1', '1'],
  275. ['1', '3', 'x', '2'],
  276. ['X', '4', 'X', '3'],
  277. ['x', '3', '2', 'X']
  278. ]
  279. """
  280.  
  281. mf = copy.deepcopy(minefield_c)
  282. # mf[0][0] = '#'
  283. print(get_minefield_string(walk(mf, "WEESE", 2)))
  284. """
  285. .....
  286. .#...
  287. ..x..
  288. ..x..
  289. ..x..
  290. """
  291.  
  292. mf = create_minefield(3, 5)
  293. mf = add_mines(mf, [[0, 0, 1, 2]])
  294. mf = add_mines(mf, [[0, 1, 1, 1]])
  295. print(get_minefield_string(mf))
  296. """
  297. .xXX.
  298. .xXX.
  299. ..xx.
  300. """
  301. # mf[0][4] = "#"
  302. mf = walk(mf, "WSSWN", 2)
  303. print(get_minefield_string(mf))
  304. """
  305. .xX..
  306. .xX#.
  307. ..x..
  308. """
  309. # minesweeper would die if stepping into the mine, therefore he stops
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement