Skip to content

Commit

Permalink
feat(inner hole): handle adding hole
Browse files Browse the repository at this point in the history
  • Loading branch information
anhgelus committed Jun 23, 2023
1 parent 9e73634 commit 5cbdcc7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
21 changes: 21 additions & 0 deletions Generators/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Cell struct {
MergedCell *[]*Cell
// MergedRef refers to the parent cell containing the full MergedCell
MergedRef *Cell
Disabled bool
}

type Scheme struct {
Expand Down Expand Up @@ -117,9 +118,23 @@ func (m *Maze) generateCells() []*Cell {
}
}
m.Cells = cells
m.innerHole()
return cells
}

func (m *Maze) innerHole() {
cI := m.Width/2
cJ := m.Height/2
mid := m.Inner/2
for _, c := range m.Cells {
i, j := m.GenIJFromIDForCell(uint(c.ID))
if !(i >= cI - mid && i < cI + mid && j >= cJ - mid && j < cJ + mid) {
continue
}
c.Disabled = true
}
}

// GetHorizontalWallsNumber return the number of horizontal walls
func (m *Maze) GetHorizontalWallsNumber() uint {
return m.Width * m.Height
Expand Down Expand Up @@ -186,6 +201,12 @@ func (m *Maze) GenIDFromIJForCell(i uint, j uint) uint {
return m.Height*i + j
}

func (m *Maze) GenIJFromIDForCell(id uint) (uint, uint) {
// id = m.Height*i + j
j := id%m.Height
return (id-j)/m.Height, j
}

// GenJForLeftWallFromJOfCell generate the J coords for the wall at the left of the cell
//
// j is the number of columns
Expand Down
14 changes: 11 additions & 3 deletions Generators/randomised_kruskal.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ func NewRandomisedKruskal(b *Maze) error {

// mergeRandomly merge two random cells
func (m *kruskal) mergeRandomly() error {
id := utils.RandMax(uint(len(m.Cells) - 1))
cell := m.Cells[id]
valid := false
var cell *Cell
for !valid {
id := utils.RandMax(uint(len(m.Cells) - 1))
cell = m.Cells[id]
valid = !cell.Disabled
}
var walls []*Wall
if cell.WallLeft != nil {
walls = append(walls, cell.WallLeft)
Expand Down Expand Up @@ -88,6 +93,9 @@ func (m *kruskal) mergeRandomly() error {
} else {
return nil
}
if mergeWith.Disabled {
return nil
}
wall.IsPresent = false
if mergeWith.ID == cell.ID {
return fmt.Errorf("the cell with the id %d is the same as the cell with the id %d", mergeWith.ID, cell.ID)
Expand All @@ -106,5 +114,5 @@ func (m *kruskal) mergeRandomly() error {
//
// Return true if the maze is finished, false otherwise
func (m *kruskal) isFinished() bool {
return uint(len(*m.Cells[0].MergedRef.MergedCell)) == m.Width*m.Height
return uint(len(*m.Cells[0].MergedRef.MergedCell)) == m.Width*m.Height - m.Inner*m.Inner
}

0 comments on commit 5cbdcc7

Please sign in to comment.