Skip to content

Commit

Permalink
fix: fix getPawnMove
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Guimaraes <[email protected]>
  • Loading branch information
gabrielg2020 committed Oct 25, 2024
1 parent 34b4b69 commit a745afe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions api/mocks/move_service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type MockMoveService struct {
mock.Mock
}

func (m *MockMoveService) FindBestMove(chessboard entity.ChessboardEntityInterface) ([]entity.MoveEntityInterface, error) {
func (m *MockMoveService) FindBestMove(chessboard entity.ChessboardEntityInterface) (entity.MoveEntityInterface, error) {
args := m.Called(chessboard)
return args.Get(0).([]entity.MoveEntityInterface), args.Error(1)
return args.Get(0).(entity.MoveEntityInterface), args.Error(1)
}
25 changes: 18 additions & 7 deletions api/service/move_service/move_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type MoveServiceInterface interface {
FindBestMove(chessboard entity.ChessboardEntityInterface) ([]entity.MoveEntityInterface, error)
FindBestMove(chessboard entity.ChessboardEntityInterface) (entity.MoveEntityInterface, error)
}

type MoveService struct{}
Expand All @@ -18,12 +18,12 @@ func NewMoveService() *MoveService {
return &MoveService{}
}

func (service *MoveService) FindBestMove(chessboard entity.ChessboardEntityInterface) ([]entity.MoveEntityInterface, error) {
// 1. Find Pseudo Legal Moves
func (service *MoveService) FindBestMove(chessboard entity.ChessboardEntityInterface) (entity.MoveEntityInterface, error) {
// 1. Find Pseudo Legal Moves // TODO move Pseudo Legal Moves into separate function
// a. Create a moves array
var moves []entity.MoveEntityInterface
board, err := chessboard.GetBoard()
if err == nil {
if err != nil {
return nil, errors.New("failed to retrieve chessboard")
}
// b. Loop through the board
Expand All @@ -35,7 +35,7 @@ func (service *MoveService) FindBestMove(chessboard entity.ChessboardEntityInter
}
switch math.Abs(float64(piece)) {
case 1: // Get Pawn Move
pawnMoves, err := getPawnMove(piece, row, col, chessboard)
pawnMoves, err := getPawnMove(piece, col, row, chessboard)
if err != nil {
return nil, errors.New("failed to get pawn moves")
}
Expand All @@ -54,12 +54,12 @@ func (service *MoveService) FindBestMove(chessboard entity.ChessboardEntityInter
}
}
// c. For each piece, find all moves
// 2. Filter for Legal Moves
// 2. Filter for Legal Moves // TODO moveFilter for Legal Moves into separate function
// a. Remove any move that goes off the board
// b. Remove any move that place king in check
// 3. Return Legal Moves
// a. Return moves array
return moves, nil
return moves[0], nil
}

// TODO needs to be tested ... :(
Expand All @@ -80,6 +80,10 @@ func getPawnMove (piece int, fromX int, fromY int, chessboard entity.ChessboardE

// 1 move forward
toX, toY := fromX, fromY + direction
if !chessboard.IsWithinBounds(toX, toY) {
return nil, nil // Shouldn't error if out of bounds
}

isSquareEmpty, err := chessboard.IsSquareEmpty(toX, toY)

if err != nil {
Expand Down Expand Up @@ -112,6 +116,10 @@ func getPawnMove (piece int, fromX int, fromY int, chessboard entity.ChessboardE

if fromY == startRank {
toX, toY := fromX, fromY + (direction * 2)
if !chessboard.IsWithinBounds(toX, toY) {
return nil, nil // Shouldn't error if out of bounds
}

isSquareEmpty, err := chessboard.IsSquareEmpty(toX, toY)

if err != nil {
Expand All @@ -134,6 +142,9 @@ func getPawnMove (piece int, fromX int, fromY int, chessboard entity.ChessboardE

for _, deltaX := range []int{-1, 1} {
toX, toY = fromX + deltaX, fromY + direction
if !chessboard.IsWithinBounds(toX, toY) {
return nil, nil // Shouldn't error if out of bounds
}

IsOpponent, err := chessboard.IsOpponent(piece, toX, toY)

Expand Down
1 change: 1 addition & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gin-gonic/gin"
)

// FEAT Add a way to propagate error messages. Maybe add a logger like logrus?
func main() {
engine := setUpEngine()

Expand Down

0 comments on commit a745afe

Please sign in to comment.