🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to High Scores in the Scala Track

Published at Aug 23 2019 · 0 comments
Test suite

Manage a game player's High Score list.

Your task is to build a high-score component of the classic Frogger game, one of the highest selling and addictive games of all time, and a classic of the arcade era. Your task is to write methods that return the highest score from the list, the last added score, the three highest scores, and a report on the difference between the last and the highest scores.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.


Tribute to the eighties' arcade game Frogger

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import org.scalatest.{Matchers, FunSuite}

/** @version 2.0.0 */
class HighScoresTest extends FunSuite with Matchers {

  test("Latest score") {
    HighScores.latest(List(100, 0, 90, 30)) should be(30)

  test("Personal best") {
    HighScores.personalBest(List(40, 100, 70)) should be(100)

  test("Personal top") {
    HighScores.personalTop(List(50, 30, 10)) should be(List(50, 30, 10))

  test("Personal top highest to lowest") {
    HighScores.personalTop(List(20, 10, 30)) should be(List(30, 20, 10))

  test("Personal top when there is a tie") {
    HighScores.personalTop(List(40, 20, 40, 30)) should be(List(40, 40, 30))

  test("Personal top when there are less than 3") {
    HighScores.personalTop(List(30, 70)) should be(List(70, 30))

  test("Personal top when there is only one") {
    HighScores.personalTop(List(40)) should be(List(40))

  test("Personal top from a long list") {
      List(10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70)) should be(
      List(100, 90, 70))

  test("Message for new personal best") {
    HighScores.report(List(20, 40, 0, 30, 70)) should be(
      "Your latest score was 70. That's your personal best!")

  test("Message when latest score is not the highest score") {
    HighScores.report(List(20, 100, 0, 30, 70)) should be(
      "Your latest score was 70. That's 30 short of your personal best!")

  test("Message for repeated personal best") {
    HighScores.report(List(20, 70, 50, 70, 30)) should be(
      "Your latest score was 30. That's 40 short of your personal best!")
object HighScores {
  def personalTop(scores: List[Int]): List[Int] = scores.sortBy(-_) take 3

  def personalBest(scores: List[Int]): Int = scores.max

  def latest(scores: List[Int]): Int = scores.last

  def report(scores: List[Int]): String = {
    val latestScore: Int = latest(scores)
    val best = personalBest(scores)
    val diff = if(best <= latestScore) ""
    else  best - latestScore + " short of "
    s"Your latest score was ${latestScore}. That's ${diff}your personal best!"

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?