-
Notifications
You must be signed in to change notification settings - Fork 0
/
roce_cmp.go
262 lines (219 loc) · 5.35 KB
/
roce_cmp.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"
"github.com/schafer14/MtM/board"
)
type divideErrorType uint
const (
LEGAL_MOVE_NOT_FOUND divideErrorType = iota
ILLEGAL_MOVE_FOUND
MOVE_COUNT_MISMATCH
MISMATCH_MOVE_NUM
)
// The start position and the moves to make from the start position
type position struct {
fen string
moves []string
}
type divideError struct {
errorType divideErrorType
move string
position position
}
func roceCmp(position position, depth int) []divideError {
// Setup the board
b := board.FromFen(position.fen)
// Make moves on board
for _, moveStr := range position.moves {
move, _ := b.MoveFromSrcDestNotation(moveStr)
b.Move(move)
}
// Actual testing
for i := depth; i > 0; i++ {
bDivide := divide(b, depth)
rDivide := roceDivide(b.String(), depth)
errors := cmpDivideOutput(bDivide, rDivide, position)
if len(errors) == 0 {
return errors
}
for _, err := range errors {
if err.errorType == MISMATCH_MOVE_NUM || err.errorType == ILLEGAL_MOVE_FOUND || err.errorType == LEGAL_MOVE_NOT_FOUND {
return errors
}
}
position.moves = append(position.moves, errors[0].move)
return roceCmp(position, depth-1)
}
return []divideError{}
}
func (de divideError) String() string {
var moves string
if len(de.position.moves) > 0 {
moves = "after moves " + strings.Join(de.position.moves, ", ")
}
if de.errorType == LEGAL_MOVE_NOT_FOUND {
return fmt.Sprintf(
"Legal move %v not available in position '%v' %v",
de.move,
de.position.fen,
moves,
)
}
if de.errorType == ILLEGAL_MOVE_FOUND {
return fmt.Sprintf(
"Illegal move %v available in position '%v' %v",
de.move,
de.position.fen,
moves,
)
}
if de.errorType == MOVE_COUNT_MISMATCH {
return fmt.Sprintf(
"Move count mismatch for move %v after position '%v' %v",
de.move,
de.position.fen,
moves,
)
}
if de.errorType == MISMATCH_MOVE_NUM {
return fmt.Sprintf(
"Move count mismatch after position '%v' %v",
de.position.fen,
moves,
)
}
return "Unknown Divide Error"
}
func cmpDivideOutput(output divideOutput, groundTruth divideOutput, position position) (errs []divideError) {
if output.moves != groundTruth.moves {
errs = append(errs, divideError{
errorType: MISMATCH_MOVE_NUM,
position: position,
})
}
for _, trueMoveCount := range groundTruth.moveStr {
hasMove := false
foundCorrespondingMove := false
for _, outputMoveCount := range output.moveStr {
if outputMoveCount.move == trueMoveCount.move {
hasMove = true
}
if outputMoveCount.move == trueMoveCount.move && outputMoveCount.count == trueMoveCount.count {
foundCorrespondingMove = true
}
}
if !hasMove {
errs = append(errs, divideError{
errorType: LEGAL_MOVE_NOT_FOUND,
move: trueMoveCount.move,
position: position,
})
}
if !foundCorrespondingMove {
errs = append(errs, divideError{
errorType: MOVE_COUNT_MISMATCH,
move: trueMoveCount.move,
position: position,
})
}
}
for _, outputMoveCount := range output.moveStr {
hasMove := false
for _, trueMoveCount := range groundTruth.moveStr {
if outputMoveCount.move == trueMoveCount.move {
hasMove = true
}
}
if !hasMove {
errs = append(errs, divideError{
errorType: ILLEGAL_MOVE_FOUND,
move: outputMoveCount.move,
position: position,
})
}
}
return errs
}
func roceDivide(fen string, d int) divideOutput {
if fen == "startpos" {
fen = STARTPOS
}
cmd := exec.Command("roce38")
cmd.Stderr = os.Stderr
stdin, err := cmd.StdinPipe()
if nil != err {
log.Fatalf("Error obtaining stdin: %s", err.Error())
}
stdout, err := cmd.StdoutPipe()
if nil != err {
log.Fatalf("Error obtaining stdout: %s", err.Error())
}
if err := cmd.Start(); nil != err {
log.Fatalf("Error starting program: %s, %s", cmd.Path, err.Error())
}
reader := bufio.NewReader(stdout)
waitForPrompt(reader)
stdin.Write([]byte(fmt.Sprintf("st 1\n")))
waitForPrompt(reader)
stdin.Write([]byte(fmt.Sprintf("setboard %s\n", fen)))
waitForPrompt(reader)
stdin.Write([]byte(fmt.Sprintf("divide %v\n", d)))
divide := waitForPrompt(reader)
return parseDivide(divide)
}
func parseDivide(lines []string) divideOutput {
var moves, nodes int
var moveStr []moveStringCount
length := len(lines)
for i := 0; i < length; i++ {
if strings.TrimSpace(lines[i]) == "" {
continue
}
parts := strings.Split(strings.TrimSpace(lines[i]), " ")
if parts[0] == "Moves" {
moveString := strings.Split(lines[length-2], " ")[1]
moveInt, _ := strconv.ParseInt(moveString, 10, 32)
moves = int(moveInt)
} else if parts[0] == "Nodes" {
nodeString := strings.Split(lines[length-1], " ")[1]
nodesInt, _ := strconv.ParseInt(nodeString, 10, 32)
nodes = int(nodesInt)
} else {
numInt, _ := strconv.ParseInt(parts[1], 10, 32)
moveStr = append(moveStr, moveStringCount{move: parts[0], count: int(numInt)})
}
}
return divideOutput{
moveStr: moveStr,
moves: moves,
nodes: nodes,
}
}
func waitForPrompt(scanner *bufio.Reader) []string {
var curLine string
var outputs []string
loop:
for {
ch, _ := scanner.ReadByte()
switch ch {
case '\n':
if curLine != "" {
outputs = append(outputs, curLine)
curLine = ""
}
case ':':
if curLine == "roce" {
break loop
}
default:
curLine = curLine + string(ch)
}
}
return outputs
}