斯卡拉的基于文本的冒险游戏

时间:2014-10-30 02:17:43

标签: scala adventure

我必须制作一个基于文本的冒险游戏,其中玩家必须按顺序收集四个物体并将其返回到10号房间来构建它。玩家只有20次移动来收集物品,但只要他们拥有全部四个,他们只有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  
 }
}   

1 个答案:

答案 0 :(得分:3)

听起来你已经开始使用architecture了。解决这个问题的一个好方法是分别模拟游戏,从显示游戏到用户的方式,与你阅读用户的方式分开输入。一个流行的架构是MVC,但也有其他架构。

策略基本上是这样的:

  1. 你能想出一种通过类型描述地下城的方法吗?
  2. 你能想出一种描述游戏需要的方式吗?知道"关于用户(即国家)
  3. 然后问问自己:

    • 用户的输入如何修改游戏状态?
    • 如何更新状态更改时用户看到的内容?

    游戏通常有一个游戏循环,可以读取输入,更新游戏状态,然后将演示文稿更新到播放器。一旦你建模(例如创建课程)你需要什么"知道",就像现在的房间,玩家的背包里有什么等等,那么你可以更新该状态。

    顺便说一句,以您使用NESW方向连接在一起的房间实际上是 di-graph 。一般来说,了解有关图表和游戏编程的更多信息的网站是http://www.redblobgames.com/