2023-06-20 21:53:54 +00:00
|
|
|
package chess
|
|
|
|
|
|
|
|
import (
|
2023-06-25 14:11:29 +00:00
|
|
|
"mchess_server/types"
|
2023-08-12 09:24:40 +00:00
|
|
|
"strings"
|
2023-06-20 21:53:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2023-06-20 21:53:54 +00:00
|
|
|
GetColor() types.ChessColor
|
2023-08-12 09:24:40 +00:00
|
|
|
AfterMoveAction(board *Board, fromSquare types.Coordinate)
|
2023-06-25 14:11:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 09:24:40 +00:00
|
|
|
func GetPieceForShortName(name types.PieceShortName) Piece {
|
2023-06-25 14:11:29 +00:00
|
|
|
var piece Piece
|
2023-08-12 09:24:40 +00:00
|
|
|
nameString := name.String()
|
2023-06-25 14:11:29 +00:00
|
|
|
|
2023-08-12 09:24:40 +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-08-12 09:24:40 +00:00
|
|
|
|
2023-06-25 14:11:29 +00:00
|
|
|
return piece
|
2023-06-20 21:53:54 +00:00
|
|
|
}
|
2023-06-25 22:51:20 +00:00
|
|
|
|
|
|
|
func GetShortNameForPiece(piece Piece) types.PieceShortName {
|
2023-08-12 09:24:40 +00:00
|
|
|
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
|
|
|
}
|
2023-08-12 09:24:40 +00:00
|
|
|
|
|
|
|
if piece.GetColor() == types.White {
|
|
|
|
name = strings.ToUpper(name)
|
|
|
|
}
|
|
|
|
return types.PieceShortName(name)
|
2023-06-25 22:51:20 +00:00
|
|
|
}
|