~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to doc/play/solitaire.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
...........
29
29
`)
30
30
 
31
 
// center is the position of the center hole if 
 
31
// center is the position of the center hole if
32
32
// there is a single one; otherwise it is -1.
33
33
var center int
34
34
 
47
47
 
48
48
var moves int // number of times move is called
49
49
 
50
 
// move tests if there is a peg at position pos that 
 
50
// move tests if there is a peg at position pos that
51
51
// can jump over another peg in direction dir. If the
52
52
// move is valid, it is executed and move returns true.
53
53
// Otherwise, move returns false.
69
69
        board[pos+2*dir] = '○'
70
70
}
71
71
 
72
 
// solve tries to find a sequence of moves such that 
73
 
// there is only one peg left at the end; if center is 
 
72
// solve tries to find a sequence of moves such that
 
73
// there is only one peg left at the end; if center is
74
74
// >= 0, that last peg must be in the center position.
75
75
// If a solution is found, solve prints the board after
76
 
// each move in a backward fashion (i.e., the last 
 
76
// each move in a backward fashion (i.e., the last
77
77
// board position is printed first, all the way back to
78
78
// the starting board position).
79
79
func solve() bool {
89
89
                                        // see if this new board has a solution
90
90
                                        if solve() {
91
91
                                                unmove(pos, dir)
92
 
                                                println(string(board))
 
92
                                                fmt.Println(string(board))
93
93
                                                return true
94
94
                                        }
95
95
                                        unmove(pos, dir)
102
102
        // tried each possible move
103
103
        if n == 1 && (center < 0 || last == center) {
104
104
                // there's only one peg left
105
 
                println(string(board))
 
105
                fmt.Println(string(board))
106
106
                return true
107
107
        }
108
108
        // no solution found for this board