def generate_puzzle_list(self): self.puzzle_list.clear() for color_obj in Colors: for shape in Shapes: self.puzzle_list.append(Puzzle(shape, color_obj, 0, 0)) self.puzzle_list.append(Puzzle(shape, color_obj, 0, 0)) random.shuffle(self.puzzle_list) print(self.puzzle_list) positions = [] translation_x = (self.screen_width - 60 * 6) / 2 translation_y = (self.screen_height - 60 * 4) / 2 for x in range(6): for y in range(4): positions.append({ 'x': translation_x + x * 60, 'y': translation_y + y * 60 }) it = 0 for puzzle in self.puzzle_list: puzzle.x_position = positions[it]['x'] puzzle.y_position = positions[it]['y'] print( str(it) + " Position: ", (puzzle.x_position, puzzle.y_position)) it += 1 self.puzzles_left = len(self.puzzle_list) / 2
def ucs(tableroInicial, tableroFinal): f = open('resultado.txt', 'w') agenda = Queue.PriorityQueue() root = Puzzle.Puzzle(tableroInicial, None, 2, 2, "inicial") #root.setheuristic(Puzzle.Puzzle.calculaHeuristica(root.value)) agenda.put((0, root)) #print(root.heuristic) #print(Puzzle.Puzzle.calculaHeuristica(tableroInicial)) expandido = agenda.get()[ 1] #el [1 ] indica que obtienes el nodo y no la prioridad. #print(expandido.heuristic) #print(expandido.heuristic) expansion = Puzzle.Puzzle.returnMoves(expandido) nodosExpandidos = {str(expandido.value): expandido} for hijo in expansion: #hijo.setheuristic(Puzzle.Puzzle.calculaHeuristica(hijo.value)) #hijo.setheuristic(Puzzle.Puzzle.calculaHeuristica(hijo.value)+expandido.heuristic) agenda.put((1, hijo)) while Puzzle.Puzzle.comparePuzzles( tableroFinal, expansion ) == -1: #si es igual a cero quiere decir que no son iguales por lo tanto seguimos buscando expandido = agenda.get()[1] pesoExpandido = agenda.get()[0] #print(pesoExpandido) if not nodosExpandidos.__contains__(str(expandido.value)): # print(expandido.heuristic) expansion = Puzzle.Puzzle.returnMoves(expandido) nodosExpandidos[str(expandido.value)] = expandido for hijo in expansion: hijo.setheuristic( Puzzle.Puzzle.calculaHeuristica(hijo.value)) #hijo.setheuristic(Puzzle.Puzzle.calculaHeuristica(hijo.value)+expandido.heuristic) #hijo.setheuristic(Puzzle.Puzzle.calculaHeuristica(hijo.value)+expandido.heuristic) agenda.put((1 + pesoExpandido, hijo)) print("terminado por UCS esta es la respuesta:") resultado = [ Puzzle.Puzzle(tableroFinal, expandido, 3, 3, "Final?"), expandido ] parcial = expandido.padre while parcial.padre != None: resultado.append(parcial) parcial = parcial.padre resultado.append(parcial) resultado.reverse() Puzzle.Puzzle.animaResultado(resultado, tableroInicial) for tablerin in resultado: f.write(repr(tablerin) + "\n") f.write("numero de movimientos:" + str(resultado.__len__() - 1))
def test_any_duplicates(self): """Tests any_duplicates()""" la = [1,0] # No duplicates lb = [1,1] # 1 duplicate lc = [1,2,3,4,5,6,7,8,9,0] # Longer list, no duplicates ld = [1,1,2,3,4,5,6,7,8,9] # Longer list, 1 duplicate le = [1,1,2,2,3,3,4,4,5,5] # Longer list, multiple duplicates # Makes sure it spots duplicates self.assertTrue(Puzzle.any_duplicates(lb)) self.assertTrue(Puzzle.any_duplicates(ld)) self.assertTrue(Puzzle.any_duplicates(le)) # Makes sure it knows there are none self.assertFalse(Puzzle.any_duplicates(la)) self.assertFalse(Puzzle.any_duplicates(lc))
def _puzzle8(): puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,6,1) puzzle.set_loc_answer(1,0,2) puzzle.set_loc_answer(1,3,7) puzzle.set_loc_answer(1,7,9) puzzle.set_loc_answer(2,2,3) puzzle.set_loc_answer(2,3,1) puzzle.set_loc_answer(2,7,8) puzzle.set_loc_answer(2,8,4) puzzle.set_loc_answer(3,1,6) puzzle.set_loc_answer(3,5,8) puzzle.set_loc_answer(4,0,1) puzzle.set_loc_answer(4,1,8) puzzle.set_loc_answer(4,4,2) puzzle.set_loc_answer(4,6,7) puzzle.set_loc_answer(4,7,6) puzzle.set_loc_answer(5,2,4) puzzle.set_loc_answer(6,1,3) puzzle.set_loc_answer(6,5,4) puzzle.set_loc_answer(7,0,6) puzzle.set_loc_answer(7,3,2) puzzle.set_loc_answer(7,6,8) puzzle.set_loc_answer(8,5,3) puzzle.set_loc_answer(8,6,5) puzzle.set_loc_answer(8,8,9) return puzzle
def _puzzle5(): #expert puzzle puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,0,3) puzzle.set_loc_answer(0,1,7) puzzle.set_loc_answer(0,2,9) puzzle.set_loc_answer(1,0,8) puzzle.set_loc_answer(1,3,4) puzzle.set_loc_answer(1,4,2) puzzle.set_loc_answer(2,0,2) puzzle.set_loc_answer(2,4,1) puzzle.set_loc_answer(3,1,5) puzzle.set_loc_answer(3,7,2) puzzle.set_loc_answer(4,2,7) puzzle.set_loc_answer(4,8,8) puzzle.set_loc_answer(5,4,6) puzzle.set_loc_answer(5,5,5) puzzle.set_loc_answer(5,8,4) puzzle.set_loc_answer(6,3,9) puzzle.set_loc_answer(7,1,8) puzzle.set_loc_answer(7,3,3) puzzle.set_loc_answer(7,6,7) puzzle.set_loc_answer(8,5,2) puzzle.set_loc_answer(8,6,3) puzzle.set_loc_answer(8,7,9) return puzzle
def solve_astar(puzzle_index, puzzle_array): puzzle = Puzzle(puzzle_array) for i in range(0, 1): print('- heuristic:', i) start_time = time.time() solution, visited = a_star(puzzle, i) end_time = time.time() if solution == 0: with open( "output/" + str(puzzle_index) + "_astar-" + h_to_text(i) + "_search.txt", "w") as search_file: search_file.write("no solution") with open( "output/" + str(puzzle_index) + "_astar-" + h_to_text(i) + "_solution.txt", "w") as solution_file: solution_file.write("no solution") else: solution.reverse() index = 0 with open( "output/" + str(puzzle_index) + "_astar-" + h_to_text(i) + "_solution.txt", "w") as solution_file: for step in solution: if index == 0: tc = "0 0" index += 1 else: tc = str(np.amax(np.bitwise_xor(step.state, solution[index - 1].state))) \ +" "+str(step.cost) index += 1 solution_file.write(tc + " " + getArrayInString(step.state) + "\n") total_cost = get_cost( visited[len(visited) - 1], visited[visited[len(visited) - 1].parent_index], visited) solution_file.write( str(total_cost) + " " + str(round(end_time - start_time, 4))) index = 0 with open( "output/" + str(puzzle_index) + "_astar-" + h_to_text(i) + "_search.txt", "w") as search_file: for visit in visited: try: parent = visited[visit.parent_index] except TypeError: parent = None search_file.write( str(visit.f_val) + " " + str(get_cost(visit, parent, visited)) + " " + str(visit.f_val - get_cost(visit, parent, visited)) + " " + getArrayInString(visit.state) + "\n") index += 1 #solve_astar(99, [1, 3, 5, 7, 2, 4, 6, 0])
def _puzzle_xwing_row(): #used to test solving method x_wing_row puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,0,1) puzzle.set_loc_answer(0,6,5) puzzle.set_loc_answer(0,7,6) puzzle.set_loc_answer(0,8,9) puzzle.set_loc_answer(1,0,4) puzzle.set_loc_answer(1,1,9) puzzle.set_loc_answer(1,2,2) puzzle.set_loc_answer(1,4,5) puzzle.set_loc_answer(1,5,6) puzzle.set_loc_answer(1,6,1) puzzle.set_loc_answer(1,8,8) puzzle.set_loc_answer(2,1,5) puzzle.set_loc_answer(2,2,6) puzzle.set_loc_answer(2,3,1) puzzle.set_loc_answer(2,5,9) puzzle.set_loc_answer(2,6,2) puzzle.set_loc_answer(2,7,4) puzzle.set_loc_answer(3,2,9) puzzle.set_loc_answer(3,3,6) puzzle.set_loc_answer(3,4,4) puzzle.set_loc_answer(3,6,8) puzzle.set_loc_answer(3,8,1) puzzle.set_loc_answer(4,1,6) puzzle.set_loc_answer(4,2,4) puzzle.set_loc_answer(4,4,1) puzzle.set_loc_answer(5,0,2) puzzle.set_loc_answer(5,1,1) puzzle.set_loc_answer(5,2,8) puzzle.set_loc_answer(5,4,3) puzzle.set_loc_answer(5,5,5) puzzle.set_loc_answer(5,6,6) puzzle.set_loc_answer(5,8,4) puzzle.set_loc_answer(6,1,4) puzzle.set_loc_answer(6,3,5) puzzle.set_loc_answer(6,7,1) puzzle.set_loc_answer(6,8,6) puzzle.set_loc_answer(7,0,9) puzzle.set_loc_answer(7,2,5) puzzle.set_loc_answer(7,4,6) puzzle.set_loc_answer(7,5,1) puzzle.set_loc_answer(7,6,4) puzzle.set_loc_answer(7,8,2) puzzle.set_loc_answer(8,0,6) puzzle.set_loc_answer(8,1,2) puzzle.set_loc_answer(8,2,1) puzzle.set_loc_answer(8,8,5) return puzzle
def solve_gbfs(puzzle_array, puzzle_index): for i in range(0, 1): print('- heuristic:', i) puzzle = Puzzle(puzzle_array) start_time = time.time() search_file = open("output/" + str(puzzle_index) + "_gbfs-h" + str(i) + "_search.txt", "w") solution_file = open("output/" + str(puzzle_index) + "_gbfs-h" + str(i) + "_solution.txt", "w") solution, history = gbfs(puzzle, i, start_time) write_search(search_file, history, i) write_solution(solution_file, start_time, solution)
def make_grid_lst(lst): puzzle = Puzzle.Puzzle() grid_lst = [puzzle.get_grid(0, x) for x in range(len(lst))] for ind in range(len(grid_lst)): grid = grid_lst[ind] grid._note = lst[ind] """ if lst[ind] == [5,6] or lst[ind] == [5,6,7]: # for test_quad grid.belong = True """ return grid_lst, puzzle
def action(txt): # if unable to complete, set puzzle to a new empty puzzle if txt == 'Puzzle Solved': update_vals() elif txt == "This puzzle is unsolvable": messagebox.showinfo(message=txt) global puzzle puzzle = Puzzle.Puzzle() elif txt == 'Puzzle already solved': messagebox.showinfo(message=txt) update_vals()
def test_remove_grid_vals(): grid1 = Grid.Grid(0, 0) grid2 = Grid.Grid(2, 3) grid3 = Grid.Grid(6, 1) lst1 = [grid1, grid2, grid3] show_run_func("lst1 = [grid1, grid2, grid3]") keep1 = [grid2] show_run_func("keep1 = [grid2]") remove_grid_vals(lst1, keep1, [1, 2, 3, 4, 5, 6, 7], Puzzle.Puzzle()) show_test("remove_grid_vals(lst1, keep1, [1,2,3,4,5,6,7])", [grid.get_note() for grid in lst1], [[8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [8, 9]])
def dfs(tableroInicial, tableroFinal): f = open('resultado.txt', 'w') agenda = [Puzzle.Puzzle(tableroInicial,None,1,3,"inicial")] expandido = agenda.pop() expansion = Puzzle.Puzzle.returnMoves(expandido) nodosExpandidos = {str(expandido.value):expandido} for hijo in expansion: agenda.append(hijo) while Puzzle.Puzzle.comparePuzzles(tableroFinal,expansion)==-1: #si es igual a cero quiere decir que no son iguales por lo tanto seguimos buscando expandido = agenda.pop() if not nodosExpandidos.__contains__(str(expandido.value)): expansion = Puzzle.Puzzle.returnMoves(expandido) nodosExpandidos[str(expandido.value)] = expandido for hijo in expansion: agenda.append(hijo) print("terminado por dFS esta es la respuesta:") resultado = [Puzzle.Puzzle(tableroFinal,expandido,2,2,"Final?"),expandido] parcial = expandido.padre while parcial.padre!=None: resultado.append(parcial) parcial =parcial.padre resultado.append(parcial) resultado.reverse() for tablerin in resultado: f.write(repr(tablerin)+"\n") f.write("numero de movimientos:"+str(resultado.__len__()-1)) Puzzle.Puzzle.animaResultado(resultado, tableroInicial)
def bfs(tableroInicial,tableroFinal): agenda = deque([Puzzle.Puzzle(tableroInicial,None,1,2,"inicial")]) expandido = agenda.popleft() expansion = Puzzle.Puzzle.returnMoves(expandido) nodosExpandidos = {str(expandido.value):expandido} for hijo in expansion: agenda.append(hijo) print(hijo.value) while Puzzle.Puzzle.comparePuzzles(tableroFinal,expansion)==-1: #si es igual a cero quiere decir que no son iguales por lo tanto seguimos buscando expandido = agenda.popleft() if not nodosExpandidos.__contains__(str(expandido.value)): expansion = Puzzle.Puzzle.returnMoves(expandido) nodosExpandidos[str(expandido.value)] = expandido for hijo in expansion: agenda.append(hijo) print("terminado por BFS esta es la respuesta:") resultado = [Puzzle.Puzzle(tableroFinal,expandido,3,3,"Final?"),expandido] parcial = expandido.padre while parcial.padre!=None: resultado.append(parcial) parcial =parcial.padre resultado.append(parcial) resultado.reverse() Puzzle.Puzzle.animaResultado(resultado, tableroInicial) print("numero de movimientos:"+str(resultado.__len__()))
def _puzzle1(): #easy puzzle puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,1,4) puzzle.set_loc_answer(0,2,7) puzzle.set_loc_answer(0,4,1) puzzle.set_loc_answer(0,7,3) puzzle.set_loc_answer(1,0,9) puzzle.set_loc_answer(1, 3, 3) puzzle.set_loc_answer(1,5,6) puzzle.set_loc_answer(1,7,7) puzzle.set_loc_answer(2,0,5) puzzle.set_loc_answer(2,2,6) puzzle.set_loc_answer(2,3,8) puzzle.set_loc_answer(2,4,4) puzzle.set_loc_answer(2,5,7) puzzle.set_loc_answer(2,6,2) puzzle.set_loc_answer(3,1,7) puzzle.set_loc_answer(3,2,2) puzzle.set_loc_answer(3,6,1) puzzle.set_loc_answer(3,7,5) puzzle.set_loc_answer(3,8,3) puzzle.set_loc_answer(4,0,1) puzzle.set_loc_answer(4,2,5) puzzle.set_loc_answer(4,5,4) puzzle.set_loc_answer(4,7,9) puzzle.set_loc_answer(4,8,2) puzzle.set_loc_answer(5,1,6) puzzle.set_loc_answer(5,2,9) puzzle.set_loc_answer(5,4,2) puzzle.set_loc_answer(5,6,7) puzzle.set_loc_answer(6,2,4) puzzle.set_loc_answer(6,3,9) puzzle.set_loc_answer(6,5,2) puzzle.set_loc_answer(6,6,3) puzzle.set_loc_answer(7,0,2) puzzle.set_loc_answer(7,1,9) puzzle.set_loc_answer(7,3,5) puzzle.set_loc_answer(7,4,7) puzzle.set_loc_answer(8,3,4) puzzle.set_loc_answer(8,4,8) puzzle.set_loc_answer(8,8,7) return puzzle
def _puzzle2(): # IMPOSSIBLE TO SOLVE PUZZLE puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,0,1) puzzle.set_loc_answer(0,1,2) puzzle.set_loc_answer(0,2,3) puzzle.set_loc_answer(0,3,4) puzzle.set_loc_answer(0,4,5) puzzle.set_loc_answer(0,5,6) puzzle.set_loc_answer(0,6,7) puzzle.set_loc_answer(0,7,8) puzzle.set_loc_answer(0,8,9) puzzle.set_loc_answer(1,0,1) return puzzle
def __init__(self, board): #size = side length 1, 2, 3, etc... len = board.__len__() size = sqrt(board.__len__()) if round(size) != size: raise 'BadBoardError', str(len) + ' is not a valid board size, yo.' return self.size = int(size) #self.board = self.rep_board(board) self.board = board #save point Puzzle(True)
def __init__(self, B = (3,2), V = ((0,0),(1,0),(3,0),(4,0)), \ H = ((0,2),(0,3)), S = ((2,2),(2,3)), E = ((2,0),(2,1)) ): if linear(H[0]) > linear(H[1]): H = (H[1], H[0]) if linear(S[0]) > linear(S[1]): S = (S[1], S[0]) if linear(E[0]) > linear(E[1]): E = (E[1], E[0]) # sort V tmp = map(lambda elt: (linear(elt), elt), V) # cons linear num to front tmp.sort() # sort by it (linear num) V = tuple(map(lambda elt: elt[1], tmp)) # reassign to sorted list self.B, self.V, self.H, self.S, self.E = B, V, H, S, E Puzzle(False) # Multiple solutions!
def _puzzle4(): #hard puzzle puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,4, 6) puzzle.set_loc_answer(0,7,1) puzzle.set_loc_answer(0,8,9) puzzle.set_loc_answer(1,1,1) puzzle.set_loc_answer(1,6,7) puzzle.set_loc_answer(1,7,8) puzzle.set_loc_answer(1,8,6) puzzle.set_loc_answer(2,0,6) puzzle.set_loc_answer(2,2,3) puzzle.set_loc_answer(2,3,1) puzzle.set_loc_answer(2,4,9) puzzle.set_loc_answer(3,0,7) puzzle.set_loc_answer(3,1,6) puzzle.set_loc_answer(3,5,9) puzzle.set_loc_answer(4,3,7) puzzle.set_loc_answer(4,7,6) puzzle.set_loc_answer(5,4,8) puzzle.set_loc_answer(5,6,9) puzzle.set_loc_answer(5,7,7) puzzle.set_loc_answer(5,8,4) puzzle.set_loc_answer(6,0,8) puzzle.set_loc_answer(6,2,1) puzzle.set_loc_answer(6,3,9) puzzle.set_loc_answer(6,4,2) puzzle.set_loc_answer(6,6,5) puzzle.set_loc_answer(7,1,5) puzzle.set_loc_answer(7,4,7) puzzle.set_loc_answer(7,5,3) puzzle.set_loc_answer(8,1,4) puzzle.set_loc_answer(8,5,1) return puzzle
def _puzzle_xwing_col(): puzzle= Puzzle.Puzzle() puzzle.set_loc_answer(0,7,9) puzzle.set_loc_answer(0,8,4) puzzle.set_loc_answer(1,0,7) puzzle.set_loc_answer(1,1,6) puzzle.set_loc_answer(1,3,9) puzzle.set_loc_answer(1,4,1) puzzle.set_loc_answer(1,7,5) puzzle.set_loc_answer(2,1,9) puzzle.set_loc_answer(2,5,2) puzzle.set_loc_answer(2,7,8) puzzle.set_loc_answer(2,8,1) puzzle.set_loc_answer(3,1,7) puzzle.set_loc_answer(3,4,5) puzzle.set_loc_answer(3,7,1) puzzle.set_loc_answer(4,3,7) puzzle.set_loc_answer(4,5,9) puzzle.set_loc_answer(5,1,8) puzzle.set_loc_answer(5,4,3) puzzle.set_loc_answer(5,5,1) puzzle.set_loc_answer(5,7,6) puzzle.set_loc_answer(5,8,7) puzzle.set_loc_answer(6,0,2) puzzle.set_loc_answer(6,1,4) puzzle.set_loc_answer(6,3,1) puzzle.set_loc_answer(6,7,7) puzzle.set_loc_answer(7,1,1) puzzle.set_loc_answer(7,4,9) puzzle.set_loc_answer(7,7,4) puzzle.set_loc_answer(7,8,5) puzzle.set_loc_answer(8,0,9) puzzle.set_loc_answer(8,6,1) return puzzle
def test_lonely(): puzz = Puzzle.Puzzle() grid1 = puzz.get_grid(0, 0) grid2 = puzz.get_grid(0, 6) grid1._note = [1] grid2._note = [9] lonely(puzz.get_row_grids(grid1), puzz) show_run_func("lonely(puzz.get_row_grids(grid1), puzz)") show_test("get_grid(0,0)", str(puzz.get_grid(0, 0)), 'Grid at (0, 0), square: 0; Value: 1') show_test("get_grid(0,6)", str(puzz.get_grid(0, 6)), 'Grid at (0, 6), square: 2; Value: 9') show_test('get_grid(0,3).get_note()', puzz.get_grid(0, 3).get_note(), [x for x in range(2, 9)]) #check column show_test('get_grid(6,0).get_note()', puzz.get_grid(6, 0).get_note(), [x for x in range(2, 10)]) #check row show_test('get_grid(2,2).get_note()', puzz.get_grid(2, 2).get_note(), [x for x in range(2, 10)]) #check square
def test_lonestar(): helper = helper_grid(lonestar, 'lonestar') helper(lst1, 'lst1', lst1result) helper(lst2, 'lst2', lst2result) helper(lst3, 'lst3', lst3result) helper(lst4, 'lst4', lst4result) puzzle = Puzzle.Puzzle() grid_lst = [puzzle.get_grid(0, x) for x in range(len(lst4))] for ind in range(len(grid_lst)): grid = grid_lst[ind] grid._note = lst4[ind] lonestar(grid_lst, puzzle) show_test("get_grid(0,2)", str(puzzle.get_grid(0, 2)), 'Grid at (0, 2), square: 0; Value: 9') show_test("get_grid(0,4)", str(puzzle.get_grid(0, 4)), 'Grid at (0, 4), square: 1; Value: 5') show_test('get_grid(4,2).get_note()', puzzle.get_grid(4, 2).get_note(), [x for x in range(1, 9)]) #check column note is missing 9 show_test('get_grid(1,1).get_note()', puzzle.get_grid(1, 1).get_note(), [x for x in range(1, 9)]) #check row note is mssing 9
def _puzzle6(): # expert puzzle puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,0,8) puzzle.set_loc_answer(0,1,4) puzzle.set_loc_answer(0,4,2) puzzle.set_loc_answer(0,5,7) puzzle.set_loc_answer(0,7,3) puzzle.set_loc_answer(0,8,1) puzzle.set_loc_answer(1,3,8) puzzle.set_loc_answer(2,0,1) puzzle.set_loc_answer(2,1,3) puzzle.set_loc_answer(2,5,4) puzzle.set_loc_answer(3,0,3) puzzle.set_loc_answer(3,1,2) puzzle.set_loc_answer(3,4,7) puzzle.set_loc_answer(3,6,4) puzzle.set_loc_answer(4,1,5) puzzle.set_loc_answer(4,7,1) puzzle.set_loc_answer(4,8,8) puzzle.set_loc_answer(5,7,5) puzzle.set_loc_answer(6,0,6) puzzle.set_loc_answer(6,3,9) puzzle.set_loc_answer(6,4,8) puzzle.set_loc_answer(7,8,5) puzzle.set_loc_answer(8,0,7) puzzle.set_loc_answer(8,7,6) return puzzle
def _puzzle7(): puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,1,5) puzzle.set_loc_answer(1,0,4) puzzle.set_loc_answer(1,1,6) puzzle.set_loc_answer(1,2,9) puzzle.set_loc_answer(1,8,5) puzzle.set_loc_answer(2,5,9) puzzle.set_loc_answer(2,6,3) puzzle.set_loc_answer(3,3,5) puzzle.set_loc_answer(3,5,7) puzzle.set_loc_answer(3,6,2) puzzle.set_loc_answer(4,0,1) puzzle.set_loc_answer(4,4,3) puzzle.set_loc_answer(5,7,1) puzzle.set_loc_answer(6,0,6) puzzle.set_loc_answer(6,8,7) puzzle.set_loc_answer(7,0,7) puzzle.set_loc_answer(7,2,4) puzzle.set_loc_answer(7,3,2) puzzle.set_loc_answer(7,6,1) puzzle.set_loc_answer(8,0,8) puzzle.set_loc_answer(8,3,6) puzzle.set_loc_answer(8,7,4) puzzle.set_loc_answer(8,8,2) return puzzle
action='store_true', help="Display per-puzzle statistics") parser.add_argument("-ida", default=False, action='store_true', help="IdA-Star search or A-Star") parser.add_argument("input", help="input start") parser.parse_args() args = parser.parse_args() try: f = open(args.input, "r") args.input = f.read() except Exception: print("NB : This is not a file") size, start = parse_puzzel(args) temp = Puzzle(0) # goals if args.s == 'zero_first': goal = temp.ft_zero_first(size) elif args.s == 'zero_last': goal = temp.ft_zero_last(size) else: goal = temp.ft_spiralPrint(size) sol = temp.ft_solvable(start, goal, size) if sol: cur = Puzzle(size) start_time = time.time() print "Wait please..." # apply the right algorithm according to flags if args.ida: root = Node(start, -1, 0, 0, 0, 0)
def __init__(self, F = False, C = False, G = False, B = False): self.F, self.C, self.G, self.B = F, C, G, B Puzzle(True) # Fox | Chicken | Grain | Boat
import Puzzle connector = Puzzle.Puzzle('MapSettings.json') def ForcedSolvingAction(): print("im solving the puzzle") connector.UpdateSensedValue('1324') def CheckingAction(newVal): if newVal == '33': return True else: return False def ResetAction(): connector.UpdateSensedValue('0') connector.UpdateStatus("unsolved") def WhenSolvedAction(): print(" ++ Congratulations") connector.DoForceSolve = ForcedSolvingAction connector.CheckIfSolved = CheckingAction connector.OnSolved = WhenSolvedAction connector.DoReset = ResetAction
import Puzzle if __name__ == '__main__': value = raw_input("Enter") print("You entered " + value) puzzle = Puzzle(3)
#!/usr/bin/env python import sys import Puzzle from RandomWalk import RandomWalk from DepthFirstSearch import DepthFirstSearch from BreadthFirstSearch import BreadthFirstSearch from AStarSearch import * if __name__ == '__main__': if len(sys.argv) < 3: print 'Usage: python main.py [random/depth/breadth/uniform] [filenames]' sys.exit(0) else: for filename in sys.argv[2:]: puzzle = Puzzle.loadGameState(filename) if sys.argv[1] == 'random': print '\nSearching ' + filename + ' by a random walk\n' print puzzle randomWalker = RandomWalk(puzzle) randomWalker.search() elif sys.argv[1] == 'depth': print '\nSolving ' + filename + ' by depth-first search\n' print puzzle searcher = DepthFirstSearch() searcher.solve(puzzle) print depthSearcher.getStatistics() elif sys.argv[1] == 'breadth': print '\nSolving ' + filename + ' by breadth-first search\n' print puzzle searcher = BreadthFirstSearch()
def __init__(self, size=4): self.size = size temp = range(1, size**2) temp.append(0) self.board = tuple(temp) Puzzle(True)
def __init__(self, seqSize=20, rotSize=4): if (rotSize > 1) and (seqSize > rotSize): self.seqSize = seqSize self.rotSize = rotSize self.sequence = tuple(range(1, self.seqSize + 1)) Puzzle(True) # 1, 2, 3, ... 20 are in order
def __init__(self, pieces=range(8), orientations=[0 for i in range(8)]): self.pieces = pieces self.orientations = orientations if len(pieces) != len(orientations): raise "Pieces array different length than orientations array" Puzzle(True)
def _puzzle9(): # Impossible puzzle puzzle = Puzzle.Puzzle() puzzle.set_loc_answer(0,0,1) return puzzle