-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathliving.go
More file actions
87 lines (72 loc) · 1.36 KB
/
living.go
File metadata and controls
87 lines (72 loc) · 1.36 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import "math/rand"
type LivingThing struct {
x int
y int
unitRange int
}
func NewLivingThing(unit int) *LivingThing {
return &LivingThing{rand.Intn(AREA_SIZE), rand.Intn(AREA_SIZE), unit}
}
func (l *LivingThing) Move() (int, int) {
// fmt.Printf("area.action: %v\n", area.action)
again:
// look after for intermetiate directions
randDirection := rand.Intn(4)
x, y := l.x, l.y
switch randDirection {
case 0: // up
y += l.unitRange
case 1: // right
x += l.unitRange
case 2: // down
y -= l.unitRange
case 3: // left
x -= l.unitRange
// case 4: // up-right
// x += l.unit
// y += l.unit
// case 5: // down-right
// x += l.unit
// y -= l.unit
// case 6: // down-left
// x -= l.unit
// y -= l.unit
// case 7: // up-left
// x -= l.unit
// y += l.unit
}
l.checkBoundaries(&x, &y)
if area.At(x, y) != nil {
goto again
}
l.x, l.y = x, y
return x, y
}
func (l *LivingThing) checkBoundaries(x, y *int) {
if *x < 0 {
*x = 0
} else if *x > AREA_SIZE-1 {
*x = AREA_SIZE - 1
}
if *y < 0 {
*y = 0
} else if *y > AREA_SIZE-1 {
*y = AREA_SIZE - 1
}
}
func (l *LivingThing) X() int {
return l.x
}
func (l *LivingThing) Y() int {
return l.y
}
func (l *LivingThing) SetX(x int) {
l.x = x
}
func (l *LivingThing) SetY(y int) {
l.y = y
}
func (l *LivingThing) Unit() int {
return l.unitRange
}