プレーヤーが 4 つのオブジェクトを順番に収集し、部屋 10 に戻って構築するテキスト ベースのアドベンチャー ゲームを作成する必要があります。プレイヤーは、オブジェクトを収集するために 20 回の移動しかできませんが、4 つすべてを収集するとすぐに、5 回の移動しかできなくなります。
収集したオブジェクトを追跡する方法と、移動を追跡する方法についても混乱しています。
これは私がすでにコーディングしたものです:
/*********
* getRequest:
* Prompts the user for a move
* and returns the string entered
*********/
def getRequest(): String = {
println("Where do you go now?")
readLine()
}
/*********
* printHelp:
* help menu with basic instructions
*********/
def printHelp() {
println("N=North, E=East, S=South and W=West")
}
/*********
* processSpecialCommand:
* Processes the special commands: H, Q (only)
*********/
def processSpecialCommand(req: String) {
if (req == "H")
printHelp
else if (req == "Q") {
println("I can't believe you are giving up. Are you afraid of Perry?")
println("Oh well, maybe another day then.")
sys.exit(1) // This quits the program immediately (aka- abort)
} else {
println("What are you saying?")
println("Use 'H' for help.")
}
}
/*** Room 1: Foyer (E=6, S=2, W=3) ***/
def room1() {
// Print the message
println()
println("------")
println("Room 1")
println("------")
println(" Ah, the foyer. I probably should call it a lobby")
println(" but foyer just sounds so much better. Don't you agree?")
println("There are doors to the East, South, and West")
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
println("You cannot go there.")
return room1() // Go back to room 1
case "E" =>
// Go to room 6
return room6()
case "S" =>
// Go to room 2
return room2()
case "W" =>
// Go to room 3
return room3()
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room1() // Go back to room 1
}
}
/*** Room 2: (N=1, W=4, S=7) ***/
def room2() {
// Print the message
println()
println("------")
println("Room 2")
println("------")
println("There are doors to the North, South, and West")
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
// Go to room 1
return room1() // Go to room 1
case "E" =>
println("You cannot go there.")
return room2() // Go back to room 2
case "S" =>
// Go to room 7
return room7()
case "W" =>
// Go to room 4
return room4()
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room2() // Go back to room 2
}
}
/*** Room 3: (E=1, S=4) ***/
def room3() {
// Print the message
println()
println("------")
println("Room 3")
println("------")
println("You found piece number 4!!!")
println("There are doors to the East and South")
//if you have pieces 1,2 and 3 you can collect this piece else this part cannot be collected yet
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
println("You cannot go there.")
return room3() // Go back to room 3
case "E" =>
// Go to room 1
return room1()
case "S" =>
// Go to room 4
return room4()
case "W" =>
println("You cannot go there.")
return room3() // Go back to room 3
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room3() // Go back to room 3
}
}
/*** Room 4: (N=3, E=2) ***/
def room4() {
// Print the message
println()
println("------")
println("Room 4")
println("------")
println("You found piece number 2!!!")
println("There are doors to the North and East")
//if you have piece number 1 you can collect this piece else this part cannot be collected yet
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
// Go to room 3
return room3()
case "E" =>
// Go to room 2
return room2()
case "S" =>
println("You cannot go there.")
return room4() // Go back to room 4
case "W" =>
println("You cannot go there.")
return room4() // Go back to room 4
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room4() // Go back to room 4
}
}
/*** Room 5: (N=6, S=8) ***/
def room5() {
// Print the message
println()
println("------")
println("Room 5")
println("------")
println("You found piece number3!!!")
println("There are doors to the North and South")
//if you have pieces 1 and 2 you can collect this piece else this part cannot be collected yet
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
// Go to room 6
return room6()
case "E" =>
println("You cannot go there.")
return room5()
case "S" =>
// Go to room 8
return room8()
case "W" =>
println("You cannot go there.")
return room5() // Go back to room 5
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room5() // Go back to room 5
}
}
/*** Room 6: (E=9, S=5, W=1) ***/
def room6() {
// Print the message
println()
println("------")
println("Room 6")
println("------")
println("There are doors to the East, South and West")
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
println("You cannot go there.")
return room6()
case "E" =>
// Go to room 9
return room9()
case "S" =>
// Go to room 5
return room5()
case "W" =>
//Go to room 1
return room1()
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room6() // Go back to room 6
}
}
/*** Room 7: (N=2, E=8) ***/
def room7() {
// Print the message
println()
println("------")
println("Room 7")
println("------")
println("There are doors to the North and East")
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
// Go to room 2
return room2()
case "E" =>
// Go to room 8
return room8()
case "S" =>
println("You cannont go there.")
return room7()
case "W" =>
println("You cannont go there.")
return room7()
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room7() // Go back to room 7
}
}
/*** Room 8: (N=5, E=10, W=7) ***/
def room8() {
// Print the message
println()
println("------")
println("Room 8")
println("------")
println("There are doors to the North, East and West")
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
// Go to room 5
return room5()
case "E" =>
// Go to room 10
return room10()
case "S" =>
println("You cannont go there.")
return room8()
case "W" =>
// Go to room 7
return room7()
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room8() // Go back to room 8
}
}
/*** Room 9: (S=10, W=6) ***/
def room9() {
// Print the message
println()
println("------")
println("Room 9")
println("------")
println("You found piece number 1!!!")
println("There are doors to the South and West")
//collect the first piece
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
println("You cannot go there.")
return room9()
case "E" =>
println("You cannot go there.")
return room9()
case "S" =>
// Go to room 10
return room10()
case "W" =>
// Go to room 6
return room6()
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room9() // Go back to room 9
}
}
/*** Room 10: (N=9, W=8) ***/
def room10() {
// Print the message
println()
println("------")
println("Room 10")
println("------")
println("There are doors to the North and West")
// Get and process the request (moving on to the next state/room)
val move = getRequest.toUpperCase
move match {
case "N" =>
// Go to room 9
return room9()
case "E" =>
println("You cannot go there.")
return room10()
case "S" =>
println("You cannot go there.")
return room10()
case "W" =>
// Go to room 8
return room8()
case cmd =>
// Maybe it is a special request (Help or Quit)
processSpecialCommand(cmd)
return room10() // Go back to room 10
}
}