Fixed parser
This commit is contained in:
parent
3f8f0a2e92
commit
19f83899be
@ -30,10 +30,7 @@ func Parse(reader reader.Reader) (*maze.Maze, error) {
|
|||||||
for y = 1; y < raw_maze.Height-1; y++ {
|
for y = 1; y < raw_maze.Height-1; y++ {
|
||||||
for x := 1; x < raw_maze.Width-1; x++ {
|
for x := 1; x < raw_maze.Width-1; x++ {
|
||||||
// Parse middle of the maze
|
// Parse middle of the maze
|
||||||
if raw_maze.IsPath(x, y) &&
|
if isCoordEligibleForNode(x, y, raw_maze) {
|
||||||
(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))) {
|
|
||||||
coords := maze.Coordinates{X: x, Y: y}
|
coords := maze.Coordinates{X: x, Y: y}
|
||||||
node := maze.NewNode(coords)
|
node := maze.NewNode(coords)
|
||||||
|
|
||||||
@ -43,7 +40,8 @@ func Parse(reader reader.Reader) (*maze.Maze, error) {
|
|||||||
nodesByCoord[coords] = node
|
nodesByCoord[coords] = node
|
||||||
|
|
||||||
if raw_maze.IsPath(x-1, y) && raw_maze.IsWall(x+1, y) ||
|
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)
|
lookupNeighbourLeft(raw_maze, node, &nodesByCoord)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,6 +62,14 @@ func Parse(reader reader.Reader) (*maze.Maze, error) {
|
|||||||
return ret, nil
|
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) {
|
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-- {
|
for y := node.Coords.Y - 1; y >= 0; y-- {
|
||||||
neighbour, ok := (*nodesByCoord)[maze.Coordinates{X: node.Coords.X, Y: 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) {
|
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 {
|
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}]
|
neighbour, ok := (*nodesByCoord)[maze.Coordinates{X: x, Y: node.Coords.Y}]
|
||||||
|
@ -215,9 +215,9 @@ func TestTextReadNormal(t *testing.T) {
|
|||||||
##### ### #
|
##### ### #
|
||||||
#5 6#7 8#
|
#5 6#7 8#
|
||||||
# # ##### #
|
# # ##### #
|
||||||
#9#A G B#
|
#9#A F B#
|
||||||
### ### # #
|
### ### # #
|
||||||
#C D#E F# #
|
#C D#E G# #
|
||||||
# ####### #
|
# ####### #
|
||||||
#H I#J K#
|
#H I#J K#
|
||||||
#####L#####
|
#####L#####
|
||||||
@ -241,13 +241,13 @@ func TestTextReadNormal(t *testing.T) {
|
|||||||
|
|
||||||
{1, 5}, // 9
|
{1, 5}, // 9
|
||||||
{3, 5}, // A (10)
|
{3, 5}, // A (10)
|
||||||
{7, 5}, // G (16)
|
|
||||||
{9, 5}, // B (11)
|
{9, 5}, // B (11)
|
||||||
|
|
||||||
{1, 7}, // C (12)
|
{1, 7}, // C (12)
|
||||||
{3, 7}, // D (13)
|
{3, 7}, // D (13)
|
||||||
{5, 7}, // E (14)
|
{5, 7}, // E (14)
|
||||||
{7, 7}, // F (15)
|
{7, 5}, // F (15)
|
||||||
|
{7, 7}, // G (16)
|
||||||
|
|
||||||
{1, 9}, // H (17)
|
{1, 9}, // H (17)
|
||||||
{5, 9}, // I (18)
|
{5, 9}, // I (18)
|
||||||
@ -290,27 +290,27 @@ func TestTextReadNormal(t *testing.T) {
|
|||||||
nodes[9].Up = nodes[5]
|
nodes[9].Up = nodes[5]
|
||||||
|
|
||||||
nodes[10].Up = nodes[6]
|
nodes[10].Up = nodes[6]
|
||||||
nodes[10].Right = nodes[16]
|
nodes[10].Right = nodes[15]
|
||||||
nodes[10].Down = nodes[13]
|
nodes[10].Down = nodes[13]
|
||||||
|
|
||||||
nodes[11].Up = nodes[6]
|
nodes[11].Up = nodes[8]
|
||||||
nodes[11].Right = nodes[16]
|
nodes[11].Left = nodes[15]
|
||||||
nodes[11].Down = nodes[20]
|
nodes[11].Down = nodes[20]
|
||||||
|
|
||||||
nodes[12].Right = nodes[13]
|
nodes[12].Right = nodes[13]
|
||||||
nodes[12].Down = nodes[17]
|
nodes[12].Down = nodes[17]
|
||||||
|
|
||||||
nodes[13].Up = nodes[10]
|
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[10]
|
||||||
nodes[15].Left = nodes[14]
|
nodes[15].Right = nodes[11]
|
||||||
|
nodes[15].Down = nodes[16]
|
||||||
|
|
||||||
nodes[16].Left = nodes[10]
|
nodes[16].Up = nodes[15]
|
||||||
nodes[16].Right = nodes[11]
|
nodes[16].Left = nodes[14]
|
||||||
nodes[16].Down = nodes[15]
|
|
||||||
|
|
||||||
nodes[17].Up = nodes[12]
|
nodes[17].Up = nodes[12]
|
||||||
nodes[17].Right = nodes[18]
|
nodes[17].Right = nodes[18]
|
||||||
@ -323,7 +323,7 @@ func TestTextReadNormal(t *testing.T) {
|
|||||||
nodes[20].Up = nodes[11]
|
nodes[20].Up = nodes[11]
|
||||||
nodes[20].Left = nodes[19]
|
nodes[20].Left = nodes[19]
|
||||||
|
|
||||||
nodes[21].Up = nodes[20]
|
nodes[21].Up = nodes[18]
|
||||||
|
|
||||||
reader := reader.TextReader{
|
reader := reader.TextReader{
|
||||||
Filename: "../../assets/normal.txt",
|
Filename: "../../assets/normal.txt",
|
||||||
|
Loading…
Reference in New Issue
Block a user