From 7e86e29e8242b8f0d4b7f757b64487d0008dd8c5 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sat, 5 Aug 2023 16:36:49 +0200 Subject: [PATCH] Moved RawMaze to reader package since it is used mostly there --- {maze => io/reader}/raw_maze.go | 2 +- {maze => io/reader}/raw_maze_test.go | 80 +++++++++++++++------------- io/reader/reader.go | 4 +- io/reader/strings.go | 15 +++--- io/reader/text.go | 3 +- maze/parser/parser.go | 6 +-- 6 files changed, 56 insertions(+), 54 deletions(-) rename {maze => io/reader}/raw_maze.go (98%) rename {maze => io/reader}/raw_maze_test.go (85%) diff --git a/maze/raw_maze.go b/io/reader/raw_maze.go similarity index 98% rename from maze/raw_maze.go rename to io/reader/raw_maze.go index f4be5c2..e3a52de 100644 --- a/maze/raw_maze.go +++ b/io/reader/raw_maze.go @@ -1,4 +1,4 @@ -package maze +package reader import ( "strings" diff --git a/maze/raw_maze_test.go b/io/reader/raw_maze_test.go similarity index 85% rename from maze/raw_maze_test.go rename to io/reader/raw_maze_test.go index efb43c5..aa078fd 100644 --- a/maze/raw_maze_test.go +++ b/io/reader/raw_maze_test.go @@ -1,21 +1,23 @@ -package maze +package reader import "testing" func TestRawMazeWall(t *testing.T) { tests := []struct { - name string - width, height int - data [][]byte - expected [][]bool + name string + width, height int + pathChar, wallChar byte + data []string + expected [][]bool }{ { "Trivial", 5, 3, - [][]byte{ - {0b_00100_000}, - {0b_01110_000}, - {0b_00010_000}, + ' ', '#', + []string{ + "## ##", + "# #", + "### #", }, [][]bool{ {true, true, false, true, true}, @@ -26,12 +28,13 @@ func TestRawMazeWall(t *testing.T) { { "Trivial Bigger", 7, 5, - [][]byte{ - {0b_0001000_0}, - {0b_0001000_0}, - {0b_0111110_0}, - {0b_0000010_0}, - {0b_0000010_0}, + ' ', '#', + []string{ + "### ###", + "### ###", + "# #", + "##### #", + "##### #", }, [][]bool{ {true, true, true, false, true, true, true}, @@ -44,12 +47,13 @@ func TestRawMazeWall(t *testing.T) { { "Bigger Staggered", 7, 5, - [][]byte{ - {0b_0001000_0}, - {0b_0001000_0}, - {0b_0111110_0}, - {0b_0000100_0}, - {0b_0000100_0}, + ' ', '#', + []string{ + "### ###", + "### ###", + "# #", + "#### ##", + "#### ##", }, [][]bool{ {true, true, true, false, true, true, true}, @@ -62,18 +66,19 @@ func TestRawMazeWall(t *testing.T) { { "Normal", 11, 11, - [][]byte{ - {0b_00000100, 0b000_00000}, - {0b_01111101, 0b110_00000}, - {0b_00000100, 0b010_00000}, - {0b_01110111, 0b110_00000}, - {0b_01010000, 0b010_00000}, - {0b_01011111, 0b110_00000}, - {0b_00010001, 0b010_00000}, - {0b_01110111, 0b010_00000}, - {0b_01000000, 0b010_00000}, - {0b_01111101, 0b110_00000}, - {0b_00000100, 0b000_00000}, + ' ', '#', + []string{ + "##### #####", + "# # #", + "##### ### #", + "# # #", + "# # ##### #", + "# # #", + "### ### # #", + "# # # #", + "# ####### #", + "# # #", + "##### #####", }, [][]bool{ {true, true, true, true, true, false, true, true, true, true, true}, @@ -92,11 +97,12 @@ func TestRawMazeWall(t *testing.T) { } for _, test := range tests { - rawMaze := RawMaze{ - Width: test.width, - Height: test.height, - Data: test.data, + reader := StringsReader{ + PathChar: test.pathChar, + WallChar: test.wallChar, + Lines: &test.data, } + rawMaze, _ := reader.Read() for y, row := range test.expected { for x, expected := range row { if rawMaze.IsWall(x, y) != expected { diff --git a/io/reader/reader.go b/io/reader/reader.go index 304d842..e42e293 100644 --- a/io/reader/reader.go +++ b/io/reader/reader.go @@ -1,7 +1,5 @@ package reader -import "maze-solver/maze" - type Reader interface { - Read() (*maze.RawMaze, error) + Read() (*RawMaze, error) } diff --git a/io/reader/strings.go b/io/reader/strings.go index e026490..d6b2fed 100644 --- a/io/reader/strings.go +++ b/io/reader/strings.go @@ -2,7 +2,6 @@ package reader import ( "fmt" - "maze-solver/maze" "maze-solver/utils" ) @@ -11,16 +10,16 @@ type StringsReader struct { Lines *[]string } -func (r *StringsReader) Read() (*maze.RawMaze, error) { +func (r *StringsReader) Read() (*RawMaze, error) { width, height := len((*r.Lines)[0]), len(*r.Lines) - ret := &maze.RawMaze{ + ret := &RawMaze{ Width: width, Height: height, Data: make([][]byte, height), } for i := 0; i < height; i++ { - ret.Data[i] = make([]byte, width/maze.CHUNK_SIZE+1) + ret.Data[i] = make([]byte, width/CHUNK_SIZE+1) } for y, line := range *r.Lines { @@ -31,7 +30,7 @@ func (r *StringsReader) Read() (*maze.RawMaze, error) { } func (r *StringsReader) processLine(line string, dest *[]byte) { - n_chunks := len(line)/maze.CHUNK_SIZE + 1 + n_chunks := len(line)/CHUNK_SIZE + 1 if len(*dest) != n_chunks { panic(fmt.Sprintf("The row that should receive the chunks does not have the correct length (%v, want %v)", len(*dest), n_chunks)) @@ -40,11 +39,11 @@ func (r *StringsReader) processLine(line string, dest *[]byte) { for i := 0; i < n_chunks; i++ { var chunk byte = 0 // all walls - end_index := utils.Min((i+1)*maze.CHUNK_SIZE, len(line)) + end_index := utils.Min((i+1)*CHUNK_SIZE, len(line)) - for x, c := range line[i*maze.CHUNK_SIZE : end_index] { + for x, c := range line[i*CHUNK_SIZE : end_index] { if c == rune(r.PathChar) { - chunk |= 1 << (maze.CHUNK_SIZE - 1 - x) + chunk |= 1 << (CHUNK_SIZE - 1 - x) } } diff --git a/io/reader/text.go b/io/reader/text.go index 084cc8d..6907088 100644 --- a/io/reader/text.go +++ b/io/reader/text.go @@ -2,7 +2,6 @@ package reader import ( "bufio" - "maze-solver/maze" "os" ) @@ -11,7 +10,7 @@ type TextReader struct { PathChar, WallChar byte } -func (r TextReader) Read() (*maze.RawMaze, error) { +func (r TextReader) Read() (*RawMaze, error) { lines, err := getLines(r.Filename) if err != nil { return nil, err diff --git a/maze/parser/parser.go b/maze/parser/parser.go index 33cb81f..5da9046 100644 --- a/maze/parser/parser.go +++ b/maze/parser/parser.go @@ -64,7 +64,7 @@ func Parse(reader reader.Reader) (*maze.Maze, error) { return ret, nil } -func lookupNeighbourAbove(raw_maze *maze.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-- { neighbour, ok := (*nodesByCoord)[maze.Coordinates{X: node.Coords.X, Y: y}] @@ -94,7 +94,7 @@ func lookupNeighbourAbove(raw_maze *maze.RawMaze, node *maze.Node, nodesByCoord } } -func lookupNeighbourLeft(raw_maze *maze.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-- { 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)) @@ -109,7 +109,7 @@ func lookupNeighbourLeft(raw_maze *maze.RawMaze, node *maze.Node, nodesByCoord * } } -func lookupNeighbourRight(raw_maze *maze.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++ { if raw_maze.IsWall(x, node.Coords.Y) { panic(fmt.Sprintf("Found no node before wall while looking to the right at neighbours of node %v", node))