Help with Peg Solitaire 15 hole triangle version
jspauli_at_gravity.phys.uwm.edu
Date: 08/22/04
- Next message: Christopher Browne: "Re: Small, Fast Prolog in Lisp?"
- Previous message: Balpreeta Scaramozzi: "Hi"
- Next in thread: R Kym Horsell: "Re: Help with Peg Solitaire 15 hole triangle version"
- Reply: R Kym Horsell: "Re: Help with Peg Solitaire 15 hole triangle version"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Aug 2004 19:42:12 -0700
I am trying to write a program in swi-prolog that solves a 15 hole
triangle (5 row max) peg solitaire (IQ) game. I have been programming
in
traditional imperative languages for quite some time, but thought
maybe prolog would lend itself to this problem best.
So, for the configuration
1
2 3
4 5 6
7 8 9 a
b c d e f
I believe I need to write some sort of list of legal moves,
represented as facts, not predicates. I tried writing a generalized
list of predicates such that you could try to move any given peg
sw,se,nw,ne, etc, etc. but I had no clue how to write the algorithm
that chooses the moves on various pegs (depth first search of a tree
of exactly 13 moves), which seems like a job for the prolog language
system itself anyway.
So on a second rendition I tried to use pattern matching of board
states,
so I had dozens of things like:
move([f,f,X,e,X,X,X,X,X,X,X,X,X,X,X],sw,[e,e,X,f,X,X,X,X,X,X,X,X,X,X,X])
:-
write ('Moved Peg 1 sw to hole 4').
then I had solution predicates such as:
solution( [e,e,e,e,e,e,e,e,e,e,e,e,e,e,f],[])
for each possible hole, such that the game is won
when there is only one peg left on the board.
I couldn't figure out how the recursive version of solution (the move
engine)
would work, however. This solution was based on a similar farmer,
goat, cabbage, wolf book by Adam Brooks Webber that covers several
non-imperative languages.
I thought it would be similar since essentially you need to maintain a
start
state, make one of a few basic moves, and end up at a goal state.
In C I could do this with a depth first search of a tree, and a stack
of moves,
but I thought prolog would do this for me, and find more than one
solution due
to its backtrace mechanism...so how do I teach it the rules of the
game?
Would something such as
move(1,2,4). be a better way (less verbose) of representing legal
moves, given
that most of the board never changes? What would the move engine look
like for
this? I would like to be able to feed in a board state, probably using
an
ordered list and get a result, but the initial board state could be
defined
using assert predicates as well.
I would really appreciate suggestions/example code, etc. Esp. via
email
in case I am without a news reader. jspauli(at)gravity.phys.uwm.edu
Thanks!
- Next message: Christopher Browne: "Re: Small, Fast Prolog in Lisp?"
- Previous message: Balpreeta Scaramozzi: "Hi"
- Next in thread: R Kym Horsell: "Re: Help with Peg Solitaire 15 hole triangle version"
- Reply: R Kym Horsell: "Re: Help with Peg Solitaire 15 hole triangle version"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|