-
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
adagrams #20
base: master
Are you sure you want to change the base?
adagrams #20
Changes from all commits
47657fe
3653a56
9d27901
0e8641d
639abf5
2ef26bb
02799ea
a38aa3b
2473f24
8f469db
c76081a
6ab9fbf
c888cfa
7c25155
583d09f
901f159
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,84 @@ | ||
require "pry" | ||
SCORE_TO_LETTERS = {1 => ["a", "e", "i", "o", "u", "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"]} | ||
LETTERS_TO_QUANTITY = {"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} | ||
def draw_letters | ||
letter_bank = Array.new | ||
10.times do | ||
random_letter_picked = LETTERS_TO_QUANTITY.keys.sample | ||
letter_bank << random_letter_picked | ||
while letter_bank.count(random_letter_picked) > LETTERS_TO_QUANTITY[random_letter_picked] | ||
letter_bank.pop #what happens if you pop out letter and put back in the same letter from the next line | ||
random_letter_picked = LETTERS_TO_QUANTITY.keys.sample | ||
letter_bank << random_letter_picked | ||
end | ||
end | ||
return letter_bank | ||
end | ||
|
||
|
||
def letters_to_hash(letters) | ||
letter_to_quantity = Hash.new(0) | ||
letters.each do |letter| | ||
letter_to_quantity[letter] +=1 | ||
end | ||
return letter_to_quantity | ||
end | ||
|
||
def uses_available_letters?(user_input_word, letter_bank) | ||
array_of_input_letters = user_input_word.split('') | ||
letter_bank_hash = letters_to_hash(letter_bank) | ||
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. Is there any reason you couldn't have done this with 2 arrays? |
||
user_input_letters_hash = letters_to_hash(array_of_input_letters) | ||
output = true | ||
user_input_letters_hash.each do |key,value| | ||
if !letter_bank_hash.keys.include?(key) # look at looping of outputs, see what happens if use | ||
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. As soon as one of these is false, do you need to check the whole hash still? |
||
output = false | ||
elsif user_input_letters_hash[key] > letter_bank_hash[key] | ||
output= false | ||
end | ||
end | ||
return output | ||
end | ||
|
||
def score_word(user_input_word) #String | ||
if user_input_word.length == 0 | ||
total_score = 0 | ||
else | ||
array_of_input_letters = user_input_word.downcase.split("") | ||
user_input_word_hash = letters_to_hash(array_of_input_letters) | ||
array_of_letter_scores = [] | ||
user_input_word_hash.each do |word_key, word_value| | ||
SCORE_TO_LETTERS.each do |score_key, score_value| | ||
if score_value.include?(word_key) | ||
score = score_key*word_value | ||
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. It took me a minute to see why this works. Again, using a hash here is actually more work for you, and hindering clarity. |
||
array_of_letter_scores.push(score) | ||
end | ||
end | ||
end | ||
total_score = array_of_letter_scores.reduce(:+) | ||
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.
|
||
if user_input_word.length >= 7 && user_input_word.length < 11 | ||
total_score += 8 | ||
end | ||
return total_score | ||
end | ||
end | ||
#played_words = ['BBBBBB', 'AAAAAAAAAA'] | ||
|
||
def highest_score_from(played_words) | ||
collection_of_played_words_and_scores = []#array of hashes | ||
played_words.each do |word| | ||
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! I love this, very clear! |
||
played_word_to_score = {} #how to deal with word duplicates | ||
played_word_to_score[:word] = word | ||
played_word_to_score[:score] = score_word(word) | ||
collection_of_played_words_and_scores.push(played_word_to_score) | ||
end | ||
top_score = collection_of_played_words_and_scores.reduce(0){ |memo, h| h[:score] > memo ? h[:score] : memo } # ==> {score: n} | ||
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. Yum, this is also great! |
||
tied_highest_scores = collection_of_played_words_and_scores.select {|h| h[:score].to_i == top_score} #highest_score[:score]} # all the hashes in an array | ||
highest_score_hash = tied_highest_scores.find {|h| h[:word].length == 10} | ||
if highest_score_hash == nil | ||
highest_score_hash = tied_highest_scores.min_by{|h| h[:word].length} | ||
end | ||
return highest_score_hash #supposed to be a hash | ||
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.
So, you have a probability problem here. When you sample from the keys, the odds are not weighted by the number of each tile in the
letter_bank
. Your results give a 1/26 chance to draw any letter, but that's not accurate is it?