开发者

How to create a program that solves a Triangular Peg solitare - C++ / Java programming?

开发者 https://www.devze.com 2023-04-10 02:44 出处:网络
Honestly, I only knew of such a game recently and I wonder how one can create a solving algorithm using the recursive se开发者_Python百科arch method?

Honestly, I only knew of such a game recently and I wonder how one can create a solving algorithm using the recursive se开发者_Python百科arch method?

There are 15 holes in total in the triangular board. Making that 14 pegs with a total of 13 moves. I don't know where to start with this in C++ nor Java. I have studied C++ for an year before. So I'm familiar with the concepts of stacks, linked lists etc.

I just don't know how to start the code. The program firstly asks the user where they want to start (How is this done?) Then once it solves it , a certain number of pegs more than just one will be left and the program will ask the user for a better solution (like this until the board is left to just one peg.)

I certainly cannot think of how to make the moves possible ( How do I write a code that "SHOWS" that one peg moves over a hole ,into another?)

I'd love some coding assistance here. It would really be appreciated.


Try treating the board a linked list of holes or positions. Each data field in a node would represent a hole. Each node would have a vector of links to other holes, depending on its position relative to the edge of the board.

To move a peg, iterate over the possible links.

This is just one method, there are probably better ones out there.


Take a look at my answer here: Timeout on a php Peg Puzzle solver. The 15-peg puzzle was the first program that I ever wrote (over 10 years ago), after just learning c++.

The posted solution is a re-write that I did several years later.

Since I was the answerer I can tell you that a triplet is a term that I made up for a move. There are three pegs involved in a move (a triplet). Each peg is represented by a bit. In order for a move to be legal two consecutive pegs (bits) need to be set and the other (the target location) needs to be clear.

The entire solution involves a depth first search using a 16-bit integer to represent the board. Simple bit manipulation can be used to update the board state. For example if you xor the current state with a move (triplet) it will make the move against the board. The same operation applied a second time against the board is an undo of the move.


To be successful as a programmer, you need to develop the skill of examining a problem, figuring out how it can be solved, and come up with a reasonable in-program representation that enables you to solve it.

For problems like this, I find it helpful to have the puzzle in front of me, so I can try it out by hand. Failing that, at least having a picture of the thing will help: http://www.obsidiandesigns.com/pyramidsol.jpg

Here's another way to look at it, and possibly consider representing it in memory like this:

OXXXX
XXXX
XXX
XX
X

Each "X" is a peg. The "O" is a hole. One can change "OXX" to "XOO" by jumping the 3rd X over the middle one, into the hole. Also consider vertical and diagonal jumps.

Linked lists could make a lot of sense. You might actually want "2D" links, instead of the usual "1D" links. That is each "hole" instance could contain pointers to the two or three holes next to it.

Once you have a representation, you need a way to find valid moves. And to make valid moves.

For this "game," you may want to search the whole space of possible moves, so you'll probably want to either have each move produce a new board, or to have a way to "undo" moves.

A valid move is where one peg jumps over another peg, into an empty hole. The jumped peg is removed. You can't jump from "just anywhere" to anywhere, over any peg you wish -- the three holes have to be next to each other and in line.

The above should give you some hints that will help you get started.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号