0

对于课堂,我们必须制作一个基于测试的游戏,在这个游戏中,我们穿过一系列房间,比如老式的文字游戏 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()

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

谢谢你。

4

1 回答 1

1

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

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

... paste stuff ...

... press Ctrl-d
于 2013-03-27T05:02:08.363 回答