Corrected some bugs for when it came to parsing

and writing mazes
This commit is contained in:
Karma Riuk 2023-08-10 19:13:24 +02:00
parent 3ef61967a5
commit 42cc4f8717
3 changed files with 28 additions and 6 deletions

View File

@ -56,17 +56,28 @@ func (w *ImageWriter) Write() error {
width, height = w.CellWidth, w.CellHeight width, height = w.CellWidth, w.CellHeight
for i, from := range w.Maze.Solution[:len(w.Maze.Solution)-1] { for i, from := range w.Maze.Solution[:len(w.Maze.Solution)-1] {
to := w.Maze.Solution[i+1] to := w.Maze.Solution[i+1]
if from.Coords.X == to.Coords.X { if from.Coords.X == to.Coords.X {
// Fill verticallly
x0 = from.Coords.X * w.CellWidth x0 = from.Coords.X * w.CellWidth
if from.Coords.Y < to.Coords.Y {
for y := from.Coords.Y; y < to.Coords.Y; y++ { for y := from.Coords.Y; y < to.Coords.Y; y++ {
y0 = y * w.CellHeight y0 = y * w.CellHeight
w.draw(x0, y0, width, height, colors[c]) w.draw(x0, y0, width, height, colors[c])
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 y0 = to.Coords.Y * w.CellHeight
w.draw(x0, y0, width, height, colors[c]) w.draw(x0, y0, width, height, colors[c])
} else { } else {
// Fill horizontally
y0 = from.Coords.Y * w.CellHeight y0 = from.Coords.Y * w.CellHeight
if from.Coords.X < to.Coords.X { if from.Coords.X < to.Coords.X {

View File

@ -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) { func (w *StringsWriter) fillVertically(from maze.Coordinates, to maze.Coordinates, char byte) {
x := from.X x := from.X
if from.Y > to.Y {
from, to = to, from
}
for y := from.Y; y <= to.Y; y++ { for y := from.Y; y <= to.Y; y++ {
w.lines[y][x] = char w.lines[y][x] = char
} }

View File

@ -85,7 +85,7 @@ func lookupNeighbourAbove(raw_maze *reader.RawMaze, node *maze.Node, nodesByCoor
break break
} }
if y > 0 && raw_maze.IsWall(node.Coords.X, y) { if y >= 0 && raw_maze.IsWall(node.Coords.X, y) {
y++ y++
if y == node.Coords.Y { if y == node.Coords.Y {
break 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) { func lookupNeighbourLeft(raw_maze *reader.RawMaze, node *maze.Node, nodesByCoord *map[maze.Coordinates]*maze.Node) {
for x := node.Coords.X - 1; x > 0; x-- { 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 { 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)) 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) { 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++ { 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)) panic(fmt.Sprintf("Found no node before wall while looking to the right at neighbours of node %v", node))
} }