-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_test.go
75 lines (68 loc) · 1.89 KB
/
move_test.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
package octad
import (
"log"
"testing"
)
func unsafeOFEN(s string) *Position {
pos, err := decodeOFEN(s)
if err != nil {
log.Fatal(err)
}
return pos
}
type perfTest struct {
pos *Position
nodesPerDepth []int
}
// https://www.chessprogramming.org/Perft_Results
var perfResults = []perfTest{
{pos: unsafeOFEN("ppkn/4/4/NKPP w NCFncf - 0 1"), nodesPerDepth: []int{
10, 84, 642, 4375, 29309, 183931,
}},
{pos: unsafeOFEN("ppkn/4/2P1/NK1P w NCFncf - 0 1"), nodesPerDepth: []int{
10, 75, 538, 3433,
}},
}
func TestPerfResults(t *testing.T) {
for _, perf := range perfResults {
countMoves(t, perf.pos, []*Position{perf.pos}, perf.nodesPerDepth, len(perf.nodesPerDepth))
}
}
func countMoves(t *testing.T, originalPosition *Position, positions []*Position, nodesPerDepth []int, maxDepth int) {
if len(nodesPerDepth) == 0 {
return
}
depth := maxDepth - len(nodesPerDepth) + 1
expNodes := nodesPerDepth[0]
newPositions := make([]*Position, 0)
for _, pos := range positions {
for _, move := range pos.ValidMoves() {
newPos := pos.Update(move)
newPositions = append(newPositions, newPos)
}
}
gotNodes := len(newPositions)
if expNodes != gotNodes {
t.Errorf("Depth: %d Expected: %d Got: %d", depth, expNodes, gotNodes)
t.Log("##############################")
t.Log("# Original position info")
t.Log("###")
t.Log(originalPosition.String())
t.Log(originalPosition.board.Draw())
t.Log("##############################")
t.Log("# Details in JSONL (http://jsonlines.org)")
t.Log("###")
//for _, pos := range positions {
// //t.Logf(`{"position": "%s", "moves": %d}`, pos.String(), len(pos.ValidMoves()))
//}
}
countMoves(t, originalPosition, newPositions, nodesPerDepth[1:], maxDepth)
}
func BenchmarkValidMoves(b *testing.B) {
pos := unsafeOFEN("ppkn/4/4/NKPP w NCFncf - 0 1")
b.ResetTimer()
for n := 0; n < b.N; n++ {
pos.ValidMoves()
pos.validMoves = nil
}
}