Skip to content

Commit

Permalink
refactor(maze): merge horizontal and vertical walls into one slice
Browse files Browse the repository at this point in the history
  • Loading branch information
anhgelus committed May 31, 2023
1 parent b4e59f6 commit 4c19d36
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
49 changes: 20 additions & 29 deletions Generators/global.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
package Generators

type Maze struct {
VerticalWalls []Wall
HorizontalWalls []Wall
Width uint
Height uint
Walls []Wall
Width uint
Height uint
}

type Wall struct {
IsVertical bool
IsPresent bool
ID int
IsRemovable bool
IsVertical bool
ID int
}

// GenerateNewMaze generate a new maze with the given information.
//
// w is the width, h is the height and algo is the algorithm used
//
// Return an error if a problem occurs and nil if there are no errors
func GenerateNewMaze(w uint, h uint, algo func(*Maze) error) error {
func GenerateNewMaze(w uint, h uint, algo func(*Maze) error) (Maze, error) {
maze := Maze{Height: h, Width: w}
return algo(&maze)
}

// CalcID calculate the ID of the Wall
//
// l is the length of the current ID (the width of the maze for a horizontal wall).
// i is the column number.
// j is the row number.
//
// Return the ID
func CalcID(l uint, i uint, j uint) int {
return int(l*(j-1) + i)
err := algo(&maze)
return maze, err
}

// generateWalls generate the default walls
func generateWalls(a uint, b uint, isVertical bool) []Wall {
walls := make([]Wall, a)
for i := uint(0); i < a; i++ {
for j := uint(0); j < b; j++ {
id := CalcID(a, i, j)
removable := !(i == 0 || i == a-1)
walls = append(walls, Wall{ID: id, IsVertical: isVertical, IsPresent: true, IsRemovable: removable})
}
func (m *Maze) generateWalls() []Wall {
xV := (m.Width - 1) * m.Height
xH := m.Width * m.Height
x := xH + xV
walls := make([]Wall, x)
for i := uint(0); i < x; i++ {
walls[i] = Wall{IsVertical: i >= xH, ID: int(i)}
}
m.Walls = walls
return walls
}

func (m *Maze) RenderWalls() {

}
7 changes: 1 addition & 6 deletions Generators/randomised_kruskal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ type kruskal struct {
// NewRandomisedKruskal is a func generating a new maze with the Randomized Kruskal's Algorithm
func NewRandomisedKruskal(b *Maze) error {
m := kruskal{b}
m.firstWalls()
m.generateWalls()
return nil
}

func (m *kruskal) firstWalls() {
m.HorizontalWalls = generateWalls(m.Width, m.Height, false)
m.VerticalWalls = generateWalls(m.Height, m.Width, true)
}

0 comments on commit 4c19d36

Please sign in to comment.