package roba import ( "bufio" "bytes" "fmt" "io" "os" "strconv" "strings" ) type Tile byte const ( TileWall = '#' ) type Layout struct { Height int Width int layout [][]byte } type Map interface { DebugPrint() } func NewMap(path string) (Map, 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, " ") m.Width, err = strconv.Atoi(s[0]) if err != nil { return nil, err } m.Height, err = strconv.Atoi(s[1]) if err != nil { return nil, err } for j := 0; j < m.Height; j++ { b, err := br.ReadBytes('\n') if err == io.EOF { b = bytes.Repeat([]byte{TileWall}, m.Width) } if len(b) < m.Width { bytes.TrimRight(b, "\n") b = append(b, bytes.Repeat([]byte{TileWall}, m.Width-len(b))...) } if len(b) > m.Width { b = b[:m.Width] } m.layout = append(m.layout, b) } return m, nil } func (l *Layout) DebugPrint() { for _, x := range l.layout { fmt.Println(string(x)) } }