Skip to content

Commit 433e43a

Browse files
committed
fix(difficulty): #5
1 parent 664df06 commit 433e43a

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

Generators/randomised_kruskal.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,26 @@ func NewRandomisedKruskal(b *Maze) error {
3737
if m.Difficulty > 0 {
3838
size := len(m.Walls)
3939
for n := 0; n < int(math.Floor(float64(size/4)*(float64(m.Difficulty)*0.05))); n++ {
40-
id := uint(utils.RandMax(uint(size - 1)))
41-
i, j := m.GenIJFromIDOfWall(id)
42-
if i == 0 || i == m.Width-1 || j == m.Height-1 {
43-
continue
40+
valid := false
41+
var wall Wall
42+
for !valid {
43+
id := uint(utils.RandMax(uint(size - 1)))
44+
i, j := m.GenIJFromIDOfWall(id)
45+
if i == 0 || i == m.Width-1 || j == m.Height-1 {
46+
continue
47+
}
48+
wall = m.Walls[id]
49+
if wall.IsVertical {
50+
if !(wall.CellsNear[0].Disabled || wall.CellsNear[1].Disabled) {
51+
valid = true
52+
}
53+
} else {
54+
if !wall.CellsNear[0].Disabled {
55+
valid = true
56+
}
57+
}
4458
}
45-
m.Walls[id].IsPresent = false
59+
wall.IsPresent = false
4660
}
4761
}
4862
println("Merging done!")
@@ -114,5 +128,5 @@ func (m *kruskal) mergeRandomly() error {
114128
//
115129
// Return true if the maze is finished, false otherwise
116130
func (m *kruskal) isFinished() bool {
117-
return uint(len(*m.Cells[0].MergedRef.MergedCell)) == m.Width*m.Height - m.Inner*m.Inner
131+
return uint(len(*m.Cells[0].MergedRef.MergedCell)) == m.Width*m.Height-m.Inner*m.Inner
118132
}

0 commit comments

Comments
 (0)