-
Notifications
You must be signed in to change notification settings - Fork 29
Sockets - Erica & Karla G. #7
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
fed8287
9744db6
68eea4f
68b26a4
fc53190
5c2c927
7bffb60
d862f8a
f1d1a98
37e9f52
95aaa89
03339fd
d661ca3
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,103 @@ | ||
| require "csv" | ||
|
|
||
| def draw_letters | ||
| available_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} | ||
| drawn_letters = [] | ||
| 10.times do | ||
| letter = available_letters.keys.sample | ||
| while available_letters[letter] == 0 | ||
| letter = available_letters.keys.sample | ||
|
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. By this logic, you're as likely to draw an Instead, you might create an array containing all the letters, with code similar to this: letter_counts = {
# the hash you had before
}
letters = []
letter_counts.each do |letter, count|
count.times do
letters << letter
end
endThen you could pull a random tile with |
||
| end | ||
| drawn_letters.push(letter) | ||
| available_letters[letter] = available_letters[letter] - 1 | ||
| end | ||
| return drawn_letters | ||
| end | ||
|
|
||
| # Method that deletes element in array at its specific index. Prevents deletion of mathcing elements | ||
| class Array | ||
| def delete_elements_in_(array) | ||
| array.each do |x| | ||
| if index = index(x) | ||
|
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. Be careful about this sort of monkey patching - it's typically considered bad form. Instead, you could write a helper method that's not part of the def delete_from(hay, needles)
needles.each do |needle|
if index = hay.index(needle)
hay.delete_at(index)
end
end
end |
||
| delete_at(index) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Checks if user inputs letters from their letter bank | ||
| def uses_available_letters?(input, letters_in_hand) | ||
| used_letters = [] | ||
| input.each_char do |letter| | ||
| if letters_in_hand.include?(letter) == true | ||
| letters_in_hand.delete_elements_in_([letter]) | ||
| used_letters << 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. Since you only use Please make sure you fully understand the code you borrow from other sources before including it in your project. |
||
| else | ||
| letters_in_hand = (letters_in_hand << used_letters).flatten! # Resets letter bank if previous user input included invalid letters | ||
| return false | ||
| end | ||
| end | ||
| letters_in_hand = (letters_in_hand << used_letters).flatten! | ||
| return true | ||
| end | ||
|
|
||
| #Wave 3 | ||
| def score_word(word) | ||
| word = word.downcase | ||
| score = 0 | ||
| word.each_char do |letter| | ||
| case letter | ||
| when "a", "e", "i", "o", "u", "l", "n", "r", "s", "t" | ||
| score += 1 | ||
| when "d", "g" | ||
| 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 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 |
||
| when "b", "c", "m", "p" | ||
| score += 3 | ||
| when "f", "h", "v", "w", "y" | ||
| score += 4 | ||
| when "k" | ||
| score += 5 | ||
| when "j", "x" | ||
| score += 8 | ||
| when "q", "z" | ||
| score += 10 | ||
| end | ||
| end | ||
| if word.length >= 7 | ||
| score += 8 | ||
| end | ||
| return score | ||
| end | ||
|
|
||
| #Wave 4 | ||
| def highest_score_from(words) | ||
| winning = {} | ||
| high_score = 0 | ||
| best_word = nil | ||
| words.each do |word| | ||
| if score_word(word) > high_score | ||
| high_score = score_word(word) | ||
| best_word = word | ||
| elsif score_word(word) == high_score | ||
| if best_word.length == 10 | ||
| next | ||
| elsif word.length == 10 | ||
| best_word = word | ||
| elsif word.length < best_word.length | ||
| best_word = word | ||
| end | ||
| end | ||
| end | ||
| winning[:word] = best_word | ||
| winning[:score] = high_score | ||
| return winning | ||
| end | ||
|
|
||
| # Wave 5 | ||
| def is_in_english_dict?(input) | ||
| english_words = CSV.read("assets/dictionary-english.csv") | ||
| return english_words.flatten!.include?(input) | ||
| 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 love this data structure - it makes it very clear how many there are of each letter. Could you store it in a constant?