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

部屋に番号と名前が割り当てられるように、次に説明を取得し、出口の数をリストし、それらが何であるかをリストし、次に、答えがどの番号の部屋に行くべきかをリストします。1 つは最初の 3 ビットの情報を部屋の情報として取得し、もう 1 つは出口の数を読み取り、その数の出口の配列を作成します。次に、出口とその番号を読み取ります。

私が持っているのは

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読み込んでください。

MapScalaにはすでに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(" ")文字列で役立つ場合があります)、すべてをcaseクラスに入れます。これをRoom代わりに呼び出しMapます。ユーザー入力を受け取り(を参照Console)、整数に変換して(try .toInt)、で検索します。部屋のリスト。そのためには、実際のマップが役立つ場合があります。

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

次に、 '番目の部屋roomsByNumber(n)を検索します。n

うまくいけば、これはあなたが始めるのに役立つでしょう。幸運を!

PSあなたがすでに何を教えられているのかわからないので、私が書いていることが理にかなっているのか、それともぎこちないように見えるのかわかりません。ほとんどのクラスでは、ほとんどの場合、すでに教えられていることを使用することを期待しているため、ここで提案した戦略を採用しようとしてすでに学んだことを踏まえて、何を管理できるかを確認する必要があります。ただし、明示的に指示されていない限り、ルームケースクラスに名前を付けないでください。Mapこれは、組み込みのを使用する際の問題を求めているだけMapです。

于 2011-10-27T17:49:12.307 に答える