-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
174 lines (149 loc) · 2.93 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"math/rand"
"time"
)
type Weapon string
type Hero string
const (
baseballBat Weapon = "baseball bat"
hatchet Weapon = "hatchet"
scythe Weapon = "scythe"
machete Weapon = "machete"
)
const (
tex Hero = "Tex Willer"
kcarson Hero = "Kit Carson"
jack Hero = "Jack Tiger"
kwiller Hero = "Kit Willer"
)
const (
timeout = 5 * time.Second
)
var heroes = map[Weapon]Hero{
baseballBat: tex,
hatchet: kcarson,
scythe: jack,
machete: kwiller,
}
type Zombie struct {
name string
weapon Weapon
}
type ZombieChannel chan Zombie
type HeroChannel chan Zombie
func randomWeapon() Weapon {
switch rand.Intn(4) {
case 0:
return baseballBat
case 1:
return hatchet
case 2:
return scythe
case 3:
return machete
}
panic("Unexpected weapon index")
}
func makeZombie(id int, prefix string) Zombie {
return Zombie{
fmt.Sprintf("%s-%05d", prefix, id),
randomWeapon(),
}
}
func delay(baseMs int, additionalMs int) {
time.Sleep(time.Duration(baseMs+rand.Intn(additionalMs)) * time.Millisecond)
}
func spawnZombies(howMany int, prefix string, zombieCh ZombieChannel) {
for i := 1; i <= howMany; i++ {
delay(150, 1000)
zombieCh <- makeZombie(i, prefix)
}
close(zombieCh)
}
func fightZombie(ok bool, z Zombie, h Hero) {
if ok {
fmt.Printf("[FIGHT] %s confronts %s\n", h, z.name)
delay(250, 1500)
fmt.Printf(" [KILL] %s defeats %s\n", h, z.name)
}
}
func main() {
northCh := make(ZombieChannel)
southCh := make(ZombieChannel)
eastCh := make(ZombieChannel)
westCh := make(ZombieChannel)
texCh := make(HeroChannel, 5)
kcarsonCh := make(HeroChannel, 5)
jackCh := make(HeroChannel, 5)
kwillerCh := make(HeroChannel, 5)
terminationCh := make(chan bool)
var t0 = time.Now()
go spawnZombies(20, "N", northCh)
go spawnZombies(18, "S", southCh)
go spawnZombies(12, "E", eastCh)
go spawnZombies(22, "W", westCh)
go func() {
lastSpawn := time.Now()
for {
var z Zombie
var ok bool
select {
case z, ok = <-northCh:
case z, ok = <-southCh:
case z, ok = <-eastCh:
case z, ok = <-westCh:
}
if ok {
fmt.Printf("[SPAWN] %s with a %s\n", z.name, z.weapon)
lastSpawn = time.Now()
switch heroes[z.weapon] {
case tex:
texCh <- z
case kcarson:
kcarsonCh <- z
case jack:
jackCh <- z
case kwiller:
kwillerCh <- z
}
}
if time.Since(lastSpawn) > timeout {
close(kwillerCh)
close(jackCh)
close(kcarsonCh)
close(texCh)
terminationCh <- true
return
}
}
}()
go func() {
for {
z, ok := <-texCh
fightZombie(ok, z, tex)
}
}()
go func() {
for {
z, ok := <-kcarsonCh
fightZombie(ok, z, kcarson)
}
}()
go func() {
for {
z, ok := <-jackCh
fightZombie(ok, z, jack)
}
}()
go func() {
for {
z, ok := <-kwillerCh
fightZombie(ok, z, kwiller)
}
}()
<-terminationCh
tn := time.Since(t0)
fmt.Printf("Outbreak lasted for %dms\n", tn.Milliseconds())
}