I started learning scala recently and stumbled upon a problem trying to "print" cells:
class Cell(
val x: Int,
val y: Int,
val left: Option[Cell],
val right: Option[Cell],
val top: Option[Cell],
val bottom: Option[Cell],
var isPainted: Boolean) {
def paint(radius: Int) = {
println("Printing")
if (radius == 0)
isPainted = true
else {
isPainted = true
top.isPainted = true
bottom.isPainted = true
left.isPainted = true
right.isPainted = true
Why are these neighbouring cells
top.left.paint(r - 1)
top.right.paint(r - 1)
bottom.left.paint(r - 1)
bottom.right.paint(r - 1)
not accessible?
}
}
How can I access the top, bottom, left, right cells?
edit:
thank you for the great answer. one addon- question:
how could I check that all the cells around cell x are set to true? My try unfortunately does not work.
def isMiddleCell()={
if(List(top, bottom, left, right).forall(_.Some))
true
else
false
}
Cells, they areOption[Cell]. Read aboutOptions..get()needs to be called similar to:val ttop = top.getOrElse("No top neighbour") ttop.isPainted = true top.getOrElse("No top neighbour").isPainted = trueHowever both of these do not work. Even wrapping thetrueinSome(true)does not work.