Promotions should work now.

This commit is contained in:
Marco 2023-07-03 19:32:39 +02:00
parent b052d8e21c
commit c8407881bf
5 changed files with 41 additions and 37 deletions

View File

@ -124,7 +124,7 @@ func Test_CheckMove_invalidPawnMoves(t *testing.T) {
board.position[types.Coordinate{Col: 2, Row: 6}] = Pawn{Color: types.White} board.position[types.Coordinate{Col: 2, Row: 6}] = Pawn{Color: types.White}
board.position[types.Coordinate{Col: 7, Row: 6}] = Rook{Color: types.Black} board.position[types.Coordinate{Col: 7, Row: 6}] = Rook{Color: types.Black}
board.position[types.Coordinate{Col: 8, Row: 8}] = King{Color: types.Black} board.position[types.Coordinate{Col: 8, Row: 8}] = King{Color: types.Black}
boardBeforeMove := board boardBeforeMove := board
move := types.Move{ move := types.Move{
StartSquare: types.Coordinate{Col: 2, Row: 6}, StartSquare: types.Coordinate{Col: 2, Row: 6},
@ -133,7 +133,7 @@ func Test_CheckMove_invalidPawnMoves(t *testing.T) {
good, _ := board.CheckAndPlay(move) good, _ := board.CheckAndPlay(move)
assert.False(t, good) assert.False(t, good)
assert.Equal(t, boardBeforeMove, board) assert.Equal(t, boardBeforeMove, board)
}) })
} }
@ -145,7 +145,7 @@ func Test_CheckMove_validPromotion(t *testing.T) {
board.position[types.Coordinate{Col: 7, Row: 7}] = King{Color: types.Black} board.position[types.Coordinate{Col: 7, Row: 7}] = King{Color: types.Black}
shortName := types.QueenShortName shortName := types.QueenShortName.String()
move := types.Move{ move := types.Move{
StartSquare: types.Coordinate{Col: 1, Row: 7}, StartSquare: types.Coordinate{Col: 1, Row: 7},
EndSquare: types.Coordinate{Col: 1, Row: 8}, EndSquare: types.Coordinate{Col: 1, Row: 8},
@ -192,19 +192,19 @@ func Test_CheckMove_HistoryWorks(t *testing.T) {
{ {
StartSquare: types.Coordinate{Col: 1, Row: 2}, StartSquare: types.Coordinate{Col: 1, Row: 2},
EndSquare: types.Coordinate{Col: 1, Row: 3}, EndSquare: types.Coordinate{Col: 1, Row: 3},
PieceMoved: 'p', PieceMoved: "p",
ColorMoved: "white", ColorMoved: "white",
}, },
{ {
StartSquare: types.Coordinate{Col: 3, Row: 7}, StartSquare: types.Coordinate{Col: 3, Row: 7},
EndSquare: types.Coordinate{Col: 3, Row: 5}, EndSquare: types.Coordinate{Col: 3, Row: 5},
PieceMoved: 'p', PieceMoved: "p",
ColorMoved: "black", ColorMoved: "black",
}, },
{ {
StartSquare: types.Coordinate{Col: 1, Row: 3}, StartSquare: types.Coordinate{Col: 1, Row: 3},
EndSquare: types.Coordinate{Col: 1, Row: 4}, EndSquare: types.Coordinate{Col: 1, Row: 4},
PieceMoved: 'p', PieceMoved: "p",
ColorMoved: "white", ColorMoved: "white",
}, },
} }

View File

@ -11,14 +11,14 @@ type Pawn struct {
} }
func (p Pawn) GetAllAttackedSquares(board Board, fromSquare types.Coordinate) []types.Coordinate { func (p Pawn) GetAllAttackedSquares(board Board, fromSquare types.Coordinate) []types.Coordinate {
attackingMoves := make([]types.Coordinate,0,2) attackingMoves := make([]types.Coordinate, 0, 2)
allMoves := p.GetAllNonBlockedMoves(board, fromSquare) allMoves := p.GetAllNonBlockedMoves(board, fromSquare)
for _,move := range allMoves { for _, move := range allMoves {
if move.Col != fromSquare.Col { if move.Col != fromSquare.Col {
attackingMoves = append(attackingMoves, move) attackingMoves = append(attackingMoves, move)
} }
} }
return attackingMoves return attackingMoves
} }
func (p Pawn) GetAllNonBlockedMoves(board Board, fromSquare types.Coordinate) []types.Coordinate { func (p Pawn) GetAllNonBlockedMoves(board Board, fromSquare types.Coordinate) []types.Coordinate {
@ -40,7 +40,7 @@ func (p *Pawn) HandlePossiblePromotion(b *Board, move types.Move) bool {
messageContainsPromotion := move.IsPromotionMove() messageContainsPromotion := move.IsPromotionMove()
if messageContainsPromotion { if messageContainsPromotion {
promotionToPiece = *move.PromotionToPiece promotionToPiece = types.PieceShortName(*move.PromotionToPiece)
} }
switch move.ColorMoved { switch move.ColorMoved {

View File

@ -6,7 +6,7 @@ import (
type Piece interface { type Piece interface {
GetAllNonBlockedMoves(board Board, fromSquare types.Coordinate) []types.Coordinate GetAllNonBlockedMoves(board Board, fromSquare types.Coordinate) []types.Coordinate
GetAllAttackedSquares(board Board, fromSquare types.Coordinate) []types.Coordinate GetAllAttackedSquares(board Board, fromSquare types.Coordinate) []types.Coordinate
GetColor() types.ChessColor GetColor() types.ChessColor
} }
@ -14,17 +14,17 @@ func GetPieceForShortName(name types.PieceShortName, color types.ChessColor) Pie
var piece Piece var piece Piece
switch name { switch name {
case 'p': case "p":
piece = Pawn{Color: color} piece = Pawn{Color: color}
case 'q': case "q":
piece = Queen{Color: color} piece = Queen{Color: color}
case 'k': case "k":
piece = King{Color: color} piece = King{Color: color}
case 'b': case "b":
piece = Bishop{Color: color} piece = Bishop{Color: color}
case 'r': case "r":
piece = Rook{Color: color} piece = Rook{Color: color}
case 'n': case "n":
piece = Knight{Color: color} piece = Knight{Color: color}
} }
return piece return piece
@ -34,17 +34,17 @@ func GetShortNameForPiece(piece Piece) types.PieceShortName {
var name types.PieceShortName var name types.PieceShortName
switch piece.(type) { switch piece.(type) {
case Pawn: case Pawn:
name = 'p' name = "p"
case Queen: case Queen:
name = 'q' name = "q"
case King: case King:
name = 'k' name = "k"
case Bishop: case Bishop:
name = 'b' name = "b"
case Rook: case Rook:
name = 'r' name = "r"
case Knight: case Knight:
name = 'n' name = "n"
} }
return name return name
} }

View File

@ -3,9 +3,9 @@ package types
type Move struct { type Move struct {
StartSquare Coordinate `json:"startSquare"` StartSquare Coordinate `json:"startSquare"`
EndSquare Coordinate `json:"endSquare"` EndSquare Coordinate `json:"endSquare"`
PieceMoved PieceShortName PromotionToPiece *string `json:"promotionToPiece,omitempty"`
ColorMoved ChessColor ColorMoved ChessColor
PromotionToPiece *PieceShortName `json:"promotionToPiece,omitempty"` PieceMoved PieceShortName
} }
func (m Move) IsPromotionMove() bool { func (m Move) IsPromotionMove() bool {

View File

@ -1,12 +1,16 @@
package types package types
type PieceShortName rune type PieceShortName string
const ( const (
PawnShortName PieceShortName = 'p' PawnShortName PieceShortName = "p"
RookShortName PieceShortName = 'r' RookShortName PieceShortName = "r"
KnightShortName PieceShortName = 'n' KnightShortName PieceShortName = "n"
BishopShortName PieceShortName = 'b' BishopShortName PieceShortName = "b"
QueenShortName PieceShortName = 'q' QueenShortName PieceShortName = "q"
KingShortName PieceShortName = 'k' KingShortName PieceShortName = "k"
) )
func (p PieceShortName) String() string {
return string(p)
}