Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- class Minesweeper:
- def __init__(
- self, board_height=8, board_width=8,
- mines_count=10):
- self.board_height = board_height
- self.board_width = board_width
- self.mines_count = mines_count
- self.continue_game = True
- self.mines = []
- self.flags = []
- self.opened_cells = []
- self.place_mines()
- def count_mines(self, x, y):
- count = 0
- count += 1 if (x+1, y) in self.mines else 0
- count += 1 if (x-1, y) in self.mines else 0
- count += 1 if (x, y+1) in self.mines else 0
- count += 1 if (x, y-1) in self.mines else 0
- count += 1 if (x+1, y+1) in self.mines else 0
- count += 1 if (x+1, y-1) in self.mines else 0
- count += 1 if (x-1, y+1) in self.mines else 0
- count += 1 if (x-1, y-1) in self.mines else 0
- return count
- def place_mines(self):
- import random
- i = 0
- while i < self.mines_count:
- coords = (
- random.randint(0, self.board_width-1),
- random.randint(0, self.board_height-1))
- if coords not in self.mines:
- self.mines.append(coords)
- i += 1
- def draw(self, show_mines=False):
- print(' | '+' | '.join([str(x) for x in range(self.board_width)])+' |')
- for y in range(self.board_height):
- print('-'*34)
- print('%d|' % y, end='')
- for x in range(self.board_width):
- symbol = ' '
- if (x, y) in self.opened_cells:
- symbol = str(self.count_mines(x, y))
- if (x, y) in self.flags:
- symbol = 'P'
- if show_mines and (x, y) in self.mines:
- symbol = '@' if (x, y) in self.flags else '*'
- print(' '+symbol, end=' |')
- print()
- print('-'*34)
- def ask_for_turn(self):
- answer = input('Your turn. Use "X,Y" notation for open the cell, or "pX,Y" to set the flag: ')
- if answer.startswith('p'):
- return ('flag', *[int(x) for x in answer[1:].split(',')])
- else:
- return ('open', *[int(x) for x in answer.split(',')])
- def lose(self):
- self.draw(show_mines=True)
- print('You lose!')
- def win(self):
- self.draw(show_mines=True)
- print('You win!')
- def open_cell(self, x, y):
- if 0 <= x < self.board_width and \
- 0 <= y < self.board_width and \
- (x, y) not in self.opened_cells:
- count = self.count_mines(x, y)
- # print(x, y, count)
- self.opened_cells.append((x, y))
- if count == 0:
- self.open_cell(x+1, y)
- self.open_cell(x-1, y)
- self.open_cell(x, y+1)
- self.open_cell(x, y-1)
- self.open_cell(x+1, y+1)
- self.open_cell(x+1, y-1)
- self.open_cell(x-1, y+1)
- self.open_cell(x-1, y-1)
- def play(self):
- print('Hello! Let\'s play now!')
- while self.continue_game:
- self.draw()
- action, x, y = self.ask_for_turn()
- if 0 <= x < self.board_width and \
- 0 <= y < self.board_width:
- if action == 'open' and (x, y) in self.mines:
- self.continue_game = False
- self.lose()
- elif action == 'flag':
- if (x, y) not in self.flags:
- self.flags.append((x, y))
- else:
- print('You already have flag in that cell.')
- elif action == 'open':
- if (x, y) not in self.opened_cells:
- self.open_cell(x, y)
- else:
- print('This cell is already opened!')
- if set(self.flags) == set(self.mines):
- self.continue_game = False
- self.win()
- else:
- print('Incorrect cell coordinates')
- if __name__ == '__main__':
- game = Minesweeper()
- game.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement