can load and output a layout

This commit is contained in:
Barak Michener 2016-08-05 15:52:46 -07:00
parent 4946483fde
commit 3b1033ff1a
3 changed files with 108 additions and 0 deletions

11
cmd/main.go Normal file
View file

@ -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()
}

71
map.go Normal file
View file

@ -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))
}
}

26
test.txt Normal file
View file

@ -0,0 +1,26 @@
80 25
################################################################################
################################################################################
################################################################################
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
#####......................................................................#####
################################################################################
################################################################################
################################################################################