我有两个scala项目 - 基本相同 - 一个工作,一个不工作。有人可以告诉我为什么吗?

时间:2013-03-27 04:51:58

标签: function scala text-based mud

对于课程,我们必须制作一个基于测试的游戏,在这个游戏中,我们会经历一系列的房间,如旧学校文本游戏Colossal Cave Adventure。

我首先定义了不同房间的功能,以便在房间内输入方向时可以轻松切换。

以下代码大部分时间都在REPL中运行,但我希望它每次都能正常工作:

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()

以下代码根本不在REPL中工作:

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()

有人可以帮帮我吗?我整天都在尝试不同的事情,似乎无法让它发挥作用。为什么第一个工作有时并且不总是?为什么第二个从不工作?如何在每次运行时让第二个工作?

谢谢。

1 个答案:

答案 0 :(得分:1)

查看pastebin,问题可能在于您将代码粘贴到REPL中。试试这个:

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

... paste stuff ...

... press Ctrl-d