0

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.

Chris
  • 233
  • 2
  • 5
  • 14

1 Answers1

1

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
Chris B
  • 9,149
  • 4
  • 32
  • 38
  • I actually have two scala files that I load into the interpreter with:load. I tried pasting that way and it works every time. Do you know how to fix it with the load method? – Chris Mar 27 '13 at 05:06
  • 1
    No, but I've sometimes found the :load command to be a bit buggy. You might have better luck passing the file with the `-i` option: `scala -i myfile.scala` – Chris B Mar 27 '13 at 05:27
  • In addition to what Chris B says, I'd also recommend using a build tool in addition to the REPL. I personally use [Scala IDE for Eclipse](http://scala-ide.org/), which integrates everything automatically. There's a steep learning curve, but also a big payoff. – Kipton Barros Mar 27 '13 at 14:39