forked from codelympics/go_game_jam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem.go
54 lines (48 loc) · 1.01 KB
/
problem.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
package main
import (
"fmt"
"math/rand"
)
// Problem contains number pair for solving
type Problem struct {
n1 int
n2 int
valid bool
}
// ProblemList contains list of number pairs
type ProblemList []*Problem
// String returns textual representation of the number pair
func (pr *Problem) String() string {
return fmt.Sprintf("%2d+%-2d", pr.n1, pr.n2)
}
// NewProblem generates new pair of numbers for the board
func NewProblem(level int, isValid bool) *Problem {
sum := level
n1 := rand.Intn(sum + 1)
n2 := sum - n1
if !isValid {
n2 += 1 + rand.Intn(3)
}
r := rand.Intn(10)
if r > 4 {
n1, n2 = n2, n1
}
return &Problem{n1, n2, isValid}
}
// NewProblemList generates all number pairs for a level
func NewProblemList(level, valid, size int) ProblemList {
pl := make(ProblemList, size)
isHit := true
for i := range pl {
if i >= valid {
isHit = false
}
pl[i] = NewProblem(level, isHit)
}
// Shuffle problems
for i := range pl {
j := rand.Intn(i + 1)
pl[i], pl[j] = pl[j], pl[i]
}
return pl
}