INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Prolog - Puzzle 8

Prolog - Puzzle 8

(OP)
I have to make a prolog progam that solves the puzzle 8. It's for my programming club in school that I'm in just for fun. I'm new to Prolog - I'm having a hard time. Here's my code:

CODE --> prolog

puzzle([1, 2, 3, 4, 5, 6, 7, 8, 0]).

%%up
c([0, X2, X3, X4, X5, X6, X7, X8, X9], [X4, X2, X3, 0, X5, X6, X7, X8, X9]).
c([X1, 0, X3, X4, X5, X6, X7, X8, X9], [X1, X5, X3, X4, 0, X6, X7, X8, X9]).
c([X1, X2, 0, X4, X5, X6, X7, X8, X9], [X1, X2, X6, X4, X5, 0, X7, X8, X9]).
c([X1, X2, X3, 0, X5, X6, X7, X8, X9], [X1, X2, X3, X7, X5, X6, 0, X8, X9]).
c([X1, X2, X3, X4, 0, X6, X7, X8, X9], [X1, X2, X3, X4, X8, X6, X7, 0, X9]).
c([X1, X2, X3, X4, X5, 0, X7, X8, X9], [X1, X2, X3, X4, X5, X9, X7, X8, 0]).

%%right
d([0, X2, X3, X4, X5, X6, X7, X8, X9], [X2, 0, X3, X4, X5, X6, X7, X8, X9]).
d([X1, 0, X3, X4, X5, X6, X7, X8, X9], [X1, X3, 0, X4, X5, X6, X7, X8, X9]).
d([X1, X2, X3, 0, X5, X6, X7, X8, X9], [X1, X2, X3, X5, 0, X6, X7, X8, X9]).
d([X1, X2, X3, X4, 0, X6, X7, X8, X9], [X1, X2, X3, X4, X6, 0, X7, X8, X9]).
d([X1, X2, X3, X4, X5, X6, 0, X8, X9], [X1, X2, X3, X4, X5, X6, X8, 0, X9]).
d([X1, X2, X3, X4, X5, X6, X7, 0, X9], [X1, X2, X3, X4, X5, X6, X7, X9, 0]).

%%down
b([X1, X2, X3, 0, X5, X6, X7, X8, X9], [0, X2, X3, X1, X5, X6, X7, X8, X9]).
b([X1, X2, X3, X4, 0, X6, X7, X8, X9], [X1, 0, X3, X4, X2, X6, X7, X8, X9]).
b([X1, X2, X3, X4, X5, 0, X7, X8, X9], [X1, X2, 0, X4, X5, X3, X7, X8, X9]).
b([X1, X2, X3, X4, X5, X6, 0, X8, X9], [X1, X2, X3, 0, X5, X6, X4, X8, X9]).
b([X1, X2, X3, X4, X5, X6, X7, 0, X9], [X1, X2, X3, X4, 0, X6, X7, X5, X9]).
b([X1, X2, X3, X4, X5, X6, X7, X8, 0], [X1, X2, X3, X4, X5, 0, X7, X8, X6]).

%%left
e([X1, 0, X3, X4, X5, X6, X7, X8, X9], [0, X1, X3, X4, X5, X6, X7, X8, X9]).
e([X1, X2, 0, X4, X5, X6, X7, X8, X9], [X1, 0, X2, X4, X5, X6, X7, X8, X9]).
e([X1, X2, X3, 0, X5, X6, X7, X8, X9], [X1, X2, X3, X5, 0, X6, X7, X8, X9]).
e([X1, X2, X3, X4, 0, X6, X7, X8, X9], [X1, X2, X3, X4, X6, 0, X7, X8, X9]).
e([X1, X2, X3, X4, X5, X6, 0, X8, X9], [X1, X2, X3, X4, X5, X6, X8, 0, X9]).
e([X1, X2, X3, X4, X5, X6, X7, 0, X9], [X1, X2, X3, X4, X5, X6, X7, X9, 0]).

%%prints puzzle
affiche_puzzle :-
    puzzle(Puzzle),
    printe(Puzzle).
printe([]).
printe([H1,H2,H3|Puzzle]) :-
    format('~w ~w ~w~n', [H1, H2, H3]),
    printe(Puzzle). 

Now I have to create mov_legal(C1, M, P, C2) --> c2 is obtained from configuration c1, doing the move M, moving the piece P. Here's one expected result:

CODE --> prolog

?- mov_legal([1, 2, 3, 4, 5, 0, 7, 8, 6], M, P, C).
M = c,
P = 6,
C = [1, 2, 3, 4, 5, 6, 7, 8, 0] ;
M = b,
P = 3,
C = [1, 2, 0, 4, 5, 3, 7, 8, 6] ;
... 

I would be grateful if you help me!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close