3 // don't run it - produces too much output
5 // Copyright 2010 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
9 // This program solves the (English) peg solitaire board game.
10 // See also: http://en.wikipedia.org/wiki/Peg_solitaire
14 const N = 11 + 1 // length of a board row (+1 for newline)
16 // The board must be surrounded by 2 illegal fields in each direction
17 // so that move() doesn't need to check the board boundaries. Periods
18 // represent illegal fields, ● are pegs, and ○ are holes.
33 // center is the position of the center hole if there is a single one;
34 // otherwise it is -1.
39 for pos, field := range board {
46 center = -1 // no single hole
50 var moves int // number of times move is called
52 // move tests if there is a peg at position pos that can jump over another peg
53 // in direction dir. If the move is valid, it is executed and move returns true.
54 // Otherwise, move returns false.
55 func move(pos, dir int) bool {
57 if board[pos] == '●' && board[pos+dir] == '●' && board[pos+2*dir] == '○' {
60 board[pos+2*dir] = '●'
66 // unmove reverts a previously executed valid move.
67 func unmove(pos, dir int) {
70 board[pos+2*dir] = '○'
73 // solve tries to find a sequence of moves such that there is only one peg left
74 // at the end; if center is >= 0, that last peg must be in the center position.
75 // If a solution is found, solve prints the board after each move in a backward
76 // fashion (i.e., the last board position is printed first, all the way back to
77 // the starting board position).
80 for pos, field := range board {
81 // try each board position
84 for _, dir := range [...]int{-1, -N, +1, +N} {
87 // a valid move was found and executed,
88 // see if this new board has a solution
91 println(string(board))
101 // tried each possible move
102 if n == 1 && (center < 0 || last == center) {
103 // there's only one peg left
104 println(string(board))
107 // no solution found for this board
113 println("no solution found")
115 println(moves, "moves tried")