ruby - high-scores

This commit is contained in:
James Walker 2018-11-07 15:19:08 -05:00
parent a69fbfe259
commit 505afd4b98
Signed by: walkah
GPG Key ID: 3C127179D6086E93
3 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,39 @@
# High Scores
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](http://exercism.io/languages/ruby/resources).
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
## Source
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.

View File

@ -0,0 +1,25 @@
# High Scores class
class HighScores
attr_accessor :scores
def initialize(scores)
@scores = scores
end
def latest
@scores.last
end
def highest
@scores.max
end
def top
@scores.sort{ |a, b| b <=> a }[0, 3]
end
def report
short_of = "#{highest - latest} short of " if highest != latest
"Your latest score was #{latest}. That's #{short_of}your personal best!"
end
end

View File

@ -0,0 +1,89 @@
require 'minitest/autorun'
require_relative 'high_scores'
# Common test data version: 1.0.0 3204fc2
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_highest_score
# skip
scores = [40, 100, 70]
expected = 100
assert_equal expected, HighScores.new(scores).highest
end
def test_personal_bests
# skip
scores = [50, 30, 10]
expected = [50, 30, 10]
assert_equal expected, HighScores.new(scores).top
end
def test_personal_bests_highest_to_lowest
# skip
scores = [20, 10, 30]
expected = [30, 20, 10]
assert_equal expected, HighScores.new(scores).top
end
def test_personal_bests_when_there_is_a_tie
# skip
scores = [40, 20, 40, 30]
expected = [40, 40, 30]
assert_equal expected, HighScores.new(scores).top
end
def test_personal_bests_when_there_are_less_than_3
# skip
scores = [30, 70]
expected = [70, 30]
assert_equal expected, HighScores.new(scores).top
end
def test_personal_bests_when_there_is_only_one
# skip
scores = [40]
expected = [40]
assert_equal expected, HighScores.new(scores).top
end
def test_personal_bests_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).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