Skip to content

Commit

Permalink
fixes comments panic when using move history from Move()
Browse files Browse the repository at this point in the history
  • Loading branch information
notnil committed Nov 25, 2024
1 parent 5e6af1d commit a285eed
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (g *Game) Move(m *Move) error {
g.moves = append(g.moves, valid)
g.pos = g.pos.Update(valid)
g.positions = append(g.positions, g.pos)
g.comments = append(g.comments, []string{})
g.updatePosition()
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ func TestMoveHistory(t *testing.T) {
}
}

func TestMoveHistoryProgrammatic(t *testing.T) {
game := NewGame()
game.MoveStr("e4")
game.MoveStr("e5")
game.Resign(Black)
history := game.MoveHistory() // Panics here
if len(history) != 2 {
t.Fatal("Didn't retrieve full history")
}
}

func BenchmarkStalemateStatus(b *testing.B) {
fenStr := "k1K5/8/8/8/8/8/8/1Q6 w - - 0 1"
fen, err := FEN(fenStr)
Expand Down
1 change: 1 addition & 0 deletions pgn.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func decodePGN(pgn string) (*Game, error) {
if err := g.Move(m); err != nil {
return nil, fmt.Errorf("chess: pgn invalid move error %s on move %d", err.Error(), g.Position().moveCount)
}
g.comments = g.comments[:len(g.comments)-1]
g.comments = append(g.comments, move.Comments)
}
g.outcome = outcome
Expand Down

0 comments on commit a285eed

Please sign in to comment.