1package hashiwokakero23import (4 "encoding/json"5 "fmt"6)78type Save struct {9 Width int `json:"width"`10 Height int `json:"height"`11 Islands []Island `json:"islands"`12 Bridges []Bridge `json:"bridges"`13 ModeTitle string `json:"mode_title,omitempty"`14}1516func ImportModel(data []byte) (*Model, error) {17 var save Save18 if err := json.Unmarshal(data, &save); err != nil {19 return nil, err20 }2122 cursorIsland := 023 if len(save.Islands) > 0 {24 cursorIsland = save.Islands[0].ID25 }2627 return &Model{28 puzzle: Puzzle{29 Width: save.Width,30 Height: save.Height,31 Islands: save.Islands,32 Bridges: save.Bridges,33 },34 cursorIsland: cursorIsland,35 modeTitle: save.ModeTitle,36 keys: DefaultKeyMap,37 }, nil38}3940func (m Model) GetSave() ([]byte, error) {41 save := Save{42 Width: m.puzzle.Width,43 Height: m.puzzle.Height,44 Islands: m.puzzle.Islands,45 Bridges: m.puzzle.Bridges,46 ModeTitle: m.modeTitle,47 }48 jsonData, err := json.Marshal(save)49 if err != nil {50 return nil, fmt.Errorf("unable to marshal save data: %w", err)51 }52 return jsonData, nil53}