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: 7, Row: 6}] = Rook{Color: types.Black}
board.position[types.Coordinate{Col: 8, Row: 8}] = King{Color: types.Black}
boardBeforeMove := board
boardBeforeMove := board
move := types.Move{
StartSquare: types.Coordinate{Col: 2, Row: 6},
@ -133,7 +133,7 @@ func Test_CheckMove_invalidPawnMoves(t *testing.T) {
good, _ := board.CheckAndPlay(move)
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}
shortName := types.QueenShortName
shortName := types.QueenShortName.String()
move := types.Move{
StartSquare: types.Coordinate{Col: 1, Row: 7},
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},
EndSquare: types.Coordinate{Col: 1, Row: 3},
PieceMoved: 'p',
PieceMoved: "p",
ColorMoved: "white",
},
{
StartSquare: types.Coordinate{Col: 3, Row: 7},
EndSquare: types.Coordinate{Col: 3, Row: 5},
PieceMoved: 'p',
PieceMoved: "p",
ColorMoved: "black",
},
{
StartSquare: types.Coordinate{Col: 1, Row: 3},
EndSquare: types.Coordinate{Col: 1, Row: 4},
PieceMoved: 'p',
PieceMoved: "p",
ColorMoved: "white",
},
}

View File

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

View File

@ -6,7 +6,7 @@ import (
type Piece interface {
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
}
@ -14,17 +14,17 @@ func GetPieceForShortName(name types.PieceShortName, color types.ChessColor) Pie
var piece Piece
switch name {
case 'p':
case "p":
piece = Pawn{Color: color}
case 'q':
case "q":
piece = Queen{Color: color}
case 'k':
case "k":
piece = King{Color: color}
case 'b':
case "b":
piece = Bishop{Color: color}
case 'r':
case "r":
piece = Rook{Color: color}
case 'n':
case "n":
piece = Knight{Color: color}
}
return piece
@ -34,17 +34,17 @@ func GetShortNameForPiece(piece Piece) types.PieceShortName {
var name types.PieceShortName
switch piece.(type) {
case Pawn:
name = 'p'
name = "p"
case Queen:
name = 'q'
name = "q"
case King:
name = 'k'
name = "k"
case Bishop:
name = 'b'
name = "b"
case Rook:
name = 'r'
name = "r"
case Knight:
name = 'n'
name = "n"
}
return name
}

View File

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

View File

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