mchess-server/server/chess_move.go

46 lines
988 B
Go
Raw Normal View History

2022-11-19 12:25:00 +00:00
package server
import (
"errors"
"log"
"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, " ")
log.Println(splitReceived)
realMoveString := splitReceived[1]
startSquareString := splitReceived[2]
endSquareString := splitReceived[3]
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
}