Add sequence generator

master
En Yi 2018-07-11 18:56:14 +08:00
parent 1b43486560
commit 6137158ea8
1 changed files with 65 additions and 22 deletions

View File

@ -5,6 +5,9 @@ Credits for Generator: http://zhangroup.aporc.org/images/files/Paper_3485.pdf
""" """
import random import random
import re
filledcell = re.compile('(?!0)')
def cross(array1, array2): def cross(array1, array2):
"""Cross product of elements in A and elements in B.""" """Cross product of elements in A and elements in B."""
@ -138,35 +141,75 @@ def generate_completed_grid():
return grid return grid
def dig_holes(board, difficulty): def generate_dig_sequence(difficulty):
# Empty out some cells in a completed board
pass
# TODO: Determine the number of givens and lower bound of given # TODO: Determine the number of givens and lower bound of given
if difficulty <= 1:
# TODO: Determine the sequence of digging random_number = list(range(81))
while len(random_number) > 0:
# TODO: Set all cells as "Can be Dug" print(len(random_number))
yield random_number.pop(random.randint(0, len(random_number)-1))
# TODO: Is there a cell that can be dug? elif difficulty == 2:
current = 0
# TODO: Select the next cell that "can be dug"and Yield unique solution? while current < 81:
row = int(current / 9)
# TODO: Propagate and Output if not row % 2:
yield current
else:
yield (row+1) * 9 - 1 - (current % 9)
current += 2
elif difficulty == 3:
current = 0
while current < 81:
row = int(current / 9)
if not row % 2:
yield current
else:
yield (row+1) * 9 - 1 - (current % 9)
current += 1
elif difficulty == 4:
current = 0
while current < 81:
yield current
current += 1
def generate_sudoku_puzzle(difficulty): def generate_sudoku_puzzle(difficulty):
grid = generate_completed_grid() grid = generate_completed_grid()
puzzle = dig_holes(board, difficulty) if difficulty == 0:
n_givens = random.randint(50, 60)
lower_bound = 5
elif difficulty == 1:
n_givens = random.randint(36, 49)
lower_bound = 4
elif difficulty == 2:
n_givens = random.randint(32, 35)
lower_bound = 3
elif difficulty == 3:
n_givens = random.randint(28, 31)
lower_bound = 2
elif difficulty == 4:
n_givens = random.randint(22, 27)
lower_bound = 0
return puzzle dig_sequence = generate_dig_sequence(difficulty)
holes = 0
while holes<81-n_givens:
try:
i = next(dig_sequence)
except StopIteration:
print("Reach end of Sequence")
break
# TODO: Check if givens at current row and column is at lower bound
# TODO: Dig the current hole and check for uniqueness
# TODO: Propagate and Output
return grid
if __name__ == "__main__": if __name__ == "__main__":
#print(generate_completed_grid()) #print(generate_completed_grid())
success = True func = generate_dig_sequence(3)
for i in range(500): #print(next(func))
if not parse_grid(generate_completed_grid()): [print(a) for a in next(func)]
print("failed at test" + str(i))
success = False
print(success)