Skip to content
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ To do so, add a method called `uses_available_letters?` in `adagrams.rb`. This m
- Returns `true` if every letter in the `input` word is available (in the right quantities) in the `letters_in_hand`
- Returns `false` if not; if there is a letter in `input` that is not present in the `letters_in_hand` or has too much of compared to the `letters_in_hand`

<!---### Wave 3
### Wave 3

We want a method that returns the score of a given word as defined by the Adagrams game.

Expand Down Expand Up @@ -192,7 +192,6 @@ Add a method called `is_in_english_dict?` in `adagrams.rb`. This method should h
There are no unit tests provided for this wave, but there is driver code found in `wave-5-game.rb`. Feel free to alter `specs/adagrams_sepc.rb` and add some!

Nota Bene: The original data for all of the alpha words of the English dictionary was found freely available at [`dwyl/english-word`'s repo](https://github.com/dwyl/english-words), and was modified to only include words under 10 characters.
--->

## What Instructors Are Looking For
Check out the [feedback template](feedback.md) which lists the items instructors will be looking for as they evaluate your project.
114 changes: 114 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
def draw_letters
def create_letter_array(letter, amount)
letter_array = []
amount.times do
letter_array << letter
end
return letter_array
end

letter_bag = create_letter_array("A", 9) + create_letter_array("B", 2) + create_letter_array("C", 2) + create_letter_array("D", 4) + create_letter_array("E", 12) + create_letter_array("F", 2) + create_letter_array("G", 3) + create_letter_array("H", 2) + create_letter_array("I", 9) + create_letter_array("J", 1) + create_letter_array("K", 1) + create_letter_array("L", 4) + create_letter_array("M", 2) + create_letter_array("N", 6) + create_letter_array("O", 8) + create_letter_array("P", 2) + create_letter_array("Q", 1) + create_letter_array("R", 6) + create_letter_array("S", 4) + create_letter_array("T", 6) + create_letter_array("U", 4) + create_letter_array("V", 2) + create_letter_array("W", 2) + create_letter_array("X", 1) + create_letter_array("Y", 2) + create_letter_array("Z", 1)

Choose a reason for hiding this comment

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

I'd suggest having a hash with letters and the amount of each letter and using that to generate the letter bag instead of this. Less typing.

return letter_bag.sample(10)
end

def uses_available_letters?(input, letters_in_hand)
new_array = letters_in_hand.clone

Choose a reason for hiding this comment

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

Very clever to use .clone that way you avoid destroying elements of letters_in_hand.

input_to_array = input.upcase.split("")

input_to_array.each do |letter|
if new_array.include?(letter)
new_array.delete_at(new_array.index(letter))
else
return false
end
end
return true
end

def score_word(word)
point_value = [

Choose a reason for hiding this comment

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

Excellent data structure!

{
letters: ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
values: 1,
},
{
letters: ["D", "G"],
values: 2,
},
{
letters: ["B", "C", "M", "P"],
values: 3,
},
{
letters: ["F", "H", "V", "W", "Y"],
values: 4,
},
{
letters: ["K"],
values: 5,
},
{
letters: ["J", "X"],
values: 8,
},
{
letters: ["Q", "Z"],
values: 10,
},
]

word_score = 0
arr_word = word.upcase.split("")
letter_score = 0
arr_word.each do |letter|
point_value.each do |pair|
if pair[:letters].include?(letter)
letter_score = pair[:values]
end
end
word_score += letter_score
end
if arr_word.length > 6
word_score += 8
end
return word_score
end

def highest_score_from(words)
words_with_scores_array = []
score_of_words = words.map do |word|
words_with_scores_array << {
word: word,
score: score_word(word),
}
end
winning_word = words_with_scores_array.max_by do |word_score_hash|
word_score_hash[:score]
end
winners = []
words_with_scores_array.each do |word|
if word[:score] == winning_word[:score]
winners << word
end
end
if winners.length == 1
return winning_word
else # if there's a tie

Choose a reason for hiding this comment

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

Your indentation is a bit of a mess here.

Choose a reason for hiding this comment

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

Hey Chris, thanks so much for your review & all your feedback -- it's much appreciated! Could you clarify this comment a little more? I'm looking through and trying to see where the indentation could be fixed so that I don't make this mistake again.

winners.each do |winner|
return winner if winner[:word].length == 10
end
return winners.min_by do |winner|
winner[:word].length
end
end
end

def is_in_english_dict?(input)
require "csv"
dictionary_array = []
CSV.foreach("assets/dictionary-english.csv") do |row|
dictionary_array << row
end
dictionary_array.flatten!
return dictionary_array.include?(input) ? true : false
end
19 changes: 19 additions & 0 deletions lib/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# array_of_letters = ["d", "o", "o", "g"]
# string = "dog"
# string_to_array = string.split("")

# leftovers = array_of_letters - string_to_array

# if (array_of_letters.length - string.length) == leftovers.length
# puts "all the letters were available"
# else
# puts "they weren't"
# end

longest = %w{ cat sheep bear }.reduce do |memo, word|
puts "memo: #{memo}"
puts "word: #{word}"
memo.length > word.length ? memo : word
end

puts longest
91 changes: 44 additions & 47 deletions specs/adagrams_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"

require_relative '../lib/adagrams'
require_relative "../lib/adagrams"

# Get that nice colorized output
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe 'Adagrams' do
describe 'draw_letters method' do
it 'draws ten letters from the letter pool' do
describe "Adagrams" do
describe "draw_letters method" do
it "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 "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 @@ -26,137 +26,134 @@
end
end

describe 'uses_available_letters? method' do

it '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'
describe "uses_available_letters? method" do
it "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"

is_valid = uses_available_letters? test_word, drawn_letters

expect(is_valid).must_equal true
end

it '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'
it "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"

is_valid = uses_available_letters? test_word, drawn_letters

expect(is_valid).must_equal false
end

it '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'
it "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"

is_valid = uses_available_letters? test_word, drawn_letters

expect(is_valid).must_equal false
end

end

describe 'score_word method' do
it 'returns an accurate numerical score according to the score chart' do
describe "score_word method" do
it "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 "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 "returns a score of 0 if given an empty input" do
expect(score_word("")).must_equal 0
end

it 'adds an extra 8 points if the word is 7 or more characters long' do
it "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
end
end

describe 'highest_score_from method' do
it 'returns a hash that contains the word and score of best word in an array' do
words = ['X', 'XX', 'XXX', 'XXXX']
describe "highest_score_from method" do
it "returns a hash that contains the word and score of best word in an array" do
words = ["X", "XX", "XXX", "XXXX"]
best_word = highest_score_from words

expect(best_word[:word]).must_equal 'XXXX'
expect(best_word[:word]).must_equal "XXXX"
expect(best_word[:score]).must_equal 32
end

it 'accurately finds best scoring word even if not sorted' do
words = ['XXX', 'XXXX', 'XX', 'X']
it "accurately finds best scoring word even if not sorted" do
words = ["XXX", "XXXX", "XX", "X"]
best_word = highest_score_from words

expect(best_word[:word]).must_equal 'XXXX'
expect(best_word[:word]).must_equal "XXXX"
expect(best_word[:score]).must_equal 32
end

it 'in case of tied score, prefers the word with fewer letters' do
it "in case of tied score, prefers the word with fewer letters" do
# the character 'M' is worth 3 points, 'W' is 4 points
words = ['MMMM', 'WWW']
words = ["MMMM", "WWW"]

# verify both have a score of 12
expect(score_word(words.first)).must_equal 12
expect(score_word(words.last)).must_equal 12

best_word = highest_score_from words

expect(best_word[:word]).must_equal 'WWW'
expect(best_word[:word]).must_equal "WWW"
expect(best_word[:score]).must_equal 12
end

it 'in case of tied score, prefers the word with fewer letters regardless of order' do
it "in case of tied score, prefers the word with fewer letters regardless of order" do
# the character 'M' is worth 3 points, 'W' is 4 points
words = ['WWW', 'MMMM']
words = ["WWW", "MMMM"]

# verify both have a score of 12
expect(score_word(words.first)).must_equal 12
expect(score_word(words.last)).must_equal 12

best_word = highest_score_from words

expect(best_word[:word]).must_equal 'WWW'
expect(best_word[:word]).must_equal "WWW"
expect(best_word[:score]).must_equal 12
end

it 'in case of tied score, prefers most the word with 10 letters' do
it "in case of tied score, prefers most the word with 10 letters" do
# the character 'A' is worth 1 point, 'B' is 3 points
words = ['AAAAAAAAAA', 'BBBBBB']
words = ["AAAAAAAAAA", "BBBBBB"]

# verify both have a score of 10
expect(score_word(words.first)).must_equal 18
expect(score_word(words.last)).must_equal 18

best_word = highest_score_from words

expect(best_word[:word]).must_equal 'AAAAAAAAAA'
expect(best_word[:word]).must_equal "AAAAAAAAAA"
expect(best_word[:score]).must_equal 18
end

it 'in case of tied score, prefers most the word with 10 letters regardless of order' do
it "in case of tied score, prefers most the word with 10 letters regardless of order" do
# the character 'A' is worth 1 point, 'B' is 3 points
words = ['BBBBBB', 'AAAAAAAAAA']
words = ["BBBBBB", "AAAAAAAAAA"]

# verify both have a score of 10
expect(score_word(words.first)).must_equal 18
expect(score_word(words.last)).must_equal 18

best_word = highest_score_from words

expect(best_word[:word]).must_equal 'AAAAAAAAAA'
expect(best_word[:word]).must_equal "AAAAAAAAAA"
expect(best_word[:score]).must_equal 18
end

it 'in case of tied score and same length words, prefers the first word' do
it "in case of tied score and same length words, prefers the first word" do
# the character 'A' is worth 1 point, 'E' is 1 point
words = ['AAAAAAAAAA', 'EEEEEEEEEE']
words = ["AAAAAAAAAA", "EEEEEEEEEE"]

# verify both have a score of 10
expect(score_word(words.first)).must_equal 18
Expand Down
4 changes: 2 additions & 2 deletions wave-1-game.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative 'lib/adagrams'
require_relative "lib/adagrams"

def display_welcome_message
puts "Welcome to Adagrams!"
Expand All @@ -7,7 +7,7 @@ def display_welcome_message

def display_drawn_letters(letters)
puts "You have drawn the letters:"
puts letters.join(', ')
puts letters.join(", ")
end

def run_game
Expand Down
6 changes: 3 additions & 3 deletions wave-2-game.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require_relative 'lib/adagrams'
require_relative "lib/adagrams"

def display_welcome_message
puts "Welcome to Adagrams!"
end

def display_drawn_letters(letters)
puts "You have drawn the letters:"
puts letters.join(', ')
puts letters.join(", ")
end

def display_game_instructions
Expand Down Expand Up @@ -50,7 +50,7 @@ def run_game

user_input_word = get_user_input

while ( !(uses_available_letters?(user_input_word, letter_bank)) )
while (!(uses_available_letters?(user_input_word, letter_bank)))
display_needs_valid_input_message
user_input_word = get_user_input
end
Expand Down