Moved RawMaze to reader package since it is used

mostly there
This commit is contained in:
Karma Riuk 2023-08-05 16:36:49 +02:00
parent fa4c13812d
commit 7e86e29e82
6 changed files with 56 additions and 54 deletions

View File

@ -1,4 +1,4 @@
package maze
package reader
import (
"strings"

View File

@ -1,4 +1,4 @@
package maze
package reader
import "testing"
@ -6,16 +6,18 @@ func TestRawMazeWall(t *testing.T) {
tests := []struct {
name string
width, height int
data [][]byte
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 {

View File

@ -1,7 +1,5 @@
package reader
import "maze-solver/maze"
type Reader interface {
Read() (*maze.RawMaze, error)
Read() (*RawMaze, error)
}

View File

@ -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)
}
}

View File

@ -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

View File

@ -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))