-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (58 loc) · 1.61 KB
/
main.go
File metadata and controls
71 lines (58 loc) · 1.61 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
70
71
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/darkfronza/alien-invasion/simulation"
"github.com/darkfronza/alien-invasion/worldmap"
)
var Version string
func main() {
// TODO: add daemon feature to test systemd on nix
if len(os.Args) == 2 && os.Args[1] == "daemon" {
fmt.Printf("daemon mode..... version=%s\n", Version)
time.Sleep(70 * time.Minute)
os.Exit(0)
}
if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "version") {
fmt.Printf("alien-invasion simulator %s\n", Version)
os.Exit(0)
}
if len(os.Args) < 3 {
fmt.Printf("Usage: %s <map-file> <n-aliens>\n", os.Args[0])
os.Exit(0)
}
nAliens, err := strconv.Atoi(os.Args[2])
if err != nil || nAliens <= 0 {
fmt.Printf("Invalid number:%d, <n-aliens> must be > 0\n", nAliens)
os.Exit(1)
}
// Open map file for reading
fp, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer fp.Close()
worldMap, err := worldmap.Load(fp)
if err != nil {
log.Fatal(err)
}
alienInvasionSimul, err := simulation.New(worldMap, nAliens)
if err != nil {
log.Fatal(err)
}
for alienInvasionSimul.Step() {
}
if !worldMap.IsDestroyed() {
fmt.Println("\nSome cities survived the alien attack, let's celebrate!")
// Print final world state
fmt.Println("\nWorld map after the end of alien invasion:")
fmt.Println("-----------------------------------------------------------------")
worldMap.Print()
fmt.Println("-----------------------------------------------------------------")
} else {
fmt.Println("\nUnfortunately, the world was totally destroyed by the aliens :(\n\nSee you in the heavens!")
}
}