-
Notifications
You must be signed in to change notification settings - Fork 33
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
Branches - Michaela & Angele #17
base: master
Are you sure you want to change the base?
Changes from all commits
46ffd89
6699156
3313c15
20b9bb1
6edc5ee
524ab10
28b1ee5
f604990
3e55626
31877fc
ac9e9c8
9a7792f
22ecc24
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,139 @@ | ||
require 'csv' | ||
|
||
def return_letter_pool_hash | ||
|
||
letter_pool_hash = { | ||
"A" => {number_of_tiles: 9, number_of_points: 1}, | ||
"B" => {number_of_tiles: 2, number_of_points: 3}, | ||
"C" => {number_of_tiles: 2, number_of_points: 3}, | ||
"D" => {number_of_tiles: 4, number_of_points: 2}, | ||
"E" => {number_of_tiles: 12, number_of_points: 1}, | ||
"F" => {number_of_tiles: 2, number_of_points: 4}, | ||
"G" => {number_of_tiles: 3, number_of_points: 2}, | ||
"H" => {number_of_tiles: 2, number_of_points: 4}, | ||
"I" => {number_of_tiles: 9, number_of_points: 1}, | ||
"J" => {number_of_tiles: 1, number_of_points: 8}, | ||
"K" => {number_of_tiles: 1, number_of_points: 5}, | ||
"L" => {number_of_tiles: 4, number_of_points: 1}, | ||
"M" => {number_of_tiles: 2, number_of_points: 3}, | ||
"N" => {number_of_tiles: 6, number_of_points: 1}, | ||
"O" => {number_of_tiles: 8, number_of_points: 1}, | ||
"P" => {number_of_tiles: 2, number_of_points: 3}, | ||
"Q" => {number_of_tiles: 1, number_of_points: 10}, | ||
"R" => {number_of_tiles: 6, number_of_points: 1}, | ||
"S" => {number_of_tiles: 4, number_of_points: 1}, | ||
"T" => {number_of_tiles: 6, number_of_points: 1}, | ||
"U" => {number_of_tiles: 4, number_of_points: 1}, | ||
"V" => {number_of_tiles: 2, number_of_points: 4}, | ||
"W" => {number_of_tiles: 2, number_of_points: 4}, | ||
"X" => {number_of_tiles: 1, number_of_points: 8}, | ||
"Y" => {number_of_tiles: 2, number_of_points: 4}, | ||
"Z" => {number_of_tiles: 1, number_of_points: 10} | ||
} | ||
|
||
return letter_pool_hash | ||
|
||
end | ||
|
||
def draw_letters | ||
|
||
letter_pool_hash = return_letter_pool_hash | ||
|
||
letter_pool_array = letter_pool_hash.map do |letter, letter_info| | ||
(letter * letter_info[:number_of_tiles]).split("") | ||
end | ||
|
||
letter_pool_array = letter_pool_array.flatten | ||
|
||
drawn_letters = letter_pool_array.sample(10) | ||
return drawn_letters | ||
|
||
end | ||
|
||
def uses_available_letters? (input, letters_in_hand) | ||
|
||
copy_of_hand = letters_in_hand.clone | ||
letters_of_input = input.upcase.split("") | ||
|
||
letters_of_input.each do |letter_from_input| | ||
if copy_of_hand.include? letter_from_input | ||
|
||
index_of_input_letter = copy_of_hand.index(letter_from_input) | ||
copy_of_hand.delete_at(index_of_input_letter) | ||
|
||
else | ||
|
||
copy_of_hand = letters_in_hand.clone | ||
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 method looks great! I just wanted to name that this line isn't necessary as the |
||
return false | ||
|
||
end | ||
end | ||
|
||
return true | ||
|
||
end | ||
|
||
def score_word(word) | ||
|
||
letter_pool_hash = return_letter_pool_hash | ||
|
||
word_array = word.upcase.split("") | ||
|
||
word_points = word_array.map do |letter| | ||
letter_pool_hash[letter][:number_of_points] | ||
end | ||
|
||
if word_array.length >= 7 | ||
word_points << 8 | ||
end | ||
|
||
return word_points.sum | ||
|
||
end | ||
|
||
def highest_score_from(words) | ||
scores_hash = {} | ||
|
||
words.each do |word| | ||
scores_hash[word] = score_word(word) | ||
end | ||
|
||
highest_score = scores_hash.values.max | ||
highest_scores_hash = scores_hash.select do |word, score| | ||
score == highest_score | ||
end | ||
|
||
if highest_scores_hash.length == 1 | ||
return {word: highest_scores_hash.keys[0], score: highest_scores_hash.values[0]} | ||
else | ||
|
||
highest_scores_hash.each do |word, score| | ||
if word.length == 10 | ||
return {word: word, score: score} | ||
end | ||
end | ||
|
||
min_length = highest_scores_hash.map { |word, score| word.length }.min | ||
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 is a super clever and interesting way to do this! |
||
highest_scores_hash.each do |word, score| | ||
if word.length == min_length | ||
return {word: word, score: score} | ||
end | ||
end | ||
|
||
end | ||
end | ||
|
||
def is_in_english_dict?(input) | ||
|
||
dict_array = [] | ||
CSV.foreach("assets/dictionary-english.csv") do |row| | ||
dict_array << row | ||
end | ||
|
||
dict_array = dict_array.flatten | ||
|
||
answer = dict_array.include?(input.downcase) ? true : false | ||
|
||
return answer | ||
|
||
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.
This is great just as it is! I just wanted to mention for the future that this is usually a great example of when a global variable (ie.
LETTER_POOL_HASH = ...
). That way you wouldn't need to write a method and then gets called several times and a new copy of the hash wouldn't get created each time.