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
12 changes: 12 additions & 0 deletions adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# wirte a method with no parameters

Choose a reason for hiding this comment

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

In the future make sure to clean up files you aren't using.

letters = []
9.times do
letters.push("A")
end
puts letters






75 changes: 75 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
def draw_letters
pool_letters = %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]

letters = pool_letters
letters.shuffle
letters_in_hand = letters.sample(10)
letters_in_hand
end

def uses_available_letters?(input, letters_in_hand)
input.each_char do |letter|
index = letters_in_hand.index(letter.upcase)
if index.nil?
return false
else
letters_in_hand.delete_at(index)

Choose a reason for hiding this comment

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

Because this deletes letters as it finds them if the user inputs an invalid word the letters it does use will be deleted.

For example:

$ ruby wave-2-game.rb
Welcome to Adagrams!
Let's draw 10 letters from the letter pool...
You have drawn the letters:
N, K, E, I, W, V, R, A, A, E
Please input a word that only uses the letters from the letter bank
NAAA
You entered in a word that contains characters not in the letter bank
Please input a word that only uses the letters from the letter bank
NA
You entered in a word that contains characters not in the letter bank
Please input a word that only uses the letters from the letter bank

end
end
true
end

def score_word(word)
score_value = { 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 }
scored = 0
word = word.upcase
word.each_char do |letter|
scored += score_value[letter.to_sym].to_i
end
scored += 8 if word.length > 6
scored
end

def highest_score_from(words)
scores = []
words.each do |word|
score = score_word(word.upcase)
scores << score
end

scores_hash = Hash[words.zip scores]
puts "scores_hash #{scores_hash}"

Choose a reason for hiding this comment

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

In the future you should remove debugging puts before submitting an assignment.

Leaving the puts in means the user will see extra output when they try to play the game.

$ ruby wave-4-game.rb
Welcome to Adagrams!
Let's draw 10 letters from the letter pool...
You have drawn the letters:
L, E, M, G, E, F, O, N, D, R
Please input your submission for the longest anagram you can come up with
LEMG
Your submitted anagram scored 7 points
Should we play another round?
Enter y to replay
y
Let's draw 10 letters from the letter pool...
You have drawn the letters:
O, P, Z, E, E, O, D, J, H, T
Please input your submission for the longest anagram you can come up with
OPZEE
Your submitted anagram scored 16 points
Should we play another round?
Enter y to replay

scores_hash {"LEMG"=>7, "OPZEE"=>16}
winner_score {"OPZEE"=>16}
winner_score.length 1
Thanks for playing Adagrams!
The highest score from this game was OPZEE, which was worth 16 points
Goodbye!

winner_score = scores_hash.select { |_key, value| value == scores_hash.values.max }
puts "winner_score #{winner_score}"
puts "winner_score.length #{winner_score.length}"

Choose a reason for hiding this comment

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

Remove debugging puts.


long_words = []
winner_score.each do |key, _value|
if key.length == 10
long_words << key
end
end

winner = ""
if long_words.length == 1
winner = long_words[0]
elsif long_words.length > 1
winner = long_words[0]
else
short_words = winner_score.min_by { |key, _value| key.length }

Choose a reason for hiding this comment

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

Try to use more descriptive names. For example in this case you could do something like:

short_words = winner_score.min_by { |word, _score| word.length }

winner = short_words[0]
end

winner_values = {}
winner_values[:word] = winner
winner_values[:score] = scores.max
winner_values
end