Пример #1
0
 def solve(self):
     if solve(self.model):
         # print_board(self.model)
         self.board = self.model.copy()
         # print_board(self.box)
         for i in range(self.rows):  # drawing boxes
             for j in range(self.cols):
                 self.box[i][j].set(self.board[i][j])
         self.update_model()
Пример #2
0
    def place(self, val):
        row, col = self.selected
        if self.cubes[row][col].value == 0:
            self.cubes[row][col].set(val)
            self.update_model()

            if valid(self.model, val, (row,col)) and solve(self.model):
                return True
            else:
                self.cubes[row][col].set(0)
                self.cubes[row][col].set_temp(0)
                self.update_model()
                return False
Пример #3
0
    def placetemp(self):
        row, col = self.selected
        if self.box[row][col].value == 0:
            self.box[row][col].set(self.box[row][col].temp)
            self.update_model()

            if valid(self.model, self.box[row][col].temp,
                     (row, col)) and solve(self.model):
                return True
            else:
                self.box[row][col].set(0)
                self.box[row][col].settemp(0)
                self.update_model()
                return False
Пример #4
0
def calculate():
    # do stuff
    # print request.form
    # print request.form['inputTime']
    # print float(request.form['inputTime'])
    desiredMetric = request.form['desiredBalanceMetric']
    if desiredMetric == "Personalized":
        time = float(request.form['inputTime'])
        landone = float(request.form['inputLandSun1'])
        landtwo = float(request.form['inputLandSun2'])
        landthree = float(request.form['inputLandSun3'])
        v1 = float(request.form['Cucumber'])
        v2 = float(request.form['Eggplant'])
        v3 = float(request.form['Tomato'])
        v4 = float(request.form['Green Beans'])
        v5 = float(request.form['Butternut Squash'])
        v6 = float(request.form['Cherry Tomato'])
        v7 = float(request.form['Zucchini'])
        v8 = float(request.form['Watermelon'])
        v9 = float(request.form['Bell Pepper'])
        v10 = float(request.form['Strawberries'])
        v11 = float(request.form['Potato'])
        v12 = float(request.form['Sweet Corn'])
        v13 = float(request.form['Carrots'])
        v14 = float(request.form['Beets'])
        v15 = float(request.form['Onion'])
        v16 = float(request.form['Radish'])
        v17 = float(request.form['Sweet Peas'])
        v18 = float(request.form['Lettuce'])
        v19 = float(request.form['Garlic'])
        v20 = float(request.form['Brussel Sprouts'])
        v21 = float(request.form['Kale'])
        v22 = float(request.form['Spinach'])
        result = solve_man(time, landone, landtwo, landthree, desiredMetric, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22)
        # print result
    else:
        time = float(request.form['inputTime'])
        landone = float(request.form['inputLandSun1'])
        landtwo = float(request.form['inputLandSun2'])
        landthree = float(request.form['inputLandSun3'])
        result = solve(time,landone,landtwo,landthree,desiredMetric)
        # print result

    # could do a different page to avoid the problem of planpage not rendering intially. any alternative?
    return render_template('resultspage.html', result=result)
Пример #5
0
def generate():
    board = [
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0]
    ]
    rand = randint(15, 26)
    # print(rand)
    for i in range(rand):
        # seed(i * rand)
        row = randint(0, 8)
        col = randint(0, 8)
        value = randint(1, 9)

        # print('row: %r\n col: %r\nvalue:%r' % (row, col, value))
        # print_board(board)
        if valid(board, value, (row, col)):
            board[row][col] = value
        else:
            rand += 1
    for i in range(0, 8):   # validates the current value so recursion function doesn't hang
        for j in range(0, 8):
            if board[i][j] != 0:
                if valid(board, board[i][j], (i, j)):
                    pass
                    # print(self.model[i][j])
                else:
                    generate()
    temp = copy.deepcopy(board)
    print_board(temp)
    with recursionlimit(500):
        if solve(board):
            return temp
        else:
            clearboard()
            generate()
Пример #6
0
def solve_window():
    pygame.display.init()
    screen2 = pygame.display.set_mode((540, 600), 0, 32)
    pygame.display.set_caption('Solver')
    screen2.fill(color_white)
    board = Grid(9, 9, 540, 540)
    key = None

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    # pygame.quit()
                    pygame.display.set_mode((300, 250), 0, 32)
                    running = False
                if event.key == pygame.K_1 or event.key == pygame.K_KP1:
                    key = 1
                if event.key == pygame.K_2 or event.key == pygame.K_KP2:
                    key = 2
                if event.key == pygame.K_3 or event.key == pygame.K_KP3:
                    key = 3
                if event.key == pygame.K_4 or event.key == pygame.K_KP4:
                    key = 4
                if event.key == pygame.K_5 or event.key == pygame.K_KP5:
                    key = 5
                if event.key == pygame.K_6 or event.key == pygame.K_KP6:
                    key = 6
                if event.key == pygame.K_7 or event.key == pygame.K_KP7:
                    key = 7
                if event.key == pygame.K_8 or event.key == pygame.K_KP8:
                    key = 8
                if event.key == pygame.K_9 or event.key == pygame.K_KP9:
                    key = 9
                if event.key == pygame.K_DELETE or event.key == pygame.K_BACKSPACE:
                    key = 0
                # if event.key == pygame.K_SPACE:  # used for debug
                #     board.clean()
                #     board.update_model()
                #     print_board(board.model)
            try:
                if event.type == pygame.MOUSEBUTTONDOWN:  # mouse selects a box
                    pos = pygame.mouse.get_pos()
                    clicked = board.click(pos)
                    if clicked:
                        board.select(clicked[0], clicked[1])
                        key = None
                    elif board.leftbuttonclick(pos):
                        board.clean()
                        board.update_model()
                        key = 0
                    elif board.rightbuttonclick(pos):
                        if board.validate(
                        ):  # have to check before solving or else the program will hang
                            if solve(board.model):
                                board.solve()
                                key = None
                            else:
                                Tk().wm_withdraw()
                                messagebox.showinfo(
                                    'Notice', 'This puzzle is not solvable')
                        else:
                            Tk().wm_withdraw()
                            messagebox.showinfo('Notice',
                                                'This puzzle is not solvable')
            finally:
                pass

        if board.selected and key is not None:
            board.place(key)

        redraw_solver(screen2, board, True)  # true for solver
        pygame.display.update()
        mainClock.tick(60)
