-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
69 lines (55 loc) · 1.02 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"math"
)
type Positioner interface {
Position() (int, int)
SetPosition(x, y int)
}
type Direction uint8
const (
directionUp Direction = iota
directionDown
directionLeft
directionRight
)
type GridCells [][]Cell
type Grid struct {
width int
height int
cells GridCells
}
func newGrid(width, height int) *Grid {
cells := make(GridCells, width)
for i := range cells {
cells[i] = make([]Cell, height)
}
grid := Grid{
width: width,
height: height,
cells: cells,
}
return &grid
}
func (g *Grid) Cell(x, y int) *Cell {
if (0 > y || 0 > x || x > len(g.cells)-1 || y > len(g.cells[x])-1) {
return nil
}
return &g.cells[x][y]
}
func (g *Grid) CenterPosition() (int, int) {
return int(math.Floor(float64(g.width / 2))), int(math.Floor(float64(g.height / 2)))
}
func (g *Grid) PositionAdjacent(x int, y int, d Direction) (int, int) {
switch d {
case directionUp:
y = y-1
case directionDown:
y = y+1
case directionLeft:
x = x-1
case directionRight:
x = x+1
}
return x, y
}