diff --git a/game.go b/game.go index 67f7cd2..e9617ce 100644 --- a/game.go +++ b/game.go @@ -154,9 +154,9 @@ func NewGame(options ...func(*Game)) *Game { return game } -// Move updates the game with the given move. An error is returned +// MoveWithComments updates the game with the given move and set comments. An error is returned // if the move is invalid or the game has already been completed. -func (g *Game) Move(m *Move) error { +func (g *Game) MoveWithComments(m *Move, comments []string) error { valid := moveSlice(g.ValidMoves()).find(m) if valid == nil { return fmt.Errorf("chess: invalid move %s", m) @@ -165,9 +165,16 @@ func (g *Game) Move(m *Move) error { g.pos = g.pos.Update(valid) g.positions = append(g.positions, g.pos) g.updatePosition() + g.comments = append(g.comments, comments) return nil } +// Move updates the game with the given move. An error is returned +// if the move is invalid or the game has already been completed. +func (g *Game) Move(m *Move) error { + return g.MoveWithComments(m, nil) +} + // MoveStr decodes the given string in game's notation // and calls the Move function. An error is returned if // the move can't be decoded or the move is invalid. @@ -176,7 +183,18 @@ func (g *Game) MoveStr(s string) error { if err != nil { return err } - return g.Move(m) + return g.MoveWithComments(m, nil) +} + +// MoveStrWithComments decodes the given string in game's notation +// and calls the Move function. An error is returned if +// the move can't be decoded or the move is invalid. +func (g *Game) MoveStrWithComments(s string, comments []string) error { + m, err := g.notation.Decode(g.pos, s) + if err != nil { + return err + } + return g.MoveWithComments(m, comments) } // ValidMoves returns a list of valid moves in the diff --git a/pgn.go b/pgn.go index 37b76af..c0d3d26 100644 --- a/pgn.go +++ b/pgn.go @@ -171,10 +171,9 @@ func decodePGN(pgn string) (*Game, error) { if err != nil { return nil, fmt.Errorf("chess: pgn decode error %s on move %d", err.Error(), g.Position().moveCount) } - if err := g.Move(m); err != nil { + if err := g.MoveWithComments(m, move.Comments); err != nil { return nil, fmt.Errorf("chess: pgn invalid move error %s on move %d", err.Error(), g.Position().moveCount) } - g.comments = append(g.comments, move.Comments) } g.outcome = outcome return g, nil