def try_number(grid, position, list_attempts):

	isValid = False
	num = 0

	# try all numbers 1-9 in position of grid
	while not isValid and num <= 9:

		num = num + 1
		
		grid[position[0]][position[1]] = num
		isValid = check_sudoku(grid)

		# not found valid number for position
		if num >= 9 and not isValid:
				
			# delete previous entry in grid and try to find another valid number for previous position
			if len(list_attempts) is not 0:

				# reset grid position to empty
				grid[position[0]][position[1]] = 0

				#delete last entry in grid and try next number
				num = list_attempts[-1][0]
				position[0] = list_attempts[-1][1]
				position[1] = list_attempts[-1][2]

				del list_attempts[-1]

				#print 'del', num

			# no possible valid number found => grid cannot be completed
			else:
				return False

	list_attempts.append([num, position[0], position[1]])

	return grid
def complete_simple_sudoku(grid):

    # check grid is valid
    isValid = check_sudoku(grid)

    # grid is incorrectly formed
    if isValid is None or isValid is False:
        return isValid

    # check if grid is complete
    incomplete = check_incomplete(grid)
    finding_solutions = True

    while incomplete and finding_solutions:

        finding_solutions = False

        for i in range(1, 10):

            grid_bool = [
                [True, True, True, True, True, True, True, True, True],
                [True, True, True, True, True, True, True, True, True],
                [True, True, True, True, True, True, True, True, True],
                [True, True, True, True, True, True, True, True, True],
                [True, True, True, True, True, True, True, True, True],
                [True, True, True, True, True, True, True, True, True],
                [True, True, True, True, True, True, True, True, True],
                [True, True, True, True, True, True, True, True, True],
                [True, True, True, True, True, True, True, True, True]
            ]

            # eliminate non valid positions for number i in boolean grid
            # check rows of grid
            count_row = 0

            for row in grid:
                if i in row:
                    grid_bool[count_row] = [
                        False, False, False, False, False, False, False, False,
                        False
                    ]
                else:
                    count_line = 0
                    for num in row:
                        if num > 0:
                            grid_bool[count_row][count_line] = False
                        count_line = count_line + 1

                count_row = count_row + 1

            # check columns of grid
            for count_column in range(9):
                with_i = False
                count_row = 0
                while not with_i and count_row < 9:
                    if grid[count_row][count_column] == i:
                        with_i = True
                    count_row = count_row + 1

                if with_i:
                    # convert column in boolean grid to False
                    for count_row in range(9):
                        grid_bool[count_row][count_column] = False

                else:
                    for count_row in range(9):
                        if grid[count_row][count_column] > 0:
                            grid_bool[count_row][count_column] = False

            # check boxes of grid
            # loop over box limits
            for row_limit in range(0, 9, 3):

                for column_limit in range(0, 9, 3):

                    # start checking a new box
                    with_i = False
                    for row_count in range(row_limit, (row_limit + 3)):

                        for column_count in range(column_limit,
                                                  (column_limit + 3)):
                            if grid[row_count][column_count] == i:
                                with_i = True

                    if with_i:
                        #convert entire box in boolean grid to false
                        for row_count in range(row_limit, (row_limit + 3)):

                            for column_count in range(column_limit,
                                                      (column_limit + 3)):
                                grid_bool[row_count][column_count] = False

                    else:
                        #convert filled positions in box in boolean grid to false
                        for row_count in range(row_limit, (row_limit + 3)):

                            for column_count in range(column_limit,
                                                      (column_limit + 3)):
                                if grid[row_count][column_count] > 0:
                                    grid_bool[row_count][column_count] = False

            # check boolean grid for valid positions
            # check rows of boolean grid
            count_row = 0

            for row in grid_bool:
                count_valid = 0
                count_line = 0
                position_valid = 0

                for isValid in row:
                    if isValid:
                        count_valid = count_valid + 1
                        position_valid = count_line
                    count_line = count_line + 1

                if count_valid == 1:
                    grid[count_row][position_valid] = i
                    finding_solutions = True

                count_row = count_row + 1

            # check columns
            for count_column in range(9):
                count_valid = 0
                position_valid = 0
                for count_row in range(9):
                    if grid_bool[count_row][count_column] == True:
                        count_valid = count_valid + 1
                        position_valid = count_row

                if count_valid == 1:
                    grid[position_valid][count_column] = i
                    finding_solutions = True

            # check boxes
            for row_limit in range(0, 9, 3):
                for column_limit in range(0, 9, 3):
                    # start checking a new box
                    count_valid = 0
                    column_valid = 0
                    row_valid = 0
                    for row_count in range(row_limit, (row_limit + 3)):
                        for column_count in range(column_limit,
                                                  (column_limit + 3)):
                            if grid_bool[row_count][column_count] == True:
                                count_valid = count_valid + 1
                                column_valid = column_count
                                row_valid = row_count

                    if count_valid == 1:
                        grid[row_valid][column_valid] = i
                        finding_solutions = True

        incomplete = check_incomplete(grid)

        # check grid is valid
        isValid = check_sudoku(grid)

        if not isValid:
            return False

    return grid
