% Representation of the 8 Puzzle state % The puzzle is represented as a list of lists, where each list represents a row. % Empty tile is represented as 'e'. % Example initial state: [[1, 2, 3], [4, 5, 6], [7, 8, 'e']] % Example goal state: [[1, 2, 3], [4, 5, 6], [7, 8, 'e']] % Solving the 8 Puzzle problem using breadth-first search solve8Puzzle(StartState, GoalState, Solution):- bfs([[StartState]], GoalState, Solution). % Base case: When the current state is the goal state, return the solution. bfs([[GoalState|Path]|_], GoalState, [GoalState|Path]). % Recursive case: Expand the current state and continue the search. bfs([Path|Paths], GoalState, Solution):- extend(Path, NewPaths), append(Paths, NewPaths, UpdatedPaths), bfs(UpdatedPaths, GoalState, Solution). % Extend a path by exploring all possible moves from the current state. extend([CurrentState|Path], NewPaths):- findall([NewState, CurrentState|Path], (move(CurrentState, NewState), \+ member(NewState, [CurrentState|Path])), NewPaths). % Define possible moves in the 8 Puzzle problem move(State, NewState):- moveLeft(State, NewState). move(State, NewState):- moveRight(State, NewState). move(State, NewState):- moveUp(State, NewState). move(State, NewState):- moveDown(State, NewState). % Implement moveLeft, moveRight, moveUp, and moveDown based on the current empty position. % Example moveLeft implementation: moveLeft([[X, 'e', Y], Row2, Row3], [[X, Y, 'e'], Row2, Row3]). % Implement moveRight, moveUp, and moveDown in a similar fashion. % Example moveRight implementation: moveRight([[X, 'e', Y], Row2, Row3], [[X, Y, 'e'], Row2, Row3]). % Example moveUp implementation: moveUp([[X1, X2, X3], ['e', Y2, Y3], Row3], [['e', X2, X3], [X1, Y2, Y3], Row3]). % Example moveDown implementation: moveDown([Row1, ['e', Y2, Y3], Row3], [Row1, [Y2, 'e', Y3], Row3]).
Write, Run & Share Prolog code online using OneCompiler’s Prolog online compiler for free. It’s a simple and intuitive platform to experiment with logic programming in Prolog. OneCompiler supports standard Prolog syntax, great for learning, prototyping, and practicing logic-based problems.
Prolog (Programming in Logic) is a logic programming language associated with artificial intelligence and computational linguistics. It works through facts, rules, and queries, using a form of symbolic reasoning known as backward chaining. Prolog is declarative, meaning you describe what you want instead of how to compute it.
The following is a simple Prolog program that prints a greeting:
:- initialization(main).
main :-
write('Hello, World!').
Facts represent basic assertions about the world.
likes(alice, pizza).
likes(bob, pasta).
Rules define logical relationships using facts.
friends(X, Y) :- likes(X, Z), likes(Y, Z).
Queries are used to find information based on facts and rules.
?- likes(alice, What).
Operator | Description |
---|---|
:- | Rule definition |
, | Logical AND |
; | Logical OR |
= | Unification |
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
Prolog heavily relies on recursion.
factorial(0, 1).
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
This guide provides a quick reference to Prolog programming syntax and features. Start writing Prolog code using OneCompiler’s Prolog online compiler today!