Skip to content

Commit

Permalink
fix: wrong row in CageGenerator getUnusedNeighbors()
Browse files Browse the repository at this point in the history
  • Loading branch information
kaajjo committed Jul 14, 2024
1 parent 621bf95 commit b49cbef
Showing 1 changed file with 4 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@ class CageGenerator(
private val board: List<List<Cell>>,
private val type: GameType
) {
private var unusedCells = mutableListOf<Cell>()


init {
unusedCells = board.flatten().toMutableList()
}
private var unusedCells: MutableList<Cell> = board.flatten().toMutableList()

private fun generateCage(startCell: Cell, requiredSize: Int = 2, id: Int = 0): Cage? {
if (unusedCells.isEmpty() || !unusedCells.contains(startCell)) {
return null
}
println("Generating cage")
unusedCells.remove(startCell)

var cage = Cage(
Expand All @@ -28,7 +22,6 @@ class CageGenerator(
)

if(getUnusedNeighbors(startCell).isEmpty()) {
println("No neighbors")
return cage
}

Expand All @@ -51,24 +44,21 @@ class CageGenerator(
if (neighbors.isEmpty()) {
return cage
}
println("Found neighbors")
println("Selecting random cell")

var cell: Cell? = null
for(i in neighbors) {
// select random neighbor to add in cage
// select a random neighbor to add in a cage
val tempCell = neighbors.random()
if (cage.cells.all { it.value != tempCell.value } && unusedCells.contains(tempCell)) {
cell = tempCell
}
}
if (cell != null) {
println("Selected ${cell.toString()}")
unusedCells.remove(cell)
cage = cage.copy(
cells = cage.cells + cell
)
} else {
println("Couldn't find cell break")
break
}
}
Expand Down Expand Up @@ -111,7 +101,7 @@ class CageGenerator(
neighbors.add(board[row][col + 1])
}
if (row < type.size - 1) {
neighbors.add(board[row][col])
neighbors.add(board[row + 1][col])
}

return neighbors
Expand Down

0 comments on commit b49cbef

Please sign in to comment.