1package lightsout23import (4 "encoding/json"5 "fmt"67 "github.com/FelineStateMachine/puzzletea/game"8)910type Save struct {11 Grid [][]bool `json:"grid"`12 InitialGrid [][]bool `json:"initial_grid,omitempty"`13 CursorX int `json:"cx"`14 CursorY int `json:"cy"`15 ModeTitle string `json:"mode_title"`16}1718func (m Model) GetSave() ([]byte, error) {19 save := Save{20 Grid: m.grid,21 InitialGrid: m.initialGrid,22 CursorX: m.cursor.X,23 CursorY: m.cursor.Y,24 ModeTitle: m.modeTitle,25 }26 jsonData, err := json.Marshal(save)27 if err != nil {28 return nil, fmt.Errorf("failed to marshal lightsout save: %w", err)29 }30 return jsonData, nil31}3233func ImportModel(data []byte) (*Model, error) {34 var s Save35 if err := json.Unmarshal(data, &s); err != nil {36 return nil, fmt.Errorf("failed to unmarshal lightsout save: %w", err)37 }3839 if len(s.Grid) == 0 {40 return nil, fmt.Errorf("empty grid in save")41 }4243 h := len(s.Grid)44 w := len(s.Grid[0])4546 // Fall back to a copy of the current grid for saves that predate47 // the initialGrid field.48 initial := s.InitialGrid49 if len(initial) == 0 {50 initial = make([][]bool, h)51 for y := range h {52 initial[y] = make([]bool, w)53 copy(initial[y], s.Grid[y])54 }55 }5657 return &Model{58 grid: s.Grid,59 initialGrid: initial,60 width: w,61 height: h,62 cursor: game.Cursor{X: s.CursorX, Y: s.CursorY},63 modeTitle: s.ModeTitle,64 keys: DefaultKeyMap,65 }, nil66}