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

Naheed & Sigrid - Edges - Adagrams #5

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
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
Binary file added .DS_Store
Binary file not shown.
114 changes: 114 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# require 'pry'
require 'csv'

# Draws 10 random letters from alphabetic array
def draw_letters
letters_concatenated = []
hand = []

initial_array = [["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]]

Choose a reason for hiding this comment

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

I like the idea of this data structure, but I think you could simplify it a bit by using a hash, where the keys are letters and the values are counts. Something like:

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

initial_array.each do |x|
letters_concatenated.concat([x[0]] * x[1])
end

hand = letters_concatenated.sample(10)

return hand

end

# Determines if user word uses letters in hand
def uses_available_letters?(input, letters_in_hand)
word = input.chars
word.each do |letter|
if letters_in_hand.include?(letter)
letters_in_hand.delete_at(letters_in_hand.index(letter))

Choose a reason for hiding this comment

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

This isn't something we checked for in the tests, but it's worth pointing out that this method is destructive. By calling it, you modify the array letters_in_hand. To me this feels like an unexpected behavior - it's not obvious that "check whether this word is made up of letters in this hand" is a process that will change the hand itself. Unexpected behaviors are often where bugs can creep in, especially when there's more than one person working on something.

else
return false
end
end
return true
end

# Calculates score for user word
def score_word(word)
score = 0
score_word = word.upcase.chars
score_word.each do |letter|
if %w[A E I O U L N R S T].include?(letter)
score += 1
elsif %w[D G].include?(letter)
score += 2

Choose a reason for hiding this comment

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

While using a big if statement like this works just fine, the information about letter scores is trapped in this code. If you wanted to use that information elsewhere (for example, to tell the user what the letters in their hand are worth) you would have to repeat it.

Instead, it might work well to store the data 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]

elsif %w[B C M P].include?(letter)
score += 3
elsif %w[F H V W Y].include?(letter)
score += 4
elsif %w[K].include?(letter)
score += 5
elsif %w[J X].include?(letter)
score += 8
elsif %w[Q Z].include?(letter)
score += 10
end
end

if score_word.length >= 7 && score_word.length <= 10
score += 8
end
return score
end

# Determines highest scoring word and returns both word and score
def highest_score_from(words)
array = []
max_score = 0
winning_word = ""
max_values = []
min_length = 10
hash = {}

Choose a reason for hiding this comment

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

The variable names array and hash here could use some improvement. While technically correct, they don't give the reader any information about how they're used or what sort of data they store. Instead you might use words_with_scores and best_word_and_score.


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

array.each do |hash|
if hash[:score] == max_score
winning_word = hash[:word]
max_values << {word: winning_word, score: max_score}
elsif hash[:score] > max_score
max_values = []
max_score = hash[:score]
winning_word = hash[:word]
max_values << {word: winning_word, score: max_score}
end
end

max_values.each do |array|
if array[:word].length == 10
hash = {word: array[:word], score: array[:score].to_i}
return hash
end
if array[:word].length < min_length
min_length = array[:word].length
winning_word = array[:word]
max_score = array[:score]
hash = {word: winning_word, score: max_score}
end
end
return hash
end

# Determines whether users word is a valid English word
def is_in_english_dict?(input)
check = false
dictionary = CSV.open('assets/dictionary-english.csv')
word = input.downcase

Choose a reason for hiding this comment

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

Good work on the optional!

dictionary.each do |row|
if row.include?(word)
check = true
end
end
return check
end
15 changes: 15 additions & 0 deletions specs/adagrams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,19 @@
expect(best_word[:score]).must_equal 18
end
end

describe 'in English dictionary' do

it 'returns true if user input is in English dictionary' do
input = 'DOG'

expect(is_in_english_dict?(input)).must_equal true
end

it 'returns false if user input is not in Enlish dictionary' do

input = 'RRRRR'
expect(is_in_english_dict?(input)).must_equal false
end
end
end
23 changes: 23 additions & 0 deletions wave-1-game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,27 @@ def run_game
display_drawn_letters(draw_letters)
end

def draw_letters
letter_pool = []
letters_concatenated = []
hand = []

initial_array = [["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]]

letter_pool = initial_array.map do |x|
[x[0]] * x[1]
end

letter_pool.each do |x|
letters_concatenated.concat(x)
end

10.times do |x|
hand << letters_concatenated.sample
end

return hand

end

run_game