We have this assignment on our school.
- Modify the knightpath predicate to have the same representation as the queen program.
- You only have to change the representation in the knightpath predicate itself. The easiest way to do this is to use help-predicates so that we get something like this:
knightpath().
knightpath([S1,S2|Rest]) :-
list_to_coordinate(S1,X/Y),
jump(X/Y,X1/Y1).
coordinate_to_list(X1/Y1,S2),
knightpath([S2|Rest]).
-------------------------------------------------------
jump(X/Y, X1/Y1) :-
( dxy(Dx,Dy); dxy(Dy,Dx)),
X1 is X+Dx,
inboard(X1),
Y1 is Y+Dy,
inboard(Y1).
dxy(2,-1).
dxy(2,1).
dxy(-2,1).
dxy(-2,-1).
inboard(C) :-
0<C,
0<9.
knightpath().
knightpath([S1,S2|Rest]) :-
jump(S1,S2),
knightpath([S2|Rest]).
-------------------------------------------------------
So far I have done list_to_cordinate:
jump(X/Y, X1/Y1) :-
( dxy(Dx,Dy); dxy(Dy,Dx)),
X1 is X+Dx,
inboard(X1),
Y1 is Y+Dy,
inboard(Y1).
dxy(2,-1).
dxy(2,1).
dxy(-2,1).
dxy(-2,-1).
inboard(C) :-
0<C,
0<9.
knightpath().
knightpath([S1,S2|Rest]) :-
jump(S1,S2),
knightpath([S2|Rest]).
nth_member(1,[X|L],X).
nth_member(N,[Y|L],X) :-
N1 is N -1,
nth_member(N1,L,X).
knightpath().
knightpath([S1,S2|Rest]) :-
list_to_coordinate(S1,X/Y),
jump(X/Y,X1/Y1),
cordinate_to_list(X1/Y1,S2),
knightpath([S2|Rest]).
list_to_cordinate([0|Rest],X/Y) :-
list_to_cordinate(Rest,X/Y).
list_to_cordinate([Y|Rest],X/Y) :-
length(Rest,N),
X is 8 - N.
But I can't figure out how to do coordinate_to_list...
He says that I should use :
coordinate_to_list(X/Y,L) :-
length(L,8),
(i don't know what to do here)!!
- Modify the knightpath predicate to have the same representation as the queen program.
- You only have to change the representation in the knightpath predicate itself. The easiest way to do this is to use help-predicates so that we get something like this:
knightpath(
knightpath([S1,S2|Rest]) :-
list_to_coordinate(S1,X/Y),
jump(X/Y,X1/Y1).
coordinate_to_list(X1/Y1,S2),
knightpath([S2|Rest]).
-------------------------------------------------------
jump(X/Y, X1/Y1) :-
( dxy(Dx,Dy); dxy(Dy,Dx)),
X1 is X+Dx,
inboard(X1),
Y1 is Y+Dy,
inboard(Y1).
dxy(2,-1).
dxy(2,1).
dxy(-2,1).
dxy(-2,-1).
inboard(C) :-
0<C,
0<9.
knightpath(
knightpath([S1,S2|Rest]) :-
jump(S1,S2),
knightpath([S2|Rest]).
-------------------------------------------------------
So far I have done list_to_cordinate:
jump(X/Y, X1/Y1) :-
( dxy(Dx,Dy); dxy(Dy,Dx)),
X1 is X+Dx,
inboard(X1),
Y1 is Y+Dy,
inboard(Y1).
dxy(2,-1).
dxy(2,1).
dxy(-2,1).
dxy(-2,-1).
inboard(C) :-
0<C,
0<9.
knightpath(
knightpath([S1,S2|Rest]) :-
jump(S1,S2),
knightpath([S2|Rest]).
nth_member(1,[X|L],X).
nth_member(N,[Y|L],X) :-
N1 is N -1,
nth_member(N1,L,X).
knightpath(
knightpath([S1,S2|Rest]) :-
list_to_coordinate(S1,X/Y),
jump(X/Y,X1/Y1),
cordinate_to_list(X1/Y1,S2),
knightpath([S2|Rest]).
list_to_cordinate([0|Rest],X/Y) :-
list_to_cordinate(Rest,X/Y).
list_to_cordinate([Y|Rest],X/Y) :-
length(Rest,N),
X is 8 - N.
But I can't figure out how to do coordinate_to_list...
He says that I should use :
coordinate_to_list(X/Y,L) :-
length(L,8),
(i don't know what to do here)!!