Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog - Puzzle 8

Status
Not open for further replies.

cesar9529

Programmer
May 10, 2015
1
0
0
PT
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:
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:
?- 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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top