package roba import ( "bufio" "bytes" "io" "os" "strconv" "strings" ) type Tile byte const ( TileWall = '#' ) type Layout struct { layout [][]byte } func NewLayout(path string) (*Layout, error) { m := &Layout{} f, err := os.Open(path) if err != nil { return nil, err } br := bufio.NewReader(f) buf, err := br.ReadString('\n') buf = strings.TrimRight(buf, "\n") if err != nil { return nil, err } s := strings.Split(buf, " ") width, err := strconv.Atoi(s[0]) if err != nil { return nil, err } height, err := strconv.Atoi(s[1]) if err != nil { return nil, err } for j := 0; j < height; j++ { b, err := br.ReadBytes('\n') if err == io.EOF { b = bytes.Repeat([]byte{TileWall}, width) } if len(b) < height { bytes.TrimRight(b, "\n") b = append(b, bytes.Repeat([]byte{TileWall}, width-len(b))...) } if len(b) > width { b = b[:width] } m.layout = append(m.layout, b) } return m, nil } func (l *Layout) Height() int { return len(l.layout) } func (l *Layout) Width() int { return len(l.layout[0]) }