My mom has a bunch of little games where pegs go in holes in a board, and you have to move them around via some rules to achieve a goal. One game goes like this
( Read more... )
%/* move a blue right*/ legal_move(A,B) :- append(L1,[b,s|Rest],A), append(L1,[s,b|Rest],B). %/* move a red left */ legal_move(A,B) :- append(L1,[s,r|Rest],A), append(L1,[r,s|Rest],B). %/* jump a blue right */ legal_move(A,B) :- append(L1,[b,Any,s|Rest],A), append(L1,[s,Any,b|Rest],B). %/* jump a red left */ legal_move(A,B) :- append(L1,[s,Any,r|Rest],A), append(L1,[r,Any,s|Rest],B).
This looks vaguely familiar...chaset1December 6 2008, 04:37:33 UTC
I think this is because a Mudd CS prof had a slightly modified version of this that he called Rexx, which we were required to use for some of our assignments.
I haven't tried to parse the code, but I'm guessing at first glance that a recursive solution is intuitive and elegant for something like this.
Comments 2
solution([[r,r,r,r,s,b,b,b,b]]).
solution([A,B|Rest]) :- legal_move(A,B) , solution([B|Rest]).
%/* move a blue right*/
legal_move(A,B) :- append(L1,[b,s|Rest],A), append(L1,[s,b|Rest],B).
%/* move a red left */
legal_move(A,B) :- append(L1,[s,r|Rest],A), append(L1,[r,s|Rest],B).
%/* jump a blue right */
legal_move(A,B) :- append(L1,[b,Any,s|Rest],A), append(L1,[s,Any,b|Rest],B).
%/* jump a red left */
legal_move(A,B) :- append(L1,[s,Any,r|Rest],A), append(L1,[r,Any,s|Rest],B).
print_step(A) :- format("~w~n",[A]).
print_solution :- solution([[b,b,b,b,s,r,r,r,r]|Rest]),maplist(print_step,Rest).
Reply
I haven't tried to parse the code, but I'm guessing at first glance that a recursive solution is intuitive and elegant for something like this.
Reply
Leave a comment