-
Notifications
You must be signed in to change notification settings - Fork 33
Leaves - Alice and Tiffany #4
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
52047eb
63247a7
37d4a70
2d37c76
1160391
69841a7
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,88 @@ | ||
require "csv" | ||
|
||
def draw_letters() | ||
letter_pool = ["A"] * 9 + ["B"] * 2 + ["C"] * 2 + ["D"] * 4 + ["E"] * 12 + ["F"] * 2 + ["G"] * 3 + ["H"] * 2 + ["I"] * 9 + ["J"] + ["K"] + ["L"] * 4 + ["M"] * 2 + ["N"] * 6 + ["O"] * 8 + ["P"] * 2 + ["Q"] + ["R"] * 6 + ["S"] * 4 + ["T"] * 6 + ["U"] * 4 + ["V"] * 2 + ["W"] * 2 + ["X"] + ["Y"] * 2 + ["Z"] | ||
hand = [] | ||
|
||
# Shuffling once resulted in too many duplicates. | ||
# The program shuffles multiple times. | ||
10.times do | ||
letter_pool.shuffle! | ||
hand << letter_pool.pop | ||
end | ||
|
||
return hand | ||
end | ||
|
||
def uses_available_letters?(input, letters_in_hand) | ||
split = input.upcase.split("") | ||
copy = letters_in_hand.dup | ||
|
||
split.each do |letter| | ||
if copy.include? (letter) | ||
copy.delete_at(copy.index(letter)) | ||
else | ||
return false | ||
end | ||
end | ||
|
||
return true | ||
end | ||
|
||
def score_word(word) | ||
word_arr = word.upcase.split("") | ||
points = 0 | ||
word_arr.each do |letter| | ||
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. While this 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 |
||
case letter | ||
when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" | ||
points += 1 | ||
when "D", "G" | ||
points += 2 | ||
when "B", "C", "M", "P" | ||
points += 3 | ||
when "F", "H", "V", "W", "Y" | ||
points += 4 | ||
when "K" | ||
points += 5 | ||
when "J", "X" | ||
points += 8 | ||
when "Q", "Z" | ||
points += 10 | ||
end | ||
end | ||
|
||
if word_arr.length >= 7 | ||
points += 8 | ||
end | ||
|
||
return points | ||
end | ||
|
||
def highest_score_from(words) | ||
word_score = [] | ||
|
||
words.each do |word| | ||
word_score << { word: word, score: score_word(word) } | ||
end | ||
|
||
max = word_score.max_by { |element| element[:score] }[:score] | ||
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. You've packed lots of logic in a few lines of elegant code. Consider adding a few comments that outline the rules for determining the winner, particularly the tiebreaker logic. |
||
winners = word_score.select { |element| element[:score] == max } | ||
ten_char = winners.select { |element| element[:word].length == 10 } | ||
|
||
if ten_char.length > 0 | ||
return ten_char.first | ||
else | ||
min_winners = winners.min_by { |element| element[:word].length } | ||
return min_winners | ||
end | ||
end | ||
|
||
def is_in_english_dict?(input) | ||
dictionary = CSV.read("assets/dictionary-english.csv") | ||
|
||
if dictionary.include? [input] | ||
return true | ||
else | ||
return false | ||
end | ||
end |
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.
Consider using a data structure to store the letter distribution information. You can then use this data structure to create your array.