hash out state
This commit is contained in:
parent
3b1033ff1a
commit
006a3a5b72
4 changed files with 101 additions and 71 deletions
66
layout.go
Normal file
66
layout.go
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
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])
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue