🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # alexwebgr's solution

## to Scrabble Score in the Ruby Track

Published at Feb 14 2021 · 0 comments
Instructions
Test suite
Solution

Given a word, compute the scrabble score for that word.

## Letter Values

You'll need these:

``````Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10
``````

## Examples

"cabbage" should be scored as worth 14 points:

• 3 points for C
• 1 point for A, twice
• 3 points for B, twice
• 2 points for G
• 1 point for E

And to total:

• `3 + 2*1 + 2*3 + 2 + 1`
• = `3 + 2 + 6 + 3`
• = `5 + 9`
• = 14

## Extensions

• You can play a double or a triple letter.
• You can play a double or a triple word.

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 scrabble_score_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride scrabble_score_test.rb
``````

## Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

## Submitting Incomplete Solutions

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

### scrabble_score_test.rb

``````require 'minitest/autorun'
require_relative 'scrabble_score'

class ScrabbleTest < Minitest::Test
def test_empty_word_scores_zero
assert_equal 0, Scrabble.new('').score
end

def test_whitespace_scores_zero
skip
assert_equal 0, Scrabble.new(" \t\n").score
end

def test_nil_scores_zero
skip
assert_equal 0, Scrabble.new(nil).score
end

def test_scores_very_short_word
skip
assert_equal 1, Scrabble.new('a').score
end

def test_scores_other_very_short_word
skip
assert_equal 4, Scrabble.new('f').score
end

def test_simple_word_scores_the_number_of_letters
skip
assert_equal 6, Scrabble.new('street').score
end

def test_complicated_word_scores_more
skip
assert_equal 22, Scrabble.new('quirky').score
end

def test_scores_are_case_insensitive
skip
assert_equal 41, Scrabble.new('OXYPHENBUTAZONE').score
end

def test_convenient_scoring
skip
assert_equal 13, Scrabble.score('alacrity')
end
end``````

### scrabble_score.rb

``````class Scrabble
TILE_SET = {
"A" => 1, "N" => 1,
"B" => 3, "O" => 1,
"C" => 3, "P" => 3,
"D" => 2, "Q" => 10,
"E" => 1, "R" => 1,
"F" => 4, "S" => 1,
"G" => 2, "T" => 1,
"H" => 4, "U" => 1,
"I" => 1, "V" => 4,
"J" => 8, "W" => 4,
"K" => 5, "X" => 8,
"L" => 1, "Y" => 4,
"M" => 3, "Z" => 10
}

def self.score(tiles)
new(tiles).score
end

private

def initialize(tiles, tile_set = TILE_SET)
@tiles = tiles.to_s.scan(/[[:alpha:]]/)
@tile_set = tile_set
end

public

def score
return 0 if tiles.empty?

tiles.tally.sum do |letter|
tile_set[letter.upcase] * letter
end
end
end``````

### scrabble_score_exe.rb

``````#!/usr/bin/env ruby
require_relative './scrabble_score'

hawaiian = {
"A" => 1,
"N" => 3,
"O" => 2,
"P" => 8,
"E" => 4,
"H" => 6,
"U" => 5,
"I" => 3,
"W" => 9,
"K" => 2,
"L" => 7,
"M" => 8,
"‘" => 0 # I don't know the score for this letter. I think it might be 0
}

puts Scrabble.new('hello').score # 8
puts Scrabble.new('aloha', tile_set = hawaiian).score # 17
puts Scrabble.new('aloha', hawaiian).score # 17
puts Scrabble.new('he‘e', hawaiian).score # => 14
puts Scrabble.new('Humuhumunukunukuapuaa', hawaiian).score # 94``````