I have two scala projects - basically the same - one works and one does not. Can someone tell me why?

StackOverflow https://stackoverflow.com/questions/15652143

Question

For class we have to make a test based game in which we move through a series of rooms like the old school text game Colossal Cave Adventure.

I began by defining functions for the different rooms so that when the directions are typed in the room can be switched easily.

This following code works in the REPL most of the time, but I would like it to work every time:

def roomOne():Unit = {
println("You are currently in Room 1.")
println("There are 2 doors: North and West")
println("Which door would you like to go through?")

var input = readLine(">> ").toUpperCase match {
case "NORTH" => roomFour()
case "WEST" => roomNine()
case "EAST" => {
  println("You cannot go there.")
  roomOne()
}
case "SOUTH" => {
  println("You cannot go there.")
  roomOne()
  }
 }
}
def roomFour():Unit = {
println("You are currently in Room 4.")
println("There are 2 doors: East and South")
println("Which door would you like to go through?") 
}
def roomNine():Unit = {
println("You are currently in Room 9.")
println("There are 3 doors: North, East, and South")
println("Which door would you like to go through?")
}
def startGame():Unit = {
var input = readLine(">> ").toUpperCase match {
case "YES" => roomOne()
case "NO" => {
  println("When you are ready to begin please type \"yes\".")
  startGame()
}
}
}

println("**************************************************")
println("**************************************************")

println("Hello Mr. Doofenshmirtz. Are you ready to find the parts to create the Super DOOM-inator?")

startGame()

AND the following code DOES NOT WORK in the REPL at all:

def roomOne():Unit = {
println("You are currently in Room 1.")
println("There are 2 doors: North and West")
println("Which door would you like to go through?")

var input = readLine(">> ").toUpperCase match {
case "NORTH" => roomFour()
case "WEST" => roomNine()
case "EAST" => {
  println("You cannot go there.")
  roomOne()
}
case "SOUTH" => {
  println("You cannot go there.")
  roomOne()
}
}
}
def roomFour():Unit = {
println("You are currently in Room 4.")
println("There are 2 doors: East and South")
println("Which door would you like to go through?")

var input = readLine(">> ").toUpperCase match {
case "NORTH" => {
  println("You cannot go there.")
  roomFour()
}
case "WEST" => {
  println("You cannot go there.")
  roomFour()
}
case "EAST" => roomFive()
case "SOUTH" => roomOne()
}
}
def roomFive():Unit = {
println("You are currently in Room 5.")
println("There are 3 doors: East, South, and West")
println("Which door would you like to go through?")
}
def roomNine():Unit = {
println("You are currently in Room 9.")
println("There are 3 doors: North, East, and South")
println("Which door would you like to go through?")
}
def startGame():Unit = {
var input = readLine(">> ").toUpperCase match {
case "YES" => roomOne()
case "NO" => {
  println("When you are ready to begin please type \"yes\".")
  startGame()
}
}
}

println("**************************************************")
println("**************************************************")

println("Hello Mr. Doofenshmirtz. Are you ready to find the parts to create the Super DOOM-inator?")

startGame()

Can someone please help me out? I have been trying different things all day and I can't seem to get it to work. Why does the first one work some times and not always? Why does the second one never work? How do I get the second one to work each and every time I run it?

Thank you.

Was it helpful?

Solution

Looking at the pastebin, the problem is probably with the way that you are pasting your code into the REPL. Try this:

scala> :paste
// Entering paste mode (ctrl-D to finish)

... paste stuff ...

... press Ctrl-d
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top