Skip to content

Commit 28b2451

Browse files
committed
Tweak bernstein evaluation and opening
1 parent 748ad72 commit 28b2451

File tree

9 files changed

+48
-12
lines changed

9 files changed

+48
-12
lines changed

cmd/bernstein/bernstein/book.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package bernstein
2+
3+
import "github.com/herohde/morlock/pkg/engine"
4+
5+
var opening = engine.Line([]string{"e2e4"})
6+
7+
func NewBook() engine.Book {
8+
ret, _ := engine.NewBook([]engine.Line{opening})
9+
return ret
10+
}

cmd/bernstein/bernstein/eval.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,16 @@ func Control(pos *board.Position, side board.Color) int {
109109
}
110110

111111
// KingDefense returns the number of squares around the king defended by the given side, but
112-
// with no opponent attackers. Populated squares included.
112+
// with no opponent attackers. Populated squares included. If empty, ignore the King.
113113
func KingDefense(pos *board.Position, side board.Color) int {
114114
ret := 0
115115
for _, sq := range board.KingAttackboard(pos.KingSquare(side)).ToSquares() {
116+
if pos.IsEmpty(sq) {
117+
if pos.IsDefendedBy(side, sq, board.QueenRookKnightBishopPawn) && !pos.IsAttacked(side, sq) {
118+
ret++
119+
}
120+
continue
121+
}
116122
if pos.IsDefended(side, sq) && !pos.IsAttacked(side, sq) {
117123
ret++
118124
}

cmd/bernstein/bernstein/eval_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ func TestEval(t *testing.T) {
1717
material int
1818
}{
1919
{fen.Initial, 20, 22, 5, 39},
20-
{"k7/8/8/8/8/8/8/K7 w - - 0 1", 3, 3, 3, 0}, // K
21-
{"k7/7R/8/8/8/8/8/K7 w - - 0 1", 3 + 7 + 7, 3 + 7 + 5 /* -2 for k */, 3, 5}, // K+R
22-
{"k7/7R/8/8/8/8/8/K7 b - - 0 1", 1 /* -2 due to R */, 1, 1, 0}, // K with limited mobility
23-
{"k7/p6R/8/8/8/8/8/K7 b - - 0 1", 1 + 2, 1 + 1, 1, 1}, // K+P
24-
{"k7/1p5R/8/8/8/8/8/K7 b - - 0 1", 2 + 2, 2 + 2, 2, 1}, // K+P w/ block
20+
{"k7/8/8/8/8/8/8/K7 w - - 0 1", 3, 3, 0, 0}, // K
21+
{"k7/7R/8/8/8/8/8/K7 w - - 0 1", 3 + 7 + 7, 3 + 7 + 5 /* -2 for k */, 0, 5}, // K+R
22+
{"k7/7R/8/8/8/8/8/K7 b - - 0 1", 1 /* -2 due to R */, 1, 0, 0}, // K with limited mobility
23+
{"k7/p6R/8/8/8/8/8/K7 b - - 0 1", 1 + 2, 1 + 1, 0, 1}, // K+P
24+
{"k7/1p5R/8/8/8/8/8/K7 b - - 0 1", 2 + 2, 2 + 2, 0, 1}, // K+P w/ block
25+
{"k7/pp5R/8/8/8/8/8/K7 b - - 0 1", 2 + 2 + 1, 2 + 2 + 1, 1, 2}, // K+2P
2526
{"k7/7R/1R6/8/8/8/8/K7 b - - 0 1", 0, 0, 0, 0}, // stalemate
2627
}
2728

cmd/bernstein/bernstein/search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,13 @@ func Table1(move board.Move) board.MovePriority {
238238
case board.FileB:
239239
return 3
240240
case board.FileC:
241-
return 5
241+
return 6 // favor C over F
242242
case board.FileD:
243243
return 7
244244
case board.FileE:
245245
return 8
246246
case board.FileF:
247-
return 6
247+
return 5
248248
case board.FileG:
249249
return 4
250250
case board.FileH:

cmd/bernstein/bernstein/search_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestFindPlausibleMoves(t *testing.T) {
2121
// 2023 game2
2222
{"rnbqk1nr/ppp2ppp/3p4/2b1p3/1PP1P3/P4N2/3P1PPP/RNBQKB1R b KQkq - 0 5", 7, "Bc5-d4 Bc5-b6 Bc8-g4 Ng8-h6 Ng8-f6 Bc8-e6 Nb8-c6"}, // move 5: search did not pick B
2323
{"rn1q1rk1/ppp2ppp/3p1n2/8/1PP1p1b1/PQ3N2/4BPPP/RNB2RK1 b - - 1 12", 7, "e4*f3 Bg4*f3 Nb8-c6 Nb8-a6 Nb8-d7 c7-c5 d6-d5"}, // move 12: no capture
24+
{"rnbqkbnr/pppp1ppp/8/4p3/8/2N5/PPPPPPPP/R1BQKBNR w KQkq e6 0 2", 7, "Ng1-f3 Ng1-h3 e2-e4 e2-e3 d2-d4 d2-d3 f2-f3"}, // quirk that 4ply search picks "f3"
2425
}
2526

2627
for _, tt := range tests {

cmd/bernstein/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/herohde/morlock/pkg/search"
1313
"github.com/seekerror/logw"
1414
"os"
15+
"time"
1516
)
1617

1718
var (
@@ -55,7 +56,7 @@ func main() {
5556
in := engine.ReadStdinLines(ctx)
5657
switch <-in {
5758
case uci.ProtocolName:
58-
driver, out := uci.NewDriver(ctx, e, in)
59+
driver, out := uci.NewDriver(ctx, e, in, uci.UseBook(bernstein.NewBook(), time.Now().UnixNano()))
5960
go engine.WriteStdoutLines(ctx, out)
6061

6162
<-driver.Closed()

pkg/board/piece.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
)
2020

2121
var (
22+
AllPieces = []Piece{King, Queen, Rook, Knight, Bishop, Pawn}
2223
KingQueen = []Piece{King, Queen}
2324
KingQueenRookKnightBishop = []Piece{King, Queen, Rook, Knight, Bishop}
2425
QueenRookBishop = []Piece{Queen, Rook, Bishop}

pkg/board/position.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,32 @@ func (p *Position) IsDefended(c Color, sq Square) bool {
167167
return p.IsAttacked(c.Opponent(), sq)
168168
}
169169

170+
// IsDefendedBy returns true iff the square is defended by pieces of the color.
171+
func (p *Position) IsDefendedBy(c Color, sq Square, list []Piece) bool {
172+
return p.IsAttackedBy(c.Opponent(), sq, list)
173+
}
174+
170175
// IsAttacked returns true iff the square is attacked by the opposing color. Does not include en passant.
171176
func (p *Position) IsAttacked(c Color, sq Square) bool {
177+
return p.IsAttackedBy(c, sq, AllPieces)
178+
}
179+
180+
// IsAttackedBy returns true iff the square is attacked by the given pieces of the opposing color. Does not include en passant.
181+
func (p *Position) IsAttackedBy(c Color, sq Square, list []Piece) bool {
172182
opp := c.Opponent()
173183

174-
for _, piece := range KingQueenRookKnightBishop {
184+
for _, piece := range list {
185+
if piece == Pawn {
186+
if PawnCaptureboard(opp, p.pieces[opp][Pawn])&BitMask(sq) != 0 {
187+
return true
188+
}
189+
continue
190+
}
175191
if pieces := p.pieces[opp][piece]; pieces != 0 && Attackboard(p.rotated, sq, piece)&pieces != 0 {
176192
return true
177193
}
178194
}
179-
return PawnCaptureboard(opp, p.pieces[opp][Pawn])&BitMask(sq) != 0
195+
return false
180196
}
181197

182198
// IsChecked returns true iff the color is in check. Convenient for IsAttacked(King).

pkg/engine/engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"sync"
1515
)
1616

17-
var version = build.NewVersion(0, 91, 0)
17+
var version = build.NewVersion(0, 91, 1)
1818

1919
// Options are search creation options.
2020
type Options struct {

0 commit comments

Comments
 (0)