Skip to content

Commit

Permalink
Add enpassant rules to getCapturesInOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcticXWolf committed Apr 22, 2021
1 parent a8b2346 commit 5111048
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
10 changes: 10 additions & 0 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package game
import (
"errors"
"math/bits"
"reflect"

"github.com/dylhunn/dragontoothmg"
)
Expand Down Expand Up @@ -226,3 +227,12 @@ func (g *Game) PopMove() error {

return nil
}

func (g *Game) GetEnPassentSquare() uint8 {
value := reflect.ValueOf(g.Position).Elem().FieldByName("enpassant")
if value.Kind() != reflect.Uint8 {
return 0
}

return uint8(value.Uint())
}
20 changes: 20 additions & 0 deletions game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,23 @@ func TestPopNoMoves(t *testing.T) {
t.Errorf("PopNoMoves = %v, is not an error", err)
}
}

func TestGame_GetEnPassentSquare(t *testing.T) {
tests := []struct {
name string
game *Game
want uint8
}{
{"Startposition", New(), 0},
{"On c6", NewFromFen("rnbqkbnr/pp3ppp/3p4/2pPp3/4P3/8/PPP2PPP/RNBQKBNR w KQkq c6 0 41"), 42},
{"On b3", NewFromFen("rnbqkbnr/pp3ppp/3p4/3Pp3/1Pp1P3/5P2/P1P3PP/RNBQKBNR b KQkq b3 0 5"), 17},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

if got := tt.game.GetEnPassentSquare(); got != tt.want {
t.Errorf("Game.GetEnPassentSquare() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (s *Search) getCapturesInOrder() []dragontoothmg.Move {
}

for _, move := range s.Game.Position.GenerateLegalMoves() {
if bitboardsOpponent.All&(1<<move.To()) > 0 {
if isCaptureOrPromotionMove(s.Game, move) {
captures = append(captures, move)
}
}
Expand Down
4 changes: 0 additions & 4 deletions search/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ func BenchmarkSearchFullEvaluation9(b *testing.B) { benchmarkSearchFullEvaluati
func BenchmarkSearchFullEvaluation10(b *testing.B) { benchmarkSearchFullEvaluation(10, b) }
func BenchmarkSearchFullEvaluation11(b *testing.B) { benchmarkSearchFullEvaluation(11, b) }

func getMove(moveStr string) dragontoothmg.Move {
move, _ := dragontoothmg.ParseMove(moveStr)
return move
}
func TestSearch_SearchBestMove(t *testing.T) {
type fields struct {
Game *game.Game
Expand Down
10 changes: 8 additions & 2 deletions search/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ import (
"go.janniklasrichter.de/axwchessbot/game"
)

func getMove(moveStr string) dragontoothmg.Move {
move, _ := dragontoothmg.ParseMove(moveStr)
return move
}

func (s *Search) getCaptureMVVLVA(move dragontoothmg.Move, bitboardsOwn dragontoothmg.Bitboards, bitboardsOpponent dragontoothmg.Bitboards) (score int) {
pieceTypeFrom, _ := getPieceTypeAtPosition(move.From(), bitboardsOwn)
pieceTypeTo, _ := getPieceTypeAtPosition(move.To(), bitboardsOpponent)

return (1200 - s.evaluationProvider.GetPieceTypeValue(pieceTypeTo)) + int(pieceTypeFrom)
}

// TODO: No en_passent_rules checked yet!
func isCaptureOrPromotionMove(game *game.Game, move dragontoothmg.Move) bool {
bitboardsOwn := game.Position.White
bitboardsOpponent := game.Position.Black
if !game.Position.Wtomove {
bitboardsOwn = game.Position.Black
bitboardsOpponent = game.Position.White
}

return bitboardsOpponent.All&(1<<move.To()) > 0 || move.Promote() > 0
return bitboardsOpponent.All&(1<<move.To()) > 0 || move.Promote() > 0 || (bitboardsOwn.Pawns&(1<<move.From()) > 0 && game.GetEnPassentSquare() == move.To())
}

func getPieceTypeAtPosition(position uint8, bitboards dragontoothmg.Bitboards) (pieceType dragontoothmg.Piece, occupied bool) {
Expand Down
30 changes: 30 additions & 0 deletions search/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package search

import (
"testing"

"github.com/dylhunn/dragontoothmg"
"go.janniklasrichter.de/axwchessbot/game"
)

func Test_isCaptureOrPromotionMove(t *testing.T) {
type args struct {
game *game.Game
move dragontoothmg.Move
}
tests := []struct {
name string
args args
want bool
}{
{"En Passant", args{game.NewFromFen("rnbqkbnr/pp3ppp/3p4/3Pp3/1Pp1P3/5P2/P1P3PP/RNBQKBNR b KQkq b3 0 5"), getMove("c4b3")}, true},
{"Missed En Passant", args{game.NewFromFen("rnbqkbnr/pp4pp/3p1p2/3Pp3/1Pp1P3/5P1N/P1P3PP/RNBQKB1R b KQkq - 1 6"), getMove("c4b3")}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isCaptureOrPromotionMove(tt.args.game, tt.args.move); got != tt.want {
t.Errorf("isCaptureOrPromotionMove() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 5111048

Please sign in to comment.