Exemplo n.º 3
0
def test_dos():
    incorrect = [[1, 2, 3, 4], [2, 3, 1, 3], [3, 1, 2, 3], [4, 4, 4, 2]]
    assert check_sudoku(incorrect) == False
Exemplo n.º 4
0
def test_nueve():
    nuevo = [[]]
    assert check_sudoku(nuevo) == False
Exemplo n.º 5
0
def test_ocho():
    irregular2 = [[1, 2, 3], [2, 3, 1], [3, 1]]
    assert check_sudoku(irregular2) == False
Exemplo n.º 6
0
def test_siete():
    irregular = [[1, 2, 3], [2, 3, 1]]
    assert check_sudoku(irregular) == False
Exemplo n.º 7
0
def test_siete():
    incorrect5 = [[1, 1.5], [1.5, 1]]
    assert check_sudoku(incorrect5) == False
Exemplo n.º 8
0
def test_uno():
    correct = [[1, 2, 3], [2, 3, 1], [3, 1, 2]]
    assert check_sudoku(correct) == True
Exemplo n.º 9
0
def test_seis():
    incorrect4 = [['a', 'b', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b']]
    assert check_sudoku(incorrect4) == False
Exemplo n.º 10
0
def test_quinto():
    incorrect3 = [[1, 2, 3, 4, 5], [2, 3, 1, 5, 6], [4, 5, 2, 1, 3],
                  [3, 4, 5, 2, 1], [5, 6, 4, 3, 2]]
    assert check_sudoku(incorrect3) == False
Exemplo n.º 11
0
def test_cuatro():
    incorrect2 = [[1, 2, 3, 4], [2, 3, 1, 2], [4, 1, 2, 3], [2, 3, 1, 4]]
    assert check_sudoku(incorrect2) == False
Exemplo n.º 12
0
def test_tres():
    incorrect1 = [[1, 2, 3], [2, 3, 1], [2, 3, 1]]
    assert check_sudoku(incorrect1) == False
Exemplo n.º 13
0
        # We need it for data analysis
        init_len_clauses = len(rules)

        finish = False
        while finish == False:
            bk = False
            # Simplify
            rules, literals_dict, truth_values, split_choice, neg_literal, \
            rules_before_split, literals_dict_before_split, truth_values_before_split, results[4], bk = \
                simplify.simplify(rules, literals_dict, truth_values, split_choice, neg_literal,
                            rules_before_split, literals_dict_before_split, truth_values_before_split, results[4], bk)
            new_len = len(rules)
            print('    #clauses: after simplify:', new_len, end='\r')
            if new_len == 0 :
                # Solution
                if not check_sudoku.check_sudoku(sorted(list([val for val in truth_values if val > 0]))):
                    print(list(truth_values))
                    print(type(list(truth_values)[0]))

                    pretty_print.solution(truth_values)
                    quit()

                pretty_print.solution(truth_values)
                finish = True

                # get the solution time
                results[5] = float("{0:.2f}".format(time.time() - problem_start_time))
                print("\nTime:    %.2f seconds " % (time.time() - problem_start_time))
                print("Runtime: %.2f seconds " % (time.time() - start_time))
                print("Initial #unit clauses :",results[1] )
                print("Reduction from first simplify: ", results[2], '%')
Exemplo n.º 14
0
def complete_simple_sudoku(grid):

	# check grid is valid
	isValid = check_sudoku(grid)
	
	# grid is incorrectly formed
	if isValid is None or isValid is False:
		return isValid

	# check if grid is complete
	incomplete = check_incomplete(grid)
	finding_solutions = True

	while incomplete and finding_solutions:

		finding_solutions = False

		for i in range(1, 10):

			grid_bool = [[True,True,True,True,True,True,True,True,True],
	        			[True,True,True,True,True,True,True,True,True],
	        			[True,True,True,True,True,True,True,True,True],
	        			[True,True,True,True,True,True,True,True,True],
	        			[True,True,True,True,True,True,True,True,True],
	        			[True,True,True,True,True,True,True,True,True],
	        			[True,True,True,True,True,True,True,True,True],
	        			[True,True,True,True,True,True,True,True,True],
	        			[True,True,True,True,True,True,True,True,True]]
			
			# eliminate non valid positions for number i in boolean grid
			# check rows of grid			
			count_row = 0

			for row in grid:
				if i in row:
					grid_bool[count_row] = [False,False,False,False,False,False,False,False,False]
				else:
					count_line = 0		
					for num in row:
						if num > 0:
							grid_bool[count_row][count_line] = False
						count_line = count_line + 1
				
				count_row = count_row + 1

			# check columns of grid
			for count_column in range(9):
				with_i = False
				count_row = 0
				while not with_i and count_row < 9:
					if grid[count_row][count_column] == i:
						with_i = True
					count_row = count_row + 1

				if with_i:
					# convert column in boolean grid to False
					for count_row in range(9):
						grid_bool[count_row][count_column] = False

				else:
					for count_row in range(9):
						if grid[count_row][count_column] > 0:
							grid_bool[count_row][count_column] = False

			# check boxes of grid
			# loop over box limits
			for row_limit in range(0, 9, 3):

				for column_limit in range(0, 9, 3):

					# start checking a new box
					with_i = False
					for row_count in range(row_limit, (row_limit + 3)):

						for column_count in range(column_limit, (column_limit + 3)):
			 				if grid[row_count][column_count] == i:
								with_i = True

					if with_i:
						#convert entire box in boolean grid to false
						for row_count in range(row_limit, (row_limit + 3)):

							for column_count in range(column_limit, (column_limit + 3)):
								grid_bool[row_count][column_count] = False

					else:
						#convert filled positions in box in boolean grid to false
						for row_count in range(row_limit, (row_limit + 3)):

							for column_count in range(column_limit, (column_limit + 3)):
								if grid[row_count][column_count] > 0:
									grid_bool[row_count][column_count] = False


			# check boolean grid for valid positions
			# check rows of boolean grid
			count_row = 0

			for row in grid_bool:
				count_valid = 0
				count_line = 0
				position_valid = 0

				for isValid in row:
					if isValid:
						count_valid = count_valid + 1
						position_valid = count_line
					count_line = count_line + 1

				if count_valid == 1:
					grid[count_row][position_valid] = i
					finding_solutions = True

				count_row = count_row + 1

			# check columns
			for count_column in range(9):
				count_valid = 0
				position_valid = 0
				for count_row in range(9):
					if grid_bool[count_row][count_column] == True:
						count_valid = count_valid + 1
						position_valid = count_row

				if count_valid == 1:
					grid[position_valid][count_column] = i
					finding_solutions = True

			# check boxes
			for row_limit in range(0, 9, 3):
				for column_limit in range(0, 9, 3):
					# start checking a new box
					count_valid = 0
					column_valid = 0
					row_valid = 0
					for row_count in range(row_limit, (row_limit + 3)):
						for column_count in range(column_limit, (column_limit + 3)):
							if grid_bool[row_count][column_count] == True:
								count_valid = count_valid + 1
								column_valid = column_count
								row_valid = row_count

					if count_valid == 1:
						grid[row_valid][column_valid] = i
						finding_solutions = True

		incomplete = check_incomplete(grid)

		# check grid is valid
		isValid = check_sudoku(grid)
		
		if not isValid:
			return False

	return grid