-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (173 loc) · 4.94 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"errors"
"fmt"
"os"
"strings"
"github.com/pkg/profile"
)
// CubeEnv : Cube representation
type CubeEnv struct {
cube [6]int32
cost int
heuristic int
internationalMove string
}
// Env : Game environnement
type Env struct {
startCube CubeEnv //start cube
currentCube CubeEnv //current cube
res string //result list
debug bool
}
func parseArgs(arg string) ([]string, error) {
var steps []string
arg = strings.Replace(arg, "\n", "", -1)
if len(arg) != 0 {
if arg[len(arg)-1] == ' ' {
arg = arg[0 : len(arg)-1]
}
steps = strings.Split(arg, " ")
for step := range steps {
steps[step] = strings.ReplaceAll(steps[step], "’", "'")
if len(steps[step]) == 0 || len(steps[step]) > 3 || (len(steps[step]) > 0 &&
!strings.Contains("FRUBLD", steps[step][0:1])) {
return nil, errors.New("Error : Invalid step name")
} else if len(steps[step]) == 2 && !strings.Contains("'2", steps[step][1:2]) {
return nil, errors.New("Error : Invalid step arg")
} else if len(steps[step]) == 3 &&
(!strings.Contains("'2", steps[step][1:2]) || !strings.Contains("2", steps[step][2:3])) {
return nil, errors.New("Error : Invalid step arg")
}
}
} else {
return nil, errors.New("Error : No arg")
}
return steps, nil
}
func parseDoubles(output string) string {
outTab := strings.Split(output, " ")
for i := 0; i < len(outTab)-1; i++ {
if outTab[i] != "" {
if outTab[i] == outTab[i+1] {
if !strings.Contains(outTab[i], "2") {
outTab[i] = outTab[i] + "2"
outTab[i+1] = ""
} else {
outTab[i] = ""
outTab[i+1] = ""
}
} else if len(outTab[i]) > 0 && len(outTab[i+1]) > 0 && outTab[i][0] == outTab[i+1][0] {
if !strings.Contains(outTab[i], "2") && !strings.Contains(outTab[i+1], "2") {
if !strings.Contains(outTab[i], "'") && strings.Contains(outTab[i+1], "'") {
outTab[i] = ""
outTab[i+1] = ""
} else if strings.Contains(outTab[i], "'") && !strings.Contains(outTab[i+1], "'") {
outTab[i] = ""
outTab[i+1] = ""
}
} else if strings.Contains(outTab[i], "2") && strings.Contains(outTab[i+1], "2") {
if !strings.Contains(outTab[i], "'") && strings.Contains(outTab[i+1], "'") {
outTab[i] = ""
outTab[i+1] = ""
} else if strings.Contains(outTab[i], "'") && !strings.Contains(outTab[i+1], "'") {
outTab[i] = ""
outTab[i+1] = ""
}
} else if !strings.Contains(outTab[i], "2") && strings.Contains(outTab[i+1], "2") {
if !strings.Contains(outTab[i], "'") && !strings.Contains(outTab[i+1], "'") {
outTab[i] = outTab[i] + "'"
outTab[i+1] = ""
} else if strings.Contains(outTab[i], "'") && strings.Contains(outTab[i+1], "'") {
outTab[i] = outTab[i][:1]
outTab[i+1] = ""
} else if !strings.Contains(outTab[i], "'") && strings.Contains(outTab[i+1], "'") {
outTab[i] = outTab[i] + "'"
outTab[i+1] = ""
} else if strings.Contains(outTab[i], "'") && !strings.Contains(outTab[i+1], "'") {
outTab[i] = outTab[i][:1]
outTab[i+1] = ""
}
} else if strings.Contains(outTab[i], "2") && !strings.Contains(outTab[i+1], "2") {
if !strings.Contains(outTab[i], "'") && !strings.Contains(outTab[i+1], "'") {
outTab[i] = outTab[i][:1] + "'"
outTab[i+1] = ""
} else if strings.Contains(outTab[i], "'") && strings.Contains(outTab[i+1], "'") {
outTab[i] = outTab[i][:1]
outTab[i+1] = ""
} else if !strings.Contains(outTab[i], "'") && strings.Contains(outTab[i+1], "'") {
outTab[i] = outTab[i][:1]
outTab[i+1] = ""
} else if strings.Contains(outTab[i], "'") && !strings.Contains(outTab[i+1], "'") {
outTab[i] = outTab[i][:1] + "'"
outTab[i+1] = ""
}
}
}
}
}
output = ""
for i, step := range outTab {
if step != "" {
output += step
if i < len(outTab)-1 {
output += " "
}
}
}
return output
}
func checkOver(output string) bool {
outTab := strings.Split(output, " ")
for i := 0; i < len(outTab)-1; i++ {
if len(outTab[i]) > 0 && len(outTab[i+1]) > 0 && outTab[i][0] == outTab[i+1][0] {
return false
}
}
return true
}
func parseOutput(output string) string {
output = parseDoubles(output)
if checkOver(output) == false {
return parseOutput(output)
}
return output
}
func main() {
var mix string
var debug bool
var human bool
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("./Rubik[.exe] [-d] [-h] \"R2 D’ B’ D F2 R F2 R2 U L’ F2 U’ B’ L2 R D B’ R’ B2 L2 F2 L2 R2 U2 D2\"")
return
}
for _, arg := range args {
if arg == "-d" {
debug = true
} else if arg == "-h" {
human = true
} else {
mix = string(arg)
}
}
if debug {
defer profile.Start(profile.ProfilePath(".")).Stop()
}
env := Env{debug: debug}
env.setCube()
// parsing
steps, err := parseArgs(mix)
if err != nil {
fmt.Println(err)
return
}
// Shuffling
env.shuffle(steps)
// Solve
if human {
env.beginner()
} else {
env.idAstar()
}
}