mchess-server/server/chess_move.go

48 lines
1.0 KiB
Go
Raw Normal View History

2022-11-19 12:25:00 +00:00
package server
import (
"errors"
"strings"
)
type chessMove struct {
realMove bool
startSquare chessCoordinate
endSquare chessCoordinate
}
type chessCoordinate struct {
col int
row int
}
func parseMove(received string) (*chessMove, error) {
var move chessMove
splitReceived := strings.Split(received, " ")
if len(splitReceived) != 3 {
return nil, errors.New("invalid move command")
}
realMoveString := splitReceived[0]
startSquareString := splitReceived[1]
endSquareString := splitReceived[2]
2022-11-19 12:25:00 +00:00
if strings.Compare(realMoveString, "mv") == 0 {
move.realMove = true
} else if strings.Compare(realMoveString, "pc") == 0 {
move.realMove = false
} else {
return nil, errors.New("NEITHER MV OR PC RECEIVED AS FIRST BYTES")
}
move.startSquare.col = int(([]rune(startSquareString)[0] - '0'))
move.startSquare.row = int(([]rune(startSquareString)[1] - '0'))
move.endSquare.col = int(([]rune(endSquareString)[0] - '0'))
move.endSquare.row = int(([]rune(endSquareString)[1] - '0'))
return &move, nil
}