Пример #7
0
def play_window():
    pygame.display.init()
    screen3 = pygame.display.set_mode((540, 600), 0, 32)
    pygame.display.set_caption('Player')
    screen3.fill(color_white)
    board2 = Grid(9, 9, 540, 540)
    key = None

    running = True
    while running:
        for event2 in pygame.event.get():
            if event2.type == QUIT:
                pygame.quit()
                sys.exit()
            if event2.type == KEYDOWN:
                if event2.key == K_ESCAPE:
                    # pygame.quit()
                    pygame.display.set_mode((300, 250), 0, 32)
                    running = False
                if event2.key == pygame.K_1 or event2.key == pygame.K_KP1:
                    key = 1
                if event2.key == pygame.K_2 or event2.key == pygame.K_KP2:
                    key = 2
                if event2.key == pygame.K_3 or event2.key == pygame.K_KP3:
                    key = 3
                if event2.key == pygame.K_4 or event2.key == pygame.K_KP4:
                    key = 4
                if event2.key == pygame.K_5 or event2.key == pygame.K_KP5:
                    key = 5
                if event2.key == pygame.K_6 or event2.key == pygame.K_KP6:
                    key = 6
                if event2.key == pygame.K_7 or event2.key == pygame.K_KP7:
                    key = 7
                if event2.key == pygame.K_8 or event2.key == pygame.K_KP8:
                    key = 8
                if event2.key == pygame.K_9 or event2.key == pygame.K_KP9:
                    key = 9
                if event2.key == pygame.K_DELETE or event2.key == pygame.K_BACKSPACE:
                    key = 0
                if event2.key == pygame.K_RETURN or event2.key == pygame.K_KP_ENTER:
                    i, j = board2.selected
                    if board2.box[i][j].temp != 0:
                        if board2.placetemp():
                            print("correct")
                        else:
                            print("wrong")
                    key = None
            if event2.type == pygame.MOUSEBUTTONDOWN:  # mouse selects a box
                pos2 = pygame.mouse.get_pos()
                clicked = board2.click(pos2)
                if clicked:
                    board2.select(clicked[0], clicked[1])
                    key = None
                elif board2.leftbuttonclick(pos2):
                    board2.clean()
                    board2.update_model()
                    board2.update_board(generate())
                    key = None
                elif board2.rightbuttonclick(pos2):
                    if board2.validate(
                    ):  # have to check before solving or else the program will hang
                        if solve(board2.model):
                            board2.solve()
                            key = None
                        else:
                            Tk().wm_withdraw()
                            messagebox.showinfo('Notice',
                                                'This puzzle is not solvable')
                            key = None
                    else:
                        Tk().wm_withdraw()
                        messagebox.showinfo('Notice',
                                            'This puzzle is not solvable')
                        key = None

        if board2.selected and key is not None:
            board2.settemp(key, screen3)

        redraw_solver(screen3, board2, False)  # false for play window
        pygame.display.update()
        mainClock.tick(60)
Пример #8
0
        def step(self, label, amount): return step(self.board, label, amount)
    return NextStep(newboard if newboard else board)

if __name__ == '__main__':
    Tk().withdraw()
    file_name = askopenfilename(title='Select a level to solve')

    if(file_name):
        print('solving {}...'.format(file_name))
        board = board_from_file(file_name)
        if board:

            print('initial board: ')
            print(board.prettify())
            print('solving, please wait...')

            solved = solve(board)

            if solved:
                print ("solution: ")

                board_ref = board
                for move in solved.moves:

                    board_ref = board_ref.move_car(move.label, move.amount)
                    print('({}, {})'.format(move.label, move.amount))
                    print(board_ref.prettify())
                print('solved in {} steps'.format(len(solved.moves)))

            else:
                print("no solution")
Пример #9
0
    return NextStep(newboard if newboard else board)


if __name__ == '__main__':
    Tk().withdraw()
    file_name = askopenfilename(title='Select a level to solve')

    if (file_name):
        print('solving {}...'.format(file_name))
        board = board_from_file(file_name)
        if board:

            print('initial board: ')
            print(board.prettify())
            print('solving, please wait...')

            solved = solve(board)

            if solved:
                print("solution: ")

                board_ref = board
                for move in solved.moves:

                    board_ref = board_ref.move_car(move.label, move.amount)
                    print('({}, {})'.format(move.label, move.amount))
                    print(board_ref.prettify())
                print('solved in {} steps'.format(len(solved.moves)))

            else:
                print("no solution")
Пример #10
0
 def solver(self):
     solve(self.board, self.cubes, self)
#!/usr/bin/python3

import sys
import os
from SATParser import parse
from Solver import solve

clauses, numVariables, numClauses = parse(sys.argv[1], False)

isSolved, solutions = solve(clauses, [])

if isSolved:
    print("% SZS status Satisfiable")
    #print("is solved", isSolved, "solutions", solutions)
else:
    print("% SZS status Unsatisfiable")