# Untitled

a guest
Mar 25th, 2019
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Minesweeper:
2. def __init__(self, difficulty='Beginner'):
3. from random import randint
4.
5. print('Your difficulty is', end=' ')
6. if difficulty == 'Intermediate':
7. self.size_x = 18
8. self.size_y = 14
9. self.mines = 40
10. print(difficulty)
11. elif difficulty == 'Expert':
12. self.size_x = 24
13. self.size_y = 20
14. self.mines = 99
15. print(difficulty)
16. else:
17. self.size_x = 10
18. self.size_y = 8
19. self.mines = 10
20. print('Beginner')
21.
22. self.user_interface = [['[ ]' for _ in range(self.size_x)] for _ in range(self.size_y)]
23. self.flags = self.mines
24. self.show_field()
25.
26. x, y = map(lambda el: int(el) -1, input().split())
27.
28. self.user_interface[y][x] = ' '
29. self.field = [[None for _ in range(self.size_x)] for _ in range(self.size_y)]
30.
31. self.mine_coordinates = set()
32. self.f_coordinates = set()
33.
34. while self.mines:
35. mine_x, mine_y = randint(0, self.size_x - 1), randint(0, self.size_y - 1)
36. if mine_x in range(x - 1, x + 2) or mine_y in range(y - 1, y + 2) \
37. or self.field[mine_y][mine_x] is not None:
38. continue
39.
40. self.field[mine_y][mine_x] = 'MINE'
42. self.mines -= 1
43.
44. self.numbers()
45. self.open_whites_numbers(x, y)
46. self.show_field()
47. self.play()
48.
49. def open_field(self):
50. for j in range(self.size_y):
51. for i in range(self.size_x):
52. if self.field[j][i] == 'MINE':
53. self.user_interface[j][i] = '[*]'
54.
55. def numbers(self):
56. for j in range(self.size_y):
57. for i in range(self.size_x):
58. if self.field[j][i] == 'MINE':
59. continue
60.
61. count = 0
62. for (x, y) in (
63. (1, 0), (1, -1), (1, 1), (0, 1), (0, -1), (-1, -1), (-1, 0), (-1, 1)
64. ):
65. if j + y not in range(self.size_y) or x + i not in range(self.size_x):
66. continue
67. if self.field[j + y][i + x] == 'MINE':
68. count += 1
69.
70. self.field[j][i] = count
71.
72. def white_step(self, row, column):
73. self.field[row][column] = '0'
74. coords = [(row, column)]
75.
76. for (x, y) in (
77. (1, 0), (-1, 0), (0, 1), (0, -1)
78. ):
79. X, Y = column + x, row + y
80. if X in range(self.size_x) and Y in range(self.size_y):
81. if self.field[Y][X] == 0:
82. coords.extend(self.white_step(Y, X))
83.
84. return coords
85.
86. def open_whites_numbers(self, x_, y_):
87. for (row, column) in self.white_step(y_, x_):
88. self.user_interface[row][column] = ' '
89. for (x, y) in (
90. (1, 0), (1, -1), (1, 1), (0, 1), (0, -1), (-1, -1), (-1, 0), (-1, 1)
91. ):
92. X, Y = column + x, row + y
93. if X in range(self.size_x) and Y in range(self.size_y):
94. if isinstance(self.field[Y][X], int):
95. self.user_interface[Y][X] = ' {} '.format(self.field[Y][X])
96.
97. def show_field(self):
98. print('-' * (self.size_x * 3))
99. print('Your flag count is', self.flags)
100. for string in self.user_interface:
101. print(''.join(map(str, string)))
102.
103. def check(self):
104. if not self.mine_coordinates.difference_update(self.f_coordinates):
105. return True
106. return False
107.
108. def play(self):
109. """PLAY
110. If style is not stated - open the cell
111. Also you can put 'flag', 'question'
112. these are only visual and have no impact on game
113. If you try to put on the cell with user markings, you erase them
114. To win you have to put all flags on all mines
115. Yoy lose stepping on mine
116. """
117. x, y, *style = input().split()
118. if not style:
119. style = [None]
120. x, y, = map(lambda el: int(el) - 1, [x, y])
121.
122. if style[0] == 'flag':
123. if self.user_interface[y][x] == '[ ]':
125. self.user_interface[y][x] = '[F]'
126. self.flags -= 1
127. elif style[0] == 'question':
128. if self.user_interface[y][x] == '[ ]':
129. self.user_interface[y][x] = '[?]'
130. else:
131.
132. if self.user_interface[y][x] == '[?]':
133. self.user_interface[y][x] = '[ ]'
134. elif self.user_interface[y][x] == '[F]':
135. self.flags += 1
137. self.user_interface[y][x] = '[ ]'
138. else:
139. if self.field[y][x] == 'MINE':
140. print('You Lost')
141. self.open_field()
142. self.show_field()
143. return
144. else:
145. self.open_whites_numbers(x, y)
146.
147. self.show_field()
148. if self.check():
149. print('You have won!')
150. self.open_field()
151. self.show_field()
152. self.play()
RAW Paste Data