使用 scala 将地图链接到文件,以运行基于文本的游戏
我正在参加初级编程课程,我们正在制作基于文本的游戏,更像是阅读你自己的冒险经历,然后是实际的游戏,我不太明白如何从我的地图中获取信息,地图包含所有内容房间信息,放入将运行它的文件中。 我将房间文件设置为......
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些天我似乎看到了解析器组合器的所有其他问题。
It seems every other question these days I see parser combinators.
使用 vallines = scala.io.source.fromFile.getLines().toList 或类似方法读取文件。
不要调用任何
Map
,因为 Scala 已经有一个您可能想要使用的Map
。现在您已经掌握了所有行,您可以弄清楚如何解析它们。您可能想要使用匹配:
并且您可能还希望使用递归(这只会将其分解为房间列表,但不会从每个房间中提取信息):
一旦您进行更多解析(
.split(" ")
字符串可能会有所帮助)并将所有内容放入案例类中,我将其称为Room
而不是Map
,您可以使用用户输入(参见Console
),将其转换为整数(尝试.toInt
),然后在房间列表中查找它。为此,真实的地图可能会很有用:现在
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 aMap
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:
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):
Once you do more parsing (
.split(" ")
on strings may be helpful) and get everything into the case class, which I will callRoom
instead ofMap
, you take user input (seeConsole
), convert it to an integer (try.toInt
), and look it up in your list of rooms. For that, a real map might be useful:Now
roomsByNumber(n)
will look up then
'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-inMap
.