クラスでは、古い学校のテキストゲームであるコロッサルケーブアドベンチャーのような一連の部屋を移動するテストベースのゲームを作成する必要があります。
まず、部屋の方向を入力したときに簡単に切り替えることができるように、部屋ごとに機能を定義することから始めました。
この次のコードはほとんどの場合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()
誰かが私を助けてくれますか?私は一日中さまざまなことを試みてきましたが、それを機能させることができないようです。なぜ最初のものが時々機能し、常に機能するとは限らないのですか?なぜ2番目のものが機能しないのですか?実行するたびに2番目のものを機能させるにはどうすればよいですか?
ありがとうございました。