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

Katrina and Hayden's Adagrams #17

Open
wants to merge 12 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
125 changes: 125 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

require 'csv'

# require_relative '../assets/dictionary-english.csv'

def draw_letters
letter_pool = %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
)

player_hand = letter_pool.sample(10)

return player_hand
end


def uses_available_letters?(input, letters_in_hand)
letter_match_counter = 0
temp_letters_in_hand = letters_in_hand.dup

input.chars.each do |letter|
if temp_letters_in_hand.include?(letter.upcase)
temp_letters_in_hand.delete_at(temp_letters_in_hand.index(letter.upcase))
letter_match_counter += 1
end
end
Copy link

Choose a reason for hiding this comment

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

Clever way of realizing that counting all "successful matches" with letter_match_counter should be the same size as the input!

I'll leave this as a "vague thing" to think about... I think that while you iterate through input.chars, you can actually determine that uses_available_letters? can return false before the loop finishes.

Remember, when you use the return keyword, it will kick the program execution out of the method early, and return whatever value you give it.


if letter_match_counter == input.length
return true
else
return false
end
Copy link

Choose a reason for hiding this comment

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

Is there a way you could shorten this set of if/else conditionals with a postfix conditional or a ternary?

end


def score_word(word)
word_score = 0
letter_score = {
1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2 => ["D", "G"],
3 => ["B", "C", "M", "P"],
4 => ["F", "H", "V", "W", "Y"],
5 => ["K"],
8 => ["J", "X"],
10 => ["Q", "Z"]
}

word.chars.each do |letter|
letter_score.each do |point_value, letters|
if letters.include?(letter.upcase)
word_score += point_value
end
end
end


if word.length >= 7
word_score += 8
end

return word_score
end


def highest_score_from(words)
highest_points_word = nil
score = 0

words.each do |word|
if score_word(word) > score
highest_points_word = word
score = score_word(word)

elsif score_word(word) == score
if word.length == 10 && word.length != highest_points_word.length
highest_points_word = word
elsif word.length < highest_points_word.length && highest_points_word.length != 10
highest_points_word = word
end
end
end

winning_word = {word: highest_points_word, score: score}
return winning_word

end


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

dictionary.each do |word|
if word.include?(input)
verify = true
end
end
Copy link

Choose a reason for hiding this comment

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

Similar to a comment above but...

"Vague thing" to think about...While you iterate through dictionary, you can actually determine that is_in_english_dict? can return true before the loop finishes.

Remember, when you use the return keyword, it will kick the program execution out of the method early, and return whatever value you give it.


return verify
end
26 changes: 26 additions & 0 deletions specs/adagrams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,30 @@
expect(best_word[:score]).must_equal 18
end
end

describe 'is_in_english_dict? method' do
it 'verifies that valid input matches an entry in the dictionary CSV file' do
input = "codeine"

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

it 'verifies that invalid word input returns false' do
input = "poopydiscoopscoopdiddywhoopwhoopdiscoopdipoop"

expect(is_in_english_dict?(input)).must_equal false
Copy link

Choose a reason for hiding this comment

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

👍

end

it 'verifies that first entry in the dictionary CSV file is read properly' do
input = "Word"

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

it 'verifies that first entry in the dictionary CSV file is read properly' do
Copy link

Choose a reason for hiding this comment

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

me being nitpicky: this test is not named accurately!

input = "zwitterion"

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