-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
57 lines (49 loc) · 943 Bytes
/
node.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
package main
import "fmt"
type Node struct {
Row int
Col int
}
func (n *Node) String() string {
return fmt.Sprintf("{%d %d}", n.Row, n.Col)
}
var AllNodes map[int]map[int]*Node
func InitNode(s int) {
AllNodes = make(map[int]map[int]*Node, s)
for row := 0; row < s; row++ {
AllNodes[row] = make(map[int]*Node, s)
for col := 0; col < s; col++ {
AllNodes[row][col] = &Node{row, col}
}
}
}
// Nullable
func GetNode(r, c int) *Node {
ns, ok := AllNodes[r]
if !ok {
return nil
}
n, ok := ns[c]
if !ok {
return nil
}
return n
}
// Nullable <- GetNode
func (n *Node) Move(d Dir) *Node {
switch d {
case DIR_RIGHT:
return GetNode(n.Row, n.Col+1)
case DIR_LEFT:
return GetNode(n.Row, n.Col-1)
case DIR_UP:
return GetNode(n.Row-1, n.Col)
case DIR_DOWN:
return GetNode(n.Row+1, n.Col)
default:
panic(d)
}
}
func (n *Node) Within(s int) bool {
return 0 <= n.Row && n.Row < s && 0 <= n.Col && n.Col < s
}