-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
51 lines (41 loc) · 1.02 KB
/
main.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
package main
import (
"fmt"
"go-tdd/captcha"
"math/rand"
"time"
)
const (
maxPatternNumber = 2
maxOperatorNumber = 3
maxSideOperandNumber = 9
)
type captchaInput struct {
pattern int
leftOperand int
operator int
rightOperand int
}
func randomNumber(max int) int {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
min := 1
return r1.Intn(max-min+1) + min
}
func main() {
input := captchaInput{
pattern: randomNumber(maxPatternNumber),
leftOperand: randomNumber(maxSideOperandNumber),
operator: randomNumber(maxOperatorNumber),
rightOperand: randomNumber(maxSideOperandNumber),
}
captcha := captcha.New(input.pattern, input.leftOperand, input.operator, input.rightOperand)
printInput(input)
printExpression(captcha)
}
func printInput(c captchaInput) {
fmt.Printf("pattern: %d, %d %d %d = ", c.pattern, c.leftOperand, c.operator, c.rightOperand)
}
func printExpression(c captcha.Captcha) {
fmt.Printf("%s %s %s\n", c.LeftOperand(), c.Operator(), c.RightOperand())
}