Docsity

Solved Assignment for the Triangle Puzzle | EECS 12, Assignments of Electrical and Electronics Engineering

Toggle sidebar
bg1
EECS12 Homework #5 Due: 11:59 PM, Friday, February 18, 2005
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 recur si on .
The Triangle Puzzle
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.
y
yy
yiy
yyyy
yyyyy
(a)
y
yy
yyy
yyiy
yyyiy
(b)
y
yy
yyy
yyiy
yiiyy
(c)
How to Represent the Puzzle
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
0
12
345
6789
10 11 12 13 14
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→4and
11→12→13. Or, to use a Python idiom, we could use 3-tuples: (13,8,4) and (11,12,13). The first
1While any labelling with do, for grading purposes it is important that you use exactly this labelling.
1
pf3
pf4

Partial preview of the text

Download Solved Assignment for the Triangle Puzzle | EECS 12 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EECS12 Homework #5 Due: 11:59 PM, Friday, February 18, 2005

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

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)

How to Represent the Puzzle

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.

What You Need to Do

You are to create and submit a Python module named “hw5.py” containing definitions for the following functions (with precisely these spellings, including case):

  1. MakeConfig( hole list )
  2. LegalMoves( config )
  3. MakeMove( config, move )
  4. SolveTrianglePuzzle( config )
  5. PrintSolution( solution )

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:

  1. Read chapter 14 of How to Think Like a Computer Scientist. Chapter 5 of Dive into Python is also recommended. You will not require classes and methods to complete this assignment, but we will be discussing them in lecture. (^2) We might also have chosen to used a dictionary to associate the 1 or 0 with the given index.
  1. Use classes to encode the puzzle and configurations. For example, a Configuration class could not only encode where the pegs are, but also include a method that makes a move. A Puzzle class could contain a Solve method.
  2. Try to find all solutions, not just one solution.
  3. Solve a slightly harder (but similar) puzzle. For example, the puzzle called “Hi-Q” or “Peg Solitaire” looks like this:                                 

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.