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, " ")
|
|
|
|
|
2022-12-21 22:12:19 +00:00
|
|
|
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
|
|
|
|
}
|