-
Notifications
You must be signed in to change notification settings - Fork 1
/
attack.go
79 lines (73 loc) · 1.85 KB
/
attack.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
package main
import "errors"
// SqAttacked evaluates if a particular square(sq) is attacked by opposing side(side) or not
func SqAttacked(sq, side int, pos *Board) (bool, error) {
// check if square, side and position of board is valid or not
if !SqOnBoard(sq) {
return false, errors.New("Square is not on board")
}
if !SideValid(side) {
return false, errors.New("Side is invalid")
}
err := pos.Check()
if err != nil {
return false, err
}
// pawns are attacking or not
if side == White {
if (pos.Pieces[sq-9] == Wp) || (pos.Pieces[sq-11] == Wp) {
return true, nil
}
} else {
if (pos.Pieces[sq+9] == Bp) || (pos.Pieces[sq+11] == Bp) {
return true, nil
}
}
// knights are attacking or not
for index := 0; index < 8; index++ {
piece := pos.Pieces[sq+KnDir[index]]
if (piece != Offboard) && IsKn(piece) == True && PieceCol[piece] == side {
return true, nil
}
}
// rooks or queens are attacking or not
for index := 0; index < 4; index++ {
direction := RkDir[index]
checkSq := sq + direction
piece := pos.Pieces[checkSq]
for piece != Offboard {
if piece != Empty {
if IsRQ(piece) == True && PieceCol[piece] == side {
return true, nil
}
break
}
checkSq += direction
piece = pos.Pieces[checkSq]
}
}
// bishops or queens are attacking or not
for index := 0; index < 4; index++ {
direction := BiDir[index]
checkSq := sq + direction
piece := pos.Pieces[checkSq]
for piece != Offboard {
if piece != Empty {
if IsBQ(piece) == True && PieceCol[piece] == side {
return true, nil
}
break
}
checkSq += direction
piece = pos.Pieces[checkSq]
}
}
// kings are attacking or not
for index := 0; index < 8; index++ {
piece := pos.Pieces[sq+KiDir[index]]
if (piece != Offboard) && IsKi(piece) == True && PieceCol[piece] == side {
return true, nil
}
}
return false, nil
}