2011-10-27 6 views
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 

と部屋番号と名前を割り当てられるようにように、それは説明を取得し、リスト出口の番号を持っているか、何を一覧表示彼らは、答えがあなたに連れていかなければならない番号のついた部屋を列挙します。部屋の情報として最初の3ビットの情報を取るものと、出口の数を読み込んでその数の出口を持つ配列を作成するものの2つに分かれるように、それを得る方法はわかりませんが、出口とその数を読み込みます。私が持っているもの

は、私はいくつかの簡単な答えはおそらくあります知っている

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

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

ですが、私は本当にかわいい私がやるべきかに迷ってしまいました。私は私のファイルを読み込む方法を知らないので、適切な部分が適切な場所に行くので、私は読んでいたことの多くが私にはあまり明確ではないので、うまくいけば私の初心者です誰かが私を助けてくれることを十分に明確にしている質問、私はこのような権利についてもやっているのかどうか、もし私が実際にそれをまとめようとするとうまくいくのかどうか、ユーザーの入力を受け取り、出口の配列から入力された方向を調べ、それに関連付けられた目的地を見て、その目的地を取得して、その番号を持つ地図内の場所を探してそこに連れて行き、println(Map.description )次の入力を待ちますか?

答えて

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代わりのMapを呼ぶであろう、ケースクラスにすべてを取得したら、(Consoleを参照)、ユーザの入力を取り、整数に変換(.toIntを試してください)、あなたの部屋のリストでそれを調べてください。そのため、実際のマップが便利かもしれません:

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

roomsByNumber(n)n番目の部屋を見ていきます。

この情報を参考にしてください。がんばろう!

P.S.私はあなたがすでに教えられていることを知らないので、私が書いていることが意味をなさないか、あわてるように見えるかどうかは分かりません。ほとんどのクラスはすでに教えていることをほとんど使用することを期待していますので、私がここで提案した戦略を採用しようとする以上に、あなたがすでに学んだことを考えれば、管理できるものを見てください。しかし、明示的に言わない限り、ルームケースクラスの名前をMapにしないでください。それは組み込みのMapを使用するだけで問題を尋ねています。

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 
} 
関連する問題