I'm working on a game written in Scala where the user has to type "BANG" as quickly as possible in order to hunt an animal.
// ...
println("When you see an animal, type BANG as quickly as possible.")
Thread.sleep(2000 + Random.nextInt(6000))
val start = System.currentTimeMillis()
println("You see an animal!")
val bang = readLine("")
val time = System.currentTimeMillis() - start
if (bang == "BANG" && time <= 1500) {
// ...you get the point
Unfortunately, there's a bug that makes so that you can type "BANG" ahead of time and always win, because it accepts input before the readLine()
.
I have tried a few different ways of clearing the input. The obvious solution, putting a readLine()
before val start = System.currentTimeMillis()
, doesn't work because it forces the user to press enter first.
Any ideas?