Skip to content
Open
Show file tree
Hide file tree
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
85 changes: 85 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require 'csv'

# A method to build a hand of 10 letters for the user.
def draw_letters
# Letters A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
letter_dist = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1]
hand_size = 10
# ASCII table number for a
letter_a_offset = 65

letter_pool = letter_dist.map.with_index { |dist, index| (index + letter_a_offset).chr * dist }
return letter_pool.join('').split('').sample(hand_size)
end

# A method to check if the word is an anagram of some or all of the given letters in the hand.
def uses_available_letters?(input, letter_in_hand)
hand_copy = letter_in_hand.clone
input.upcase.split("").each do |letter|
if hand_copy.include?(letter)
hand_copy.delete_at(hand_copy.index(letter))
else
return false
end
end
return true
end

# A method that returns the score of a given word as defined by the Adagrams game.
def score_word(word)
letter_values = word.upcase.split("").map do |letter|
case letter
when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
1
when "D", "G"
2
when "B", "C", "M", "P"
3
when "F", "H", "V", "W", "Y"
4
when "K"
5
when "J", "X"
8
when "Q", "Z"
10
end
end

if word.length >= 7 && word.length <= 10
letter_values << 8
end

return letter_values.sum
end

# A method looks at the array of words and calculates which of these words has the highest score.
def highest_score_from(words)
maximum_score = words.map { |word| score_word(word) }.max
highest = words.select { |word| score_word(word) == maximum_score }
if highest.length == 1
winning_word = highest.first
else
highest_lengths = highest.map {|i| i.length}
if highest_lengths.any? { |x| x == 10}
index_of_length_10 = highest_lengths.find_index(10)
winning_word = highest[index_of_length_10]
else
winning_word = highest[highest_lengths.find_index(highest_lengths.min)]
end
end

results = Hash.new
results[:score] = maximum_score
results[:word] = winning_word

return results
end

def is_in_english_dict?(input)
dict_path = File.join(File.dirname(__FILE__), "../assets/dictionary-english.csv")
dictionary = CSV.read(dict_path, headers: true).map { |row| row[0] }
return dictionary.include?(input)
end


16 changes: 8 additions & 8 deletions wave-2-game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,28 @@ def get_user_input

def run_game
display_welcome_message

should_continue = true

while should_continue
puts "Let's draw 10 letters from the letter pool..."

letter_bank = draw_letters
display_drawn_letters(letter_bank)

display_game_instructions

user_input_word = get_user_input

while ( !(uses_available_letters?(user_input_word, letter_bank)) )
display_needs_valid_input_message
user_input_word = get_user_input
end

display_retry_instructions
should_continue = get_user_input == "y"
end

display_goodbye_message
end

Expand Down