def BFS(board, attempt, attempt_num, parent, k): myboard[0] = actions.MoveLeft(board) myboard[1] = actions.MoveRight(board) myboard[2] = actions.MoveUp(board) myboard[3] = actions.MoveDown(board) for i in range(0, 4): print(myboard[i]) for i in range(0, 4): if (square2flat(myboard[i]) in board_tracker ): # board configuration is already saved. Move on verbose("Board configuration is already saved.") return else: verbose("Adding board to list") board_tracker[k] = square2flat(myboard[i]) k = k + 1 #print(attempt_num) attempt[attempt_num] = attempt[parent].copy() attempt[attempt_num].append(square2flat(myboard[i])) parent = attempt_num attempt_num += 1 # print(attempt[parent]) BFS(myboard[i], attempt, attempt_num + i, parent, k)
def findAllPerms(board, k, attempt): #4 ways to move myboard[0] = actions.MoveLeft(board) myboard[1] = actions.MoveRight(board) myboard[2] = actions.MoveUp(board) myboard[3] = actions.MoveDown(board) for i in range(0, 4): verbose("Flatboard is: " + flatboard_spaceless(myboard[i])) if (flatboard_spaceless(myboard[i]) in board_tracker ): # board configuration is already saved. Move on verbose("Board configuration is already saved.") #return elif (flatboard_spaceless( myboard[i]) == flatboard_spaceless(goal)): #goal found print("Goal found!") print(flatboard_spaceless(myboard[i])) board_tracker[k] = flatboard_spaceless(myboard[i]) k = k + 1 return else: verbose("Adding board to list") board_tracker[k] = flatboard_spaceless( myboard[i]) # board configuration is not saved yet. Save it k = k + 1 findAllPerms(myboard[i], k)
def adjacent_moves(self): #default actions a player can do moves = [] #pdb.set_trace() if world.tile_exists(self.x + 1, self.y): moves.append(actions.MoveRight()) if world.tile_exists(self.x - 1, self.y): moves.append(actions.MoveLeft()) if world.tile_exists(self.x, self.y - 1): moves.append(actions.MoveUp()) if world.tile_exists(self.x, self.y + 1): moves.append(actions.MoveDown()) return moves
def adjacent_moves(self): """ Returns all move actions for adjacent tiles """ moves = [] if world.tile_exists(self.x + 1, self.y): moves.append(actions.MoveLeft()) if world.tile_exists(self.x - 1, self.y): moves.append(actions.MoveRight()) if world.tile_exists(self.x, self.y - 1): moves.append(actions.MoveUp()) if world.tile_exists(self.x, self.y + 1): moves.append(actions.MoveDown()) return moves
verbose("Board configuration is already saved.") return else: verbose("Adding board to list") board_tracker[k] = square2flat(myboard[i]) k = k + 1 #print(attempt_num) attempt[attempt_num] = attempt[parent].copy() attempt[attempt_num].append(square2flat(myboard[i])) parent = attempt_num attempt_num += 1 # print(attempt[parent]) BFS(myboard[i], attempt, attempt_num + i, parent, k) BFS(initialboard, attempt, 0, 0, k) print(attempt) a = flat2square("123456780") b = actions.MoveLeft(a) #print(attempt) #print(a) #print(b) end = datetime.now() runtime = end - start #runtime=runtime.strftime("%H:%M:%S") print("Finished in " + str(runtime) + " (hours:min:sec)")
nodes_list = [] # board = square2flat(initialboard.copy()) nodes_list.append([board, -1]) queue = collections.deque() queue.append(0) i = 0 myboard = np.empty(4, dtype='object') found_goal = False while queue: # Add new moves to the queue. The Moves return empty if the move is invalid parent_node = queue.popleft() board = nodes_list[parent_node][0] myboard[0] = actions.MoveLeft(board) myboard[1] = actions.MoveRight(board) myboard[2] = actions.MoveUp(board) myboard[3] = actions.MoveDown(board) for b in range(0, 4): # Don't want empty entries and don't want any repeat entries if (myboard[b] is not None) and (myboard[b] not in board_tracker): board_tracker.add(myboard[b]) #add it to the board_tracker PrintNodesFile(myboard[b]) PrintNodesInfo(len(nodes_list) - 1, parent_node) nodes_list.append([myboard[b], parent_node]) queue.append(len(nodes_list) - 1) #print(nodes_list) #print(len(nodes_list))