libchess/pkg/pgn/game.go

45 lines
845 B
Go
Raw Normal View History

2020-05-08 20:55:25 +00:00
package pgn
import (
"fmt"
"code.c-base.org/gochess/libchess/pkg/board"
"code.c-base.org/gochess/libchess/pkg/game"
"code.c-base.org/gochess/libchess/pkg/pgn/move"
)
// Game represents a PGN game.
type Game struct {
Tags []game.Tag
Moves []string
}
func newGame() *Game {
return &Game{
Tags: []game.Tag{},
Moves: []string{},
}
}
// Game returns a *game.Game representation of this PGN game.
func (g *Game) Game() (*game.Game, error) {
// parse all moves
moves := make([]*board.Move, len(g.Moves))
for i, _m := range g.Moves {
m, err := move.NewParser(_m).Move()
if err != nil {
return nil, err
}
moves[i] = m
}
// return initialized *game.Game
return &game.Game{
Tags: g.Tags,
Moves: moves,
}, nil
}
func (g Game) String() string {
return fmt.Sprintf("<Game(Tags: %q, Moves: %q)>", g.Tags, g.Moves)
}