diff --git a/chess/board.go b/chess/board.go index 50c663e..d5427e7 100644 --- a/chess/board.go +++ b/chess/board.go @@ -56,10 +56,10 @@ func NewBoard() *Board { return board } -func NewGame(move string) (*Board, error) { +func NewGame(moves string) (*Board, error) { board := NewBoard() - if err := board.Move(move); err != nil { + if err := board.Parse(moves); err != nil { return nil, err } @@ -149,16 +149,20 @@ func (b *Board) AlgebraicNotation() string { text += fmt.Sprintf(" %s ", m) } } - return text + return fmt.Sprintf("`%s`", text) } func (b *Board) Parse(pgn string) error { var ( - moves = strings.Split(pgn, " ") + moves = strings.Split(strings.Trim(pgn, " "), " ") err error ) for _, move := range moves { + move = strings.Trim(move, " ") + if move == "" { + continue + } if err = b.Move(move); err != nil { return err } diff --git a/main.go b/main.go index 6e26a5e..7458ada 100644 --- a/main.go +++ b/main.go @@ -71,7 +71,7 @@ func tickGameStart(c *sn.Client) { continue } - text := fmt.Sprintf("`%s`\n\n%s", b.AlgebraicNotation(), imgUrl) + text := strings.Trim(fmt.Sprintf("%s\n\n%s", b.AlgebraicNotation(), imgUrl), " ") var cId int if cId, err = c.CreateComment(n.Item.Id, text); err != nil { log.Printf("error creating reply: %v\n", err) @@ -130,8 +130,9 @@ func tickGameProgress(c *sn.Client) { if item.User.Id == meId { continue } - move := strings.Trim(strings.ReplaceAll(item.Text, "@chess", ""), " ") - if err = b.Move(move); err != nil { + + moves := strings.Trim(strings.ReplaceAll(item.Text, "@chess", ""), " ") + if err = b.Parse(moves); err != nil { log.Printf("error moving piece: %v: id=%d\n", err, item.Id) break } @@ -142,7 +143,7 @@ func tickGameProgress(c *sn.Client) { continue } - if err = b.Move(move); err != nil { + if err = b.Parse(move); err != nil { log.Printf("error moving piece: %v: id=%d\n", err, n.Item.ParentId) continue }