Skip to content

Instantly share code, notes, and snippets.

@johaywood
Created December 16, 2015 01:11
Show Gist options
  • Save johaywood/329f7525dd361aa71d29 to your computer and use it in GitHub Desktop.
Save johaywood/329f7525dd361aa71d29 to your computer and use it in GitHub Desktop.
class Scrabble
attr_reader :string, :scores, :multiplier
def initialize(string, multiplier = :single)
@string = string
@multiplier = multiplier
end
def score
score = 0
string_ary = string.to_s.gsub(/\s+/, "").downcase.split("")
string_ary.each do |char|
score += SCORES[char]
end
score * MULTI[multiplier]
end
def self.score(str)
Scrabble.new(str).score
end
SCORES = {
"" => 0,
"a" => 1,
"b" => 3,
"c" => 3,
"d" => 2,
"e" => 1,
"f" => 4,
"g" => 2,
"h" => 4,
"i" => 1,
"j" => 8,
"k" => 5,
"l" => 1,
"m" => 3,
"n" => 1,
"o" => 1,
"p" => 3,
"q" => 10,
"r" => 1,
"s" => 1,
"t" => 1,
"u" => 1,
"v" => 4,
"w" => 4,
"x" => 8,
"y" => 4,
"z" => 10
}
MULTI = { single: 1, double: 2, triple: 3}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment