-
Notifications
You must be signed in to change notification settings - Fork 33
Branches - Brianna + Monick #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
9c5bb3b
3f32c79
1a06a7b
48df937
50e3efd
2eda458
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice work on this optional wave! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") |
There was a problem hiding this comment.
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 replacecopy_of_letters
here withletters
and the tests still pass! Let me know if you have questions on that!