


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



In this assignment you will write a program that solves a classic puzzle, sometimes called the triangle puzzle. Solving this puzzle will require the use of a technique known as backtracking to search through all possible sequences of moves, which is easy to implement using recursion.
The Triangle Puzzle is a collection of 15 holes organized into a triangle, into which pegs are placed, leaving at least one empty hole. Typically, the puzzle begins with only one hole, as in configuration (a) below, where solid circles represent pegs, and empty circles represent holes. The object of the puzzle is to make a sequence of moves, each move causing one peg to be removed, such that only one peg remains. A Peg is removed only by “jumping” it with another peg. Hence, a legal move can only be made if there is a peg flanked by both another peg and a hole, arranged in a line. For example, configuration (b) is obtained from (a) by one jump, and then (c) is obtained from (b) by making a second jump.
(a)
(b)
(c)
To write a program that solves this puzzle, we need to somehow represent both the configurations of the puzzle and the moves. Toward this end, we shall first pick a convenient labelling for the holes; this will make it easy to refer to them:^1
According to this labelling, configuration (a) above has a hole at position 4. Configuration (b) is obtained by jumping the peg at position 8 with the peg at position 13. Then configuration (c) is obtained by jumping peg 12 with peg 11. We might denote these moves as 13 → 8 → 4 and 11 → 12 → 13. Or, to use a Python idiom, we could use 3-tuples: (13, 8 , 4) and (11, 12 , 13). The first (^1) While any labelling with do, for grading purposes it is important that you use exactly this labelling.
number is the peg that will do the jumping, the second number is the peg that will be jumped, and the third is the empty hole that the jumping peg will end up in. Using this idea, we can create a list of all potential moves that one could make in solving the puzzle; that is, a list of all tuples encoding all conceivable moves. Such a list would begin like this:
PotentialMoves = [ (0,1,3), (0,2,5), (1,4,8), (1,3,6), (2,4,7), ...]
Of course, only some of these potential moves can actually be carried out at any stage of solving an actual puzzle; we will refer to those as the legal moves. Thus, the legal moves depend on the current configuration of the puzzle, but they will always be a subset of the potential moves. For example, in configuration (a) above, the legal moves are (11, 7 , 4) and (13, 8 , 4), while in configuration (c) the legal moves are (3, 7 , 12), (1, 4 , 8), and (14, 13 , 12).
Having a convenient representation of potential moves, we need a representation of the puzzle at each stage of the solution process in order to determine what the legal moves are. An easy solution is to simply use an list of fifteen numbers (one for each hole), indicating whether or not there is a peg in each hole. For example, the configurations shown above would be
[1, 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1] [1, 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1] [1, 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 1]
respectively. That is, a 1 in the element with index k means there is a peg in that hole; a 0 means the hole is empty.^2 Given such a representation, we can now determine the set of legal moves that correspond to each configuration. Note that “legality” needn’t have anything to do with whether the move is actually a good move or not. That is, a legal move is simply one that follows the rules; it need not be a move that will actually lead to a solution. We will address that below.
You are to create and submit a Python module named “hw5.py” containing definitions for the following functions (with precisely these spellings, including case):
To complete this assignment you will also need a list of potential moves in the puzzle, most of which has been provided for you on the downloads page. Here is what you need to do:
You can use the same strategy to solve this puzzle as the triangle puzzle, although you will need new representations for both configurations and potential moves.
Contact one of the TAs if you wish to submit something for extra credit.