對於課堂,我們必須製作一個基於測試的遊戲,在這個遊戲中我們會穿過一系列像老派文本遊戲Colossal Cave Adventure一樣的房間。我有兩個scala項目 - 基本相同 - 一個工作,一個不工作。有人能告訴我爲什麼嗎?
我開始爲不同房間定義功能,以便在房間中輸入方向時可以輕鬆切換。
這下面的代碼工作在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()
有人可以幫我嗎?我一整天都在嘗試不同的東西,而我似乎無法讓它工作。爲什麼第一個工作一些而不總是?爲什麼第二個不工作?我每次運行它時如何獲得第二個工作?
謝謝。
當它不起作用什麼是你得到的錯誤? – 2013-03-27 04:56:24
這是我得到的第一個:http://pastebin.com/Ji0qVxc5 – Chris 2013-03-27 04:58:44
它究竟如何「不工作」?他們是否拋出錯誤?我只是在REPL中嘗試了他們兩個,他們似乎按預期工作。 – 2013-03-27 04:59:22