mchess-server/chess/piece_interface.go

67 lines
1.2 KiB
Go
Raw Normal View History

package chess
import (
2023-06-25 14:11:29 +00:00
"mchess_server/types"
"strings"
)
type Piece interface {
2023-06-27 20:32:24 +00:00
GetAllNonBlockedMoves(board Board, fromSquare types.Coordinate) []types.Coordinate
2023-07-03 17:32:39 +00:00
GetAllAttackedSquares(board Board, fromSquare types.Coordinate) []types.Coordinate
GetColor() types.ChessColor
AfterMoveAction(board *Board, fromSquare types.Coordinate)
2023-06-25 14:11:29 +00:00
}
func GetPieceForShortName(name types.PieceShortName) Piece {
2023-06-25 14:11:29 +00:00
var piece Piece
nameString := name.String()
2023-06-25 14:11:29 +00:00
var color = types.Black
if nameString < "Z" && nameString > "A" {
color = types.White
}
nameString = strings.ToLower(nameString)
switch nameString {
2023-07-03 17:32:39 +00:00
case "p":
2023-06-25 22:51:20 +00:00
piece = Pawn{Color: color}
2023-07-03 17:32:39 +00:00
case "q":
2023-06-25 22:51:20 +00:00
piece = Queen{Color: color}
2023-07-03 17:32:39 +00:00
case "k":
2023-06-25 22:51:20 +00:00
piece = King{Color: color}
2023-07-03 17:32:39 +00:00
case "b":
2023-06-25 22:51:20 +00:00
piece = Bishop{Color: color}
2023-07-03 17:32:39 +00:00
case "r":
2023-06-25 22:51:20 +00:00
piece = Rook{Color: color}
2023-07-03 17:32:39 +00:00
case "n":
2023-06-25 22:51:20 +00:00
piece = Knight{Color: color}
2023-06-25 14:11:29 +00:00
}
2023-06-25 14:11:29 +00:00
return piece
}
2023-06-25 22:51:20 +00:00
func GetShortNameForPiece(piece Piece) types.PieceShortName {
var name string
2023-06-25 22:51:20 +00:00
switch piece.(type) {
case Pawn:
2023-07-03 17:32:39 +00:00
name = "p"
2023-06-25 22:51:20 +00:00
case Queen:
2023-07-03 17:32:39 +00:00
name = "q"
2023-06-25 22:51:20 +00:00
case King:
2023-07-03 17:32:39 +00:00
name = "k"
2023-06-25 22:51:20 +00:00
case Bishop:
2023-07-03 17:32:39 +00:00
name = "b"
2023-06-25 22:51:20 +00:00
case Rook:
2023-07-03 17:32:39 +00:00
name = "r"
2023-06-25 22:51:20 +00:00
case Knight:
2023-07-03 17:32:39 +00:00
name = "n"
2023-06-25 22:51:20 +00:00
}
if piece.GetColor() == types.White {
name = strings.ToUpper(name)
}
return types.PieceShortName(name)
2023-06-25 22:51:20 +00:00
}