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

Michelle & Aurea - Edges - Adagrams #8

Open
wants to merge 16 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
161 changes: 161 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# adds csv gem
require "csv"

# initialize global variable
$words_hash = {}

# wave 1
def draw_letters
# draw letters

# letter pool
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').chars
Copy link

Choose a reason for hiding this comment

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

nice 👍


# randomize letters
# return the first 10 letters from randomized pool
return pool.shuffle.pop(10)
end

# wave 2
def uses_available_letters?(input, letters)
# input --> user's word (string)
# letters --> available letters in pool

# made a clone of letters; previously, we ran into memory location/duplication issues
# we don't want to alter letters since it needs to reset with all 10 pulled letters available upon each call
letters_copy = letters.clone
Copy link

Choose a reason for hiding this comment

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

We'll talk about reference vs. value in class formally soon. But also, I'm pretty curious, what side effects did you see if you didn't make a copy of letters?

Choose a reason for hiding this comment

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

If you input in an incorrect word, and then on the second try input in a valid word using any of the same letters, the program will not recognize it as valid because it modifies the hand of letters after the first attempt.

Copy link
Author

Choose a reason for hiding this comment

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

If I'm remembering correctly, we had the following issue if we didn't make a copy: if someone drew the letters "a", "b", "c", "d", "e", "f" for example and made the nonsense word "abc" then they would be asked to try again. But "a", "b", and "c" would have been mistakenly deleted from the list, leaving only "d", "e", and "f". So if they next tried the word "cab" they would get an error message. We also tried letters_copy = letters, but I think we figured out that they're pointing to the same place in memory because the "copy" would have the same issue.

Copy link

Choose a reason for hiding this comment

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

Right! Got it, makes sense. Thanks!


# check through each character; is it available from the letter pool?
input.upcase.chars.each do |char|
if letters_copy.include? char

# delete letter once user inputs it to avoid multiple checks on each tile
check_index = letters_copy.index(char)
letters_copy.delete_at(check_index)

else
# return false if user does NOT have available lettesr for input
return false
end
end
# return true if user has available letters for input
return true
end

# wave 3
def score_word(word)
# word --> string; user's input word

# create scoring chart
score_chart = {
"A" => 1,
"B" => 3,
"C" => 3,
"D" => 2,
"E" => 1,
"F" => 4,
"G" => 2,
"H" => 4,
"I" => 1,
"J" => 8,
"K" => 5,
"L" => 1,
"M" => 3,
"N" => 1,
"O" => 1,
"P" => 3,
"Q" => 10,
"R" => 1,
"S" => 1,
"T" => 1,
"U" => 1,
"V" => 4,
"W" => 4,
"X" => 8,
"Y" => 4,
"Z" => 10
}

# add scores together
score = word.upcase.chars.sum { |char| score_chart[char] }

# add eight extra points for words 7+ in length
if word.length >= 7
score += 8
end

# return score as int
return score
end

# wave 4
def highest_score_from(words)
# word --> string; user's input word

# initializing values
highest_score = 0
best_word = ''

# loop through each word in array of user input
words.each do |word|

if score_word(word) > highest_score

# if we have found the highest scoring word, set variable equal to that score and that word
highest_score = score_word(word)
best_word = word

# check for score ties AND check if the word lengths are unequal
elsif score_word(word) == highest_score && word.length != best_word.length

# check if word length is 10 -- OR...
# check that word length is less than best word's length AND that best word length does not equal 10
# if there is a tie between two equally high scoring words (and the current best word is not a length 10), then choose the shorter length
if (word.length == 10) || (word.length < best_word.length && best_word.length != 10)
highest_score = score_word(word)
best_word = word
end
end
end

# return hash containing highest scoring word AND the score for that word
return {word: best_word,
score: highest_score}
end


# wave 5 addition
def create_hash()
# we will create a hash for greater efficiency

# read csv file
english_words = CSV.open("assets/dictionary-english.csv", "r")

# iterate through each row in csv file
english_words.each do |row|
# each row is an array, so check the 0th index
# create hash key/value pairs: {key, value} --> {"WORD", 0}
$words_hash[row[0].upcase] = 0
end
end

# wave 5
def is_in_english_dict?(input)
# input --> string; user's input word

# if it returns a truthy value --> true
# otherwise, false
$words_hash.include?(input.upcase) ? true : false

# NOTE: we did this first, but decided to create a hash for retrieval efficiency and only load it once also for additional enhanced efficiency...
# english_words = CSV.open("assets/dictionary-english.csv", "r")
# english_words.each do |row|
# # if input.upcase == row[0].upcase
# return true
# end
# return false

end

# invoke method to create hash of English words
create_hash()
53 changes: 53 additions & 0 deletions specs/adagrams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,57 @@
expect(best_word[:score]).must_equal 18
end
end


describe 'is_in_english_dict? method' do

it 'returns true for valid english words from csv file' do

word = 'bladder'
result = is_in_english_dict?(word)
expect( result ).must_equal true

word = 'pierce'
result = is_in_english_dict?(word)
expect( result ).must_equal true

word = 'CoCoNUTs'
result = is_in_english_dict?(word)
expect( result ).must_equal true

word = 'XYLOPHONE'
result = is_in_english_dict?(word)
expect( result ).must_equal true

end


it 'returns false for gibberish' do

word = "jfdks"
result = is_in_english_dict?(word)
expect(result).must_equal false

word = "SSSSSSSS"
result = is_in_english_dict?(word)
expect(result).must_equal false

word = "iIiIi"
result = is_in_english_dict?(word)
expect(result).must_equal false

word = "pupppy"
result = is_in_english_dict?(word)
expect(result).must_equal false

end

it 'returns false if empty string' do

word = ""
result = is_in_english_dict?(word)
expect(result).must_equal false

end
end
end
4 changes: 4 additions & 0 deletions wave-5-game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ def run_game

while ( (!(uses_available_letters?(user_input_word, letter_bank)) ) || (!(is_in_english_dict?(user_input_word))) )

# puts "Beginning of while loop, letter_bank = #{letter_bank}"
if !(uses_available_letters?(user_input_word, letter_bank))
display_not_in_letter_bank_message
elsif !(is_in_english_dict?(user_input_word))
display_not_in_dictionary_message
end

# puts "End of while loop, letter_bank = #{letter_bank}"

user_input_word = get_user_input
end

Expand Down