Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game
- import java.io.File
- import java.util.Scanner
- import scala.collection.mutable.ArrayBuffer
- /** Handles read/write from/to Disk.
- * Reading uses java.util.Scanner
- * Writing uses java.io
- */
- object Disk {
- def loadLines() = {
- val fromFile = new File("score.txt" )
- val result: ArrayBuffer[String] = new ArrayBuffer[String](0)
- val scanner = new Scanner(fromFile)
- while(scanner.hasNextLine) {
- val line = scanner.nextLine
- if (!(line eq "")){
- result.append(line)
- }
- }
- result.toVector
- }
- def personalScore(name: String) = {
- loadAllResults().filter(_._1 equals name)
- }
- def loadAllResults() = {
- val output = loadLines().map(str => {
- val data = str.split(',')
- (data(0),data(1), data(2))
- })
- output.sortBy(_._2.toInt).reverse
- }
- def appendResult(playerName: String, points: Int, time: Int, fileName: String) = {
- saveString("", "score.txt")
- val fullString = (loadLines() :+ (s"$playerName,$points,$time")).mkString("\n")
- saveString(fullString, fileName)
- }
- /** Save `text` to a text file called `fileName`. */
- private def saveString(text: String, fileName: String): Unit = {
- val f = new java.io.File(fileName)
- val pw = new java.io.PrintWriter(f, "UTF-8")
- try pw.write(text) finally pw.close()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement