From fb59c890ca93e2aa468b3b80c157a2b5b5eb0493 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Thu, 10 Aug 2023 19:13:24 +0200 Subject: [PATCH] Corrected some bugs for when it came to parsing and writing mazes --- io/writer/image.go | 19 +++++++++++++++---- io/writer/strings.go | 3 +++ maze/parser/parser.go | 12 ++++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/io/writer/image.go b/io/writer/image.go index 5731a83..e79f15f 100644 --- a/io/writer/image.go +++ b/io/writer/image.go @@ -56,17 +56,28 @@ func (w *ImageWriter) Write() error { width, height = w.CellWidth, w.CellHeight for i, from := range w.Maze.Solution[:len(w.Maze.Solution)-1] { to := w.Maze.Solution[i+1] + if from.Coords.X == to.Coords.X { + // Fill verticallly x0 = from.Coords.X * w.CellWidth - for y := from.Coords.Y; y < to.Coords.Y; y++ { - y0 = y * w.CellHeight - w.draw(x0, y0, width, height, colors[c]) - c++ + if from.Coords.Y < to.Coords.Y { + for y := from.Coords.Y; y < to.Coords.Y; y++ { + y0 = y * w.CellHeight + w.draw(x0, y0, width, height, colors[c]) + c++ + } + } else { + for y := from.Coords.Y; y > to.Coords.Y; y-- { + y0 = y * w.CellHeight + w.draw(x0, y0, width, height, colors[c]) + c++ + } } y0 = to.Coords.Y * w.CellHeight w.draw(x0, y0, width, height, colors[c]) } else { + // Fill horizontally y0 = from.Coords.Y * w.CellHeight if from.Coords.X < to.Coords.X { diff --git a/io/writer/strings.go b/io/writer/strings.go index cab5510..6929072 100644 --- a/io/writer/strings.go +++ b/io/writer/strings.go @@ -58,6 +58,9 @@ func (w *StringsWriter) fillHorizontally(from maze.Coordinates, to maze.Coordina func (w *StringsWriter) fillVertically(from maze.Coordinates, to maze.Coordinates, char byte) { x := from.X + if from.Y > to.Y { + from, to = to, from + } for y := from.Y; y <= to.Y; y++ { w.lines[y][x] = char } diff --git a/maze/parser/parser.go b/maze/parser/parser.go index 7ea83a1..3422303 100644 --- a/maze/parser/parser.go +++ b/maze/parser/parser.go @@ -85,7 +85,7 @@ func lookupNeighbourAbove(raw_maze *reader.RawMaze, node *maze.Node, nodesByCoor break } - if y > 0 && raw_maze.IsWall(node.Coords.X, y) { + if y >= 0 && raw_maze.IsWall(node.Coords.X, y) { y++ if y == node.Coords.Y { break @@ -107,6 +107,10 @@ func lookupNeighbourAbove(raw_maze *reader.RawMaze, node *maze.Node, nodesByCoor func lookupNeighbourLeft(raw_maze *reader.RawMaze, node *maze.Node, nodesByCoord *map[maze.Coordinates]*maze.Node) { for x := node.Coords.X - 1; x > 0; x-- { + if raw_maze.IsWall(x, node.Coords.Y) && x == node.Coords.X-1 { + return + } + if raw_maze.IsWall(x, node.Coords.Y) && x < node.Coords.X-1 { panic(fmt.Sprintf("Found no node before wall while looking to the left at neighbours of node %v (arrived at x=%v before hitting a wall)", node, x)) } @@ -122,7 +126,11 @@ func lookupNeighbourLeft(raw_maze *reader.RawMaze, node *maze.Node, nodesByCoord func lookupNeighbourRight(raw_maze *reader.RawMaze, node *maze.Node, nodesByCoord *map[maze.Coordinates]*maze.Node) { for x := node.Coords.X + 1; x < raw_maze.Width; x++ { - if raw_maze.IsWall(x, node.Coords.Y) { + if raw_maze.IsWall(x, node.Coords.Y) && x == node.Coords.X+1 { + return + } + + if raw_maze.IsWall(x, node.Coords.Y) && x > node.Coords.X+1 { panic(fmt.Sprintf("Found no node before wall while looking to the right at neighbours of node %v", node)) }