-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (80 loc) · 2.05 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"time"
)
func main() {
var (
defaultTime = 3 * time.Second
moveTime = flag.Duration("time", 0, fmt.Sprintf("computer's time per move (default %v if no depth limit set)", defaultTime))
depth = flag.Int("depth", 0, "the search depth")
fen = flag.String("fen", InitialPositionFEN, "the FEN record of the starting position")
humanWhite = flag.Bool("w", false, "user plays White")
humanBlack = flag.Bool("b", false, "user plays Black")
)
flag.Parse()
if *moveTime <= 0 {
*moveTime = 86164091 * time.Millisecond
if *depth <= 0 {
*moveTime = defaultTime
}
}
if *depth <= 0 {
*depth = 100
}
pos, err := ParseFEN(*fen)
if err != nil {
panic(err)
}
startpos := pos
stdin := bufio.NewScanner(os.Stdin)
players := []Player{Computer{*moveTime, *depth}, Computer{*moveTime, *depth}}
if *humanWhite {
players[White] = Human{stdin}
}
if *humanBlack {
players[Black] = Human{stdin}
}
fmt.Println(pos)
startTime := time.Now()
var moves []Move
var resultText string
posZobrists := make(map[Zobrist]int)
game:
for {
moveTime := time.Now()
score, move := players[pos.ToMove].Play(pos)
if move == (Move{}) {
// player resigns
resultText = []string{"1-0", "0-1"}[pos.Opp()]
break
}
numalg := numberedAlgebraic(pos, move) // before Make
moves = append(moves, move)
pos = Make(pos, move)
if s, ok := score.err.(checkmateError); ok {
score = Abs{err: s.Next()}
}
fmt.Printf("%v %v %v\n", numalg, score, time.Since(moveTime).Truncate(time.Millisecond))
fmt.Println(pos)
// Check for end-of-game conditions
switch score.err {
case errCheckmate:
resultText = []string{"1-0", "0-1"}[pos.Opp()]
break game
case errStalemate, errInsufficient, errFiftyMove:
resultText = "1/2-1/2"
break game
}
if posZobrists[pos.z]++; posZobrists[pos.z] == 3 {
// threefold repetition
resultText = "1/2-1/2"
break
}
}
fmt.Printf("%v %v\n", Text(startpos, moves), resultText)
fmt.Println(time.Since(startTime).Truncate(time.Millisecond))
}