def solve(snapshot, screen): ##display current snapshot pygame.time.delay(200) Futoshiki_IO.displayPuzzle(snapshot, screen) pygame.display.flip() global changeWasMade changeWasMade = False if checkConsistency(snapshot) == False: return False if isComplete(snapshot): return True doPreCheck(snapshot) if changeWasMade: print len(snapshot.unsolvedCells()) solve(snapshot,screen) if isComplete(snapshot) and checkConsistency(snapshot): return True else: print "take a guess" for option in snapshot.unsolvedCells()[0].getOptions(): newSnapshot = snapshot.clone() newSnapshot.unsolvedCells()[0].setVal(option) if checkConsistency(newSnapshot): try: success = solve(newSnapshot, screen) if success: return True except IndexError: pass
def solve(snapshot, screen): ##display current snapshot pygame.time.delay(200) Futoshiki_IO.displayPuzzle(snapshot, screen) pygame.display.flip() global changeWasMade changeWasMade = False if checkConsistency(snapshot) == False: return False if isComplete(snapshot): return True doPreCheck(snapshot) if changeWasMade: print len(snapshot.unsolvedCells()) solve(snapshot, screen) if isComplete(snapshot) and checkConsistency(snapshot): return True else: print "take a guess" for option in snapshot.unsolvedCells()[0].getOptions(): newSnapshot = snapshot.clone() newSnapshot.unsolvedCells()[0].setVal(option) if checkConsistency(newSnapshot): try: success = solve(newSnapshot, screen) if success: return True except IndexError: pass
def solve(snapshot, screen): # display current snapshot pygame.time.delay(200) Futoshiki_IO.displayPuzzle(snapshot, screen) pygame.display.flip() # if current snapshot is complete return True if isComplete(snapshot): return True # update the possible values for unsolved cells - if any cell now has no possible values, return False as the solution is not solvable for cell in snapshot.unsolvedCells(): row = cell.getRow() col = cell.getCol() if not snapshot.setPossibilities(row, col, getPossibleCellValues(snapshot, row, col)): return False # if current snapshot not complete ... # get next empty cell(s) - returns list of cells with only one possibility, or list of cell with fewest possible values emptycelldata = getNextCell(snapshot) # if each cell only has one possible value, complete them all at once # the possibilities for each of these cells will not need updating as it is already a single value # if placing the single possible value in these cells creates an inconsistent solution, return False if emptycelldata[0][0] == 1: newsnapshot = snapshot.clone() for cell in emptycelldata: cellrow = cell[1] cellcol = cell[2] value = cell[3][0] newsnapshot.setCellVal(cellrow, cellcol, value) # if new snapshot is consistent perform recursive call to solve, return True if solved if checkConsistency(newsnapshot): if solve(newsnapshot, screen): return True else: return False else: cellrow = emptycelldata[0][1] cellcol = emptycelldata[0][2] possiblevalues = emptycelldata[0][3] # for possible values for the cell clone current snapshot and update the cell with the value for value in possiblevalues: newsnapshot = snapshot.clone() newsnapshot.setCellVal(cellrow, cellcol, value) newsnapshot.setPossibilities(cellrow, cellcol, [value]) # if new snapshot is consistent perform recursive call to solve, return True if solved if checkConsistency(newsnapshot): if solve(newsnapshot, screen): return True # if we get to here there is no way to solve from current snapshot return False
def solve(snapshot, screen): # display current snapshot pygame.time.delay(200) Futoshiki_IO.displayPuzzle(snapshot, screen) pygame.display.flip() if isComplete(snapshot) and checkConsistency(snapshot): return True # Singleton selection code block unsolvedCells = snapshot.unsolvedCells() # Sorts the unsolved list of cells sortedUnsolved = sorted(unsolvedCells, key=lambda c: len(c.possibles)) emptyCell = sortedUnsolved[0] if len(emptyCell.getPossVals()) == 1: newsnapshot = snapshot.clone() newsnapshot.setCellVal(emptyCell.getRow(), emptyCell.getCol(), emptyCell.getPossVals()[0]) if checkConsistency(newsnapshot): success = solve(newsnapshot, screen) if success: return True return False emptyCell = unsolvedCells[1] # Iterates through potential values in cells for val in range(5): # Checks potential values are already placed in row or column. if snapshot.notContains(emptyCell.getRow(), emptyCell.getCol(), val + 1): newsnapshot = snapshot.clone() newsnapshot.setCellVal(emptyCell.getRow(), emptyCell.getCol(), val + 1) if checkConsistency(newsnapshot): success = solve(newsnapshot, screen) if success: return True return False
def solve(snapshot, screen): # display current snapshot pygame.time.delay(200) Futoshiki_IO.display_puzzle(snapshot, screen) pygame.display.flip() if is_complete(snapshot): # for i in range print("returned true") return True else: new_snapshot = snapshot.clone() for solved_cell in new_snapshot.solved_cells(): new_snapshot.remove_invalids_from_possible_list(solved_cell) for unsolved_cell in new_snapshot.unsolved_cells()[1:]: next_empty_cell_row = unsolved_cell.get_row() next_empty_cell_column = unsolved_cell.get_column() for cell_value in new_snapshot.get_cell_possibles_list( next_empty_cell_row, next_empty_cell_column): if check_consistency(new_snapshot, next_empty_cell_row, next_empty_cell_column, cell_value): new_snapshot.set_cell_value(next_empty_cell_row, next_empty_cell_column, cell_value) if solve(new_snapshot, screen): return True return False
done = False # Used to manage how fast the screen updates clock = pygame.time.Clock() # -------- Main Program Loop ----------- while done == False: for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop if event.type == pygame.KEYDOWN: # If user wants to perform an action if event.key == pygame.K_t: start_time = time.time() # Choose a random puzzle to solve trivialpuzzle = random.choice(os.listdir("trivialpuzzles")) # change dir name if necessary trivialpuzzle = "trivialpuzzles/" + trivialpuzzle firstSnapshot = Futoshiki_IO.loadPuzzle(trivialpuzzle) Solver.solve(firstSnapshot, screen) print("--- %s : %s seconds --- " % (trivialpuzzle.split('/')[1], (time.time() - start_time))) if event.key == pygame.K_e: start_time = time.time() # Choose a random puzzle to solve easypuzzle = random.choice(os.listdir("easypuzzles")) # change dir name if necessary easypuzzle = "easypuzzles/" + easypuzzle firstSnapshot = Futoshiki_IO.loadPuzzle(easypuzzle) Solver.solve(firstSnapshot, screen) print("--- %s : %s seconds ---" % (easypuzzle.split('/')[1], (time.time() - start_time))) if event.key == pygame.K_h: start_time = time.time() # Choose a random puzzle to solve hardpuzzle = random.choice(os.listdir("hardpuzzles")) # change dir name if necessary hardpuzzle = "hardpuzzles/" + hardpuzzle
# Used to manage how fast the screen updates clock=pygame.time.Clock() # -------- Main Program Loop ----------- while done==False: for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done=True # Flag that we are done so we exit this loop if event.type == pygame.KEYDOWN: # If user wants to perform an action if event.key == pygame.K_t: # Choose a random puzzle to solve trivialpuzzle = random.choice(os.listdir("trivialpuzzles")) #change dir name if necessary trivialpuzzle = "trivialpuzzles/" + trivialpuzzle firstSnapshot = Futoshiki_IO.loadPuzzle(trivialpuzzle) Solver.solve(firstSnapshot, screen) if event.key == pygame.K_e: # Choose a random puzzle to solve easypuzzle = random.choice(os.listdir("easypuzzles")) #change dir name if necessary easypuzzle = "easypuzzles/" + easypuzzle firstSnapshot = Futoshiki_IO.loadPuzzle(easypuzzle) Solver.solve(firstSnapshot, screen) if event.key == pygame.K_h: # Choose a random puzzle to solve hardpuzzle = random.choice(os.listdir("hardpuzzles")) #change dir name if necessary hardpuzzle = "hardpuzzles/" + hardpuzzle firstSnapshot = Futoshiki_IO.loadPuzzle(hardpuzzle) Solver.solve(firstSnapshot, screen) # Limit to 20 frames per second clock.tick(10)
def solve(snapshot, screen): # display current snapshot pygame.time.delay(200) Futoshiki_IO.displayPuzzle(snapshot, screen) pygame.display.flip()
def solve(snapshot, screen): # display current snapshot # print(snapshot) less, greater = updateCellPossibles(snapshot) pygame.time.delay(20) Futoshiki_IO.displayPuzzle(snapshot, screen) pygame.display.flip() if not (checkPosible(snapshot)): return False # print("check consitency") # checkConsistency(snapshot) # print("check consitency end") if (isComplete(snapshot, less, greater) and checkConsistency(snapshot)): # print(count) return True elif (isComplete(snapshot, less, greater)): return False # if current snapshot is complete ... return a value # if isComplete(snapshot) and checkConsistency(snapshot): # return True # else: # return False # if current snapshot not complete ... # get next empty cell cellsTODO = snapshot.getCellsPoss() # getting all cells with 1 possibility and setting them cells1pos = [c for c in cellsTODO if c.getPossiblesLen() == 1] if len(cells1pos): newsnapshot = snapshot.clone() for c in cells1pos: newsnapshot.setCellVal(c.row, c.col, c.getPossibles()[0]) if (solve(newsnapshot, screen)): return True else: return False for cell in cellsTODO: possibles = cell.getPossibles() for num in cell.getPossibles(): newsnapshot = snapshot.clone() newsnapshot.setCellVal(cell.row, cell.col, num) x = solve(newsnapshot, screen) if (x): return True # w=input() return False # for each value in the cells possibles list: # newsnapshot = ....clone current snapshot and update the cell with the value # if new snapshot is consistent, perform recursive call to solve # if checkConsistency(newsnapshot): # success = solve(newsnapshot, screen) # if success: return True # if we get to here no way to solve from current snapshot # return False # Check whether a snapshot is consistent, i.e. all cell values comply # with the Futoshiki rules (each number occurs only once in each row and column, # no "<" constraints violated). return False