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

def draw_letters()
letter_pool = ["A"] * 9 + ["B"] * 2 + ["C"] * 2 + ["D"] * 4 + ["E"] * 12 + ["F"] * 2 + ["G"] * 3 + ["H"] * 2 + ["I"] * 9 + ["J"] + ["K"] + ["L"] * 4 + ["M"] * 2 + ["N"] * 6 + ["O"] * 8 + ["P"] * 2 + ["Q"] + ["R"] * 6 + ["S"] * 4 + ["T"] * 6 + ["U"] * 4 + ["V"] * 2 + ["W"] * 2 + ["X"] + ["Y"] * 2 + ["Z"]

Choose a reason for hiding this comment

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

Consider using a data structure to store the letter distribution information. You can then use this data structure to create your array.

letter_counts = {"A"=>9, "B"=>2,...}

hand = []

# Shuffling once resulted in too many duplicates.
# The program shuffles multiple times.
10.times do
letter_pool.shuffle!
hand << letter_pool.pop
end

return hand
end

def uses_available_letters?(input, letters_in_hand)
split = input.upcase.split("")
copy = letters_in_hand.dup

split.each do |letter|
if copy.include? (letter)
copy.delete_at(copy.index(letter))
else
return false
end
end

return true
end

def score_word(word)
word_arr = word.upcase.split("")
points = 0
word_arr.each do |letter|

Choose a reason for hiding this comment

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

While this case statement works, it means that the information about which letter has which score is locked into this piece of code, and can't easily be used elsewhere. For example, if you wanted to display the value of each letter in a hand, you would need to repeat this work.

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 LETTER_SCORES[letter].

case letter
when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
points += 1
when "D", "G"
points += 2
when "B", "C", "M", "P"
points += 3
when "F", "H", "V", "W", "Y"
points += 4
when "K"
points += 5
when "J", "X"
points += 8
when "Q", "Z"
points += 10
end
end

if word_arr.length >= 7
points += 8
end

return points
end

def highest_score_from(words)
word_score = []

words.each do |word|
word_score << { word: word, score: score_word(word) }
end

max = word_score.max_by { |element| element[:score] }[:score]

Choose a reason for hiding this comment

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

You've packed lots of logic in a few lines of elegant code. Consider adding a few comments that outline the rules for determining the winner, particularly the tiebreaker logic.

winners = word_score.select { |element| element[:score] == max }
ten_char = winners.select { |element| element[:word].length == 10 }

if ten_char.length > 0
return ten_char.first
else
min_winners = winners.min_by { |element| element[:word].length }
return min_winners
end
end

def is_in_english_dict?(input)
dictionary = CSV.read("assets/dictionary-english.csv")

if dictionary.include? [input]
return true
else
return false
end
end