-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (59 loc) · 1.42 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
package main
import (
"log"
"math/rand"
"net/http"
"time"
)
func init() {
rand.Seed(time.Now().Unix()) // initialize global pseudo random generator
}
func main() {
http.HandleFunc("/", simulateDog)
log.Println(http.ListenAndServe(":8000", nil))
}
func simulateDog(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("<html>"))
// make a new dog
dog := NewDog("Ace", res)
res.Write([]byte("Bruce Wayne goes to the pet store and comes home with a dog named " + dog.Name + "<br>\n"))
// begin simulating dog
for {
// let the dog die when its too old
dog.Birthday()
if dog.Age > 22 {
break
}
// if 100 hungry, the dog has to eat
if dog.Hunger >= 100 {
res.Write([]byte(dog.Name + " is incredibly hungry and stops to eat.<br>\n"))
dog.Eat()
continue
}
// if 100 tired, the dog has to sleep
if dog.Tiredness >= 100 {
res.Write([]byte(dog.Name + " is incredibly tired and stops to sleep.<br>\n"))
dog.Sleep()
continue
}
// pick a random thing to do and do it
choiceNumber := rand.Intn(7) // 7 being the number of things the dog can possibly do
switch choiceNumber + 1 {
case 1:
dog.Bark()
case 2:
dog.Run()
case 3:
dog.Eat()
case 4:
dog.Sleep()
case 5:
dog.Play()
case 6:
dog.ChaseBird()
case 7:
dog.StopsCrime()
}
}
res.Write([]byte(dog.Name + " had a great life, he came, he saw, he conquered, now he is dead and Bruce is sad."))
}