diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..9a84a9c --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,11 @@ +package main + +import "github.com/barakmich/roba" + +func main() { + m, err := roba.NewMap("test.txt") + if err != nil { + panic(err) + } + m.DebugPrint() +} diff --git a/map.go b/map.go new file mode 100644 index 0000000..26de9ed --- /dev/null +++ b/map.go @@ -0,0 +1,71 @@ +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)) + } +} diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..02631d0 --- /dev/null +++ b/test.txt @@ -0,0 +1,26 @@ +80 25 +################################################################################ +################################################################################ +################################################################################ +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +#####......................................................................##### +################################################################################ +################################################################################ +################################################################################