Created
February 28, 2013 16:46
-
-
Save burtlo/5058100 to your computer and use it in GitHub Desktop.
Scrabble intermediate solutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts "Welcome to Scrabble" | |
class Scrabble | |
def self.score(word) | |
one_point_group = %w[ A E I O U L N R S T ] | |
two_point_group = %w[ D G ] | |
three_point_group = %w[ B C M P ] | |
four_point_group = %w[ F H V W Y ] | |
five_point_group = %w[ K ] | |
eight_point_group = %w[ J X ] | |
ten_point_group = %w[ Q Z ] | |
sum = 0 | |
word.split("").each do |character| | |
# figure out what the character is worth | |
character | |
if one_point_group.include? character.upcase | |
sum += 1 | |
elsif two_point_group.include? character.upcase | |
sum += 2 | |
elsif three_point_group.include? character.upcase | |
sum += 3 | |
elsif four_point_group.include? character.upcase | |
sum += 4 | |
elsif five_point_group.include? character.upcase | |
sum += 5 | |
elsif eight_point_group.include? character.upcase | |
sum += 8 | |
elsif ten_point_group.include? character.upcase | |
sum += 10 | |
end | |
end | |
# break the word into characters | |
# word.length.times do |index| | |
# puts word[index] | |
# end | |
sum | |
end | |
def self.score(word) | |
one_point_group = %w[ A E I O U L N R S T ] | |
two_point_group = %w[ D G ] | |
three_point_group = %w[ B C M P ] | |
four_point_group = %w[ F H V W Y ] | |
five_point_group = %w[ K ] | |
eight_point_group = %w[ J X ] | |
ten_point_group = %w[ Q Z ] | |
score_to_group = { | |
1 => one_point_group, | |
2 => two_point_group, | |
3 => three_point_group, | |
4 => four_point_group, | |
5 => five_point_group, | |
8 => eight_point_group, | |
10 => ten_point_group, | |
} | |
# score_to_group.each do |score,group| | |
# if group.include? character.upcase | |
# sum += score | |
# end | |
# end | |
sum = word.split("").reduce(0) do |sum,character| | |
character | |
score_to_group.find do |score,group| | |
if group.include? character.upcase | |
sum += score | |
end | |
end | |
sum | |
end | |
sum | |
end | |
end | |
score = Scrabble.score("cabbage") | |
if score == 14 | |
puts "YAY!" | |
else | |
puts "Keep Trying: #{score}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment