/
see.go
140 lines (117 loc) · 4.67 KB
/
see.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
//
// FrankyGo - UCI chess engine in GO for learning purposes
//
// MIT License
//
// Copyright (c) 2018-2020 Frank Kopp
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package search
import (
"github.com/frankkopp/FrankyGo/internal/attacks"
"github.com/frankkopp/FrankyGo/internal/position"
. "github.com/frankkopp/FrankyGo/internal/types"
)
func see(p *position.Position, move Move) Value {
// enpassant moves are ignored in a sense that it will be winning
// capture and therefore should lead to no cut-offs when using see()
if move.MoveType() == EnPassant {
return 100
}
// prepare short array to store the captures - max 32 pieces
gain := make([]Value, 32, 32)
ply := 0
toSquare := move.To()
fromSquare := move.From()
movedPiece := p.GetPiece(fromSquare)
nextPlayer := p.NextPlayer()
// get a bitboard of all occupied squares to remove single pieces later
// to reveal hidden attacks (x-ray)
occupiedBitboard := p.OccupiedAll()
// get all attacks to the square as a bitboard
remainingAttacks := attacks.AttacksTo(p, toSquare, White) | attacks.AttacksTo(p, toSquare, Black)
// log := myLogging.GetLog()
// log.Debugf("Determine gain for %s %s", p.StringFen(), move.StringUci())
// initial value of the first capture
capturedValue := p.GetPiece(toSquare).ValueOf()
gain[ply] = capturedValue
// log.Debugf("gain[%d] = %s | %s", ply, gain[ply].String(), move.StringUci());
// loop through all remaining attacks/captures
for {
ply++
nextPlayer = nextPlayer.Flip()
// speculative store, if defended
if move.MoveType() == Promotion {
gain[ply] = move.PromotionType().ValueOf() - Pawn.ValueOf() - gain[ply-1]
} else {
gain[ply] = movedPiece.ValueOf() - gain[ply-1]
}
// pruning if defended - will not change final see score
if max(-gain[ply-1], gain[ply]) < 0 {
break
}
remainingAttacks.PopSquare(fromSquare) // reset bit in set to traverse
occupiedBitboard.PopSquare(fromSquare) // reset bit in temporary occupancy (for x-Rays)
// reevaluate attacks to reveal attacks after removing the moving piece
remainingAttacks |= attacks.RevealedAttacks(p, toSquare, occupiedBitboard, White) |
attacks.RevealedAttacks(p, toSquare, occupiedBitboard, Black)
// determine next capture
fromSquare = getLeastValuablePiece(p, remainingAttacks, nextPlayer)
// break if no more attackers
if fromSquare == SqNone {
break
}
// log.Debugf("gain[%d] = %s | %s%s", ply, gain[ply].String(), fromSquare.String(), toSquare.String())
movedPiece = p.GetPiece(fromSquare)
}
ply--
for ply > 0 {
gain[ply-1] = -max(-gain[ply-1], gain[ply])
ply--
}
return gain[0]
}
// Returns a square with the least valuable attacker. When several of same
// type are available it uses the least significant bit of the bitboard.
func getLeastValuablePiece(position *position.Position, bitboard Bitboard, color Color) Square {
// check all piece types with increasing value
switch {
case (bitboard & position.PiecesBb(color, Pawn)) != 0:
return (bitboard & position.PiecesBb(color, Pawn)).Lsb()
case (bitboard & position.PiecesBb(color, Knight)) != 0:
return (bitboard & position.PiecesBb(color, Knight)).Lsb()
case (bitboard & position.PiecesBb(color, Bishop)) != 0:
return (bitboard & position.PiecesBb(color, Bishop)).Lsb()
case (bitboard & position.PiecesBb(color, Rook)) != 0:
return (bitboard & position.PiecesBb(color, Rook)).Lsb()
case (bitboard & position.PiecesBb(color, Queen)) != 0:
return (bitboard & position.PiecesBb(color, Queen)).Lsb()
case (bitboard & position.PiecesBb(color, King)) != 0:
return (bitboard & position.PiecesBb(color, King)).Lsb()
default:
return SqNone
}
}
func max(x, y Value) Value {
if x > y {
return x
}
return y
}