mchess-server/chess/piece_interface.go
Marco af993876fd Introduce check if the game has ended (checkmate/stalemate)
Introduce checkmate check and send out 'gameEnded' message
2024-01-17 23:06:48 +01:00

67 lines
1.2 KiB
Go

package chess
import (
"mchess_server/types"
"strings"
)
type Piece interface {
GetAllNonBlockedSquares(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)
}