-// $G $F.go && $L $F.$A # don't run it - produces too much output
+// build
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// Test general operation by solving a peg solitaire game.
+// A version of this is in the Go playground.
+// Don't run it - produces too much output.
+
// This program solves the (English) peg solitaire board game.
// See also: http://en.wikipedia.org/wiki/Peg_solitaire
// The board must be surrounded by 2 illegal fields in each direction
// so that move() doesn't need to check the board boundaries. Periods
// represent illegal fields, ● are pegs, and ○ are holes.
-var board = []int(
+var board = []rune(
`...........
...........
....●●●....
...........
`)
-
// center is the position of the center hole if there is a single one;
// otherwise it is -1.
var center int
}
}
-
var moves int // number of times move is called
// move tests if there is a peg at position pos that can jump over another peg
return false
}
-
// unmove reverts a previously executed valid move.
func unmove(pos, dir int) {
board[pos] = '●'
board[pos+2*dir] = '○'
}
-
// solve tries to find a sequence of moves such that there is only one peg left
// at the end; if center is >= 0, that last peg must be in the center position.
// If a solution is found, solve prints the board after each move in a backward
return false
}
-
func main() {
if !solve() {
println("no solution found")