-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
108 lines (95 loc) · 2.59 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
package main
import (
replacement "cycdg/graph_replacement"
"cycdg/lib/random"
"cycdg/lib/random/pcgrandom"
"cycdg/lib/tcell_console_wrapper"
"flag"
"fmt"
"os"
"time"
"github.com/gdamore/tcell/v2"
)
var (
cw tcell_console_wrapper.ConsoleWrapper
rnd random.PRNG
testResultString string
)
func main() {
defer func() { fmt.Printf(testResultString) }()
rnd = pcgrandom.NewPCG64()
rnd.SetSeed(int(time.Now().UnixNano()))
if execArgs() {
return
}
cw.Init()
defer cw.Close()
gen := CreateGraWithParamsMenu()
key := ""
for key != "ESCAPE" {
if key == "e" {
logName := exportRulesLog(gen)
cw.SetStyle(tcell.ColorYellow, tcell.ColorBlack)
cw.PutString("Exported the log to file "+logName, 0, 0)
cw.FlushScreen()
key = cw.ReadKey()
continue
}
if key == "" || gen.FilledEnough() {
gen.Reset()
} else {
gen.ApplyRandomReplacementRuleToTheGraph()
for key == "ENTER" && !gen.FilledEnough() {
gen.ApplyRandomReplacementRuleToTheGraph()
}
}
drawGraph(gen)
cw.FlushScreen()
key = cw.ReadKey()
}
}
// returns true if the program should exit
func execArgs() bool {
var width, height, fill int
benchOnly := false
fullBenchmark := false
var testMapsCount int
flag.IntVar(&width, "w", 5, "Graph width")
flag.IntVar(&height, "h", 5, "Graph height")
flag.IntVar(&fill, "fill", 70, "Fill percentage")
flag.BoolVar(&benchOnly, "b", false, "Run benchmark only")
flag.BoolVar(&fullBenchmark, "fullbench", false, "Run full benchmark for many sizes and fills; overrides benchOnly")
flag.IntVar(&testMapsCount, "total", 1000, "Generated maps count")
flag.Parse()
if testMapsCount > 0 {
if fullBenchmark {
benchOnly = true
minFill := fill
for fill := minFill; fill <= 100; fill += 5 {
for width := 4; width <= 8; width++ {
for height := 4; height <= width; height++ {
fmt.Printf("Benchmarking %dx%d map generation filled for %d%%\n", width, height, fill)
replacement.TestGen(rnd, width, height, testMapsCount, fill)
}
}
}
}
testResultString = replacement.TestGen(rnd, width, height, testMapsCount, fill)
}
return benchOnly
}
func exportRulesLog(gen *replacement.GraphReplacementApplier) string {
rlog := ""
for i := range gen.AppliedRules {
if i == 0 {
rlog += "Initial rule:\n"
} else {
rlog += fmt.Sprintf("Rule %d:\n", i+1)
}
rlog += fmt.Sprintf(" %s\n", gen.AppliedRules[i].StringifyRule())
rlog += fmt.Sprintf(" %s\n", gen.AppliedRules[i].StringifyCoords())
}
fileName := time.Now().Format("2006-01-02_15-04-05") + ".log"
os.WriteFile(fileName, []byte(rlog), 0644)
return fileName
}