DEV Community

Juan Vasquez
Juan Vasquez

Posted on

3 1

Exercism Ruby - High Scores

Alt Text

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 and the three highest scores.

require 'minitest/autorun'
require_relative 'high_scores'

# Common test data version: 5.0.0 7dfb96c
class HighScoresTest < Minitest::Test
  def test_list_of_scores
    # skip
    scores = [30, 50, 20, 70]
    expected = [30, 50, 20, 70]
    assert_equal expected, HighScores.new(scores).scores
  end

  def test_latest_score
    skip
    scores = [100, 0, 90, 30]
    expected = 30
    assert_equal expected, HighScores.new(scores).latest
  end

  def test_personal_best
    skip
    scores = [40, 100, 70]
    expected = 100
    assert_equal expected, HighScores.new(scores).personal_best
  end

  def test_personal_top_three_from_a_list_of_scores
    skip
    scores = [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]
    expected = [100, 90, 70]
    assert_equal expected, HighScores.new(scores).personal_top_three
  end

  def test_personal_top_highest_to_lowest
    skip
    scores = [20, 10, 30]
    expected = [30, 20, 10]
    assert_equal expected, HighScores.new(scores).personal_top_three
  end

  def test_personal_top_when_there_is_a_tie
    skip
    scores = [40, 20, 40, 30]
    expected = [40, 40, 30]
    assert_equal expected, HighScores.new(scores).personal_top_three
  end

  def test_personal_top_when_there_are_less_than_3
    skip
    scores = [30, 70]
    expected = [70, 30]
    assert_equal expected, HighScores.new(scores).personal_top_three
  end

  def test_personal_top_when_there_is_only_one
    skip
    scores = [40]
    expected = [40]
    assert_equal expected, HighScores.new(scores).personal_top_three
  end

  def test_latest_score_is_not_the_personal_best
    skip
    scores = [100, 40, 10, 70]
    refute HighScores.new(scores).latest_is_personal_best?
  end

  def test_latest_score_is_the_personal_best
    skip
    scores = [70, 40, 10, 100]
    assert HighScores.new(scores).latest_is_personal_best?
  end
end
Enter fullscreen mode Exit fullscreen mode

My answer was:

class HighScores
  attr_reader :scores

  def initialize scores
    @scores = scores
  end

  def latest
    scores.last
  end

  def personal_best
    scores.max
  end

  def personal_top_three
    @scores.sort.reverse.take(3)
  end

  def latest_is_personal_best?
    latest == personal_best
  end
end
Enter fullscreen mode Exit fullscreen mode

I learned about attr_reader, attr_writer and attr_accessor.

attr_reader: It generates a getter method

attr_writer: It generates a setter method

attr_accessor: It generates a setter and getter method

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up