-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.py
48 lines (36 loc) · 1013 Bytes
/
generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from solver import solve
import random
import solution_counter
def random_sequence():
arr = [x for x in range(1, 10)]
random.shuffle(arr)
return arr
def prepare_board():
board = [[0 for x in range(9)] for y in range(9)]
first_row = random_sequence()
board[0] = first_row
return board
def get_filled_board():
sequence = random_sequence()
board = prepare_board()
solve(board, sequence)
return board
def generate(difficulty):
board = get_filled_board()
i = 0
while True:
if i == difficulty:
return board
posX = random.randrange(0, 9)
posY = random.randrange(0, 9)
if board[posY][posX] == 0:
continue
current = board[posY][posX]
board[posY][posX] = 0
solution_counter.solutions = 0
solution_counter.solve(board)
solutions = solution_counter.solutions
if solutions > 1:
board[posY][posX] = current
continue
i += 1