-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmv.go
More file actions
78 lines (66 loc) · 1.59 KB
/
Copy pathmv.go
File metadata and controls
78 lines (66 loc) · 1.59 KB
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
// mv.go defines the compact move type used by the engine.
package pgn
import "fmt"
// Mv represents a chess move with from/to squares and optional promotion.
type Mv struct {
From Square
To Square
Promo PromoPiece
Flags uint16
}
// String returns the move in UCI notation (e.g., "e2e4", "e7e8q").
func (m Mv) String() string {
s := m.From.String() + m.To.String()
switch m.Promo {
case PromoQueen:
s += "q"
case PromoRook:
s += "r"
case PromoBishop:
s += "b"
case PromoKnight:
s += "n"
}
return s
}
// PromoPiece represents the piece type for pawn promotion.
type PromoPiece int
const (
NoPromo PromoPiece = iota
PromoQueen
PromoRook
PromoBishop
PromoKnight
)
// ParseUCI parses a move in UCI notation (e.g., "e2e4", "e7e8q").
// Returns an error if the string is not a valid UCI move format.
// Note: This only parses the format; it does not validate legality.
func ParseUCI(uci string) (Mv, error) {
if len(uci) < 4 || len(uci) > 5 {
return Mv{}, fmt.Errorf("invalid UCI move length: %q", uci)
}
from, err := ParseSquare(uci[0:2])
if err != nil {
return Mv{}, fmt.Errorf("invalid from square in UCI move: %w", err)
}
to, err := ParseSquare(uci[2:4])
if err != nil {
return Mv{}, fmt.Errorf("invalid to square in UCI move: %w", err)
}
mv := Mv{From: from, To: to}
if len(uci) == 5 {
switch uci[4] {
case 'q', 'Q':
mv.Promo = PromoQueen
case 'r', 'R':
mv.Promo = PromoRook
case 'b', 'B':
mv.Promo = PromoBishop
case 'n', 'N':
mv.Promo = PromoKnight
default:
return Mv{}, fmt.Errorf("invalid promotion piece: %c", uci[4])
}
}
return mv, nil
}