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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nth member

Status
Not open for further replies.

793

Programmer
Oct 25, 2010
2
NO
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)!!


 
You have an error here
Code:
inboard(C) :-
    0<C,
    0<9.
Have you understood the meaning of nth_member ?
 
I don't understand nth_member.

I haven't done this, this is part of the assignment that we are doing.
inboard(C) :-
0<C,
0<9.

But my main problem is
coordinate_to_list(X/Y,L) :-
length(L,8),
(i don't know what to do here)!!

I sent the teacher a mail and he said: Put Y in the place of X in the list with N members and fill the variabels with zeros... I don't understand where he is going with this. We haven't really learned this yet so I'm confused.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top