Leaves - Mariya & Yasmin#20
Conversation
AdagramsWhat We're Looking For
|
| players_letters = [] | ||
| alphabet = ("A".."Z").to_a | ||
|
|
||
| until players_letters.length == 10 |
There was a problem hiding this comment.
This is clever. Consider this other method to draw_letters that uses the pool_of_letters hash to create an array of letters. You could then shuffle this array of letters and pop off a letter 10 times.
letters = []
pool_of_letters.each do |letter, count|
count.times do
letters << letter
end
end|
|
||
| #Wave_3 | ||
| def score_word(word) | ||
| one_point_letter = %w[A E I O U L N R S T] |
There was a problem hiding this comment.
While this if/elif control structure works, it means that the information about which letter has which score is locked into this piece of code, and can't easily be used elsewhere. For example, if you wanted to display the value of each letter in a hand, you would need to repeat this work.
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].
| user_word = input.upcase.chars | ||
| word_hash = {} | ||
| user_word.each do |letter| | ||
| if word_hash["#{letter}"].nil? |
There was a problem hiding this comment.
You do not need to use interpolation. letter is already a string.
|
|
||
| full_pile = {} | ||
| letters_in_hand.each do |letter| | ||
| if full_pile["#{letter}"].nil? |
There was a problem hiding this comment.
letters_in_hand_hash would be a more meaningful name then full_pile and would follow the convention introduced in the previous loop.
| full_pile["#{letter}"] += 1 | ||
| end | ||
| end | ||
| return word_hash.all? do |letter, count| |
There was a problem hiding this comment.
This is a lot to put into a return statement. It would be good to do something like the code below. Notice the use of a comment to explain your logic. This would be useful for the two loops above as there is a lot of logic packed into this method.
# return true if every letter of the word is in the letters_in_hand_hash and they're used no more times than they appear in the letters_in_hand array
uses_available_letters_boolean = word_hash.all? do |letter, count|
full_pile.has_key?(letter) && (full_pile[letter] >= word_hash[letter])
end
return uses_available_letters_boolean| end | ||
|
|
||
| #wave_4 | ||
| def tie_breaker(old_word, new_word) |
There was a problem hiding this comment.
This is a great use of a helper method and is elegant code/logic.
| return winner | ||
| end | ||
|
|
||
| def highest_score_from(words) |
There was a problem hiding this comment.
Again, the logic in this method is clear, elegant, and efficient.
| end | ||
|
|
||
| # wave2 code | ||
| def uses_available_letters?(input, letters_in_hand) |
There was a problem hiding this comment.
Review this implementation of this method that does not require the creation of new hashes:
def uses_available_letters?(input, letters_in_hand)
input_array = input.split('')
letters_in_hand_copy = letters_in_hand.shuffle
input_array.each do |letter_in_word|
if letters_in_hand_copy.include?(letter_in_word)
letters_in_hand_copy.delete(letter_in_word)
else
return false
end
end
return true
end
Adagrams
Congratulations! You're submitting your assignment.
Comprehension Questions
Enumerablemixin? If so, where and why was it helpful?