mchess-server/chess/pgn.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

34 lines
666 B
Go

package chess
import (
"mchess_server/types"
"strings"
)
func (b *Board) pgn() string {
var pgn string
var shortName string
for row := types.RangeFirstValid; row <= types.RangeLastValid; row++ {
for col := types.RangeFirstValid; col <= types.RangeLastValid; col++ {
coord := types.Coordinate{Col: col, Row: row}
piece := b.getPieceAt(coord)
if piece != nil {
shortName = GetShortNameForPiece(piece).String()
if piece.GetColor() == types.White {
shortName = strings.ToUpper(shortName)
}
pgn = pgn + shortName
} else {
pgn = pgn + "-"
}
}
if row != types.RangeLastValid {
pgn = pgn + "/"
}
}
return pgn
}