From 46c42cb67de69d85b63e98ceb4f27c9782fc9c5f Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Fri, 11 Aug 2023 14:09:31 +0200 Subject: [PATCH] Refactored main.go to make the entry point clearer --- main.go | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 75eaa85..86e3a34 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,28 @@ import ( ) 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?)") var verboseLevel *int = argparser.FlagCounter("v", "verbose", &argparse.Options{ @@ -124,7 +146,7 @@ func main() { if err := argparser.Parse(os.Args); err != nil { fmt.Println(argparser.Usage(err)) - return + return nil, nil, nil, false } utils.VERBOSE_LEVEL = *verboseLevel @@ -137,18 +159,5 @@ func main() { writerFactory.PathColor = color.RGBA{255, 255, 255, 255} writerFactory.SolutionGradient = colorgrad.Warm() - 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") + return &readerFactory, &writerFactory, &solverFactory, true }