Ejemplo n.º 1
0
def main():
    nSolutions = int(input("How many solutions should the puzzle have? "))
    nEmpty = int(input("How many empty cells do you want? "))
    if (nEmpty <= 45):
        print("Generating and saving initial puzzle...")
        p = Puzzle(nSolutions, nEmpty)
        p.empty()

        filNam = str(p.puzzleID()[0]) + "-" + str(p.puzzleID()[1]) + ".txt"
        out = open(filNam, 'a')

        json.dump(p.getPuzzle(), out)
        print("Now adding more puzzles...")
        n = 0
        i = 1
        while(n < 50):
            print("Trying new puzzle " + str(i))
            fullBoard = copyListOfLists(p.getOriginalBoard())
            emptyBoard = copyListOfLists(p.getPuzzle())
            board = createMoreSudoku(fullBoard, emptyBoard)
            F = createSudoku(board)
            if (exactly_n_models(F, p.puzzleID()[0])):
                json.dump(board, out)
                n+= 1
                print("Another puzzle added: " + str(n) + " puzzles this session out of "

                      + str(i) + " tried")
            i += 1
    
    else:
        print("Generating and saving 50 puzzles with your preferences...")
        for i in range(50):
            print("Generating puzzle " + str(i + 1) + " of 50...")
            p = Puzzle(nSolutions, nEmpty)
            p.empty()

            filNam = str(p.puzzleID()[0]) + "-" + str(p.puzzleID()[1]) + ".txt"
            out = open(filNam, 'a')
        
            json.dump(p.getPuzzle(), out)
Ejemplo n.º 2
0
def main():
    nSolutions = int(input("How many solutions should the puzzle have? "))
    nEmpty = int(input("How many empty cells do you want? "))
    if (nEmpty <= 45):
        print("Generating and saving initial puzzle...")
        p = Puzzle(nSolutions, nEmpty)
        p.empty()

        filNam = str(p.puzzleID()[0]) + "-" + str(p.puzzleID()[1]) + ".txt"
        out = open(filNam, 'a')

        json.dump(p.getPuzzle(), out)
        print("Now adding more puzzles...")
        n = 0
        i = 1
        while (n < 50):
            print("Trying new puzzle " + str(i))
            fullBoard = copyListOfLists(p.getOriginalBoard())
            emptyBoard = copyListOfLists(p.getPuzzle())
            board = createMoreSudoku(fullBoard, emptyBoard)
            F = createSudoku(board)
            if (exactly_n_models(F, p.puzzleID()[0])):
                json.dump(board, out)
                n += 1
                print("Another puzzle added: " + str(n) +
                      " puzzles this session out of " + str(i) + " tried")
            i += 1

    else:
        print("Generating and saving 50 puzzles with your preferences...")
        for i in range(50):
            print("Generating puzzle " + str(i + 1) + " of 50...")
            p = Puzzle(nSolutions, nEmpty)
            p.empty()

            filNam = str(p.puzzleID()[0]) + "-" + str(p.puzzleID()[1]) + ".txt"
            out = open(filNam, 'a')

            json.dump(p.getPuzzle(), out)
Ejemplo n.º 3
0
def h1(puzzle: Puzzle):
    """ Heuristic #1
  
  Returns the number of tiles out of row + number of tiles out of column
  """
    count = 0
    for item, index in puzzle.getPuzzle().items():
        if not inCorrectRow(item, index):
            count += 1

        if not inCorrectColumn(item, index):
            count += 1

    return count
Ejemplo n.º 4
0
def h2(puzzle: Puzzle):
    tiles = puzzle.getPuzzle()

    manhattan_distance = 0
    for tile, index in tiles.items():
        correctIndex = int(tile) - 1

        if tile == Puzzle.EMPTY_SLOT:
            correctIndex = 7

        coordinate1 = indexToCoordinates(correctIndex)
        coordinate2 = indexToCoordinates(index)

        coordinates = {
            'y': coordinate1['y'] - coordinate2['y'],
            'x': coordinate1['x'] - coordinate2['x']
        }
        manhattan_distance += coordinates['y']**2 + coordinates['x']**2

    return manhattan_distance