JanSkn / 8-puzzle-solver

Different AI search algorithms to solve the 8-puzzle.
MIT License
1 stars 0 forks source link

Add new algorithms to solve the 8-puzzle #1

Open JanSkn opened 4 months ago

JanSkn commented 4 months ago

So far, there is Greedy and A* to solve the puzzle. Would be nice to add further algorithms.

levo-777 commented 4 months ago

https://deniz.co/8-puzzle-solver/

Breadth-First Search (BFS) Depth-First Search (DFS) Iterative Deeping Search

example BFS:

from collections import deque

def bfs(src,target):
    queue = deque([src])
    visited_states = set()
    while queue:
        state = queue.popleft()
        print("Visiting: ", state)
        if state == target:
            print("Found solution")
            return
        for move in possible_moves(state, visited_states):
            if tuple(move) not in queue and tuple(move) not in visited_states:
                queue.append(tuple(move))
                visited_states.add(tuple(move))
    print("No solution found.")

# Call the function
bfs(src = (1, 2, 3, 4, 5, 6, 7, 8, 0), target = (0, 1, 2, 3, 4, 5, 6, 7, 8))
JanSkn commented 4 months ago

hey, thank you! Do you want to adjust that to the GUI and open a PR? @levo-777