-
Notifications
You must be signed in to change notification settings - Fork 132
/
move.go
72 lines (61 loc) · 1.59 KB
/
move.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
package chess
// A MoveTag represents a notable consequence of a move.
type MoveTag uint16
const (
// KingSideCastle indicates that the move is a king side castle.
KingSideCastle MoveTag = 1 << iota
// QueenSideCastle indicates that the move is a queen side castle.
QueenSideCastle
// Capture indicates that the move captures a piece.
Capture
// EnPassant indicates that the move captures via en passant.
EnPassant
// Check indicates that the move puts the opposing player in check.
Check
// inCheck indicates that the move puts the moving player in check and
// is therefore invalid.
inCheck
)
// A Move is the movement of a piece from one square to another.
type Move struct {
s1 Square
s2 Square
promo PieceType
tags MoveTag
}
// String returns a string useful for debugging. String doesn't return
// algebraic notation.
func (m *Move) String() string {
return m.s1.String() + m.s2.String() + m.promo.String()
}
// S1 returns the origin square of the move.
func (m *Move) S1() Square {
return m.s1
}
// S2 returns the destination square of the move.
func (m *Move) S2() Square {
return m.s2
}
// Promo returns promotion piece type of the move.
func (m *Move) Promo() PieceType {
return m.promo
}
// HasTag returns true if the move contains the MoveTag given.
func (m *Move) HasTag(tag MoveTag) bool {
return (tag & m.tags) > 0
}
func (m *Move) addTag(tag MoveTag) {
m.tags = m.tags | tag
}
type moveSlice []*Move
func (a moveSlice) find(m *Move) *Move {
if m == nil {
return nil
}
for _, move := range a {
if move.String() == m.String() {
return move
}
}
return nil
}