Advertisement
oleh_korkh

Untitled

Jan 20th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3.  
  4. class Minesweeper:
  5.  
  6.     def __init__(
  7.             self, board_height=8, board_width=8,
  8.             mines_count=10):
  9.         self.board_height = board_height
  10.         self.board_width = board_width
  11.         self.mines_count = mines_count
  12.         self.continue_game = True
  13.         self.mines = []
  14.         self.flags = []
  15.         self.opened_cells = []
  16.         self.place_mines()
  17.  
  18.     def count_mines(self, x, y):
  19.         count = 0
  20.         count += 1 if (x+1, y) in self.mines else 0
  21.         count += 1 if (x-1, y) in self.mines else 0
  22.         count += 1 if (x, y+1) in self.mines else 0
  23.         count += 1 if (x, y-1) in self.mines else 0
  24.         count += 1 if (x+1, y+1) in self.mines else 0
  25.         count += 1 if (x+1, y-1) in self.mines else 0
  26.         count += 1 if (x-1, y+1) in self.mines else 0
  27.         count += 1 if (x-1, y-1) in self.mines else 0
  28.         return count
  29.  
  30.     def place_mines(self):
  31.         import random
  32.         i = 0
  33.         while i < self.mines_count:
  34.             coords = (
  35.                 random.randint(0, self.board_width-1),
  36.                 random.randint(0, self.board_height-1))
  37.             if coords not in self.mines:
  38.                 self.mines.append(coords)
  39.                 i += 1
  40.  
  41.     def draw(self, show_mines=False):
  42.         print(' | '+' | '.join([str(x) for x in range(self.board_width)])+' |')
  43.         for y in range(self.board_height):
  44.             print('-'*34)
  45.             print('%d|' % y, end='')
  46.             for x in range(self.board_width):
  47.                 symbol = ' '
  48.                 if (x, y) in self.opened_cells:
  49.                     symbol = str(self.count_mines(x, y))
  50.                 if (x, y) in self.flags:
  51.                     symbol = 'P'
  52.                 if show_mines and (x, y) in self.mines:
  53.                     symbol = '@' if (x, y) in self.flags else '*'
  54.                 print(' '+symbol, end=' |')
  55.             print()
  56.         print('-'*34)
  57.  
  58.     def ask_for_turn(self):
  59.         answer = input('Your turn. Use "X,Y" notation for open the cell, or "pX,Y" to set the flag: ')
  60.         if answer.startswith('p'):
  61.             return ('flag', *[int(x) for x in answer[1:].split(',')])
  62.         else:
  63.             return ('open', *[int(x) for x in answer.split(',')])
  64.  
  65.     def lose(self):
  66.         self.draw(show_mines=True)
  67.         print('You lose!')
  68.  
  69.     def win(self):
  70.         self.draw(show_mines=True)
  71.         print('You win!')
  72.  
  73.     def open_cell(self, x, y):
  74.         if 0 <= x < self.board_width and \
  75.                 0 <= y < self.board_width and \
  76.                 (x, y) not in self.opened_cells:
  77.             count = self.count_mines(x, y)
  78.             # print(x, y, count)
  79.             self.opened_cells.append((x, y))
  80.             if count == 0:
  81.                 self.open_cell(x+1, y)
  82.                 self.open_cell(x-1, y)
  83.                 self.open_cell(x, y+1)
  84.                 self.open_cell(x, y-1)
  85.                 self.open_cell(x+1, y+1)
  86.                 self.open_cell(x+1, y-1)
  87.                 self.open_cell(x-1, y+1)
  88.                 self.open_cell(x-1, y-1)
  89.  
  90.     def play(self):
  91.         print('Hello! Let\'s play now!')
  92.         while self.continue_game:
  93.             self.draw()
  94.             action, x, y = self.ask_for_turn()
  95.             if 0 <= x < self.board_width and \
  96.                     0 <= y < self.board_width:
  97.                 if action == 'open' and (x, y) in self.mines:
  98.                     self.continue_game = False
  99.                     self.lose()
  100.                 elif action == 'flag':
  101.                     if (x, y) not in self.flags:
  102.                         self.flags.append((x, y))
  103.                     else:
  104.                         print('You already have flag in that cell.')
  105.                 elif action == 'open':
  106.                     if (x, y) not in self.opened_cells:
  107.                         self.open_cell(x, y)
  108.                     else:
  109.                         print('This cell is already opened!')
  110.                 if set(self.flags) == set(self.mines):
  111.                     self.continue_game = False
  112.                     self.win()
  113.             else:
  114.                 print('Incorrect cell coordinates')
  115.  
  116. if __name__ == '__main__':
  117.     game = Minesweeper()
  118.     game.play()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement