forked from unixpickle/gocube
/
random_state.go
81 lines (72 loc) · 1.7 KB
/
random_state.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
81
package gocube
import (
"math/rand"
)
func RandomCubieCube() CubieCube {
var res CubieCube
// Generate a random permutation for the corners.
pieces := rand.Perm(8)
for i, x := range pieces {
res.Corners[i].Piece = x
}
// Generate a random permutation for the edges.
cornerParity := parity(pieces)
pieces = rand.Perm(12)
for i, x := range pieces {
res.Edges[i].Piece = x
}
// Make sure the overall parity is even.
if cornerParity != parity(pieces) {
res.Edges[11], res.Edges[10] = res.Edges[10], res.Edges[11]
}
// Generate edge orientations.
lastFlip := false
for i := 0; i < 11; i++ {
if rand.Intn(2) == 0 {
lastFlip = !lastFlip
res.Edges[i].Flip = true
}
}
res.Edges[11].Flip = lastFlip
// Generate the corner orientations.
for i := 0; i < 7; i++ {
res.Corners[i].Orientation = rand.Intn(3)
}
var orientations [8]int
for i, x := range []int{0, 1, 5, 4, 6, 2, 3, 7} {
orientations[i] = res.Corners[x].Orientation
}
for i := 0; i < 7; i++ {
thisOrientation := orientations[i]
nextOrientation := orientations[i+1]
if thisOrientation == 2 {
orientations[i+1] = (nextOrientation + 2) % 3
} else if thisOrientation == 0 {
orientations[i+1] = (nextOrientation + 1) % 3
}
}
if orientations[7] == 0 {
res.Corners[7].Orientation = 2
} else if orientations[7] == 2 {
res.Corners[7].Orientation = 0
}
return res
}
// parity returns true if the parity is even.
func parity(perm []int) bool {
parity := true
for i := 0; i < len(perm); i++ {
if perm[i] == i {
continue
}
// Swap this element with i.
for j := i + 1; j < len(perm); j++ {
if perm[j] == i {
perm[i], perm[j] = perm[j], perm[i]
parity = !parity
break
}
}
}
return parity
}