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 Submission - Danielle & Alice #2

Open
wants to merge 17 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
Empty file added adagrams.rb
Empty file.
157 changes: 157 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
#Adagrams
#Danielle & Alice

require 'csv'

#Wave 1
#Picks 10 letters from the letter pool
def draw_letters
letters = {
"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
}

#Creates the array of all the letters available to pick
letter_probability = []
letters.each do |key,value|
until value == 0
letter_probability << key
value -= 1
end
end

#Select letters and replace with 0 once they are picked
your_hand = []
count = 0
while count < 10 do
index = rand(0...letter_probability.length)
if letter_probability[index] != 0

Choose a reason for hiding this comment

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

So, this works, but you end up with an array that becomes more and more likely to "bounce" as you pick letters from the pile. Instead of nulling the value at the index, why not remove just that element from the array? That way, you don't waste loops through on no-ops (no operation).

your_hand << letter_probability [index]
count += 1
letter_probability[index] = 0
end
end
return your_hand
end

#Wave 2
#Checks that the letters for the word you picked are in your hand
def uses_available_letters?(input, letters_in_hand)
input = input.upcase.split("")
value = []
if input.length > letters_in_hand.length
return false
else
input.each do |letter|
input.count(letter) > letters_in_hand.count(letter) ? value << false : value << true

Choose a reason for hiding this comment

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

You are doing more work than you need to! As soon as one of these checks comes up false, you can return false, right? The only case that requires you to complete the loop is if every letter matches!

end
end
value.all? true ? true : false

Choose a reason for hiding this comment

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

value.all? is hiding a second loop from you!

end

#Wave 3
#Returns score of the word
def score_word (word)
word = word.upcase.split("")
score = 0
word.each do |letter|
case letter
when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
score += 1
when "D", "G"
score += 2
when "B", "C", "M", "P"
score += 3
when "F", "H", "V", "W", "Y"
score += 4
when "K"
score += 5
when "J", "X"
score += 8
when "Q", "Z"
score += 10
end
end

score += 8 if word.length >= 7

return score
end

#Wave 4
#Creates the hash for the winning word
def highest_score (word, score)
highest_score = {}
highest_score[:word] = word
highest_score[:score] = score
return highest_score
end

#Returns the hash of the winning word (:word & :score) and takes into account word length and ties for an array of words
def highest_score_from words
scores = {}
words.each do |word|
scores[word] = score_word(word)
end

#Determines maximum score for all words
max_word = scores.max_by{|key,value| value}[0]
max_score = scores.max_by{|key,value| value}[1]


if scores.values.count(max_score) == 1
return highest_score(max_word, max_score)
else
max_scores = scores.select {|key,value| value==max_score}

min_length = max_scores.min_by{|key,value| key.length}[0].length

best_words = max_scores.select {|key,value| key.length == min_length || key.length == 10}

if best_words.length == 1
return highest_score(best_words.keys[0],best_words.values[0])
elsif best_words.length > 1
best_words.each do |word, score|
if word.length == 10
return highest_score(word, score)
end
end
return highest_score(best_words.keys[0],best_words.values[0])
end
end
end

#Wave 5
#Checks that the word is in the english dictionary
def is_in_english_dict? (input)
dictionary = CSV.read("assets/dictionary-english.csv")
new_dictionary = []
dictionary.each do |element|
new_dictionary << element[0]
end
new_dictionary.include?(input.downcase) ? true : false
end
24 changes: 24 additions & 0 deletions specs/adagrams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,28 @@
expect(best_word[:score]).must_equal 18
end
end

describe "is_in_english_dict" do
it "will return true if it's in the dictionary" do
# Arrange
word = "What"

# Act
check = is_in_english_dict? (word)

# Assert
expect(check).must_equal true
end

it "will return false if it's not in the dictionary" do
# Arrange
word = "blahhh"

# Act
check = is_in_english_dict? (word)

# Assert
expect(check).must_equal false
end
end
end
1 change: 1 addition & 0 deletions wave-5-game.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require_relative 'lib/adagrams'


def display_welcome_message
puts "Welcome to Adagrams!"
end
Expand Down