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.
In this exercise you're going to instantiate a class and add some instance methods. http://ruby-for-beginners.rubymonstas.org/writing_classes/initializers.html
A HighScore accepts an array with one or more numbers, each representing one 'game score'. The Array class can offer inspiration for working with arrays. https://ruby-doc.org/core-2.5.1/Array.html
For installation and learning resources, refer to the Ruby resources page.
For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:
gem install minitest
If you would like color output, you can require 'minitest/pride'
in
the test file, or note the alternative instruction, below, for running
the test file.
Run the tests from the exercise directory using the following command:
ruby high_scores_test.rb
To include color from the command line:
ruby -r minitest/pride high_scores_test.rb
Tribute to the eighties' arcade game Frogger
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
require 'minitest/autorun'
require_relative 'high_scores'
# Common test data version: 2.0.0 7a386a2
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
skip
scores = [50, 30, 10]
expected = [50, 30, 10]
assert_equal expected, HighScores.new(scores).personal_top
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
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
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
end
def test_personal_top_when_there_is_only_one
skip
scores = [40]
expected = [40]
assert_equal expected, HighScores.new(scores).personal_top
end
def test_personal_top_from_a_long_list
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
end
def test_message_for_new_personal_best
skip
scores = [20, 40, 0, 30, 70]
expected = "Your latest score was 70. That's your personal best!"
assert_equal expected, HighScores.new(scores).report
end
def test_message_when_latest_score_is_not_the_highest_score
skip
scores = [20, 100, 0, 30, 70]
expected = "Your latest score was 70. That's 30 short of your personal best!"
assert_equal expected, HighScores.new(scores).report
end
def test_message_for_repeated_personal_best
skip
scores = [20, 70, 50, 70, 30]
expected = "Your latest score was 30. That's 40 short of your personal best!"
assert_equal expected, HighScores.new(scores).report
end
end
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
scores.max(3)
end
def report
"Your latest score was #{latest}. That's #{report_difference} your personal best!".squeeze(' ')
end
private
def report_difference
"#{personal_best - latest} short of" unless latest_is_personal_best?
end
def latest_is_personal_best?
latest == personal_best
end
end
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments