-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (93 loc) · 2.36 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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"time"
termbox "github.com/nsf/termbox-go"
)
//
// MAIN
//
func main() {
// get random source (no need for cryptographic randomness)
randSource := rand.NewSource(time.Now().UnixNano())
rng := rand.New(randSource)
// parsing arguments
// Note that we assume that the map file is correct!
worldMap := flag.String("map", "", "the map file containing the cities")
numAliens := flag.Int("aliens", 1, "number of aliens to create")
genMap := flag.Int("genMap", 0, "optional argument to generate a world map")
slowFlag := flag.Bool("slow", false, "slow the game")
fullscreen := flag.Bool("fullscreen", false, "display the game on fullscreen")
printMapF := flag.Bool("printMap", false, "print the map contained in the map file")
if len(os.Args) == 1 {
fmt.Println("you need to fill in arguments")
flag.Usage()
return
}
flag.Parse()
if *fullscreen {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
}
slow = *slowFlag
// just want to generate a map ?
if numCities := *genMap; numCities > 0 {
_, resList := generateMap(numCities, rng)
printWorldForFile(resList, os.Stdout)
return
}
// need at least 1 alien
if *numAliens <= 0 {
fmt.Println("you need more aliens")
flag.Usage()
return
}
// parsing map file into a map[cityName]info
mapFile, err := os.Open(*worldMap)
if err != nil {
fmt.Fprintln(os.Stderr, "Couldn't open file")
os.Exit(1)
}
cities := parseMap(mapFile)
mapFile.Close()
if *numAliens > len(cities) {
fmt.Println("you can't have more than one aliens per city")
return
}
// create game
state := newGame(cities, *numAliens, rng)
// if we just want the map
if *printMapF {
coordMap, min_x, min_y, max_y, max_x, err := findWorldCoordinates(state.cities)
if err != nil {
panic(err)
}
if *fullscreen {
printMapGraphic(coordMap, min_x, min_y, max_y, max_x)
} else {
printMap(coordMap, min_x, min_y, max_y, max_x)
fmt.Println()
fmt.Println("press a key to exit _")
}
fmt.Scanf("%s")
return
}
// run the game
state.run(10000)
//
fmt.Println()
fmt.Println("----------- Fin. ---------------")
fmt.Println("----Current state of the map----")
fmt.Println()
// print out the current world
printWorldForFile(state.listCities, os.Stdout)
fmt.Println()
fmt.Println("press a key to exit _")
fmt.Scanf("%s")
}