Skip to content

Instantly share code, notes, and snippets.

@MinCha
Last active August 29, 2015 14:26
Show Gist options
  • Save MinCha/e78aa46d6493850434a1 to your computer and use it in GitHub Desktop.
Save MinCha/e78aa46d6493850434a1 to your computer and use it in GitHub Desktop.
package susu
import org.junit.Test
import org.scalatest.junit.JUnitSuite
import scala.annotation.tailrec
import scala.util.Random
class SusuPremium extends JUnitSuite {
@Test def elect() {
case class ScoreBoard(name: String, score: List[Int] = List.empty) {
def sum = score.sum
}
@tailrec
def cast(c: List[ScoreBoard], i: Int): List[ScoreBoard] = {
def random = new Random().nextInt(6) + 1
if (i == 0) c
else cast(c.map(e => ScoreBoard(e.name, e.score :+ random)), i - 1)
}
def printPretty(title: String, scores: List[ScoreBoard]) {
println("=== " + title + "===")
scores.sortWith(_.sum > _.sum).foreach { e =>
val score = e.score.toString().replaceAll("List", "")
println("%s -> %d%s".format(e.name, e.score.sum, score))
}
}
val candidates = List(
"oliver",
"vayne",
"glen").map(ScoreBoard(_))
val first = cast(candidates, 1)
printPretty("First", first)
val second = cast(first, 1)
printPretty("Second", second)
val third = cast(second, 1)
printPretty("Third", third)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment