mchess-server/chess/piece_interface.go
Marco ff2ec599fe Introduce PGN helpers
In order to simplify special moves like en passant or castling for the
client, we want to deliver the board state after every move (and not
only start square and end square).

With PGN we can encode a chess position into a string.
This commit implies changes to logic of the pieces' shortnames. This
will break the client/server connection (at least for promotions).
2023-08-12 11:24:40 +02:00

67 lines
1.2 KiB
Go

package chess
import (
"mchess_server/types"
"strings"
)
type Piece interface {
GetAllNonBlockedMoves(board Board, fromSquare types.Coordinate) []types.Coordinate
GetAllAttackedSquares(board Board, fromSquare types.Coordinate) []types.Coordinate
GetColor() types.ChessColor
AfterMoveAction(board *Board, fromSquare types.Coordinate)
}
func GetPieceForShortName(name types.PieceShortName) Piece {
var piece Piece
nameString := name.String()
var color = types.Black
if nameString < "Z" && nameString > "A" {
color = types.White
}
nameString = strings.ToLower(nameString)
switch nameString {
case "p":
piece = Pawn{Color: color}
case "q":
piece = Queen{Color: color}
case "k":
piece = King{Color: color}
case "b":
piece = Bishop{Color: color}
case "r":
piece = Rook{Color: color}
case "n":
piece = Knight{Color: color}
}
return piece
}
func GetShortNameForPiece(piece Piece) types.PieceShortName {
var name string
switch piece.(type) {
case Pawn:
name = "p"
case Queen:
name = "q"
case King:
name = "k"
case Bishop:
name = "b"
case Rook:
name = "r"
case Knight:
name = "n"
}
if piece.GetColor() == types.White {
name = strings.ToUpper(name)
}
return types.PieceShortName(name)
}