Advertisement
LevMukoseev

Сапер

Oct 31st, 2020
1,969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. import random
  2. from random import randint
  3. import enum
  4.  
  5.  
  6. class Cell(enum.Enum):
  7.     close_mine = 1
  8.     open = 2
  9.     flag = 3
  10.     bombed = 4
  11.     close_empty = 5
  12.  
  13.  
  14. class Field:
  15.     def __init__(self, weight: int, height: int, mines_count: int, block_skin='0', mine_skin='9'):
  16.         if mines_count > weight * height:
  17.             raise Exception("Слишком много мин")
  18.         self.mine_skin = mine_skin
  19.         self.block_skin = block_skin
  20.         self.mines_count = mines_count
  21.         self.height = height
  22.         self.weight = weight
  23.         self.mines = set()
  24.         self.field = self.generate()
  25.  
  26.     def generate(self):
  27.         field = [[self.block_skin] * self.weight for _ in range(self.height)]
  28.         while len(self.mines) < self.mines_count:
  29.             x = randint(0, self.weight - 1)
  30.             y = randint(0, self.height - 1)
  31.             if (x, y) not in self.mines:
  32.                 field[y][x] = self.mine_skin
  33.                 self.mines.add((x, y))
  34.         return field
  35.  
  36.     def get_neighbours(self, x, y):
  37.         count = 0
  38.         for dx in range(-1, 2):
  39.             for dy in range(-1, 2):
  40.                 if dx == dy == 0:
  41.                     continue
  42.                 if (x + dx, y + dy) in self.mines:
  43.                     count += 1
  44.         return count
  45.  
  46.     def check_mines(self, x, y):
  47.         pass
  48.  
  49.     def is_finished(self):
  50.         pass
  51.  
  52.     def __str__(self):
  53.         return "\n".join([" ".join(line) for line in self.field][::-1])
  54.  
  55.  
  56. class Game:
  57.     def __init__(self):
  58.         pass
  59.  
  60.     def make_turn(self, x, y):
  61.         pass
  62.  
  63.     def open_cell(self, x, y):
  64.         pass
  65.  
  66.  
  67. if __name__ == "__main__":
  68.     field = Field(5, 5, 5)
  69.     print(field)
  70.  
  71.  
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement