forked from DyegoCosta/snake-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (63 loc) · 1.79 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
package main
import (
"flag"
"fmt"
"os"
"github.com/imega/snake-game/ai"
"github.com/imega/snake-game/snake"
"github.com/imega/snake-game/state"
)
func main() {
p := state.Parameters{}
flag.IntVar(&p.Speed, "s", 100, "snake speed limit")
flag.IntVar(&p.MaxInstance, "i", 1000, "max number of instances in epoch")
flag.Float64Var(&p.MutationRate, "r", 0.1, "mutation rate on the weights of synapses")
flag.Float64Var(&p.MutationRange, "n", 0.5, "interval of the mutation changes on the synapse weight")
flag.IntVar(&p.MaxSnakeSteps, "t", 200, "max snake stept without eat")
flag.IntVar(&p.MinScoreEpoch, "m", 0, "min score in epoch")
flag.StringVar(&p.PrefixFilename, "p", "", "prefix filename with brain")
flag.BoolVar(&p.Silent, "q", false, "start in silent mode")
flag.BoolVar(&p.Human, "h", false, "start in human mode")
flag.BoolVar(&p.CreateBrain, "c", false, "create empty brain")
flag.Parse()
if p.CreateBrain {
if err := ai.CreateBrain(p); err != nil {
fmt.Printf("failed to create brain, %s\n", err)
usage()
os.Exit(1)
}
prefix := ""
if p.PrefixFilename != "" {
prefix = p.PrefixFilename + "-"
}
fmt.Printf("brain created: %sbrain-0.json\n", prefix)
os.Exit(0)
}
ch := make(chan state.SnakeGame)
statCh := make(chan state.Stat)
if !p.Human {
args := flag.Args()
if len(args) == 0 {
fmt.Printf("empty args filename\n")
usage()
os.Exit(1)
}
p.BrainFilename = args[0]
go func() {
if err := ai.New(p, ch, snake.KeyboardEventsChan, statCh); err != nil {
fmt.Printf("failed to start, %s\n", err)
usage()
os.Exit(1)
}
}()
}
snake.NewGame().Start(p, ch, statCh)
}
func usage() {
fmt.Fprintf(
flag.CommandLine.Output(),
"\nUsage: %s [-chiqmnprst] [<prefix>brain-<score>.json]\n",
os.Args[0],
)
flag.PrintDefaults()
}