-2

I have to make a text based adventure game where the player has to collect four objects in order and make it back to room 10 to build it. The player only has 20 moves to collect the objects but as soon as they have all four they only have 5 moves.

I am confused as how to keep track of the objects collected and how to track the moves as well.

This is what I have coded already:

/*********
* 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  
 }
}   
acolisto
  • 5
  • 2

1 Answers1

3

It sounds like you're getting started with architecture. A good way to approach this problem is to instead model the game separately, from how you display the game to the user, separately from how you read the user's input. A popular architecture for this is MVC, but there are others as well.

The strategy basically goes like this:

  1. Can you think of a way to describe, through types, the dungeon?
  2. Can you think of a way to describe what the game needs to "know" about the user (ie. the state)

Then ask yourself:

  • How does the user's input modify the state of the game?
  • How do I update what the user sees when the state changes?

Games typically have a game loop which reads input, updates game state, then updates the presentation to the player. Once you've modeled (eg. created classes for) what you need to "know", like the current rooms, what's in the player's backpack, etc. then you can just update that state.

By the way, rooms connected together in the way you are using NESW directions are actually a di-graph. A great site for learning more about graphs and game programming in general is http://www.redblobgames.com/.

dimo414
  • 47,227
  • 18
  • 148
  • 244
T. Stone
  • 19,209
  • 15
  • 69
  • 97
  • +1 For linking to redblobgames. The blog is extremely useful; it's always correct/accurate and easy to read. – Nate Oct 30 '14 at 14:30