Beispiel #1
0
def add_or_override_number():
    row = input("Row number:")
    if not is_valid_size_num(row):
        print("Wrong size")
        return

    column = input("Column number:")
    if not is_valid_size_num(column):
        print("Wrong size")
        return

    value = input("value:")
    if not is_valid_size_num(value):
        print("Wrong size")
        return

    restore = Board().board[int(row) - 1][int(column) - 1]

    Board().board[int(row) - 1][int(column) -
                                1] = [False] * (Board().get_size())**2

    if is_valid_sudoku_board(int(row) - 1, int(column) - 1, int(value) - 1):
        Board().board[int(row) - 1][int(column) - 1][int(value) - 1] = True
        print("The value you entered is valid sudoku-wise")

    else:
        print("The value you entered is not valid sudoku-wise")
        Board().board[int(row) - 1][int(column) - 1] = restore
Beispiel #2
0
def convert_binary_to_sudoku(A):
    board = Board()
    I = range(board.get_size()**2)
    for r in I:
        for c in I:
            for v in I:
                board.board[r][c][v] = A[r][c][v].xi(0)
    return board
Beispiel #3
0
def set_board_size():
    size = input("Enter size of sudoku: ")
    if not size.isdigit():
        print("The size you entered is not a valid number")
        return
    size = int(size)
    if size > 10:
        print("size too big, the limit is 10")
        return
    board = Board()
    board.set_size(size)
Beispiel #4
0
def remove_number():
    row = input("Row number:")
    if not is_valid_size_num(row):
        print("Wrong size")
        return

    column = input("Column number:")
    if not is_valid_size_num(column):
        print("Wrong size")
        return
    size = Board().get_size()
    Board().board[int(row) - 1][
        int(column) -
        1] = [False] * size**2  # TODO: Move to a method inside the Board class
Beispiel #5
0
def find_solution():
    board = Board()
    size_of_cube = board.get_size()
    size_of_board = board.get_size()**2
    I = range(board.get_size()**2)
    m = Model('model.lp')

    #x = [m.add_var(var_type=BINARY) for i in I]
    A = m.add_var_tensor(shape=(size_of_board, size_of_board, size_of_board),
                         name="tensor",
                         var_type=BINARY)
    #generic constraints
    for r in I:  #constraint each number appears one time in each row
        for number in I:
            m += xsum(A[r, :, number]) == 1
    for c in I:  #constraint each number appears one time in each column
        for number in I:
            m += xsum(A[:, c, number]) == 1
    for i in range(size_of_cube
                   ):  #constraint each number appears one time in each cube
        for j in range(size_of_cube):
            for number in I:
                m += A[i * size_of_cube:(i + 1) * size_of_cube,
                       j * size_of_cube:(j + 1) * size_of_cube,
                       number].sum() == 1
    for r in I:  #constarint exactly one number each cell
        for c in I:
            m += xsum(A[r, c, :]) == 1
    # specific constraints
    for r in I:
        for c in I:
            for v in I:
                if board.board[r][c][v]:
                    m += A[r, c, v] == 1
    # m += A[4,5,7] == 1 #constarint for A[4,5] = 8
    # m += A[4,6,7] == 1 #constarint for A[4,6] = 8

    m.optimize()
    solution = convert_binary_to_sudoku(A)
    solution.show()

    if not m.num_solutions:
        print("no solutions")
        return False
    print("a solution was found")
    return True
Beispiel #6
0
def is_valid_num_of_random_numbers(num_of_random_numbers):
    if not num_of_random_numbers.isdigit():
        return False
    num_of_random_numbers = int(num_of_random_numbers)
    empty_cells = Board().get_empty_cells()
    if num_of_random_numbers > len(empty_cells):
        print("number of random numbers can't be bigger than empty cells")
        return False
    return True  # Only if all the validations are ok
Beispiel #7
0
def is_valid_size_num(num):
    #Do not accept non numeric ("five" is not ok, 5.5 is  not ok, -5 is not ok)
    if not num.isdigit():
        return False
    num = int(num)
    #DO not accept higer then 9 (3x3)
    if num > Board().get_size()**2:
        return False
    #Do not accept 0
    if num <= 0:
        return False

    return True  # Only if all the validations are ok
Beispiel #8
0
def main():
    menu_factory = MenuFactory()
    board = Board()
    board.set_size(3)
    board.show()
    while (True):
        choice = get_user_choice_from_menu()
        next_method = menu_factory.get_menu_method(choice)
        # Run the next method
        next_method()
Beispiel #9
0
def add_random_numbers():
    num_of_random_numbers = input("Enter number of random numbers: ")
    if not is_valid_num_of_random_numbers(num_of_random_numbers):
        print("Wrong size")
        return
    board = Board()
    I = board.get_size()**2
    max_try = 10
    num_try = 0
    num_to_add = int(num_of_random_numbers)
    while (num_to_add > 0 and num_try < max_try):
        random_num = np.random.randint(0, I)
        empty_cells = board.get_empty_cells()
        random_cell_index = np.random.randint(len(empty_cells))
        i, j = empty_cells[random_cell_index]
        if is_valid_sudoku_board(i, j, random_num):
            board.board[i][j][random_num] = True
            num_to_add -= 1
            num_try = 0
        else:
            num_try += 1
        board.show()
        x = 0
Beispiel #10
0
def is_valid_sudoku_board(row, col, value):
    valid = True
    board = Board()
    for c in range(board.get_size()**2):
        if board.board[row][c][value] == True:
            print("row valid error")
            valid = False
    for r in range(board.get_size()**2):
        if board.board[r][col][value] == True:
            print("column valid error")
            valid = False
    rowsection = row // 3
    colsection = col // 3
    for x in range(board.get_size()):
        for y in range(board.get_size()):
            if board.board[rowsection * 3 + x][colsection * 3 +
                                               y][value] == True:
                print("cube valid error")
                valid = False
    return valid
Beispiel #11
0
def show_current_board():
    board = Board()
    board.show()
Beispiel #12
0
def clear_board():
    board = Board()
    board.clear()