OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [testsuite/] [go.test/] [test/] [solitaire.go] - Blame information for rev 700

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 700 jeremybenn
// $G $F.go && $L $F.$A  # don't run it - produces too much output
2
 
3
// Copyright 2010 The Go Authors. All rights reserved.
4
// Use of this source code is governed by a BSD-style
5
// license that can be found in the LICENSE file.
6
 
7
// This program solves the (English) peg solitaire board game.
8
// See also: http://en.wikipedia.org/wiki/Peg_solitaire
9
 
10
package main
11
 
12
const N = 11 + 1 // length of a board row (+1 for newline)
13
 
14
// The board must be surrounded by 2 illegal fields in each direction
15
// so that move() doesn't need to check the board boundaries. Periods
16
// represent illegal fields, ● are pegs, and ○ are holes.
17
var board = []rune(
18
        `...........
19
...........
20
....●●●....
21
....●●●....
22
..●●●●●●●..
23
..●●●○●●●..
24
..●●●●●●●..
25
....●●●....
26
....●●●....
27
...........
28
...........
29
`)
30
 
31
// center is the position of the center hole if there is a single one;
32
// otherwise it is -1.
33
var center int
34
 
35
func init() {
36
        n := 0
37
        for pos, field := range board {
38
                if field == '○' {
39
                        center = pos
40
                        n++
41
                }
42
        }
43
        if n != 1 {
44
                center = -1 // no single hole
45
        }
46
}
47
 
48
var moves int // number of times move is called
49
 
50
// move tests if there is a peg at position pos that can jump over another peg
51
// in direction dir. If the move is valid, it is executed and move returns true.
52
// Otherwise, move returns false.
53
func move(pos, dir int) bool {
54
        moves++
55
        if board[pos] == '●' && board[pos+dir] == '●' && board[pos+2*dir] == '○' {
56
                board[pos] = '○'
57
                board[pos+dir] = '○'
58
                board[pos+2*dir] = '●'
59
                return true
60
        }
61
        return false
62
}
63
 
64
// unmove reverts a previously executed valid move.
65
func unmove(pos, dir int) {
66
        board[pos] = '●'
67
        board[pos+dir] = '●'
68
        board[pos+2*dir] = '○'
69
}
70
 
71
// solve tries to find a sequence of moves such that there is only one peg left
72
// at the end; if center is >= 0, that last peg must be in the center position.
73
// If a solution is found, solve prints the board after each move in a backward
74
// fashion (i.e., the last board position is printed first, all the way back to
75
// the starting board position).
76
func solve() bool {
77
        var last, n int
78
        for pos, field := range board {
79
                // try each board position
80
                if field == '●' {
81
                        // found a peg
82
                        for _, dir := range [...]int{-1, -N, +1, +N} {
83
                                // try each direction
84
                                if move(pos, dir) {
85
                                        // a valid move was found and executed,
86
                                        // see if this new board has a solution
87
                                        if solve() {
88
                                                unmove(pos, dir)
89
                                                println(string(board))
90
                                                return true
91
                                        }
92
                                        unmove(pos, dir)
93
                                }
94
                        }
95
                        last = pos
96
                        n++
97
                }
98
        }
99
        // tried each possible move
100
        if n == 1 && (center < 0 || last == center) {
101
                // there's only one peg left
102
                println(string(board))
103
                return true
104
        }
105
        // no solution found for this board
106
        return false
107
}
108
 
109
func main() {
110
        if !solve() {
111
                println("no solution found")
112
        }
113
        println(moves, "moves tried")
114
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.