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

Trang & Cassy - Adagram #7

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
90a2020
Create letters data structure.
tfrego Aug 13, 2018
1b72b8a
Created outline for drawn letters method. Created array of available …
tfrego Aug 13, 2018
e81b911
Removed draw_random_letter method, added times loop to draw random le…
tfrego Aug 13, 2018
a4e902f
Created outline of uses_available_letters? method
tfrego Aug 13, 2018
00326d0
create hash of anagram letters as key and qty as value
tfrego Aug 14, 2018
8c707e1
Created method to return false if letter is not in hand
tfrego Aug 14, 2018
33830e8
validated user input against letters in hand
tfrego Aug 15, 2018
c74bc44
Made an outline of score_word
tfrego Aug 15, 2018
8359668
add case statement for scoring and bonus points for long word
tfrego Aug 15, 2018
8ed855e
create hash of word scores
tfrego Aug 15, 2018
688204e
Updated method to select all words with max score
tfrego Aug 15, 2018
03dfc16
determine if there's a tie, add pseudocode for applying tie-breaking …
tfrego Aug 15, 2018
17e9058
Revised program so potential winning hashes include key/value pair fo…
tfrego Aug 15, 2018
bf9239e
Adjusted method to select winning words with length of 10
tfrego Aug 15, 2018
2bd1aed
add if statement to assign winner to first word of length 10
tfrego Aug 15, 2018
82128e0
add min_by for potential winners
tfrego Aug 15, 2018
0717e47
Refactored code and revised comments
tfrego Aug 15, 2018
7c95153
outlined method for is_in_english_dict?
tfrego Aug 17, 2018
a764b00
placed input into array and used .any? to check if in dictionary
tfrego Aug 17, 2018
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
122 changes: 122 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@

require 'awesome_print'
require 'csv'

# Draws letters and returns an array of 10 strings
def draw_letters
# Empty array to hold drawn letters
drawn_letters = []
# Array of all the letters available to be drawn
available_letters = %w(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)
# Randomly draw letter
10.times do
# Assign random letter
letter = available_letters.sample
# Push random letter
drawn_letters << letter
# Delete drawn letter from available_letters
available_letters.delete_at(available_letters.index(letter))
end
return drawn_letters
end

# Validates input against letters_in_hand
def uses_available_letters?(input, letters_in_hand)
# Capitalizing input
input.upcase!
# Empty array to hold results
letter_results = []
# Validating quantitiy
input.chars.each do |letter|
if input.count(letter) > letters_in_hand.count(letter)
letter_results << false

Choose a reason for hiding this comment

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

As soon as you know one is false, do you need to see the rest of the letters?

else
letter_results << true
end
end
# Returns true if input quantity is valid
return letter_results.all?(true)

Choose a reason for hiding this comment

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

This is hiding a second loop from you!

end

# Scores word
def score_word(word)
# Starting score at 0
score = 0
# Capitalizing word
word.upcase!

# Updating score based on letter
word.chars.each do |letter|
case letter
when 'A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'
score += 1
when 'D', 'G'
score += 2
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
# Adding bonus points based on word length
score += 8 if word.chars.length.between?(7,10)
return score
end

# Returning winning word/score
def highest_score_from(words)
# Makes an array of words and associated score
word_scores = words.map { |word| {:word => word, :score => score_word(word)} }

# Assignign the top score value
top_score = word_scores.max_by{ |k| k[:score] } [:score]

# Selecting hashes in word_scores that have winning score
potential_winners = word_scores.select { |h| h[:score] == top_score }

# Checking length of potential winners array
if potential_winners.length > 1

# If greater than one, there's a tie
potential_winners.each do |potential_winner|
# Adding word lengths to potential winner hash
potential_winner[:word_length] = potential_winner[:word].length
end

# Creating an array if word length is 10
length_of_10 = potential_winners.select { |h| h[:word_length] == 10 }

# Checking if length_of_10 is not empty
if length_of_10.length > 0
# Returning first word/score in the length_of_10 array
return { :word => length_of_10[0][:word], :score => length_of_10[0][:score]}
else

Choose a reason for hiding this comment

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

The logic after here is kind of hairy, could have benefited from a longer comment.

# Returning first word/score and rejecting word_length key/value pair
return potential_winners.min_by { |potential_winner| potential_winner[:word_length]}\
.reject! { |k| k == :word_length
}
end
else
# Returning the value of potential_winners if there is no tie
return potential_winners[0]
end
end

# Verify if word exists in English dictionary
def is_in_english_dict?(input)
# makes input an array of one element
input.downcase!
word = [input]
# return array of arrays of dictionary words
dictionary = CSV.read('assets/dictionary-english.csv')
# checks dictionary for word
return dictionary.any?(word)
end