Esempio n. 1
0
def backtrack(r, c):
    global solved, board, rows, cols, boxes

    N = 9
    if board[r][c] == ".":
        for d in range(1, 10):
            if could_place(d, r, c):
                fill_number(d, r, c)
                if r == N - 1 and c == N - 1:
                    solved = True
                elif c == N - 1:
                    if r == 6 and board[0][6] == '6' and board[0][7] == '5':
                        boards.print_board(board)
                    backtrack(r + 1, 0)
                else:
                    if r == 8 and board[5][3] == '1' and board[0][7] == '5':
                        boards.print_board(board)
                    backtrack(r, c + 1)

                if not solved:
                    erase_number(r, c)

    else:
        if r == N - 1 and c == N - 1:
            solved = True
            return
        elif c == N - 1:
            backtrack(r + 1, 0)
        else:
            backtrack(r, c + 1)
    def run(self, max_eps=None, printstep=500):
        """Runs the simulator.
        Generates possible moves and chooses one.
        Loops until game_over or max_eps is exceeded.
        """
        ep = 0
        while ep != max_eps and self.get_options():
            # generate options and features
            scores = {x: self.control(*x) for x in self.options}
            best_moves = tuple(k for k, v in scores.items()
                               if v == max(scores.values()))
            orient, pos = self.sequence.choice(best_moves)
            self.space.imprint_zoid(self.curr_z, orient=orient, pos=pos)
            self.update_score()
            self.new_zoids()
            # the True parameter means full rows are counted *and* cleared
            self.space.check_full(True)

            if self.show_scores and ep % printstep == 0 and not printstep == -1:
                print("\nEpisode: " + str(ep))
                self.printscores()
            ep += 1

            if self.show_choice:
                print_board(self.space, entire=True, show_full=True)
                time.sleep(self.choice_step)

        if self.show_result:
            print("\n\nGame Over\nFinal scores:")
            print("Episodes: " + str(ep))
            self.printscores()
            self.printcontroller()
            print("\n")

        return ({
            "lines": self.lines,
            "l1": self.l[1],
            "l2": self.l[2],
            "l3": self.l[3],
            "l4": self.l[4],
            "score": self.score,
            "level": self.level,
            "eps": ep
        })
Esempio n. 3
0
def solve_one():
    global board, rows, cols, boxes, solved
    n, N = 3, 9
    rows = [[0] * N for _ in range(N)]
    cols = [[0] * N for _ in range(N)]
    boxes = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            if board[i][j] != '.':
                fill_number(int(board[i][j]), i, j)

    boards.print_board(board)
    solved = False
    backtrack(0, 0)
    if solved:
        print "Solved !!!"
        boards.print_board(board)
    else:
        print "Unsatisfiable !!!"
    def run(self, max_eps=None, printstep=500):
        """Runs the simulator.
        Generates possible moves and chooses one.
        Loops until game_over or max_eps is exceeded.
        """
        ep = 0
        while ep != max_eps and self.get_options():
            # generate options and features
            scores = {x: self.control(*x) for x in self.options}
            best_moves = tuple(k for k, v in scores.items() if v == max(scores.values()))
            orient, pos = self.sequence.choice(best_moves)
            self.space.imprint_zoid(self.curr_z, orient=orient, pos=pos)
            self.update_score()
            self.new_zoids()
            # the True parameter means full rows are counted *and* cleared
            self.space.check_full(True)

            if self.show_scores and ep % printstep == 0 and not printstep == -1:
                print ("\nEpisode: " + str(ep))
                self.printscores()
            ep += 1

            if self.show_choice:
                print_board(self.space, entire=True, show_full=True)
                time.sleep(self.choice_step)

        if self.show_result:
            print ("\n\nGame Over\nFinal scores:")
            print ("Episodes: " + str(ep))
            self.printscores()
            self.printcontroller()
            print ("\n")

        return {
            "lines": self.lines,
            "l1": self.l[1],
            "l2": self.l[2],
            "l3": self.l[3],
            "l4": self.l[4],
            "score": self.score,
            "level": self.level,
            "eps": ep,
        }
Esempio n. 5
0
def solve_one():
    global board
    boards.print_board(board)
    # 9x9 matrix of integer variables
    X = [[Int("x_%s_%s" % (i + 1, j + 1)) for j in range(9)] for i in range(9)]

    # each cell contains a value in {1, ..., 9}
    cells_c = [
        And(1 <= X[i][j], X[i][j] <= 9) for i in range(9) for j in range(9)
    ]

    # each row contains a digit at most once
    rows_c = [Distinct(X[i]) for i in range(9)]

    # each column contains a digit at most once
    cols_c = [Distinct([X[i][j] for i in range(9)]) for j in range(9)]

    # each 3x3 square contains a digit at most once
    sq_c = [
        Distinct(
            [X[3 * i0 + i][3 * j0 + j] for i in range(3) for j in range(3)])
        for i0 in range(3) for j0 in range(3)
    ]

    sudoku_c = cells_c + rows_c + cols_c + sq_c
    board_c = [
        If(board[i][j] == '.', True, X[i][j] == (ord(board[i][j]) - 0x30))
        for i in range(9) for j in range(9)
    ]

    s = Solver()
    s.add(sudoku_c + board_c)
    if s.check() == sat:
        m = s.model()
        # r = [ [m.evaluate(X[i][j]) for j in range(9)] for i in range(9) ]
        #print_matrix(r)
        for i in range(9):
            for j in range(9):
                board[i][j] = m.evaluate(X[i][j])
        boards.print_board(board)
    else:
        print "failed to solve"