Skip to content
This repository has been archived by the owner on Jan 25, 2025. It is now read-only.

Added move with comments logic #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pgn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down