Re: Help with Peg Solitaire 15 hole triangle version
From: jspauli (jspauli_at_gravity.phys.uwm.edu)
Date: 08/29/04
- Previous message: Holger Kanwischer: "Re: Remove all asserted facts?"
- In reply to: R. Kym Horsell: "Re: Help with Peg Solitaire 15 hole triangle version"
- Next in thread: Pere Montolio: "Re: Help with Peg Solitaire 15 hole triangle version"
- Reply: Pere Montolio: "Re: Help with Peg Solitaire 15 hole triangle version"
- Reply: russell kym horsell: "Re: Help with Peg Solitaire 15 hole triangle version"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Aug 2004 21:29:20 -0700
Thanks for your reply. I opted to obtain a copy of The Art of Prolog
and start studying that. Ok, I know you wrote a nice implementation
for
me but my goal is to really learn. I actually chose to do this as part
of a summer independent study...dunno if I regret it or not but its
something
I would like to know how to do.
Thus far I have come up with the following representation for the
pegs/board.
I am having difficulty getting the recursive 'move engine' to work.
Currently
I get a 'variables not properly instatiated' error. The comments are
pretty
good at explaining how the game works.
Could someone please help me work with this?
Thanks much!
%
% (0,4,0)
% p
% p p
% p p p
% p p p p
% p p p p p
% (0,0,4) (4,0,0)
% (X,Y,Z)
% X: Distance from left
% Y: Distance from bottom
% Z: Distance from right (within the row specified by Y)
%
% For this simulation we only represent a collection of pegs, not
% the board. I.E. If a peg is not in the board it is not in the
% database (Ok, so this implicitly represents the board, but not
directly)
%make sure there are no coordinates outside of the bounds for
%a 5 row triangle, this means no variable can be < 0 or > 4
zto4(A) :-
A > -1,
A < 5.
%move(peg(X,Y,Z),peg(X1,Y1,Z1),peg(X2,Y2,Z2)).
% write ('move '),
% write (X), write(','), write(Y),write(','),write(Z),
% write('to'),
% write (X2), write(','), write(Y2),write(','),write(Z2).
legal_peg(peg(X,Y,Z)) :-
zto4(X),zto4(Y),zto4(Z), 4 is X + Y +Z.
%This defines all possible moves generatively by saying:
% -A move has three sets of coordinates: First, Jumped, Last.
% -These Coordinates are all valid in the triangle (zto4)
% -The First and Jumped pegs exist in the database the Last does not.
% -The jump occurs in a straight line (Vertical, Horizontal or
Diagonal)
% this is done the difference of the 3 coordinate sets, see below.
legal_move( move(peg(X,Y,Z),peg(X1,Y1,Z1),peg(X2,Y2,Z2) ) ):-
legal_peg( peg(X,Y,Z) ),
legal_peg( peg(X1,Y1,Z1) ),
legal_peg( peg(X2,Y2,Z2) ),
p(X,Y,Z),p(X1,Y1,Z1), not(p(X2,Y2,Z2)).
% Checks to see if a move is legal. A Legal move means that
% All jumps occur on a straight line where all pegs are consecutive.
% The coordinate system used has the property that for any two
% consecutive points, the diff of their coordinates will be one
% of 6 combinations of 0,1,-1 adding up to 0. Notice there are also
% 6 cardinal directions for which a peg can travel. It turns out that
% For any peg P and the peg to the immediate southwest of it Q (not
jumping now)
% the diff P-Q will be (0,1,-1). Likewise P-Q for Southeast is always
(-1,1,0),
% and similar for all the rest.
%
% --first must make sure that we don't change directions in the
middle of a jump
% (the diff of F-J must equal J-L.)
% --next we make sure the diff is one of the six valid ones that
total 0.
% NOTE: (0,0,0) is not a valid diff since this indicates a non-move.
islegal_diff( peg(X,Y,Z),peg(X1,Y1,Z1),peg(X2,Y2,Z2) ) :-
RX1 is X-X1,
RY1 is Y-Y1,
RZ1 is Z-Z1,
RX2 is X1-X2,
RY2 is Y1-Y2,
RZ2 is Z1-Z2,
RX1 == RX2,
RY1 == RY2,
RZ1 == RZ2,
good_diff(RX2,RY2,RZ2).
good_diff(0,1,-1).
good_diff(0,-1,1).
good_diff(1,0,-1).
good_diff(1,-1,0).
good_diff(-1,1,0).
good_diff(-1,0,1).
wonGame(N) :-
0 > N.
%Now we know what a legal move is, so we need a way to actually do the
move.
% retract peg F, retract peg J, and assert peg L
%
do_move( move( peg(X,Y,Z), peg(X1,Y1,Z1), peg(X2,Y2,Z2) ) ):-
legal_move( move( peg(X,Y,Z),peg(X1,Y1,Z1),peg(X2,Y2,Z2) )),
retract(p(X,Y,Z)),
retract(p(X1,Y1,Z1)),
assert( p(X2,Y2,Z2)).
play_game(N) :-
not(wonGame(N)),
do_move( move( peg(X,Y,Z), peg(X1,Y1,Z1), peg(X2,Y2,Z2) ) ),
N is N-1,
play_game(N).
initialboard :-
assert(p(0,4,0)),
assert(p(0,3,1)), assert(p(1,3,0)),
assert(e(0,2,2)),assert(p(1,2,1)),assert(p(2,2,0)),
assert(p(0,1,3)),assert(p(1,1,2)),assert(p(2,1,1)),
assert(p(3,1,0)),
assert(p(0,0,4)),assert(p(1,0,3)),assert(p(2,0,2)),assert(p(3,0,1)),
assert(p(4,0,0))
.
kymhorsell@gmail.com (R. Kym Horsell) wrote in message news:<a837c8ef.0408232044.60a9ee94@posting.google.com>...
> R Kym Horsell <kym@kymhorsell.com> wrote:
> >[usual crap]
>
> I've put a simple solution of the peg solitaire (as I understand the rules ;)
> on my junk web page.
>
> It comes in 2 parts:
> A "general" problem solver (cut down from a REAL general problem solver);
> and the Peg Solitaire problem.
>
> Feel free to cut out and hand in as homework. Everyone knows where it came from.
> ;-)
>
> If anyone makes "improvements" to the solver -- which keeps the
> interface and solution simple enough to still feed through my simple
> Prolog specialiser -- feel free to post them in.
>
> ==============
> obwebref:
>
> http://JUNK.kymhorsell.com/solve/
- Previous message: Holger Kanwischer: "Re: Remove all asserted facts?"
- In reply to: R. Kym Horsell: "Re: Help with Peg Solitaire 15 hole triangle version"
- Next in thread: Pere Montolio: "Re: Help with Peg Solitaire 15 hole triangle version"
- Reply: Pere Montolio: "Re: Help with Peg Solitaire 15 hole triangle version"
- Reply: russell kym horsell: "Re: Help with Peg Solitaire 15 hole triangle version"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|