From d2a88dbd4125c68fbc655367a5fd0c5b0568d9c0 Mon Sep 17 00:00:00 2001 From: SergBobrovsky Date: Sat, 3 Jun 2023 13:55:59 +0300 Subject: [PATCH] Update generators.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `# Do not place a bomb on another bomb or on predefined place` обещаю, не буду ) --- bot/minesweeper/generators.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/bot/minesweeper/generators.py b/bot/minesweeper/generators.py index 609cedd..cbeca2f 100644 --- a/bot/minesweeper/generators.py +++ b/bot/minesweeper/generators.py @@ -1,5 +1,5 @@ from itertools import product -from random import randint +from random import sample from typing import List, Tuple @@ -36,19 +36,11 @@ def generate_custom(size: int, bombs: int, predefined: Tuple[int, int]) -> List[ :return: an array of arrays of cells. """ field = generate_square_field(size) - current_count = 0 - - while current_count < bombs: - x = randint(0, size-1) - y = randint(0, size-1) - - # Do not place a bomb on another bomb or on predefined place - if (x == predefined[0] and y == predefined[1]) or field[x][y] == "*": - continue + freeCells = list(product(range(size), repeat=2)) + freeCells.remove(predefined) + for x, y in sample(freeCells, bombs): field[x][y] = "*" - neighbours = __find_neighbours(x, y, size) - for nx, ny in neighbours: + for nx, ny in __find_neighbours(x, y, size): if field[nx][ny] != "*": field[nx][ny] += 1 - current_count += 1 return field