-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid.go
58 lines (44 loc) · 795 Bytes
/
grid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
)
type Grid struct {
height, width int
grid []byte
}
func main() {
w:=3
h:=3
g := NewGrid(w,h)
g.Draw()
}
func NewGrid(x, y int) Grid {
wth := 2*x + 2
hgt := 2*y + 1
g := make([]byte, wth*hgt)
for i := 0; i < hgt; i += 2 {
row0 := i * wth
row1 := (i + 1) * wth
for j := 0; j < wth-2; j += 2 {
g[row0+j], g[row0+j+1] = '+', '-'
if row1+j+1 <= wth*hgt {
g[row1+j], g[row1+j+1] = '|', ' '
}
}
g[row0+wth-2], g[row0+wth-1] = '+', '\n'
if row1+wth < wth*hgt {
g[row1+wth-2], g[row1+wth-1] = '|', '\n'
}
}
return Grid {
height: y,
width: x,
grid: g,
}
}
func (g Grid) Draw() {
fmt.Print("\x0c", g, "\n") // Print frame
}
func (g Grid) String() string{
return string(g.grid)
}