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 }