mchess-server/server/chess_move.go

55 lines
1.2 KiB
Go
Raw Normal View History

2022-11-19 12:25:00 +00:00
package server
import (
"errors"
"fmt"
"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
}
func moveToString(move chessMove) string {
str := fmt.Sprint(move.startSquare.col) + fmt.Sprint(move.startSquare.row) +
" " + fmt.Sprint(move.endSquare.col) + fmt.Sprint(move.endSquare.row)
return str
}