def replay(self): s = self.init steps = self.goal.moves i = 0 print("\033[1H", end="") print("\033[J", end="") print(s) print_moves(steps, i) try: while getKey() != moves.right: continue except KeyboardInterrupt: return for step in steps: if not step.normalMove(): continue if step == moves.swap: s.toggleActive() else: move(s, step) i += 1 print("\033[1H", end="") print("\033[J", end="") print(s) print_moves(steps, i) try: k = getKey() while k != moves.right: if k == '\n': break continue except KeyboardInterrupt: break
def HILL(s): setTrace = s.setTrace count = 0 state = s.queue.get() goal = None for line in state.board: for tile in line: if tile is not None and tile.isGoal(): goal = [state.board.index(line), line.index(tile)] stack = [state] while len(stack) > 0: print("[+] Looping... {:5} / 50000".format(count), end='\r') if count > 50000: return False count += 1 cur_state = stack[-1] stack.pop() if cur_state.isGoal(): s.goal = cur_state print('[+] I have found the solution after {} iterations'.format( count)) return True priority = [] for numBlocks in range(2 if cur_state.isSplit() else 1): # for each block in blox for step in range(0 if cur_state.isSplit() else 1, 5): # up down left right try: new_state = deepcopy(cur_state) new_state.setActiveBlock(numBlocks + 1) m = moves(step) move(new_state, m) new_trace = getTrace(new_state.getBoard(), new_state.blox) newEval = EVAL(new_state, goal) if new_trace not in setTrace: priority.append(pair(new_state, newEval)) setTrace.add(new_trace) except Exception: continue priority.sort(reverse=True) for i in range(len(priority)): stack.append(priority[i].state) return False
def DFS(s): stack = [s.queue.get()] setTrace = s.setTrace count = 0 while len(stack) > 0: print("[+] Looping... {:5} / 50000".format(count), end='\r') if count > 50000: return False count += 1 cur_state = stack[-1] stack.pop() if cur_state.isGoal(): s.goal = cur_state print('[+] I have found the solution after {} iterations'.format(count)) return True for numBlocks in range(2 if cur_state.isSplit() else 1): # for each block in blox for step in range(0 if cur_state.isSplit() else 1, 5): # up down left right try: new_state = deepcopy(cur_state) new_state.setActiveBlock(numBlocks + 1) m = moves(step) move(new_state, m) new_trace = getTrace(new_state.getBoard(), new_state.blox) # print(new_trace) if new_trace not in setTrace: # print(new_state) stack.append(new_state) setTrace.add(new_trace) # if step != 0: # if cur_state.getSelectingBlock() != (numBlocks + 1): # new_state.moves.append(' ') # new_state.moves.append(m) except Exception: continue return False
def play(f, mode=None, replay=None): stage = load(f) init = State(stage) if mode is None: """ print("Load moves files? Please enter file name") filename = input("$>> ") if filename: pass else: input("Gaming mode, Ctrl+C to quit") """ input("Welcome to stage: {}".format(init.name)) s = init while True: print("\033[1H", end="") print("\033[J", end="") print(s) print_moves(s.moves) if s.isGoal(): print("[+] You made {} moves".format(s.moves_made())) print("[+] Press to continue") click.getchar() break try: print("[+] Make a move") key = getKey() while key == ' ': if not s.isSplit(): break s.toggleActive() print("\033[1H", end="") print("\033[J", end="") print(s) print_moves(s.moves) print("[+] Make a move") key = getKey() except KeyboardInterrupt: key = input("Quit now? (y/n) ") if key == "y" or key == "Y": break if not isinstance(key, moves): continue try: move(s, key) except Exception as e: print("--- Invalid move, revert\n--- {}".format(e)) click.getchar() else: problem = Solver(init, mode) # tracemalloc.start() start = time.time() status = problem.solve() end = time.time() # snapshot = tracemalloc.take_snapshot() # tracemalloc.stop() # top_stats = snapshot.statistics("lineno") # print(len(top_stats)) with open('stat.csv', 'a') as f: f.write('{},{},{}\n'.format(mode, init.name, end-start)) f.close() print(problem) if status is False: click.getchar() return if replay == 2: click.getchar() return if replay == 3: k = input("Replay? (y/n) ") if k != "y" and k != "Y": return print("Press right arrow to next move") print("If no move remains, press enter to exit") click.getchar() problem.replay()