• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Oct 24th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. """Minesweeper has to swipe the mines."""
2. from copy import deepcopy
3.
4.
5. def create_minefield(height: int, width: int) -> list:
6.     """Create and return minefield."""
7.     return [["." for _ in range(width)] for _ in range(height)]
8.
9.
10. def add_mines(minefield: list, mines: list) -> list:
11.     """
12.     Add mines to a minefield and return minefield.
13.
14.     Minefield must be length long and width wide. Each non-mine position must contain single dot.
15.     If a position is empty ("."), then a small mine is added ("x").
16.     If a position contains small mine ("x"), a large mine is added ("X").
17.     Mines are in a list.
18.     Mine is a list. Each mine has 4 integer parameters in the format [N, S, E, W].
19.         - N is the distance between area of mines and top of the minefield.
20.         - S ... area of mines and bottom of the minefield.
21.         - E ... area of mines and right of the minefield.
22.         - W ... area of mines and left of the minefield.
23.     :param new_minefield: list
24.     :param mines: list
25.     :return: list
26.     """
27.     new_minefield = deepcopy(minefield)
28.     for mine in mines:
29.         north_position = mine
30.         west_position = mine
31.         south_position = len(new_minefield) - mine - 1
32.         east_position = len(new_minefield[mine]) - 1 - mine
33.         for x in range(west_position, east_position + 1):
34.             for y in range(north_position, south_position + 1):
35.                 if new_minefield[y][x] == ".":
36.                     new_minefield[y][x] = "x"
37.                 elif new_minefield[y][x] == "x":
38.                     new_minefield[y][x] = "X"
39.     return new_minefield
40.
41.
42. def get_minefield_string(minefield: list) -> str:
43.     """Return minefield's string representation."""
44.     return "\n".join(["".join(element) for element in minefield])
45.
46.
47. def calculate_mine_count(minefield: list) -> list:
48.     """For each cell in minefield, calculate how many mines are nearby."""
49.     new_minefield = deepcopy(minefield)
50.     for row in range(len(new_minefield)):
51.         for column in range(len(new_minefield[row])):
52.             if new_minefield[row][column] == "x" or new_minefield[row][column] == "X":
53.                 continue
54.             else:
55.                 new_minefield[row][column] = count_mines(new_minefield, row, column)
56.     return new_minefield
57.
58.
59. def count_mines(new_minefield, row, column, small=False):
60.     """Count mines."""
61.     count = 0
62.     for i in range(-1, 2):
63.         for j in range(-1, 2):
64.             if len(new_minefield) > row + i >= 0 and len(new_minefield[row + i]) > column + j >= 0:
65.                 if i == j == 0:
66.                     continue
67.                 if not small:
68.                     if new_minefield[row + i][column + j] == "x" or new_minefield[row + i][column + j] == "X":
69.                         count += 1
70.                 elif small:
71.                     if new_minefield[row + i][column + j] == "x":
72.                         count += 1
73.     return str(count)
74.
75.
76. def walk(minefield, moves, lives) -> list:
77.     """Make moves on the minefield."""
78.     new_minefield = deepcopy(minefield)
79.     current_row = 0
80.     current_column = 0
81.     for x, row in enumerate(new_minefield):
82.         for y, element in enumerate(row):
83.             if element == "#":
84.                 current_row = x
85.                 current_column = y
86.                 new_minefield[current_row][current_column] = "."
87.
88.     for move in moves:
89.         if move == "N" and current_row != 0:
90.             new_row = current_row - 1
91.             new_column = current_column
92.             if new_minefield[new_row][new_column] == "x":
93.                 if int(count_mines(new_minefield, new_row, new_column, small=True)) > 4:
94.                     if lives == 0:
95.                         break
96.                     else:
97.                         lives -= 1
98.                         new_minefield[new_row][new_column] = "."
99.                 else:
100.                     new_minefield[new_row][new_column] = "."
101.             if new_minefield[new_row][new_column] == "X":
102.                 if lives == 0:
103.                     break
104.                 else:
105.                     lives -= 1
106.                     current_row = new_row
107.                     current_column = new_column
108.         if move == "E" and current_column > len(new_minefield[current_row]):
109.             new_row = current_row
110.             new_column = current_column + 1
111.             if new_minefield[new_row][new_column] == "x":
112.                 if int(count_mines(new_minefield, new_row, new_column, small=True)) > 4:
113.                     if lives == 0:
114.                         break
115.                     else:
116.                         lives -= 1
117.                         new_minefield[new_row][new_column] = "."
118.                 else:
119.                     new_minefield[new_row][new_column] = "."
120.             if new_minefield[new_row][new_column] == "X":
121.                 if lives == 0:
122.                     break
123.                 else:
124.                     lives -= 1
125.                     current_row = new_row
126.                     current_column = new_column
127.         if move == "S" and current_row < len(new_minefield):
128.             new_row = current_row + 1
129.             new_column = current_column
130.             if new_minefield[new_row][new_column] == "x":
131.                 if int(count_mines(new_minefield, new_row, new_column, small=True)) > 4:
132.                     if lives == 0:
133.                         break
134.                     else:
135.                         lives -= 1
136.                         new_minefield[new_row][new_column] = "."
137.                 else:
138.                     new_minefield[new_row][new_column] = "."
139.             if new_minefield[new_row][new_column] == "X":
140.                 if lives == 0:
141.                     break
142.                 else:
143.                     lives -= 1
144.                     current_row = new_row
145.                     current_column = new_column
146.         if move == "W" and current_column != 0:
147.             new_row = current_row
148.             new_column = current_column - 1
149.             if new_minefield[new_row][new_column] == "x":
150.                 if int(count_mines(new_minefield, new_row, new_column, small=True)) > 4:
151.                     if lives == 0:
152.                         break
153.                     else:
154.                         lives -= 1
155.                         new_minefield[new_row][new_column] = "."
156.                 else:
157.                     new_minefield[new_row][new_column] = "."
158.             if new_minefield[new_row][new_column] == "X":
159.                 if lives == 0:
160.                     break
161.                 else:
162.                     lives -= 1
163.                     current_row = new_row
164.                     current_column = new_column
165.
166.     new_minefield[current_row][current_column] = "#"
167.     return new_minefield
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top