From f3a08ed23c21dd04c2c04d229c9a39183ca5c182 Mon Sep 17 00:00:00 2001 From: EugeneD Date: Thu, 21 Sep 2023 15:09:20 +0300 Subject: [PATCH 1/2] Added move with comments logic --- game.go | 12 ++++++++---- pgn.go | 3 +-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/game.go b/game.go index 67f7cd2..a4127c6 100644 --- a/game.go +++ b/game.go @@ -153,10 +153,7 @@ func NewGame(options ...func(*Game)) *Game { } return game } - -// 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 { +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 +162,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. 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 From 827b5cc3f8541ae8b4e829ee6bf0b675f3a18a51 Mon Sep 17 00:00:00 2001 From: EugeneD Date: Sun, 24 Sep 2023 20:39:26 +0300 Subject: [PATCH 2/2] Added move str with comments logic --- game.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/game.go b/game.go index a4127c6..e9617ce 100644 --- a/game.go +++ b/game.go @@ -153,6 +153,9 @@ func NewGame(options ...func(*Game)) *Game { } return game } + +// 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) MoveWithComments(m *Move, comments []string) error { valid := moveSlice(g.ValidMoves()).find(m) if valid == nil { @@ -180,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