使用 scala 将地图链接到文件,以运行基于文本的游戏

发布于 2024-12-12 03:56:55 字数 1023 浏览 0 评论 0原文

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

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) 并等待下一个输入?

I'm taking a beginning programming class, and we're on to making text based games, more like read you're own adventures then actual games and I don't quite understand how to get the information form my map, which has all the room information, into a file that will run it.
I have the rooms file set up like...

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

and so on, so that the room is assigned a number and a name, then it gets a description, then list the number of exits, list what they are, and then list which numbered room that answer should take you to. I don't know how to get it so that it reads into two things, one that takes the first three bits of information as room information, and the second which reads into the number of exits and creates an array with that number of exits, and then reads in the exits, and thier numbers.

what i have is

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

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

I know there's probably some easy answer, but I'm really pretty lost on what I should be doing. I don't know how to get my file read in so the right parts go to the right places, and I'm enough of a beginner that alot of what i've been reading isn't very clear to me, so hopefully my questions clear enough that someone can help me out, and tell if i'm going about this even sort of right, and if it will work when i actually try to put it together, since if i understand what i'm doing it has to take user input, look up the direction typed in from the array of exits, look at the destination associated with that, then take that destination and look for a location in map that has that number and take you there and then println(Map.description) and wait for the next input?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤痕我心 2024-12-19 03:56:55

这些天我似乎看到了解析器组合器的所有其他问题。

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
}

It seems every other question these days I see parser combinators.

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
}
半岛未凉 2024-12-19 03:56:55

使用 vallines = 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 而不是 Map,您可以使用用户输入(参见Console),将其转换为整数(尝试 .toInt),然后在房间列表中查找它。为此,真实的地图可能会很有用:

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

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

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

PS 我不知道你已经学过什么,所以我无法判断我所写的内容是否有意义或看起来像胡言乱语;大多数课程都希望您主要使用已经学过的内容,因此您应该看看根据您已经学到的内容,您可以管理哪些内容,而不是尝试采用我在这里建议的任何策略。但是,除非明确告知,否则不要将房间案例类命名为 Map——这只会在使用内置 Map 时带来麻烦。

Read the file in with val lines = scala.io.source.fromFile.getLines().toList or somesuch.

Don't call anything Map, since Scala already has a Map which you may want to use.

Now that you have all the lines, you can figure out how to parse them. You may want to use matching:

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
 }

And you may also wish to use recursion (this would just break the thing up into a list of rooms, but it wouldn't extract the information from each room):

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)
  }
}

Once you do more parsing (.split(" ") on strings may be helpful) and get everything into the case class, which I will call Room instead of Map, you take user input (see Console), convert it to an integer (try .toInt), and look it up in your list of rooms. For that, a real map might be useful:

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

Now roomsByNumber(n) will look up the n'th room.

Hopefully this will help you get started. Good luck!

P.S. I have no idea what you've already been taught, so I can't tell if what I'm writing makes sense or looks like gibberish; most classes expect you to use mostly what you've already been taught, so you should see what you can manage given what you've already learned over trying to adopt any strategy I've suggested here. But don't name the room case class Map unless explicitly told to--that's just asking for trouble using the built-in Map.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文