In order to learn both scala and functional programming, I've been rewriting the projects from my introductory CS class in scala. One of the projects was a plinko game, where the user picks a slot to drop a token into and then the path that the token took and the money won from the end slot is printed to the console. The number of slots is constrained and so as the token falls row by row it can never be less than 0 or greater than 8.
I opted to use a stream to calculate the path, but I feel like the way that I am determining whether or not the token is within the constraints isn't really representative of functional programming because I know that generally vals are preferred to vars. What is a better way to write this in a functional style? In general, is there a way I should think about keeping track of things like this that is more in keeping with a functional programming style?
def getPath(prevSlot: Double): Stream[Double] = { var left = Random.nextBoolean if (prevSlot <= 0.0) { left = false } else if (prevSlot >= 8.0) { left = true } if (left) { prevSlot #:: getPath(prevSlot - 0.5) } else { prevSlot #:: getPath(prevSlot + 0.5) } }