Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Wave 1
def draw_letters
raw_tiles = [
{letter: "A", weight: 9, score: 1},
{letter: "B", weight: 2, score: 3},
{letter: "C", weight: 2, score: 3},
{letter: "D", weight: 4, score: 2},
{letter: "E", weight: 12, score: 1},
{letter: "F", weight: 2, score: 4},
{letter: "G", weight: 3, score: 2},
{letter: "H", weight: 2, score: 4},
{letter: "I", weight: 9, score: 1},
{letter: "J", weight: 1, score: 8},
{letter: "K", weight: 1, score: 5},
{letter: "L", weight: 4, score: 1},
{letter: "M", weight: 2, score: 3},
{letter: "N", weight: 6, score: 1},
{letter: "O", weight: 8, score: 1},
{letter: "P", weight: 2, score: 3},
{letter: "Q", weight: 1, score: 10},
{letter: "R", weight: 6, score: 1},
{letter: "S", weight: 4, score: 1},
{letter: "T", weight: 6, score: 1},
{letter: "U", weight: 4, score: 1},
{letter: "V", weight: 2, score: 4},
{letter: "W", weight: 2, score: 4},
{letter: "X", weight: 1, score: 8},
{letter: "Y", weight: 2, score: 4},
{letter: "Z", weight: 1, score: 10}]

tile_set = []

# extracts letter and score keys and puts them in array called raw_tiles

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever use of the data structure you created. Consider also how you could use a data structure organized like this: {"A"=>9, "B"=>2,...}

raw_tiles.each do |a|
a[:weight].times do
tile_set.push(a[:letter])
end
end

# extracts 10 random letters and assigns them to the array named hand
hand = tile_set.sample(10)
return hand
end

#Official hand
letters_in_hand = draw_letters

#Wave 2
#Word is changed to array so we can see if the word is an anagram of the hand
def uses_available_letters?(input, letters_in_hand)
input = input.upcase.chars
boolean_value = true
input.each do |letter|
if input.count(letter) > letters_in_hand.count(letter)
boolean_value = false
end
end
boolean_value
end

# Wave 3
# Return the score of the given word
def score_word(word)
word = word.upcase.chars
sum = 0
total_sum = 0

score_hash = {1=> ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], 2=> ["D", "G"],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative approach would be to store the letter scores in a hash, something like this:

LETTER_SCORES = {
  "A" => 1
  "B" => 3,
  "C" => 3,
  "D" => 2,
  # ...
}

Then to get the score for a letter, you can say LETTER_SCORES[letter].

You could create this hash from your original array of hashes raw_tiles

Organizing the data this way would simplify the logic below.

3=> ["B", "C", "M", "P"], 4=> ["F", "H", "V", "W", "Y"], 5=> ["K"], 8=> ["J", "X"],
10=> ["Q", "Z"]}

total_sum = 8 if word.length >= 7
word.each do |letter|
score_hash.each do |key, value|
value.each do |check|
if check == letter
sum = key.to_i
total_sum += sum
end
end
end
end
return total_sum
end

# Wave 4
# Return word and score of word with highest score
def highest_score_from(words)
high_score_hash = {word: "", score: 0}
high_score = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We reworked the logic for this method and you don't end up using high_score and high_score_length. Rake provided this warning for you:

/Users/becca/Documents/GitHub/c12/adagrams/lib/adagrams.rb:90: warning: assigned but unused variable - high_score
/Users/becca/Documents/GitHub/c12/adagrams/lib/adagrams.rb:91: warning: assigned but unused variable - high_score_length
/Users/becca/Documents/GitHub/c12/adagrams/lib/adagrams.rb:46: warning: assigned but unused variable - letters_in_hand

You should remove these variables from your code.

high_score_length = 0
words.each do |word|
score = score_word(word)
if score > high_score_hash[:score]
high_score_hash[:score] = score_word(word)
high_score_hash[:word] = word
elsif score == high_score_hash[:score]
winner_word = tiebreaker(high_score_hash[:word], word)
high_score_hash[:score] = score
high_score_hash[:word] = winner_word
end
end
return high_score_hash
end

def tiebreaker(word1, word2)
if word1.length == 10
return word1
elsif word2.length == 10
return word2
elsif word1.length < word2.length
return word1
elsif word2.length < word1.length
return word2
end
return word1
end


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of cleanliness, remove these extra lines.