-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldmap.go
152 lines (121 loc) · 3.13 KB
/
worldmap.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"strconv"
"strings"
)
// An WorldMap represents set of attributes that belongs to a map
type WorldMap struct {
cities map[string]*City
aliens map[uint]*Alien
directions map[string]Direction
}
// NewWorldMap returns instance of WorldMap
func NewWorldMap() *WorldMap {
return &WorldMap{
cities: make(map[string]*City),
aliens: make(map[uint]*Alien),
directions: InitDirections(),
}
}
// AddCities add cities to map and associates neighbours
func (w *WorldMap) AddCities(file string) error {
lines := strings.Split(file, "\n")
for _, line := range lines {
words := strings.Split(line, " ")
if len(words) > 1 {
err := w.parseAndAddCity(TrimSpace(words[0]), words[1:])
if err != nil {
return err
}
}
}
return nil
}
// AddAliens adds aliens to random cities in the map
func (w *WorldMap) AddAliens(n uint) error {
keys := make([]string, 0, len(w.cities))
for k := range w.cities {
keys = append(keys, k)
}
cities, err := Sample(keys, n)
if err != nil {
return err
}
for i, value := range cities {
w.aliens[uint(i)] = NewAlien(strconv.Itoa(i), w.cities[value])
}
return nil
}
// Simulate the attack of aliens on world map
func (w *WorldMap) Simulate(steps int) {
for iteration := 0; iteration < steps; iteration++ {
fmt.Printf("Iteration %v\n", iteration+1)
newCities := make(map[string]uint)
if aliensLeft := w.anyAliensLeft(); !aliensLeft {
fmt.Println("No Aliens Left!")
fmt.Println(w)
break
}
for counter := range w.aliens {
if !w.aliens[counter].IsDestroyed() {
oldCity := w.aliens[counter].city.name
newCity := w.aliens[counter].Move()
fmt.Printf("\nAlien %v moved from City %s to %s\n", counter, oldCity, newCity.name)
if _, ok := newCities[newCity.name]; ok {
newCities[newCity.name]++
} else {
newCities[newCity.name] = 1
}
}
}
for city := range newCities {
if newCities[city] > 1 {
w.cities[city].Destroy()
}
}
fmt.Println(w)
}
}
func (w *WorldMap) anyAliensLeft() bool {
for counter := range w.aliens {
if !w.aliens[counter].IsDestroyed() {
return true
}
}
return false
}
func (w *WorldMap) parseAndAddCity(cityName string, neighbours []string) error {
city := w.createCity(cityName)
for _, neighbourDirection := range neighbours {
pairs := strings.Split(neighbourDirection, "=")
directionName, neighbourName := TrimSpace(pairs[0]), TrimSpace(pairs[1])
neighbour := w.createCity(neighbourName)
direction, ok := w.directions[directionName]
if !ok {
return fmt.Errorf("Invalid direction %s", directionName)
}
err := city.SetNeighbour(direction, neighbour)
if err != nil {
return err
}
}
return nil
}
func (w *WorldMap) createCity(cityName string) *City {
if _, ok := w.cities[cityName]; !ok {
w.cities[cityName] = NewCity(cityName)
}
return w.cities[cityName]
}
func (w *WorldMap) String() string {
var cities []string
for _, city := range w.cities {
cities = append(cities, city.String())
}
var aliens []string
for _, alien := range w.aliens {
aliens = append(aliens, alien.String())
}
return strings.Join(append(cities, aliens...), "\n")
}