Moved RawMaze to reader package since it is used
mostly there
This commit is contained in:
parent
0e42c0f15d
commit
8b0fa4c1f9
@ -1,4 +1,4 @@
|
|||||||
package maze
|
package reader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
@ -1,21 +1,23 @@
|
|||||||
package maze
|
package reader
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestRawMazeWall(t *testing.T) {
|
func TestRawMazeWall(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
width, height int
|
width, height int
|
||||||
data [][]byte
|
pathChar, wallChar byte
|
||||||
expected [][]bool
|
data []string
|
||||||
|
expected [][]bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"Trivial",
|
"Trivial",
|
||||||
5, 3,
|
5, 3,
|
||||||
[][]byte{
|
' ', '#',
|
||||||
{0b_00100_000},
|
[]string{
|
||||||
{0b_01110_000},
|
"## ##",
|
||||||
{0b_00010_000},
|
"# #",
|
||||||
|
"### #",
|
||||||
},
|
},
|
||||||
[][]bool{
|
[][]bool{
|
||||||
{true, true, false, true, true},
|
{true, true, false, true, true},
|
||||||
@ -26,12 +28,13 @@ func TestRawMazeWall(t *testing.T) {
|
|||||||
{
|
{
|
||||||
"Trivial Bigger",
|
"Trivial Bigger",
|
||||||
7, 5,
|
7, 5,
|
||||||
[][]byte{
|
' ', '#',
|
||||||
{0b_0001000_0},
|
[]string{
|
||||||
{0b_0001000_0},
|
"### ###",
|
||||||
{0b_0111110_0},
|
"### ###",
|
||||||
{0b_0000010_0},
|
"# #",
|
||||||
{0b_0000010_0},
|
"##### #",
|
||||||
|
"##### #",
|
||||||
},
|
},
|
||||||
[][]bool{
|
[][]bool{
|
||||||
{true, true, true, false, true, true, true},
|
{true, true, true, false, true, true, true},
|
||||||
@ -44,12 +47,13 @@ func TestRawMazeWall(t *testing.T) {
|
|||||||
{
|
{
|
||||||
"Bigger Staggered",
|
"Bigger Staggered",
|
||||||
7, 5,
|
7, 5,
|
||||||
[][]byte{
|
' ', '#',
|
||||||
{0b_0001000_0},
|
[]string{
|
||||||
{0b_0001000_0},
|
"### ###",
|
||||||
{0b_0111110_0},
|
"### ###",
|
||||||
{0b_0000100_0},
|
"# #",
|
||||||
{0b_0000100_0},
|
"#### ##",
|
||||||
|
"#### ##",
|
||||||
},
|
},
|
||||||
[][]bool{
|
[][]bool{
|
||||||
{true, true, true, false, true, true, true},
|
{true, true, true, false, true, true, true},
|
||||||
@ -62,18 +66,19 @@ func TestRawMazeWall(t *testing.T) {
|
|||||||
{
|
{
|
||||||
"Normal",
|
"Normal",
|
||||||
11, 11,
|
11, 11,
|
||||||
[][]byte{
|
' ', '#',
|
||||||
{0b_00000100, 0b000_00000},
|
[]string{
|
||||||
{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},
|
"# # #",
|
||||||
|
"##### #####",
|
||||||
},
|
},
|
||||||
[][]bool{
|
[][]bool{
|
||||||
{true, true, true, true, true, false, true, true, true, true, true},
|
{true, true, true, true, true, false, true, true, true, true, true},
|
||||||
@ -92,11 +97,12 @@ func TestRawMazeWall(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
rawMaze := RawMaze{
|
reader := StringsReader{
|
||||||
Width: test.width,
|
PathChar: test.pathChar,
|
||||||
Height: test.height,
|
WallChar: test.wallChar,
|
||||||
Data: test.data,
|
Lines: &test.data,
|
||||||
}
|
}
|
||||||
|
rawMaze, _ := reader.Read()
|
||||||
for y, row := range test.expected {
|
for y, row := range test.expected {
|
||||||
for x, expected := range row {
|
for x, expected := range row {
|
||||||
if rawMaze.IsWall(x, y) != expected {
|
if rawMaze.IsWall(x, y) != expected {
|
@ -1,7 +1,5 @@
|
|||||||
package reader
|
package reader
|
||||||
|
|
||||||
import "maze-solver/maze"
|
|
||||||
|
|
||||||
type Reader interface {
|
type Reader interface {
|
||||||
Read() (*maze.RawMaze, error)
|
Read() (*RawMaze, error)
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package reader
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"maze-solver/maze"
|
|
||||||
"maze-solver/utils"
|
"maze-solver/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,16 +10,16 @@ type StringsReader struct {
|
|||||||
Lines *[]string
|
Lines *[]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *StringsReader) Read() (*maze.RawMaze, error) {
|
func (r *StringsReader) Read() (*RawMaze, error) {
|
||||||
width, height := len((*r.Lines)[0]), len(*r.Lines)
|
width, height := len((*r.Lines)[0]), len(*r.Lines)
|
||||||
ret := &maze.RawMaze{
|
ret := &RawMaze{
|
||||||
Width: width,
|
Width: width,
|
||||||
Height: height,
|
Height: height,
|
||||||
Data: make([][]byte, height),
|
Data: make([][]byte, height),
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < height; i++ {
|
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 {
|
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) {
|
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 {
|
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))
|
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++ {
|
for i := 0; i < n_chunks; i++ {
|
||||||
var chunk byte = 0 // all walls
|
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) {
|
if c == rune(r.PathChar) {
|
||||||
chunk |= 1 << (maze.CHUNK_SIZE - 1 - x)
|
chunk |= 1 << (CHUNK_SIZE - 1 - x)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@ package reader
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"maze-solver/maze"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -11,7 +10,7 @@ type TextReader struct {
|
|||||||
PathChar, WallChar byte
|
PathChar, WallChar byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r TextReader) Read() (*maze.RawMaze, error) {
|
func (r TextReader) Read() (*RawMaze, error) {
|
||||||
lines, err := getLines(r.Filename)
|
lines, err := getLines(r.Filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -64,7 +64,7 @@ func Parse(reader reader.Reader) (*maze.Maze, error) {
|
|||||||
return ret, nil
|
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-- {
|
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}]
|
||||||
|
|
||||||
@ -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-- {
|
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", 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++ {
|
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) {
|
||||||
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))
|
||||||
|
Loading…
Reference in New Issue
Block a user