-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (101 loc) · 2.15 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
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func start() Position {
board, _ := FEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBKQBNR")
return Position{
board: board,
}
}
func cli() {
pos := start()
searcher := &Searcher{tp: map[Position]entry{}}
r := bufio.NewReader(os.Stdin)
for {
fmt.Println(pos.board)
valid := false
for !valid {
fmt.Print("Enter move: ")
input, _ := r.ReadString('\n')
input = strings.TrimSpace(input)
valid = false
for _, m := range pos.Moves() {
if input == m.String() {
pos = pos.Move(m)
valid = true
break
}
}
}
fmt.Println(pos.Flip().board)
m := searcher.Search(pos, 10000)
score := pos.value(m)
if score <= -MateValue {
fmt.Println("You won")
return
}
if score >= MateValue {
fmt.Println("You lost")
return
}
pos = pos.Move(m)
}
}
func uci() {
pos := start()
searcher := &Searcher{tp: map[Position]entry{}}
r := bufio.NewReader(os.Stdin)
sqr := map[string]Square{}
for i := Square(0); i < 120; i++ {
sqr[i.String()] = i
}
white := true
for {
input, _ := r.ReadString('\n')
input = strings.TrimSpace(input)
switch {
case input == "quit":
return
case input == "isready":
fmt.Println("readyok")
case input == "ucinewgame" || input == "position startpos":
pos = start()
white = true
case strings.HasPrefix(input, "position startpos moves "):
pos = start()
moves := strings.Split(input[24:], " ")
for i, s := range moves {
m := Move{from: sqr[s[0:2]], to: sqr[s[2:4]]}
if i%2 != 0 {
m = Move{from: m.from.Flip(), to: m.to.Flip()}
}
pos = pos.Move(m)
}
white = len(moves)%2 == 0
case strings.HasPrefix(input, "position fen "):
//takes position and returns board
b, _ := FEN(input[13:])
fmt.Println(b)
pos = Position{board: b}
case strings.HasPrefix(input, "go"):
//ask engine for best move
m := searcher.Search(pos, 10000)
if !white {
m = Move{from: m.from.Flip(), to: m.to.Flip()}
}
//uci protocol to return best move from engine to gui
fmt.Println("bestmove", m)
}
}
}
func main() {
if len(os.Args) == 2 && os.Args[1] == "cli" {
cli()
} else {
uci()
}
}