Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Minesweeper:
- def __init__(self, difficulty='Beginner'):
- from random import randint
- print('Your difficulty is', end=' ')
- if difficulty == 'Intermediate':
- self.size_x = 18
- self.size_y = 14
- self.mines = 40
- print(difficulty)
- elif difficulty == 'Expert':
- self.size_x = 24
- self.size_y = 20
- self.mines = 99
- print(difficulty)
- else:
- self.size_x = 10
- self.size_y = 8
- self.mines = 10
- print('Beginner')
- self.user_interface = [['[ ]' for _ in range(self.size_x)] for _ in range(self.size_y)]
- self.flags = self.mines
- self.show_field()
- x, y = map(lambda el: int(el) - 1, input('type in first coords: ').split())
- self.user_interface[y][x] = ' '
- self.field = [[None for _ in range(self.size_x)] for _ in range(self.size_y)]
- self.mine_coordinates = set()
- self.f_coordinates = set()
- while self.mines:
- mine_x, mine_y = randint(0, self.size_x - 1), randint(0, self.size_y - 1)
- if mine_x in range(x - 1, x + 2) or mine_y in range(y - 1, y + 2) \
- or self.field[mine_y][mine_x] is not None:
- continue
- self.field[mine_y][mine_x] = 'MINE'
- self.mine_coordinates.add((mine_y, mine_x))
- self.mines -= 1
- self.numbers()
- self.open_whites_numbers(x, y)
- self.show_field()
- self.play()
- def open_field(self):
- for j in range(self.size_y):
- for i in range(self.size_x):
- if self.field[j][i] == 'MINE':
- self.user_interface[j][i] = '[*]'
- def numbers(self):
- for j in range(self.size_y):
- for i in range(self.size_x):
- if self.field[j][i] == 'MINE':
- continue
- count = 0
- for (x, y) in (
- (1, 0), (1, -1), (1, 1), (0, 1), (0, -1), (-1, -1), (-1, 0), (-1, 1)
- ):
- if j + y not in range(self.size_y) or x + i not in range(self.size_x):
- continue
- if self.field[j + y][i + x] == 'MINE':
- count += 1
- self.field[j][i] = count
- def white_step(self, row, column):
- self.field[row][column] = '0'
- coords = [(row, column)]
- for (x, y) in (
- (1, 0), (-1, 0), (0, 1), (0, -1)
- ):
- X, Y = column + x, row + y
- if X in range(self.size_x) and Y in range(self.size_y):
- if self.field[Y][X] == 0:
- coords.extend(self.white_step(Y, X))
- return coords
- def open_whites_numbers(self, x_, y_):
- for (row, column) in self.white_step(y_, x_):
- self.user_interface[row][column] = ' _ '
- for (x, y) in (
- (1, 0), (1, -1), (1, 1), (0, 1), (0, -1), (-1, -1), (-1, 0), (-1, 1)
- ):
- X, Y = column + x, row + y
- if X in range(self.size_x) and Y in range(self.size_y):
- if isinstance(self.field[Y][X], int):
- self.user_interface[Y][X] = ' {} '.format(self.field[Y][X])
- def show_field(self):
- print('-' * (self.size_x * 3))
- print('Your flag count is', self.flags)
- print(' ', ' '.join(map(str, range(1, self.size_x + 1))))
- for ind, string in enumerate(self.user_interface, 1):
- print(ind, '', ''.join(map(str, string)))
- print()
- def check(self):
- for coord in self.mine_coordinates:
- if coord not in self.f_coordinates:
- return False
- return True
- def play(self):
- """PLAY
- If style is not stated - open the cell
- Also you can put 'flag', 'question'
- these are only visual and have no impact on game
- If you try to put on the cell with user markings, you erase them
- To win you have to put all flags on all mines
- Yoy lose stepping on mine
- """
- x, y, *style = input('type in x, y, style(optional): ').split()
- if not style:
- style = [None]
- x, y, = map(lambda el: int(el) - 1, [x, y])
- if style[0] == 'flag':
- if self.user_interface[y][x] == '[ ]':
- self.f_coordinates.add((x, y))
- self.user_interface[y][x] = '[F]'
- self.flags -= 1
- elif style[0] == 'question':
- if self.user_interface[y][x] == '[ ]':
- self.user_interface[y][x] = '[?]'
- else:
- if self.user_interface[y][x] == '[?]':
- self.user_interface[y][x] = '[ ]'
- elif self.user_interface[y][x] == '[F]':
- self.flags += 1
- self.f_coordinates.discard((x, y))
- self.user_interface[y][x] = '[ ]'
- else:
- if self.field[y][x] == 'MINE':
- print('You Lost')
- self.open_field()
- self.show_field()
- return
- else:
- self.open_whites_numbers(x, y)
- self.show_field()
- if self.check():
- print('You have won!')
- self.open_field()
- self.show_field()
- self.play()
- a = Minesweeper()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement