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

adagrams #20

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
Binary file added .DS_Store
Binary file not shown.
84 changes: 84 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require "pry"
SCORE_TO_LETTERS = {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"]}
LETTERS_TO_QUANTITY = {"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}
def draw_letters
letter_bank = Array.new
10.times do
random_letter_picked = LETTERS_TO_QUANTITY.keys.sample

Choose a reason for hiding this comment

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

So, you have a probability problem here. When you sample from the keys, the odds are not weighted by the number of each tile in the letter_bank. Your results give a 1/26 chance to draw any letter, but that's not accurate is it?

letter_bank << random_letter_picked
while letter_bank.count(random_letter_picked) > LETTERS_TO_QUANTITY[random_letter_picked]
letter_bank.pop #what happens if you pop out letter and put back in the same letter from the next line
random_letter_picked = LETTERS_TO_QUANTITY.keys.sample
letter_bank << random_letter_picked
end
end
return letter_bank
end


def letters_to_hash(letters)
letter_to_quantity = Hash.new(0)
letters.each do |letter|
letter_to_quantity[letter] +=1
end
return letter_to_quantity
end

def uses_available_letters?(user_input_word, letter_bank)
array_of_input_letters = user_input_word.split('')
letter_bank_hash = letters_to_hash(letter_bank)

Choose a reason for hiding this comment

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

Is there any reason you couldn't have done this with 2 arrays?

user_input_letters_hash = letters_to_hash(array_of_input_letters)
output = true
user_input_letters_hash.each do |key,value|
if !letter_bank_hash.keys.include?(key) # look at looping of outputs, see what happens if use

Choose a reason for hiding this comment

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

As soon as one of these is false, do you need to check the whole hash still?

output = false
elsif user_input_letters_hash[key] > letter_bank_hash[key]
output= false
end
end
return output
end

def score_word(user_input_word) #String
if user_input_word.length == 0
total_score = 0
else
array_of_input_letters = user_input_word.downcase.split("")
user_input_word_hash = letters_to_hash(array_of_input_letters)
array_of_letter_scores = []
user_input_word_hash.each do |word_key, word_value|
SCORE_TO_LETTERS.each do |score_key, score_value|
if score_value.include?(word_key)
score = score_key*word_value

Choose a reason for hiding this comment

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

It took me a minute to see why this works. Again, using a hash here is actually more work for you, and hindering clarity.

array_of_letter_scores.push(score)
end
end
end
total_score = array_of_letter_scores.reduce(:+)

Choose a reason for hiding this comment

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

reduce is hiding a loop from you, though you could have used reduce to solve this problem by replacing an earlier .each.

if user_input_word.length >= 7 && user_input_word.length < 11
total_score += 8
end
return total_score
end
end
#played_words = ['BBBBBB', 'AAAAAAAAAA']

def highest_score_from(played_words)
collection_of_played_words_and_scores = []#array of hashes
played_words.each do |word|

Choose a reason for hiding this comment

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

nice! I love this, very clear!

played_word_to_score = {} #how to deal with word duplicates
played_word_to_score[:word] = word
played_word_to_score[:score] = score_word(word)
collection_of_played_words_and_scores.push(played_word_to_score)
end
top_score = collection_of_played_words_and_scores.reduce(0){ |memo, h| h[:score] > memo ? h[:score] : memo } # ==> {score: n}

Choose a reason for hiding this comment

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

Yum, this is also great!

tied_highest_scores = collection_of_played_words_and_scores.select {|h| h[:score].to_i == top_score} #highest_score[:score]} # all the hashes in an array
highest_score_hash = tied_highest_scores.find {|h| h[:word].length == 10}
if highest_score_hash == nil
highest_score_hash = tied_highest_scores.min_by{|h| h[:word].length}
end
return highest_score_hash #supposed to be a hash
end
Binary file added specs/.DS_Store
Binary file not shown.
21 changes: 11 additions & 10 deletions specs/adagrams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

describe 'Adagrams' do
describe 'draw_letters method' do
it 'draws ten letters from the letter pool' do
it '1) draws ten letters from the letter pool' do
drawn_letters = draw_letters
expect(drawn_letters.size).must_equal 10
end

it 'returns an array, and each item is a single-letter string' do
it '2)) returns an array, and each item is a single-letter string' do
drawn_letters = draw_letters
expect(drawn_letters.size).must_equal 10

Expand All @@ -28,7 +28,7 @@

describe 'uses_available_letters? method' do

it 'returns true if the submitted letters are valid against the drawn letters' do
it '1) returns true if the submitted letters are valid against the drawn letters' do
drawn_letters = ['D', 'O', 'G', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
test_word = 'DOG'

Expand All @@ -37,7 +37,7 @@
expect(is_valid).must_equal true
end

it 'returns false word contains letters not in the drawn letters' do
it '2) returns false word contains letters not in the drawn letters' do
drawn_letters = ['D', 'O', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
test_word = 'DOG'

Expand All @@ -46,7 +46,7 @@
expect(is_valid).must_equal false
end

it 'returns false word contains repeated letters more than in the drawn letters' do
it '3) returns false word contains repeated letters more than in the drawn letters' do
drawn_letters = ['A', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
test_word = 'AAA'

Expand All @@ -58,23 +58,24 @@
end

describe 'score_word method' do
it 'returns an accurate numerical score according to the score chart' do
it '1) returns an accurate numerical score according to the score chart' do
expect(score_word("A")).must_equal 1
expect(score_word("DOG")).must_equal 5
expect(score_word("WHIMSY")).must_equal 17
end

it 'returns a score regardless of input case' do
it '2) returns a score regardless of input case' do
expect(score_word("a")).must_equal 1
expect(score_word("dog")).must_equal 5
expect(score_word("wHiMsY")).must_equal 17
end

it 'returns a score of 0 if given an empty input' do
it '3) returns a score of 0 if given an empty input' do
# FAIL
expect(score_word("")).must_equal 0
end

it 'adds an extra 8 points if the word is 7 or more characters long' do
it '4) adds an extra 8 points if the word is 7 or more characters long' do
expect(score_word("XXXXXXX")).must_equal 64
expect(score_word("XXXXXXXX")).must_equal 72
expect(score_word("XXXXXXXXX")).must_equal 80
Expand Down Expand Up @@ -168,4 +169,4 @@
expect(best_word[:score]).must_equal 18
end
end
end
end