0 Replies - 637 Views - Last Post: 29 December 2015 - 08:43 PM

#1 rymurphy12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-December 15

Help converting a program to Scala

Posted 29 December 2015 - 08:43 PM

Hi everyone,

This is my first post here and I am looking for a little help. I am attempting to learn Scala, and after having read some material on the subject I thought I would try my hand at converting a group of school projects I wrote in Java into Scala, first knocking out the basic functionality and then by iterating through it until it became more functional (for example I would like to change my mutuable list buffers so they are immutable lists). The only problem is that my program is not working as I intend to. The my program works is based on a stripped down version of the game Sorcerer's Cave (located here). It is as a GUI interface that will read in a file, go line by line through the text file and then will use that data to add parties, creatures, treasure, and artifacts. The problem that I am having is that it only seems like the the first party is getting their creatures added and none of the artifacts or look is being added. I am using the example.txt file that is included in the project repo. I walked through the code and I can see the aCreature variable get initialized with every 'c' line, but only the first two 'c' lines in example and the last c line get added (which all happen to be in the same party. Here is the code in question:

def processLine(line: String): Unit ={

    val dataList = line.split(":").map(_.trim).toList
    dataList.head match{
      case "p"  =>   cave.parties.append(new Party(dataList(1).toInt, dataList(2), "Party", ListBuffer()))
      case "c"  =>   val aCreature = new Creature(dataList(1).toInt, dataList(2), dataList(3), dataList(4).toInt,
                                                  dataList(5).toInt, dataList(6).toInt, dataList(7).toInt,
                                                  ListBuffer(), ListBuffer())
                     addCreatureToParty(aCreature)

      case "t" =>  val aTreasure = new Treasure(dataList(1).toInt, dataList(2), "Treasure", dataList(3).toInt,
                                                dataList(4).toDouble, dataList(5).toInt)
                   addTreasureToCreature(aTreasure)

      case "a" =>  val name = if(dataList.length < 5) "Unnamed" else dataList(4)
                   val anArtifact = new Artifact(dataList(1).toInt, name, dataList(2), dataList(3).toInt)
                   addArtifactToCreature(anArtifact)

      case _ =>  return
    }
  }


I tried to follow the FAQ as closely as possible, but with the problem being a bit of a challenge to explain, I apologize if I have breached any etiquette. Let me know if any more information is needed to solve this problem or if there is anything I can do to make things more clear. Also, in a more general sense, I am looking for opportunities to grow so if you an general feedback towards how I approach this problem, please let me know. I am always looking to improve. :smile2:/>/> Thanks!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1