-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
317 lines (288 loc) · 7.14 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
//"regexp"
"github.com/nelhage/taktician/cli"
"github.com/nelhage/taktician/ptn"
"github.com/nelhage/taktician/tak"
"github.com/nqeron/TakAnalysis/analysis"
"golang.org/x/net/context"
)
//VERSION -- current version
const (
VERSION = "0.1.3"
)
//regular expressions for commands
var (
//goRE = regexp.MustCompile("^go ([1-9]+)")
//t = 8
level = flag.Int("level", 8, "minimax level")
quiet = flag.Bool("q", false, "quiet the analysis printout")
showComment = flag.Bool("comm", false, "show comments with annotation")
debug = flag.Bool("d", false, "debug")
)
func main() {
flag.Parse()
fileName := flag.Arg(0)
f, err := os.Open(fileName)
defer f.Close()
useFile := false
if err == nil {
useFile = true
}
fmt.Printf("Welcome to the Tak Analysis console, version: %s \n", VERSION)
if useFile {
fmt.Printf("Using file: %s \n", fileName)
} else {
fmt.Println("Using blank PTN")
}
//Only handles files for now
if err != nil {
log.Fatal(err)
}
// first parse the PTN
parsed, e := ptn.ParsePTN(f)
if e != nil {
log.Fatal("parse: ", e)
}
doAnalysis := yn("Analyze file now?")
//if !doAnalysis
if doAnalysis {
fmt.Println("Analyzing file ...")
out := strings.Replace(fileName, ".ptn", "-analysis.ptn", -1)
//var outfile *os.File
//if outfile,err := os.Open(out); err != nil{
outfile, _ := os.Create(out)
analysis.Meta(parsed, outfile, analysis.Config{
Depth: *level,
Sensitivity: 2,
TimeLimit: time.Minute,
Debug: *debug,
Verbose: !*quiet,
AnnotationOnly: !*showComment,
})
outfile.Close()
fmt.Printf("Analysis Complete - output to file: %s \n", out)
anFile, err := os.Open(out)
if err != nil {
log.Fatal("analysis file: ", err)
}
//} else{
//fmt.Println("Using existant file")
//}
parsed, err = ptn.ParsePTN(anFile)
if err != nil {
log.Fatal("analysis to ptn: ", err)
}
}
var parsedSize int
if len(parsed.Ops)%3 == 0 {
parsedSize = len(parsed.Ops) / 3.0
} else {
parsedSize = len(parsed.Ops)/3.0 + 1
}
//options
var (
showAll = false //alt - show current
displayBoard = true
//depth = 6
moveNum = 1
moveColor = tak.White
moves = make([]*tak.Position, 0, 5)
moveNote = make([]tak.Move, 0, 5)
isExplore = false
)
//pos, err := parsed.InitialPosition()
pos, err := parsed.PositionAtMove(moveNum, moveColor)
if err != nil {
log.Fatal("position: ", err)
}
//var in string
for {
if showAll {
//hl := false
parsed.Render()
} else {
low := (moveNum - 1) * 3
up := moveNum*3 - 1
//if moveColor == tak.White && up >
if moveColor == tak.Black {
up = up + 2
}
//fmt.Printf("moveNum: %d, len(ops): %d, parsedSize: %s, moveColor: %s",moveNum,len(parsed.Ops),parsedSize, moveColor)
fmt.Printf(" (%d / %d)", moveNum, parsedSize)
for i := low; i <= up; i++ {
if i >= len(parsed.Ops) {
if moveColor == tak.Black {
fmt.Printf(" [__]")
break // don't print more
}
fmt.Printf(" __")
} else {
op := parsed.Ops[i]
switch o := op.(type) {
case *ptn.MoveNumber:
fmt.Printf("\n%d.", o.Number)
case *ptn.Move:
if (i%3 == 2 && moveColor == tak.Black) ||
(i%3 == 1 && moveColor == tak.White) {
fmt.Printf(" [%s%s]", ptn.FormatMove(o.Move), o.Modifiers)
} else {
fmt.Printf(" %s%s", ptn.FormatMove(o.Move), o.Modifiers)
}
default:
}
}
}
}
if isExplore {
fmt.Printf("\n {")
for _, m := range moveNote {
fmt.Printf(ptn.FormatMove(m) + ", ")
}
fmt.Printf("}")
}
if displayBoard {
cli.RenderBoard(&cli.DefaultGlyphs, os.Stdout, pos)
}
//keep hold of last (valid) position
lastMoveNum := moveNum
lastMoveColor := moveColor
//handle commands
for {
fmt.Printf("> ")
var cmd string
var num int
var mod string
fmt.Scanf("%s", &cmd)
switch cmd {
case "go":
fmt.Scanf("%d %s", &num, &mod)
//fmt.Printf("Called Go :",groups[1])
moveNum = num
moveColor = tak.White
if mod == "b" || mod == "black" {
moveColor = tak.Black
}
case "next", "n", "d":
if moveColor == tak.Black {
moveNum++
moveColor = tak.White
} else {
moveColor = tak.Black
}
case "prev", "p", "a":
if moveColor == tak.White {
moveNum--
moveColor = tak.Black
} else {
moveColor = tak.White
}
case "w":
moveNum++
case "s":
moveNum--
case "ai":
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Minute))
defer cancel()
ai := analysis.MakeAI(pos, *level)
moves, val, _ := ai.Analyze(ctx, pos)
fmt.Println("current value: ", val)
fmt.Printf("anticipated moves: ")
for _, m := range moves {
fmt.Printf("%s, ", ptn.FormatMove(m))
}
fmt.Printf("\n")
case "print", "render":
fmt.Println(parsed.Render())
case "undo", "u", "r":
if len(moves) <= 0 {
fmt.Println("Nothing to undo!")
} else {
pos = moves[len(moves)-1]
moves = moves[:len(moves)-1]
moveNote = moveNote[:len(moveNote)-1]
moveColor = moveColor ^ 192
if len(moves) <= 0 {
isExplore = false
}
}
case "q": //stop exploring
isExplore = false
moves = moves[:0]
moveNote = moveNote[:0]
case "help", "?":
printHelp()
case "exit":
isSure := yn("Are you sure want to exit the program?")
if isSure {
os.Exit(0)
}
default: //by default try to see if this is a legal move
newMove, err := ptn.ParseMove(cmd)
if err != nil {
fmt.Println("Command not recognized! cmd: ", cmd)
} else { // handling a move
moveNote = append(moveNote, newMove)
lastPos := pos
pos, err = pos.Move(newMove)
if err != nil {
fmt.Println("Movement error: ", err)
pos = lastPos
continue //go back to commands ?
}
moves = append(moves, lastPos)
isExplore = true
moveColor = moveColor ^ 192 // swap color ()
}
}
//check to make sure upcoming position is valid
if !isExplore {
pos, err = parsed.PositionAtMove(moveNum, moveColor)
if err != nil {
fmt.Println("position isn't valid: ", err)
moveNum = lastMoveNum
moveColor = lastMoveColor
continue
}
}
//moveNum = int(groups[1])
break
}
//fmt.Scanln()
}
}
func yn(q string) bool {
var in string
for {
fmt.Println(q)
fmt.Printf("?> ")
fmt.Scanln(&in)
in = strings.ToLower(in)
if (in == "y") || (in == "yes") {
return true
} else if (in == "n") || (in == "no") {
return false
} else {
fmt.Println("I don't recognize that response")
}
}
}
func printHelp() {
fmt.Printf(
" print: prints out the entire PTN \n" +
" go [move] {black}: moves the board to the given move line in the PTN \n" +
" n or next: moves to the next ply \n" +
" p or prev: moves to the previous ply \n" +
" ai: calls up the ai analysis for the given position \n" +
" exit: ends the program \n" +
" {move}: e.g. a1+, will make the given move in the position \n" +
" q: returns to original PTN \n" +
" u or undo: undo last move made \n" +
" For more details, see the the Readme file \n")
}