Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Гра Сапер
- import random
- DELTAS = [(0, 1), (0, -1), (1, 0),
- (1, -1), (-1, 1), (-1, 0),
- (1, 1), (-1, -1)]
- width = 5
- height = 10
- mines_count = 2
- mines = []
- field = [[" -- "] * width for _ in range(height)]
- for i in range(mines_count):
- empty = [(x, y) for x in range(width)
- for y in range(height)
- if (x, y) not in mines]
- x, y = random.choice(empty)
- mines.append((x, y))
- def mines_around(coord):
- x, y = coord
- return sum((dx + x, dy + y) in mines
- for dx, dy in DELTAS)
- while True:
- print(*field, sep="\n")
- coords = input("x, y")
- try:
- x, y = map(int, coords.split(","))
- _ = field[y][x]
- except Exception:
- print(f"Incorrect: '{coords}', try again")
- continue
- if (x, y) in mines:
- print("You lost")
- for m in mines:
- field[m[1]][m[0]] = " ** "
- field[y][x] = " XX "
- break
- to_visit = [(x, y)]
- while to_visit:
- x, y = to_visit.pop()
- field[y][x] = f" {mines_around((x, y)):02} "
- if field[y][x] == " 00 ":
- neighbours = [(dx + x, dy + y) for dx, dy in DELTAS
- if x + dx in range(width)
- and y + dy in range(height)
- and field[dy + y][dx + x] == " -- "]
- to_visit.extend(neighbours)
- empty_cells = [(x, y) for x in range(width)
- for y in range(height)
- if field[y][x] == " -- "
- and (x, y) not in mines]
- if not empty_cells:
- print("You win")
- break
- print(*field, sep="\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement