From f1cb08b94ccbda99f25c0e065fd6e28388d317d0 Mon Sep 17 00:00:00 2001 From: Barak Michener Date: Wed, 13 Jul 2022 12:13:23 -0700 Subject: [PATCH 1/2] feat: Expose getPiece as GetPiece Currently, there's no way to build a *chess.Piece from the constituent PieceType and Color... it's useful, as it exists in the package already, so it seems nice to make this helper function public. --- board.go | 2 +- engine.go | 10 +++++----- piece.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/board.go b/board.go index 209084f..a0e9fcb 100644 --- a/board.go +++ b/board.go @@ -233,7 +233,7 @@ func (b *Board) update(m *Move) { } // check promotion if m.promo != NoPieceType { - newPiece := getPiece(m.promo, p1.Color()) + newPiece := GetPiece(m.promo, p1.Color()) // remove pawn bbPawn := b.bbForPiece(p1) b.setBBForPiece(p1, bbPawn & ^s2BB) diff --git a/engine.go b/engine.go index 8271031..f96d6cc 100644 --- a/engine.go +++ b/engine.go @@ -133,25 +133,25 @@ func squaresAreAttacked(pos *Position, sqs ...Square) bool { continue } // check queen attack vector - queenBB := pos.board.bbForPiece(getPiece(Queen, otherColor)) + queenBB := pos.board.bbForPiece(GetPiece(Queen, otherColor)) bb := (diaAttack(occ, sq) | hvAttack(occ, sq)) & queenBB if bb != 0 { return true } // check rook attack vector - rookBB := pos.board.bbForPiece(getPiece(Rook, otherColor)) + rookBB := pos.board.bbForPiece(GetPiece(Rook, otherColor)) bb = hvAttack(occ, sq) & rookBB if bb != 0 { return true } // check bishop attack vector - bishopBB := pos.board.bbForPiece(getPiece(Bishop, otherColor)) + bishopBB := pos.board.bbForPiece(GetPiece(Bishop, otherColor)) bb = diaAttack(occ, sq) & bishopBB if bb != 0 { return true } // check knight attack vector - knightBB := pos.board.bbForPiece(getPiece(Knight, otherColor)) + knightBB := pos.board.bbForPiece(GetPiece(Knight, otherColor)) bb = bbKnightMoves[sq] & knightBB if bb != 0 { return true @@ -173,7 +173,7 @@ func squaresAreAttacked(pos *Position, sqs ...Square) bool { } } // check king attack vector - kingBB := pos.board.bbForPiece(getPiece(King, otherColor)) + kingBB := pos.board.bbForPiece(GetPiece(King, otherColor)) bb = bbKingMoves[sq] & kingBB if bb != 0 { return true diff --git a/piece.go b/piece.go index 975b67a..d79f987 100644 --- a/piece.go +++ b/piece.go @@ -128,7 +128,7 @@ var ( } ) -func getPiece(t PieceType, c Color) Piece { +func GetPiece(t PieceType, c Color) Piece { for _, p := range allPieces { if p.Color() == c && p.Type() == t { return p From bba806239b8b6a596345c306098f8f23b9326810 Mon Sep 17 00:00:00 2001 From: Logan Spears Date: Sun, 7 Aug 2022 19:36:38 -0700 Subject: [PATCH 2/2] renamed GetPiece to NewPiece and added comments --- board.go | 2 +- engine.go | 10 +++++----- piece.go | 4 +++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/board.go b/board.go index a0e9fcb..a61718d 100644 --- a/board.go +++ b/board.go @@ -233,7 +233,7 @@ func (b *Board) update(m *Move) { } // check promotion if m.promo != NoPieceType { - newPiece := GetPiece(m.promo, p1.Color()) + newPiece := NewPiece(m.promo, p1.Color()) // remove pawn bbPawn := b.bbForPiece(p1) b.setBBForPiece(p1, bbPawn & ^s2BB) diff --git a/engine.go b/engine.go index f96d6cc..ff49d10 100644 --- a/engine.go +++ b/engine.go @@ -133,25 +133,25 @@ func squaresAreAttacked(pos *Position, sqs ...Square) bool { continue } // check queen attack vector - queenBB := pos.board.bbForPiece(GetPiece(Queen, otherColor)) + queenBB := pos.board.bbForPiece(NewPiece(Queen, otherColor)) bb := (diaAttack(occ, sq) | hvAttack(occ, sq)) & queenBB if bb != 0 { return true } // check rook attack vector - rookBB := pos.board.bbForPiece(GetPiece(Rook, otherColor)) + rookBB := pos.board.bbForPiece(NewPiece(Rook, otherColor)) bb = hvAttack(occ, sq) & rookBB if bb != 0 { return true } // check bishop attack vector - bishopBB := pos.board.bbForPiece(GetPiece(Bishop, otherColor)) + bishopBB := pos.board.bbForPiece(NewPiece(Bishop, otherColor)) bb = diaAttack(occ, sq) & bishopBB if bb != 0 { return true } // check knight attack vector - knightBB := pos.board.bbForPiece(GetPiece(Knight, otherColor)) + knightBB := pos.board.bbForPiece(NewPiece(Knight, otherColor)) bb = bbKnightMoves[sq] & knightBB if bb != 0 { return true @@ -173,7 +173,7 @@ func squaresAreAttacked(pos *Position, sqs ...Square) bool { } } // check king attack vector - kingBB := pos.board.bbForPiece(GetPiece(King, otherColor)) + kingBB := pos.board.bbForPiece(NewPiece(King, otherColor)) bb = bbKingMoves[sq] & kingBB if bb != 0 { return true diff --git a/piece.go b/piece.go index d79f987..1063ba1 100644 --- a/piece.go +++ b/piece.go @@ -128,7 +128,9 @@ var ( } ) -func GetPiece(t PieceType, c Color) Piece { +// NewPiece returns the piece matching the PieceType and Color. +// NoPiece is returned if the PieceType or Color isn't valid. +func NewPiece(t PieceType, c Color) Piece { for _, p := range allPieces { if p.Color() == c && p.Type() == t { return p