Skip to content
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

Ports - Angela & Stephanie #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
101 changes: 101 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require "csv"

def draw_letters
pool = [
"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",
]

hand = pool[1..98].sample(10)
Copy link

Choose a reason for hiding this comment

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

Why do you have [1..98]? What does this do? The tests still pass with this part taken out

return hand
end

def uses_available_letters?(input, letters_in_hand)
word = input.upcase
split_word = word.split("")

split_word.each do |letter|
index = letters_in_hand.index(letter)
if letters_in_hand.all? { letter } && index != nil
Copy link

Choose a reason for hiding this comment

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

What does the letters_in_hand.all? { letter } part do? This code actually just checks if letter is not nil or false! Sometimes, it's good to check that letters_in_hand has no nil or false elements, but in this case, I think it's okay to assume that won't be the case. In general, just checking if index != nil (aka, if index is not nil, it's because it found something in the last line) probably suffices

letters_in_hand.delete_at(index)
else
return false
end
end
return true
end

def score_word(word)
score_hash = {
"A" => 1, "B" => 3, "C" => 3, "D" => 2,
"E" => 1, "F" => 4, "G" => 2, "H" => 4,
"I" => 1, "J" => 8, "K" => 5, "L" => 1,
"M" => 3, "N" => 1, "O" => 1, "P" => 3,
"Q" => 10, "R" => 1, "S" => 1, "T" => 1,
"U" => 1, "V" => 4, "W" => 4, "X" => 8,
"Y" => 4, "Z" => 10,
}
word = word.upcase
letters = word.split("")
score = 0
letters.each do |letter|
score += score_hash[letter]
end
if word.length >= 7 && word.length <= 10
score += 8
end
return score
end

def highest_score_from(words)
final_winner = {
word: "",
score: 0,
}
words.each do |word|
score = score_word(word)
if score > final_winner[:score]
final_winner[:score] = score
final_winner[:word] = word
elsif score == final_winner[:score]
if word.length == 10 && final_winner[:word].length != 10
final_winner[:word] = word
elsif (word.length < final_winner[:word].length) && (final_winner[:word].length != 10)
final_winner[:word] = word
end
end
end

return final_winner
end

def is_in_english_dict?(input)
CSV.open("assets/dictionary-english.csv", "r").each do |word|
return true if word.include?(input)
Copy link

Choose a reason for hiding this comment

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

Hm, is word.include?(input) the logic you want? What if input is shoe and word is horseshoe? Of course, in this case, shoe would probably be in the dictionary, but the line word.include?(input) is still probably not the logic you want here.

end
return false
end
Copy link

Choose a reason for hiding this comment

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

Nicely done on the optional method! ... where are the tests to make sure it actually works? ;)