-
Notifications
You must be signed in to change notification settings - Fork 0
/
alien.go
58 lines (47 loc) · 1.08 KB
/
alien.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"
// Alien stores properties like name. city and is alien destroyed
type Alien struct {
name string
city *City
destroyed bool
}
// NewAlien returns new alien instance
func NewAlien(name string, city *City) *Alien {
alien := &Alien{
name: name,
city: city,
destroyed: false,
}
alien.city.AddResident(alien)
return alien
}
// Move alien to neighbour city
func (a *Alien) Move() *City {
city, err := a.city.GetNeighbour()
if err != nil {
fmt.Printf("\nAlien %s is trapped in city %s!\n", a.name, a.city.name)
return a.city
}
oldCity := a.city
a.city = city
a.city.AddResident(a)
oldCity.DeleteResident(a)
return a.city
}
// Destroy alien
func (a *Alien) Destroy() {
a.destroyed = true
}
// IsDestroyed check if alien is destroyed or not
func (a *Alien) IsDestroyed() bool {
return a.destroyed
}
// String print alien information in string format
func (a *Alien) String() string {
state := "'Alive and is in city " + a.city.name
if a.destroyed {
state = "Dead!"
}
return fmt.Sprintf("Alien %s is %s", a.name, state)
}