-
Notifications
You must be signed in to change notification settings - Fork 27
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
Naheed & Sigrid - Edges - Adagrams #5
base: master
Are you sure you want to change the base?
Changes from all commits
30290f3
d543933
4104ee1
28d6e5d
e858542
afa218f
d5d4fb9
45b4db3
011a9cf
d8cd51b
e252fdc
ec7b3f6
06af639
3ce6166
1f9eee1
261fb50
e81cef4
f39287a
a29c258
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,114 @@ | ||
# require 'pry' | ||
require 'csv' | ||
|
||
# Draws 10 random letters from alphabetic array | ||
def draw_letters | ||
letters_concatenated = [] | ||
hand = [] | ||
|
||
initial_array = [["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]] | ||
|
||
initial_array.each do |x| | ||
letters_concatenated.concat([x[0]] * x[1]) | ||
end | ||
|
||
hand = letters_concatenated.sample(10) | ||
|
||
return hand | ||
|
||
end | ||
|
||
# Determines if user word uses letters in hand | ||
def uses_available_letters?(input, letters_in_hand) | ||
word = input.chars | ||
word.each do |letter| | ||
if letters_in_hand.include?(letter) | ||
letters_in_hand.delete_at(letters_in_hand.index(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. This isn't something we checked for in the tests, but it's worth pointing out that this method is destructive. By calling it, you modify the array |
||
else | ||
return false | ||
end | ||
end | ||
return true | ||
end | ||
|
||
# Calculates score for user word | ||
def score_word(word) | ||
score = 0 | ||
score_word = word.upcase.chars | ||
score_word.each do |letter| | ||
if %w[A E I O U L N R S T].include?(letter) | ||
score += 1 | ||
elsif %w[D G].include?(letter) | ||
score += 2 | ||
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 using a big Instead, it might work well to store the data 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 |
||
elsif %w[B C M P].include?(letter) | ||
score += 3 | ||
elsif %w[F H V W Y].include?(letter) | ||
score += 4 | ||
elsif %w[K].include?(letter) | ||
score += 5 | ||
elsif %w[J X].include?(letter) | ||
score += 8 | ||
elsif %w[Q Z].include?(letter) | ||
score += 10 | ||
end | ||
end | ||
|
||
if score_word.length >= 7 && score_word.length <= 10 | ||
score += 8 | ||
end | ||
return score | ||
end | ||
|
||
# Determines highest scoring word and returns both word and score | ||
def highest_score_from(words) | ||
array = [] | ||
max_score = 0 | ||
winning_word = "" | ||
max_values = [] | ||
min_length = 10 | ||
hash = {} | ||
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. The variable names |
||
|
||
words.each do |word| | ||
score = score_word(word) | ||
array << {word: word, score: score} | ||
end | ||
|
||
array.each do |hash| | ||
if hash[:score] == max_score | ||
winning_word = hash[:word] | ||
max_values << {word: winning_word, score: max_score} | ||
elsif hash[:score] > max_score | ||
max_values = [] | ||
max_score = hash[:score] | ||
winning_word = hash[:word] | ||
max_values << {word: winning_word, score: max_score} | ||
end | ||
end | ||
|
||
max_values.each do |array| | ||
if array[:word].length == 10 | ||
hash = {word: array[:word], score: array[:score].to_i} | ||
return hash | ||
end | ||
if array[:word].length < min_length | ||
min_length = array[:word].length | ||
winning_word = array[:word] | ||
max_score = array[:score] | ||
hash = {word: winning_word, score: max_score} | ||
end | ||
end | ||
return hash | ||
end | ||
|
||
# Determines whether users word is a valid English word | ||
def is_in_english_dict?(input) | ||
check = false | ||
dictionary = CSV.open('assets/dictionary-english.csv') | ||
word = input.downcase | ||
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. Good work on the optional! |
||
dictionary.each do |row| | ||
if row.include?(word) | ||
check = true | ||
end | ||
end | ||
return check | ||
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.
I like the idea of this data structure, but I think you could simplify it a bit by using a hash, where the keys are letters and the values are counts. Something like: