-
Notifications
You must be signed in to change notification settings - Fork 1
/
xboard.go
179 lines (171 loc) · 4.46 KB
/
xboard.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"time"
)
// threeFoldRepetition find out how many times current position is repeated in the game
func threeFoldRepetition(pos *Board) (r int) {
for i := 0; i < pos.HisPly; i++ {
if pos.History[i].PosKey == pos.PosKey {
r++
}
}
return
}
// drawMaterial suggests if the game is in draw in the current state of the game
func drawMaterial(pos *Board) bool {
if pos.PceNum[Wp] > 0 || pos.PceNum[Bp] > 0 {
return false
}
if pos.PceNum[Wq] > 0 || pos.PceNum[Bq] > 0 || pos.PceNum[Wr] > 0 || pos.PceNum[Br] > 0 {
return false
}
if pos.PceNum[Wb] > 1 || pos.PceNum[Bb] > 1 {
return false
}
if pos.PceNum[Wn] > 1 || pos.PceNum[Bn] > 1 {
return false
}
if pos.PceNum[Wn] > 0 || pos.PceNum[Wb] > 0 {
return false
}
if pos.PceNum[Bn] > 0 || pos.PceNum[Bb] > 0 {
return false
}
return true
}
// checkResult checks whether game is in draw, stale mate or mate position after each move in the game
func checkResult(pos *Board) bool {
if pos.FiftyMove > 100 {
fmt.Printf("1/2-1/2 {fifty move rule (claimed by go-chess)}\n")
return true
}
if threeFoldRepetition(pos) >= 2 {
fmt.Printf("1/2-1/2 {3-fold repetition (claimed by go-chess)}\n")
return true
}
if drawMaterial(pos) {
fmt.Printf("1/2-1/2 {insufficient material (claimed by go-chess)}\n")
return true
}
var list MoveList
found := false
(&list).GenerateAllMoves(pos)
for i := 0; i < list.count; i++ {
if moveMade, _ := MakeMove(list.moves[i].move, pos); !moveMade {
continue
}
found = true
TakeMove(pos)
break
}
if found {
return false
}
if attacked, _ := SqAttacked(pos.KingSq[pos.Side], pos.Side^1, pos); attacked {
if pos.Side == White {
fmt.Printf("0-1 {black mates (claimed by go-chess)}\n")
} else {
fmt.Printf("0-1 {white mates (claimed by go-chess)}\n")
}
return true
} else {
fmt.Printf("\n1/2-1/2 {stalemate (claimed by go-chess)}\n")
return true
}
return false
}
func XboardLoop(pos *Board, info *SearchInfo) {
depth := -1
mps := 0
movestogo := []int{30, 30}
engineSide := Both
var movetime time.Duration
var duration time.Duration
var inc time.Duration
for true {
if pos.Side == engineSide && !checkResult(pos) {
info.startTime = time.Now()
info.depth = depth
if duration.String() != "0s" {
info.timeSet = true
duration, _ = time.ParseDuration(fmt.Sprintf("%vms", duration.Seconds()/float64(movestogo[pos.Side])*1000-50))
info.stopTime = info.startTime.Add(duration).Add(inc)
}
if depth == -1 || depth > MaxDepth {
info.depth = MaxDepth
}
fmt.Printf("time: %s start: %s stop: %s depth: %d timeset: %v movestogo: %d movetime: %s\n", duration.String(), info.startTime.String(), info.stopTime.String(), info.depth, info.timeSet, movestogo[pos.Side], movetime.String())
SearchPosition(pos, info)
if mps != 0 {
movestogo[pos.Side^1]--
if movestogo[pos.Side^1] < 1 {
movestogo[pos.Side^1] = mps
}
}
}
command, _ := bufio.NewReader(os.Stdin).ReadString('\n')
command = strings.Trim(command, " ")
if command == "quit" {
break
}
if command == "force" {
engineSide = Both
continue
}
if command == "protover" {
fmt.Println("feature ping=1 setboard=1 colors=0 usermove=1")
fmt.Println("feature done=1")
continue
}
if strings.Contains(command, "sd") {
parseArr := strings.Split(command, "sd ")
parseArr = strings.Split(parseArr[1], " ")
depth, _ = strconv.Atoi(parseArr[0])
continue
}
if strings.Contains(command, "st") {
parseArr := strings.Split(command, "st ")
parseArr = strings.Split(parseArr[1], " ")
movetime, _ = time.ParseDuration(fmt.Sprintf("%sms", parseArr[0]))
continue
}
if strings.Contains(command, "ping") {
parseArr := strings.Split(command, "ping ")
parseArr = strings.Split(parseArr[1], " ")
fmt.Printf("pong%s\n", parseArr[0])
continue
}
if strings.Contains(command, "new") {
engineSide = Black
ParseFEN(StartFEN, pos)
depth = -1
continue
}
if strings.Contains(command, "setboard") {
engineSide = Both
parseArr := strings.Split(command, "setboard ")
ParseFEN(parseArr[1], pos)
continue
}
if strings.Contains(command, "go") {
engineSide = pos.Side
continue
}
if strings.Contains(command, "usermove") {
movestogo[pos.Side]--
parseArr := strings.Split(command, "setboard ")
move, _ := ParseMove(strings.Trim(parseArr[1], " "), pos)
if move != NoMove {
MakeMove(move, pos)
pos.Ply = 0
} else {
continue
}
}
}
}