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

def draw_letters
letters = {
"A" => 9,
"B" => 2,
"C" => 2,
"D" => 4,
"E" => 12,
"F" => 2,
"G" => 3,
"H" => 2,
"I" => 9,
"J" => 1,
"K" => 1,
"L" => 4,
"M" => 2,
"N" => 6,
"O" => 8,
"P" => 2,
"Q" => 1,
"R" => 6,
"S" => 4,
"T" => 6,
"U" => 4,
"V" => 2,
"W" => 2,
"X" => 1,
"Y" => 2,
"Z" => 1
}

keys_only = letters.keys
copy_of_letters = letters
hand = []

while hand.length < 10
tile = keys_only.sample
tile_count = copy_of_letters["#{tile}"]
if tile_count > 0
copy_of_letters["#{tile}"] -= 1
hand.push(tile)
end
end

return hand
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 method! This is a good strategy. Actually, although you're right that sometimes you need to make a copy of variables, in this case, you don't need copy_of_letters. You can replace copy_of_letters here with letters and the tests still pass! Let me know if you have questions on that!


def uses_available_letters?(input, letters_in_hand)
copy_of_hand = letters_in_hand.dup
word_array = input.upcase.split("")
result = nil

word_array.each do |letter|
if copy_of_hand.include?(letter) == true
copy_of_hand.slice!(copy_of_hand.index(letter))
result = true
else
result = false
break
end
end
return result
end

def score_word(word)
score_chart = {
1 => ["A", "E", "I", "O", "L", "N", "R", "S", "T"],
2 => ["D", "G"],
3 => ["B", "C", "M", "P"],
4 => ["F", "H", "V", "W", "Y"],
5 => ["K"],
8 => ["J", "X"],
10 => ["Q", "Z"]
}

score_total = 0
letters_array = word.upcase.split("")
letters_array.each do |letter|
score = score_chart.find {|points, letters|
letters.include?(letter)
}.first
Copy link

Choose a reason for hiding this comment

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

Nice use of find!

score_total += score
end
if word.length > 6
score_total += 8
end
return score_total
end

def highest_score_from(words)
winning_hash = {}
final_scores = {}
winning_words = []

words.each do |word|
final_scores[word] = score_word(word)
end
scores_only = final_scores.values


final_scores.each do |word, score|
if score == scores_only.max
winning_words.push(word)
end
end

winning_word = nil
sorted_words = winning_words.sort_by(&:length)
Copy link

Choose a reason for hiding this comment

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

Nice!

sorted_words.each do |word|
if word.length == 10
winning_word = word
break
else
winning_word = sorted_words.first
end
end

winning_hash[:score] = scores_only.max
winning_hash[:word] = winning_word
return winning_hash
end

# WAVE 5 - OPTIONAL
def is_in_english_dict? (input)
dictionary = CSV.read("assets/dictionary-english.csv")
input_array = []
verdict = ""

input_array.push(input)

if dictionary.include?(input_array) == true
verdict = true
else
verdict = false
end
return verdict
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 wave!

Copy link

Choose a reason for hiding this comment

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

Here's an idea for a refactoring if you had more time on this project: Is there a way to take out the else ... verdict = false ? Hint: Could changing the initial value of verdict in verdict = "" to something else help?

Copy link

Choose a reason for hiding this comment

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

Also, do you have any tests for this? ;)

# puts is_in_english_dict?("emoji")