• Sign Up
• Login
• API
• FAQ
• Tools
• Archive
daily pastebin goal
40%
SHARE
TWEET

# Untitled

a guest Mar 25th, 2019 55 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('type in first coords: ').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'
41.             self.mine_coordinates.add((mine_y, mine_x))
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.
101.         print('   ', '  '.join(map(str, range(1, self.size_x + 1))))
102.         for ind, string in enumerate(self.user_interface, 1):
103.             print(ind, '', ''.join(map(str, string)))
104.         print()
105.
106.     def check(self):
107.         for coord in self.mine_coordinates:
108.             if coord not in self.f_coordinates:
109.                 return False
110.         return True
111.
112.     def play(self):
113.         """PLAY
114.         If style is not stated - open the cell
115.         Also you can put 'flag', 'question'
116.         these are only visual and have no impact on game
117.         If you try to put on the cell with user markings, you erase them
118.         To win you have to put all flags on all mines
119.         Yoy lose stepping on mine
120.         """
121.         x, y, *style = input('type in x, y, style(optional): ').split()
122.         if not style:
123.             style = [None]
124.         x, y, = map(lambda el: int(el) - 1, [x, y])
125.
126.         if style[0] == 'flag':
127.             if self.user_interface[y][x] == '[ ]':
128.                 self.f_coordinates.add((x, y))
129.                 self.user_interface[y][x] = '[F]'
130.                 self.flags -= 1
131.         elif style[0] == 'question':
132.             if self.user_interface[y][x] == '[ ]':
133.                 self.user_interface[y][x] = '[?]'
134.         else:
135.
136.             if self.user_interface[y][x] == '[?]':
137.                 self.user_interface[y][x] = '[ ]'
138.             elif self.user_interface[y][x] == '[F]':
139.                 self.flags += 1
140.                 self.f_coordinates.discard((x, y))
141.                 self.user_interface[y][x] = '[ ]'
142.             else:
143.                 if self.field[y][x] == 'MINE':
144.                     print('You Lost')
145.                     self.open_field()
146.                     self.show_field()
147.                     return
148.                 else:
149.                     self.open_whites_numbers(x, y)
150.
151.         self.show_field()
152.         if self.check():
153.             print('You have won!')
154.             self.open_field()
155.             self.show_field()
156.         self.play()
157.
158.
159. a = Minesweeper()
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