diff --git a/Search_Algorithms/Sucessors.py b/Search_Algorithms/Sucessors.py index 05a518c8bfcaf0567722999eedf56972c71711ea..0de1748da760bb55db475c1c3638c8c07ff0795f 100644 --- a/Search_Algorithms/Sucessors.py +++ b/Search_Algorithms/Sucessors.py @@ -62,7 +62,29 @@ def sudoku_allowed(state): def puzzle_successor(state): - return [] + y, x = -1, -1 + for i in range(len(state)): + for j in range(len(state[i])): + if state[i][j] == 0: + y, x = i, j + break + + possible_moves = [(y + 1, x), (y - 1, x), (y, x + 1), (y, x - 1)] + next_states = [] + + for move in possible_moves: + new_y, new_x = move + if 0 <= new_y < len(state) and 0 <= new_x < len(state[0]): + # make a copy of the state + new_state = [list(row) for row in state] + # swap tiles + new_state[y][x], new_state[new_y][new_x] = new_state[new_y][new_x], new_state[y][x] + # Convert state to a tuple of tuples + new_state_tuple = tuple(tuple(row) for row in new_state) + if puzzle_allowed(new_state_tuple): + next_states.append(new_state_tuple) + + return next_states def queens_successor(state): @@ -82,7 +104,7 @@ def queens_successor(state): def puzzle_allowed(state): - return False + return True def queens_allowed(state): diff --git a/Search_Algorithms/__pycache__/Search_Algorithms.cpython-311.pyc b/Search_Algorithms/__pycache__/Search_Algorithms.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..324b9ebb3b744554f17659a85e3d7296e59c750e Binary files /dev/null and b/Search_Algorithms/__pycache__/Search_Algorithms.cpython-311.pyc differ diff --git a/Search_Algorithms/__pycache__/Sucessors.cpython-311.pyc b/Search_Algorithms/__pycache__/Sucessors.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..65583285d0a03c9ebcb42aede0f6c20d30783620 Binary files /dev/null and b/Search_Algorithms/__pycache__/Sucessors.cpython-311.pyc differ diff --git a/Search_Algorithms/__pycache__/is_goal.cpython-311.pyc b/Search_Algorithms/__pycache__/is_goal.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..75ad5ea3b3f97d945274f3e03bcf33ac0edb7069 Binary files /dev/null and b/Search_Algorithms/__pycache__/is_goal.cpython-311.pyc differ diff --git a/Search_Algorithms/is_goal.py b/Search_Algorithms/is_goal.py index 503cd7416154efea8253c2e5b598b01985bbd494..305ea05c30e7585cfedbf9d4c9d245409ff07bb4 100644 --- a/Search_Algorithms/is_goal.py +++ b/Search_Algorithms/is_goal.py @@ -18,5 +18,12 @@ def is_goal_queens(state): return sum(map(sum, state)) == len(state) +PUZZLE_GOAL = ( + (0, 1, 2), + (3, 4, 5), + (6, 7, 8) +) + + def is_goal_puzzle(state): - pass + return PUZZLE_GOAL == state diff --git a/Search_Algorithms/solution testing.py b/Search_Algorithms/solution testing.py index 63c1b2a59ef8d5284c2db9c11989735915a201ac..3d8b29e95cf42ba7dc9f30eed0e9c9c754488641 100644 --- a/Search_Algorithms/solution testing.py +++ b/Search_Algorithms/solution testing.py @@ -19,7 +19,8 @@ grid = ((1, 0, 0, 0, 0, 0, 0, 0, 0), # sln = (Search_Algorithms.BreadthFirstSearch(grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1]) -sln = (Search_Algorithms.DepthFirstSearch(grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1]) +sln = (Search_Algorithms.DepthFirstSearch( + grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1]) for rows in sln: print(rows) @@ -38,14 +39,27 @@ board = ((False, False, False, False, False, False, False, False, False), # sln = (Search_Algorithms.BreadthFirstSearch(board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1]) -sln = (Search_Algorithms.DepthFirstSearch(board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1]) +sln = (Search_Algorithms.DepthFirstSearch( + board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1]) -output_tuple = tuple(tuple("Q" if value else "." for value in sln) for sln in sln) +output_tuple = tuple(tuple("Q" if value else "." for value in sln) + for sln in sln) for rows in output_tuple: print(rows) -# -# puzzle = () -# -# -# print(Search_Algorithms.BreadthFirstSearch(puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1]) + +# Sliding Puzzle + +initial_puzzle = ( + (7, 2, 4), + (5, 0, 6), + (8, 3, 1) +) + +sln = (Search_Algorithms.BreadthFirstSearch(initial_puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1]) + +if sln: + for rows in sln: + print(rows) +else: + print("No solution found")