/
thinker.go
80 lines (72 loc) · 1.84 KB
/
thinker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cowbull
import (
"errors"
"fmt"
"math/rand"
"strings"
)
// AIThinker is an artificial intelligence that can think of numbers.
type AIThinker struct {
digits int
number string
perm func(int) []int
}
// LocalThinker creates new AIThinker that thinks only of n-digit numbers.
func LocalThinker(n int) *AIThinker {
return &AIThinker{
digits: n,
perm: rand.Perm,
}
}
// NewLocalThinker creates new AIThinker that thinks only of n-digit numbers.
// The perm argument specifies a function that generates permutations in the
// range [0, n). For more info, see the doc of rand.Perm.
// This method is used for testing. If you're using it, you're doing something
// wrong or you're trying to be clever. Remember: clear is better than clever.
func NewLocalThinker(n int, perm func(int) []int) *AIThinker {
return &AIThinker{
digits: n,
perm: perm,
}
}
// Think thinks of a number.
func (p *AIThinker) Think() (int, error) {
var err error
p.number, err = p.generateNumber()
if err != nil {
return 0, err
}
return len(p.number), nil
}
// Try returns the cows and bulls for number.
func (p *AIThinker) Try(number string) (int, int, error) {
if len(number) != len(p.number) {
return 0, 0, errors.New("local player: try number digit count mismatch")
}
cows, bulls := 0, 0
for i := range number {
if number[i] == p.number[i] {
bulls++
continue
}
if strings.Contains(p.number, string(number[i])) {
cows++
}
}
return cows, bulls, nil
}
func (p *AIThinker) generateNumber() (string, error) {
if p.digits < 1 || p.digits > 10 {
return "", fmt.Errorf("invalid digit count %d", p.digits)
}
perm := p.perm(10)
if perm[0] == 0 {
randIdx := rand.Intn(p.digits-1) + 1
perm[0], perm[randIdx] = perm[randIdx], perm[0]
}
res := ""
for i := 0; i < p.digits; i++ {
res = fmt.Sprintf("%s%d", res, perm[i])
}
return res, nil
}