Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 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. out = []
  69. for line in minefield:
  70. newline = "".join(line)
  71. out += newline + 'n'
  72. return out
  73.  
  74.  
  75. def calculate_mine_count(minefield: list) -> list:
  76. """
  77. For each cell in minefield, calculate how many mines are nearby.
  78.  
  79. This function cannot modify the original list.
  80. So, the result should be a new list (or copy of original).
  81.  
  82. ....
  83. ..x.
  84. X.X.
  85. x..X
  86.  
  87. =>
  88.  
  89. 0111
  90. 13x2
  91. X4X3
  92. x32X
  93.  
  94. :param minefield:
  95. :return:
  96. """
  97. pass
  98.  
  99.  
  100. def walk(minefield, moves, lives) -> list:
  101. """
  102. Make moves on the minefield.
  103.  
  104. This function cannot modify the original minefield list.
  105. Starting position is marked by #.
  106. There is always exactly one # on the field.
  107. The position you start is an empty cell (".").
  108.  
  109. Moves is a list of move "orders":
  110. N - up,
  111. S - down,
  112. E - right,
  113. W - left.
  114.  
  115. Example: "NWWES"
  116.  
  117. If the position you have to move contains "x" (small mine),
  118. then the mine is cleared (position becomes "."),
  119. but you cannot move there.
  120. In case of clearing a small mine, ff the position where the minesweeper is, has 5 or more mines nearby
  121. (see the previous function), minesweeper also loses a life.
  122. If it has 0 lives left, then clearing is not done and moving stops.
  123.  
  124. Example:
  125. #x
  126. ..
  127. moves: ESS
  128.  
  129. =>
  130.  
  131. 1st step ("E"):
  132. #.
  133. ..
  134.  
  135. 2nd step ("S"):
  136. ..
  137. #.
  138.  
  139. 3rd step ("S"):
  140. ..
  141. #.
  142.  
  143. Example #2
  144. XXX
  145. x.x
  146. .#X
  147. moves: NWES, lives = 1
  148.  
  149. 1) "N"
  150. XXX
  151. x#x
  152. ..X
  153.  
  154. 2) "W". the small mine is cleared, but with the cost of one life :'(
  155. XXX
  156. .#x
  157. ..X
  158. lives = 0
  159.  
  160. 3) "E"
  161. XXX
  162. .#x
  163. ..X
  164. As clearing the mine on the right, he would lose a life (because minesweeper has 5 or more mines nearby).
  165. But as he has no lives left, he stops there. No more moves will be carried out.
  166.  
  167. If the position you have to move contains "X" (huge mine),
  168. then you move there and lose a life.
  169.  
  170. #X
  171. ..
  172. moves: ESS
  173.  
  174. 1) (lives = lives - 1)
  175. .#
  176. ..
  177. 2)
  178. ..
  179. .#
  180. 3)
  181. ..
  182. .#
  183.  
  184. If you have to move into a position with a huge mine "X"
  185. but you don't have any more lives, then you finish your moves.
  186.  
  187. lives: 2
  188.  
  189. #XXXX
  190. .....
  191. moves: EEES
  192.  
  193. 1) lives = 1
  194. .#XXX
  195. .....
  196. 2) lives = 0
  197. ..#XX
  198. .....
  199. 3) stop, because you would die
  200. final result:
  201. ..#XX
  202. .....
  203.  
  204. :param minefield:
  205. :param moves:
  206. :param lives:
  207. :return:
  208. """
  209. pass
  210.  
  211.  
  212.  
  213. if __name__ == '__main__':
  214. minefield_a = create_minefield(4, 3)
  215. prettyprint(minefield_a) # ->
  216. """
  217. [
  218. ['.', '.', '.'],
  219. ['.', '.', '.'],
  220. ['.', '.', '.'],
  221. ['.', '.', '.']
  222. ]
  223. """
  224.  
  225. minefield_a = add_mines(minefield_a, [[0, 3, 2, 0], [2, 1, 0, 1]])
  226. prettyprint(minefield_a) # ->
  227. """
  228. [
  229. ['x', '.', '.'],
  230. ['.', '.', '.'],
  231. ['.', 'x', 'x'],
  232. ['.', '.', '.']
  233. ]
  234. """
  235.  
  236. print(get_minefield_string(minefield_a))
  237. minefield_ac = calculate_mine_count(minefield_a)
  238. print(get_minefield_string(minefield_ac))
  239.  
  240. minefield_b = create_minefield(8, 7)
  241. minefield_b = add_mines(minefield_b, [[2, 1, 3, 2], [0, 5, 3, 0]])
  242.  
  243. print(minefield_b) # ->
  244. """
  245. [
  246. ['x', 'x', 'x', 'x', '.', '.', '.'],
  247. ['x', 'x', 'x', 'x', '.', '.', '.'],
  248. ['x', 'x', 'X', 'X', '.', '.', '.'],
  249. ['.', '.', 'x', 'x', '.', '.', '.'],
  250. ['.', '.', 'x', 'x', '.', '.', '.'],
  251. ['.', '.', 'x', 'x', '.', '.', '.'],
  252. ['.', '.', 'x', 'x', '.', '.', '.'],
  253. ['.', '.', '.', '.', '.', '.', '.']
  254. ]
  255. """
  256.  
  257. minefield_c = create_minefield(5, 5)
  258. minefield_c = add_mines(minefield_c, [[0, 0, 2, 2]])
  259. print(minefield_c) # ->
  260. """
  261. [
  262. ['.', '.', 'x', '.', '.'],
  263. ['.', '.', 'x', '.', '.'],
  264. ['.', '.', 'x', '.', '.'],
  265. ['.', '.', 'x', '.', '.'],
  266. ['.', '.', 'x', '.', '.']
  267. ]
  268. """
  269.  
  270. mf = [['.', '.', '.', '.'], ['.', '.', 'x', '.'], ['X', '.', 'X', '.'], ['x', '.', '.', 'X']]
  271. print(calculate_mine_count(mf))
  272.  
  273. """
  274. [
  275. ['0', '1', '1', '1'],
  276. ['1', '3', 'x', '2'],
  277. ['X', '4', 'X', '3'],
  278. ['x', '3', '2', 'X']
  279. ]
  280. """
  281.  
  282. mf = copy.deepcopy(minefield_c)
  283. # mf[0][0] = '#'
  284. print(get_minefield_string(walk(mf, "WEESE", 2)))
  285. """
  286. .....
  287. .#...
  288. ..x..
  289. ..x..
  290. ..x..
  291. """
  292.  
  293. mf = create_minefield(3, 5)
  294. mf = add_mines(mf, [[0, 0, 1, 2]])
  295. mf = add_mines(mf, [[0, 1, 1, 1]])
  296. print(get_minefield_string(mf))
  297. """
  298. .xXX.
  299. .xXX.
  300. ..xx.
  301. """
  302. # mf[0][4] = "#"
  303. mf = walk(mf, "WSSWN", 2)
  304. print(get_minefield_string(mf))
  305. """
  306. .xX..
  307. .xX#.
  308. ..x..
  309. """
  310. # minesweeper would die if stepping into the mine, therefore he stops
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement