1

我正在上一门初级编程课程,我们正在制作基于文本的游戏,更像是阅读你自己的冒险然后是实际游戏,我不太明白如何从我的地图中获取信息,其中包含所有房间信息,放入将运行它的文件中。我将房间文件设置为...

0 roomone
you enter a room, and it looks odd. you can go north or south. which way?
2
north 1
south 2
//will having a space here make a difference? should i delete these?
1 hall
theres a hall here, with a door running east. continue north,go east, or go back south?
3
north 3
south 0
east 4

依此类推,为房间分配一个号码和一个名称,然后它会得到一个描述,然后列出出口的数量,列出它们是什么,然后列出答案应该带你去哪个编号的房间。我不知道如何获取它,以便它读入两件事,一个将前三位信息作为房间信息,第二个读取出口数量并创建一个具有该数量出口的数组,然后读入出口和他们的号码。

我所拥有的是

case class Map(location:Int,place:String,description:String,exits:Array )

case class Exits(numberexits:Int,direction:String,destination:Int)

我知道可能有一些简单的答案,但我真的很迷茫我应该做什么。我不知道如何读入我的文件,以便将正确的部分放到正确的位置,而且我已经足够初学者了,我一直在阅读的很多内容对我来说并不是很清楚,所以希望我的问题足够清楚,有人可以帮助我,并告诉我是否要这样做甚至是正确的,以及当我真正尝试将它们组合在一起时它是否会起作用,因为如果我了解我在做什么,它必须接受用户输入,查找从出口数组中输入的方向,查看与之关联的目的地,然后获取该目的地并在地图中查找具有该编号的位置,然后将您带到那里,然后 println(Map.description ) 并等待下一个输入?

4

2 回答 2

4

这些天来,我似乎看到了解析器组合器。

case class Exit(direction:String,destination:Int)
case class Map(location:Int, place:String, description:String, exits:List[Exit] )

object ReadConf extends scala.util.parsing.combinator.RegexParsers {
  override protected val whiteSpace = " +".r
  def eol = "\n"
  def number = "\\d+".r ^^ (_.toInt)

  // Overall format
  def conf = (comment.* ~> map).*
  def map = header ~ description ~ exits ^^ {
    case location ~ place ~ description ~ exits => 
      Map(location, place, description, exits)
  }
  def comment = "//.*".r ~ eol

  // Map parts (except exits)
  def header = location ~ place <~ eol
  def description = ".*".r <~ eol
  def location = number
  def place = "\\w+".r

  // Exits
  def exits = numberOfExits <~ eol >> nExits
  def nExits(n: Int) = repN(n, exit)
  def exit = direction ~ destination <~ eol ^^ {
    case direction ~ destination => Exit(direction, destination)
  }
  def numberOfExits = number
  def direction = "\\w+".r
  def destination = number
}
于 2011-10-27T21:15:29.680 回答
2

val lines = scala.io.source.fromFile.getLines().toList或类似的方式读入文件。

不要调用任何东西Map,因为 Scala 已经有了Map你可能想要使用的。

现在你已经有了所有的行,你可以弄清楚如何解析它们。您可能想要使用匹配:

lines match {
  case a :: b :: c :: remainder =>
    // Execute this code if and only if there are at least 3 lines in the list;
    // if so, pull the first 3 out and call them a, b, and c
  case _ =>
    // Otherwise execute this
 }

而且您可能还希望使用递归(这只会将事物分解为房间列表,但不会从每个房间中提取信息):

def parse(lines: List[String], roomsText: List[List[String]] = Nil): List[List[String]] = {
  lines match {
    case Nil => rooms    // Parsed all the lines, so return what we've already found
    case /* whatever conditions you need */ =>
      // do stuff to find one more room
      parse(remainingLines, newRoom :: roomsText)
  }
}

一旦您进行更多解析(.split(" ")对字符串可能会有所帮助)并将所有内容放入案例类中,我将调用它而Room不是房间列表。为此,一张真实的地图可能会很有用:MapConsole.toInt

val roomsByNumber = rooms.map(room => (room.location, room)).toMap

现在roomsByNumber(n)将查找第n'th 个房间。

希望这将帮助您入门。祝你好运!

PS我不知道你已经学了什么,所以我不知道我写的东西是否有意义或看起来像胡言乱语;大多数课程都希望你主要使用你已经教过的东西,所以你应该看看你已经学到了什么,而不是尝试采用我在这里建议的任何策略。但是除非明确告知,否则不要命名房间案例类Map——那只是在使用内置的Map.

于 2011-10-27T17:49:12.307 回答