Extended Exercise: Board Solitaire

external ~<#67844#><#42926#>Warning<#42926#>: This exercise is complex and requires serious work. At Rice, we assign it as a project after eight weeks of work and give students two weeks to think about it. Then they turn in a design for solitaire (based on generative recursion template, plus backtracking) and headers plus purpose statements for all help functions mentioned in the design. For the final product, ask that they specify for each function whether it is a function based on structural processing (including which argument's structure is used), generative recursion, or domain knowledge. Also, if a function uses an accumulator, request a comment on what the accumulator invariant is and how it is maintained during evaluation.<#67844#> Peg Solitaire is a board game for individuals. The board comes in various shapes. Here is the simplest one:

#picture42928#

The circle with a black dot represents an unoccupied hole, the others holes with little pegs. The goal of the game is to eliminate the pegs one by one, until only one peg is left. A player can eliminate a peg if one of the neighboring holes is unoccupied and if there is a peg in the hole in the opposite direction. In that case, the second peg can jump over the first one and the first one is eliminated. Consider the following configuration:

#picture42941#

Here the pegs labeled~1 and~2 could jump. If the player decides to move the peg labeled~2, the next configuration is

#picture42958#

Some configurations are dead-ends. For a simple example, consider the first board configuration. Its hole is in the middle of the board, Hence, not one peg can jump, because there are no two pegs in a row, column, or diagonal such that one can jump over the other into the hole. A player who discovers a dead-end configuration must stop or backtrack by undoing moves and trying alternatives.
<#42973#>Exercise 32.3.1<#42973#> Develop a representation for triangular Solitaire boards. Develop a data representation for peg moves. Pegs can move along a row, a column, and a diagonal. <#42975#>Hints:<#42975#> \ (1) There are at least four rows, because it is impossible to play the game with three or fewer. Still, develop the data definition independently of such constraints. (2) Translate our examples from above into the chosen data representations.~ external Solution<#67865#><#67865#> <#42981#>Exercise 32.3.2<#42981#> Develop a function that, given a board and the board position of a peg, determines whether or not the peg can jump. We call such a peg <#42983#>enabled<#42983#>. Develop a function that, given a board and the board position of an enabled peg, creates a board that represents the next configuration.~ external Solution<#67866#><#67866#> <#42989#>Exercise 32.3.3<#42989#> Develop the function <#67867#><#42991#>solitaire<#42991#><#67867#>, which solves a Solitaire problem for different sizes of the equilateral triangle. The function should consume a the board. It produces <#67868#><#42992#>false<#42992#><#67868#>, if the given problem is not solvable. Otherwise, it should produce a list of moves that specifies in what order the pegs must be moved to solve the given Solitaire problem. Formulate the tests for all functions as boolean-valued expressions.~ external Solution<#67869#><#67869#>