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
90 changes: 90 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require "awesome_print"

def draw_letters
avail_letters = %w[A A A A A A A A A B B C C D D D D E E E E E E E E E E E E F F G G G H H I I I I I I I I I J K L L L L M M N N N N N N O O O O O O O O P P Q R R R R R R S S S S T T T T T T U U U U V V W W X Y Y Z]
avail_letters.sample(10)
Copy link

Choose a reason for hiding this comment

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

Yes! I love this syntax! This implementation is so short! :) Fantastic.

Copy link

Choose a reason for hiding this comment

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

In general, your code takes advantage of Ruby's implicit return statements, but if you ever felt like going back to explicit return statements, I wouldn't be mad...

end

sample = draw_letters
# print sample
Copy link

Choose a reason for hiding this comment

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

Since this code will not be used when running tests, feel free to delete these lines before project submission


def uses_available_letters?(input, letters_in_hand)
letters_in_hand = letters_in_hand.clone
input = input.upcase
input.each_char.all? do |char|
if letters_in_hand.include?(char)
letters_in_hand.delete_at(letters_in_hand.index(char))
true
else
false
end
end
Copy link

Choose a reason for hiding this comment

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

VERY interesting and good job of implementing your own .all? logic! I love it! In general, I think that understanding the implicit return of this method might be confusing, so I might recommend rewriting this code to be something like, and take advantage of variables storing what the .all? block turns into, and using the explicit return

I have an example of maybe what I would do:

is_valid = input.each_char.all? do |char|
    if letters_in_hand.include?(char)
      letters_in_hand.delete_at(letters_in_hand.index(char))
      true
    else
      false
    end
  end
return is_valid

end

def score_word(word)
score = []
score_chart = [%w[A E I O U L N R S T], %w[D G], %w[B C M P], %w[F H V W Y], %w[K]]
word = word.upcase

score_chart.each_with_index do |letter_bank, letter_value|
word.each_char do |char|
if letter_bank.include?(char)
score << (letter_value + 1)
Copy link

Choose a reason for hiding this comment

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

Ha! Interesting relationship between the index of the letter_bank inside of score_chart and its point value. Because this relationship isn't very trustworthy, I might suggest using a different data structure with a more obvious relationship between letter and point value, but I applaud this Sherlock Holmes sleuthing ;)

end
end
end

word.each_char do |char|
if ["J", "X"].include?(char)
score << 8
elsif ["Q", "Z"].include?(char)
score << 10
end
end

if word.length > 6
score << 8
end

return score.sum
Copy link

Choose a reason for hiding this comment

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

I think it's really fascinating that you all did an array of scores and then summed it in the end! I think most people would have opted to just add them, but I think this is also valid and interesting and cool and is very compatible with thinking like a programmer :D

end

def highest_score_from(words)
scored_words = []

words.each do |played_word|
played_word = played_word.upcase
scored_words <<
{
each_word: played_word, each_score: score_word(played_word),
}
end

high_score = scored_words.max_by { |point|
point[:each_score]
}[:each_score]
Copy link

Choose a reason for hiding this comment

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

Fantastic use of max_by (this is one of my favorite solutions to this part of the project!)


highest_scored_words = scored_words.select { |semi_finalist|
semi_finalist[:each_score] == high_score
}.map { |semi_finalist|
semi_finalist[:each_word]
}
Copy link

Choose a reason for hiding this comment

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

I think that using select and map are the right ways to go about this. However, you're starting to chain a lot of methods together, which is valid, but leads to unreadable code. Breaking up these two method calls (select and map) into two different lines doesn't detract from your elegant code style, in my opinion!


if highest_scored_words.length == 1
return ultimate_winner = {word: highest_scored_words[0], score: high_score}
Copy link

Choose a reason for hiding this comment

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

Since the return statement ends the method, you're probably okay with skipping the variable assignment: return {word: highest_scored_words[0], score: high_score}

elsif highest_scored_words.length > 1
winner = highest_scored_words.find { |ten| ten.length == 10 }
Copy link

Choose a reason for hiding this comment

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

Nice! This works with knowing that .find will find the FIRST instance of true, which meets the requirements. ten is probably not my favorite variable name though.

return {word: winner, score: high_score} if winner
end
return {word: highest_scored_words.min_by { |highest| highest.length }, score: high_score}
end

def is_in_english_dict?(user_input_word)
require "csv"
Copy link

Choose a reason for hiding this comment

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

You'll want to put all require statements at the top of the file no matter what!

dictionary_array = []
CSV.foreach("assets/dictionary-english.csv") do |row|
dictionary_array << row
end
dictionary_array.flatten!
return dictionary_array.include?(user_input_word) ? true : false
Copy link

Choose a reason for hiding this comment

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

Consider why this is the same thing as above: return dictionary_array.include?(user_input_word)

end
Copy link

Choose a reason for hiding this comment

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

Nice work on this optional! :D ... Where are the tests to verify that it works? ;)