From b6dff509f9341c0acb2f722b844cd7380a702b5d Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Mon, 7 Aug 2023 17:43:35 +0200 Subject: [PATCH] Fixed parser --- maze/parser/parser.go | 18 ++++++++++++------ maze/parser/parser_test.go | 30 +++++++++++++++--------------- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/maze/parser/parser.go b/maze/parser/parser.go index 5da9046..b0cd915 100644 --- a/maze/parser/parser.go +++ b/maze/parser/parser.go @@ -30,10 +30,7 @@ func Parse(reader reader.Reader) (*maze.Maze, error) { for y = 1; y < raw_maze.Height-1; y++ { for x := 1; x < raw_maze.Width-1; x++ { // Parse middle of the maze - if raw_maze.IsPath(x, y) && - (raw_maze.IsWall(x-1, y) && raw_maze.IsPath(x+1, y) || - raw_maze.IsPath(x-1, y) && raw_maze.IsWall(x+1, y) || - raw_maze.IsPath(x, y-1) && (raw_maze.IsPath(x-1, y) || raw_maze.IsPath(x+1, y))) { + if isCoordEligibleForNode(x, y, raw_maze) { coords := maze.Coordinates{X: x, Y: y} node := maze.NewNode(coords) @@ -43,7 +40,8 @@ func Parse(reader reader.Reader) (*maze.Maze, error) { nodesByCoord[coords] = node if raw_maze.IsPath(x-1, y) && raw_maze.IsWall(x+1, y) || - raw_maze.IsPath(x, y-1) && (raw_maze.IsPath(x-1, y) || raw_maze.IsPath(x+1, y)) { + raw_maze.IsPath(x, y-1) && + (raw_maze.IsPath(x-1, y) || raw_maze.IsPath(x+1, y)) { lookupNeighbourLeft(raw_maze, node, &nodesByCoord) } } @@ -64,6 +62,14 @@ func Parse(reader reader.Reader) (*maze.Maze, error) { return ret, nil } +func isCoordEligibleForNode(x int, y int, raw_maze *reader.RawMaze) bool { + return raw_maze.IsPath(x, y) && + (raw_maze.IsWall(x-1, y) && raw_maze.IsPath(x+1, y) || // wall left, path right + raw_maze.IsPath(x-1, y) && raw_maze.IsWall(x+1, y) || // path left, wall right + raw_maze.IsPath(x, y-1) && (raw_maze.IsPath(x-1, y) || raw_maze.IsPath(x+1, y)) || // path above and not in vertical corridor + raw_maze.IsWall(x-1, y) && raw_maze.IsWall(x+1, y) && raw_maze.IsPath(x, y-1) && raw_maze.IsWall(x, y+1)) // wall to left, below, above and path above +} + func lookupNeighbourAbove(raw_maze *reader.RawMaze, node *maze.Node, nodesByCoord *map[maze.Coordinates]*maze.Node, m *maze.Maze) { for y := node.Coords.Y - 1; y >= 0; y-- { neighbour, ok := (*nodesByCoord)[maze.Coordinates{X: node.Coords.X, Y: y}] @@ -97,7 +103,7 @@ 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 { - panic(fmt.Sprintf("Found no node before wall while looking to the left at neighbours of node %v", node)) + 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)) } neighbour, ok := (*nodesByCoord)[maze.Coordinates{X: x, Y: node.Coords.Y}] diff --git a/maze/parser/parser_test.go b/maze/parser/parser_test.go index 024d3c1..61560c9 100644 --- a/maze/parser/parser_test.go +++ b/maze/parser/parser_test.go @@ -215,9 +215,9 @@ func TestTextReadNormal(t *testing.T) { ##### ### # #5 6#7 8# # # ##### # - #9#A G B# + #9#A F B# ### ### # # - #C D#E F# # + #C D#E G# # # ####### # #H I#J K# #####L##### @@ -241,13 +241,13 @@ func TestTextReadNormal(t *testing.T) { {1, 5}, // 9 {3, 5}, // A (10) - {7, 5}, // G (16) {9, 5}, // B (11) {1, 7}, // C (12) {3, 7}, // D (13) {5, 7}, // E (14) - {7, 7}, // F (15) + {7, 5}, // F (15) + {7, 7}, // G (16) {1, 9}, // H (17) {5, 9}, // I (18) @@ -290,27 +290,27 @@ func TestTextReadNormal(t *testing.T) { nodes[9].Up = nodes[5] nodes[10].Up = nodes[6] - nodes[10].Right = nodes[16] + nodes[10].Right = nodes[15] nodes[10].Down = nodes[13] - nodes[11].Up = nodes[6] - nodes[11].Right = nodes[16] + nodes[11].Up = nodes[8] + nodes[11].Left = nodes[15] nodes[11].Down = nodes[20] nodes[12].Right = nodes[13] nodes[12].Down = nodes[17] nodes[13].Up = nodes[10] - nodes[13].Left = nodes[16] + nodes[13].Left = nodes[12] - nodes[14].Right = nodes[15] + nodes[14].Right = nodes[16] - nodes[15].Up = nodes[16] - nodes[15].Left = nodes[14] + nodes[15].Left = nodes[10] + nodes[15].Right = nodes[11] + nodes[15].Down = nodes[16] - nodes[16].Left = nodes[10] - nodes[16].Right = nodes[11] - nodes[16].Down = nodes[15] + nodes[16].Up = nodes[15] + nodes[16].Left = nodes[14] nodes[17].Up = nodes[12] nodes[17].Right = nodes[18] @@ -323,7 +323,7 @@ func TestTextReadNormal(t *testing.T) { nodes[20].Up = nodes[11] nodes[20].Left = nodes[19] - nodes[21].Up = nodes[20] + nodes[21].Up = nodes[18] reader := reader.TextReader{ Filename: "../../assets/normal.txt",