Avatar of eliaahadi

eliaahadi's solution

to Scrabble Score in the Ruby Track

Published at Mar 18 2020 · 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
class Scrabble
  # An attribute reader returns the value of an instance variable.
  #soln1
  def initialize(word)
    @word = word
    @score_hash = 
    {
      'a'=>1,
      'e'=>1,
      'i'=>1,
      'o'=>1,
      'u'=>1,
      'l'=>1,
      'n'=>1,
      'r'=>1,
      's'=>1,
      't'=>1,
      'd'=>2,
      'g'=>2,
      'b'=>3,
      'c'=>3,
      'm'=>3,
      'p'=>3,
      'f'=>4,
      'h'=>4,
      'v'=>4,
      'w'=>4,
      'y'=>4,
      'k'=>5,
      'j'=>8,
      'x'=>8,
      'q'=>10,
      'z'=>10,
    }
  end
  def score
    # 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
    puts 'score word: ' + @word.to_s
    count = 0
    if @word == '' || @word == " \t\n" || @word == nil
      puts 'empty \t\n nil'
      return count
    else
      for value in @word.downcase.split('') do
        if @score_hash[value]
          count += @score_hash[value]
          puts 'value: ' + value + count.to_s
        end
      end
    end
    count
  end
  def self.score(word)
    score_hash = 
    {
      'a'=>1,
      'e'=>1,
      'i'=>1,
      'o'=>1,
      'u'=>1,
      'l'=>1,
      'n'=>1,
      'r'=>1,
      's'=>1,
      't'=>1,
      'd'=>2,
      'g'=>2,
      'b'=>3,
      'c'=>3,
      'm'=>3,
      'p'=>3,
      'f'=>4,
      'h'=>4,
      'v'=>4,
      'w'=>4,
      'y'=>4,
      'k'=>5,
      'j'=>8,
      'x'=>8,
      'q'=>10,
      'z'=>10,
    }
    puts 'score word: ' + word.to_s
    count = 0
    if word == '' || word == " \t\n" || word == nil
      puts 'empty \t\n nil'
      return count
    else
      for value in word.downcase.split('') do
        if score_hash[value]
          count += score_hash[value]
          puts 'value: ' + value + count.to_s
        end
      end
    end
    count
  end

  #soln2
  # SCRABBLE_SCORES = {
  #   'a' => 1, 'e' => 1, 'i' => 1, 'o' => 1, 'u'=> 1, 'l' => 1, 'n' => 1, 'r' => 1, 's' => 1, 't' => 1,
  #   'd' => 2, 'g' => 2,
  #   'b' => 3, 'c' => 3, 'm' => 3, 'p' => 3,
  #   'f' => 4, 'h' => 4, 'v' => 4, 'w' => 4, 'y' => 4,
  #   'k' => 5,
  #   'j' => 8, 'x' => 8,
  #   'q' => 10, 'z' => 10
  # }

  # def initialize(word)
  #   @word = word
  # end

  # def score_letter(letter)
  #   SCRABBLE_SCORES[letter.downcase]
  # end

  # def score
  #   return 0 if @word.nil?
    
  #   total_score = 0
  #   split_word = @word.chars
  #   split_word.each do |letter|
  #     next if score_letter(letter) == nil
  #     total_score += score_letter(letter)
  #   end
  #   return total_score
  # end

  # def self.score(input)
  #   new(input).score
  # end

  #soln3
  # attr_reader :word

  # SCORES = {
  #   %w(A E I O U L N R S T) => 1,
  #   %w(D G) => 2,
  #   %w(B C M P) => 3,
  #   %w(F H V W Y) => 4,
  #   %w(K) => 5,
  #   %w(J X) => 8,
  #   %w(Q Z) => 10
  # }

  # def initialize(word)
  #   @word = word.nil? ? [] : word.upcase.scan(/\w/)
  # end

  # def score
  #   word.map{ |letter| letter_to_score(letter) }.inject(0, &:+)
  # end

  # def self.score(input)
  #   new(input).score
  # end

  # private

  # def letter_to_score(letter)
  #   SCORES.detect { |k, v| k.include?(letter) }.last
  # end
end

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?