-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfish.go
More file actions
69 lines (61 loc) · 1.54 KB
/
Copy pathfish.go
File metadata and controls
69 lines (61 loc) · 1.54 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
package flyfishing
import (
"fmt"
"math"
"math/rand"
)
// This interface for a fish makes the lake struct super flexible.
type Fish interface {
getLocation() Location
lureWith(Fly, Location) bool
}
// Trout is kind of like a class. It can be used on anything that
// requires a Fish interface because it implements getLocation() and
// lureWith().
type Trout struct {
location Location
}
func (t Trout) getLocation() Location {
return t.location
}
func (t Trout) lureWith(fly Fly, loc Location) bool {
return t.noticesFly(loc) && t.isHungry() && t.likesFlyType(fly)
}
func (t Trout) noticesFly(loc Location) bool {
distance := math.Sqrt(math.Pow(loc.X-t.location.X, 2) +
math.Pow(loc.Y-t.location.Y, 2))
return rand.Float64() < 1/(1+math.Pow(distance, 2))
}
func (_ Trout) isHungry() bool {
return rand.Float64() > 0.5
}
func (_ Trout) likesFlyType(fly Fly) bool {
switch fly.(type) {
case Caddis:
return true
case WoollyBugger:
return true
}
return false
}
func (t Trout) String() string {
return fmt.Sprintf("%T at (%f, %f)", t, t.location.X, t.location.Y)
}
// Cutthroat is kind of like a subclass of Trout. It likes different
// flies than a normal trout.
type Cutthroat struct{ Trout }
func (_ Cutthroat) likesFlyType(fly Fly) bool {
switch fly.(type) {
case Caddis:
return true
case ParachuteAdams:
return true
}
return false
}
// Rainbow is kind of like a subclass of Trout. Notice how it is less
// hungry than a normal Trout.
type Rainbow struct{ Trout }
func (_ Rainbow) isHungry() bool {
return rand.Float64() > 0.3
}