/
main.go
160 lines (143 loc) · 5.06 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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"log"
"os"
"runtime/debug"
"runtime/pprof"
"time"
)
var _ = fmt.Println
func InitializeLogger() {
file, err := os.OpenFile("/tmp/ra-chess-engine.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
logger = log.New(file, "", log.LstdFlags|log.Lshortfile)
logger.Println("Starting up!")
}
func main() {
startingFen := flag.String("fen", "", "Fen board")
variation := flag.String("variation", "", "Variation to apply prior to perft/tactics/eval (pair with --fen)")
epdFile := flag.String("epd", "", "Position file in EPD format")
epdRegex := flag.String("epdregex", "", "Run only positions matching the given id")
cpuProfile := flag.String("cpuprofile", "", "File to write CPU profile to")
memProfile := flag.String("memprofile", "", "File to write Memory profile to")
isPerft := flag.Bool("perft", false, "Perft mode")
perftDepth := flag.Uint("perftdepth", 5, "Perft depth to search")
perftChecks := flag.Bool("perftchecks", false, "Perft: count check positions (slower)")
perftSanityCheck := flag.Bool("sanitycheck", false, "Perft: sanity check board and moves (slower)")
perftJSONFile := flag.String("perftjson", "", "JSON specification")
perftPrintMoves := flag.Bool("printmoves", false, "Perft: print all generates moves at final depth")
perftDivide := flag.Bool("perftdivide", false, "Perft: print divide of all moves at top depth")
isTactics := flag.Bool("tactics", false, "Tactics mode")
tacticsThinkingTime := flag.Uint("tacticsthinkingtime", 1500, "Time to think per position (ms)")
tacticsDebug := flag.String("tacticsdebug", "", "Output more information during tactics if the move matches the string")
tacticsDepth := flag.Uint("tacticsdepth", 0, "Only run tactics search for the given depth")
tacticsHashVariation := flag.String("tacticshashvariation", "", "Output transposition table information for given variation")
isMagic := flag.Bool("magic", false, "Generate magic bitboard constants (write to rook-magics.json and bishop-magics.json)")
isEval := flag.Bool("eval", false, "Run evaluation on the specified position or positions (no search)")
flag.Parse()
InitializeLogger()
InitializeMoveBitboards()
var success = true
var err error
defer func() {
if x := recover(); x != nil {
debug.PrintStack()
fmt.Printf("Fatal error: %v", x)
logger.Fatalf("Fatal error: %v", x)
}
}()
var cpuProfileFile *os.File
if *cpuProfile != "" {
cpuProfileFile, err = os.Create(*cpuProfile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
if err := pprof.StartCPUProfile(cpuProfileFile); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
}
if *isPerft || *perftJSONFile != "" {
var options PerftOptions
options.checks = *perftChecks
options.sanityCheck = *perftSanityCheck
options.perftPrintMoves = *perftPrintMoves
options.divide = *perftDivide
options.depth = *perftDepth
start := time.Now()
if *perftJSONFile != "" {
success, err = RunPerftJson(*perftJSONFile, options)
} else {
if *startingFen == "" {
*startingFen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
}
success, err = RunPerft(*startingFen, *variation, *perftDepth, options)
}
fmt.Printf("Total time: %s\n", time.Since(start))
} else if *isTactics {
var options TacticsOptions
options.thinkingtimeMs = *tacticsThinkingTime
options.epdRegex = *epdRegex
options.tacticsDebug = *tacticsDebug
options.tacticsDepth = *tacticsDepth
options.tacticsHashVariation = *tacticsHashVariation
if *epdFile != "" {
success, err = RunTacticsFile(*epdFile, *variation, options)
} else if *startingFen != "" {
prettyMove, result, err := RunTacticsFen(*startingFen, *variation, options)
if err == nil {
fmt.Printf("move=%s result=%s\n", prettyMove, result.String())
}
} else {
err = errors.New("Must specify either an EPD file or a fen argument")
}
} else if *isMagic {
GenerateMagicBitboards()
} else if *isEval {
var options EvalOptions
options.epdRegex = *epdRegex
if *epdFile != "" {
success, err = RunEvalFile(*epdFile, *variation, options)
} else if *startingFen != "" {
var eval BoardEval
eval, err = RunEvalFen(*startingFen, *variation, options)
fmt.Println(BoardEvalToString(eval))
} else {
err = errors.New("Must specify either an EPD file or a fen argument")
}
} else {
// xboard mode
scanner := bufio.NewScanner(os.Stdin)
output := bufio.NewWriter(os.Stdout)
success, err = RunXboard(scanner, output)
}
if cpuProfileFile != nil {
pprof.StopCPUProfile()
cpuProfileFile.Close()
fmt.Printf("CPU profile written to %s\n", *cpuProfile)
}
if *memProfile != "" {
memProfileFile, err := os.Create(*memProfile)
if err != nil {
log.Fatal("could not create heap profile: ", err)
}
if err := pprof.WriteHeapProfile(memProfileFile); err != nil {
log.Fatal("could not start heap profile: ", err)
} else {
fmt.Printf("Heap profile written to %s\n", *memProfile)
}
}
if err != nil {
fmt.Println(err)
os.Exit(1)
} else if success {
os.Exit(0)
} else {
os.Exit(1)
}
}