Refactored main.go to make the entry point clearer

This commit is contained in:
Karma Riuk 2023-08-11 14:09:31 +02:00
parent 6fd16b99e9
commit afceafaf49

39
main.go
View File

@ -17,6 +17,28 @@ import (
) )
func main() { func main() {
readerFactory, writerFactory, solverFactory, ok := parse_arguments()
if !ok {
return
}
defer utils.Timer("TOTAL", 1)()
reader := readerFactory.Get()
maze, err := parser.Parse(reader)
utils.Check(err, "Couldn't read maze")
solver := solverFactory.Get()
solved := solver.Solve(maze)
writer := writerFactory.Get(solved)
err = writer.Write()
utils.Check(err, "Couldn't write solved maze")
}
func parse_arguments() (*reader.ReaderFactory, *writer.WriterFactory, *solver.SolverFactory, bool) {
argparser := argparse.NewParser("maze-solver", "Solves the given maze (insane, right? who would've guessed?)") argparser := argparse.NewParser("maze-solver", "Solves the given maze (insane, right? who would've guessed?)")
var verboseLevel *int = argparser.FlagCounter("v", "verbose", &argparse.Options{ var verboseLevel *int = argparser.FlagCounter("v", "verbose", &argparse.Options{
@ -124,7 +146,7 @@ func main() {
if err := argparser.Parse(os.Args); err != nil { if err := argparser.Parse(os.Args); err != nil {
fmt.Println(argparser.Usage(err)) fmt.Println(argparser.Usage(err))
return return nil, nil, nil, false
} }
utils.VERBOSE_LEVEL = *verboseLevel utils.VERBOSE_LEVEL = *verboseLevel
@ -137,18 +159,5 @@ func main() {
writerFactory.PathColor = color.RGBA{255, 255, 255, 255} writerFactory.PathColor = color.RGBA{255, 255, 255, 255}
writerFactory.SolutionGradient = colorgrad.Warm() writerFactory.SolutionGradient = colorgrad.Warm()
defer utils.Timer("TOTAL", 1)() return &readerFactory, &writerFactory, &solverFactory, true
reader := readerFactory.Get()
maze, err := parser.Parse(reader)
utils.Check(err, "Couldn't read maze")
solver := solverFactory.Get()
solved := solver.Solve(maze)
writer := writerFactory.Get(solved)
err = writer.Write()
utils.Check(err, "Couldn't write solved maze")
} }