2013-03-27 45 views
0

對於課堂,我們必須製作一個基於測試的遊戲,在這個遊戲中我們會穿過一系列像老派文本遊戲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() 

有人可以幫我嗎?我一整天都在嘗試不同的東西,而我似乎無法讓它工作。爲什麼第一個工作一些而不總是?爲什麼第二個不工作?我每次運行它時如何獲得第二個工作?

謝謝。

+1

當它不起作用什麼是你得到的錯誤? – 2013-03-27 04:56:24

+0

這是我得到的第一個:http://pastebin.com/Ji0qVxc5 – Chris 2013-03-27 04:58:44

+0

它究竟如何「不工作」?他們是否拋出錯誤?我只是在REPL中嘗試了他們兩個,他們似乎按預期工作。 – 2013-03-27 04:59:22

回答

1

看着pastebin,問題可能出在您將代碼粘貼到REPL的方式。試試這個:

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

... paste stuff ... 

... press Ctrl-d 
+0

我實際上有兩個我加載到解釋器中的scala文件:load。我嘗試粘貼這種方式,每次都有效。你知道如何用加載方法修復它嗎? – Chris 2013-03-27 05:06:06

+1

不,但我有時會發現:load命令有點bug。用'-i'選項傳遞文件可能會有更好的結果:'scala -i myfile.scala' – 2013-03-27 05:27:50

+0

除了Chris B說的之外,我還推薦使用REPL之外的構建工具。我個人使用[Scala IDE for Eclipse](http://scala-ide.org/),它自動集成了所有內容。有一個陡峭的學習曲線,但也是一個巨大的回報。 – 2013-03-27 14:39:29

相關問題