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

SHERMAN and FAHMY Adagrams #6

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
112 changes: 112 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
require 'csv'

def draw_letters
alphabet = {
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
}

hand = []
tiles_drawn = 0
until tiles_drawn == 10
letter = alphabet.keys.sample
if alphabet[letter] > 0

Choose a reason for hiding this comment

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

Using a hash with sample gives an equal 1/26 chance to each letter, instead we need the chances to be weighted.

alphabet[letter] -= 1
hand << letter.to_s
tiles_drawn += 1
end
end
hand

end

def uses_available_letters?(input, letter_in_hand)
new_letters = letter_in_hand.map { |i| i }
input.split("").each do |letter|
if !new_letters.include? letter
return false

Choose a reason for hiding this comment

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

Good job returning when you know the outcome!

elsif new_letters.include? letter
index = new_letters.find_index(letter)
new_letters.delete_at(index)
end
end
return true
end

def score_word(word)
word_score = 0

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

word_score += 8 if (7..10).include?(word.length)

return word_score
end

def highest_score_from(words)
best_word = {word: nil, score: 0}
words.each do |word|

Choose a reason for hiding this comment

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

ONE LOOP I LOVE IT

if score_word(word) > best_word[:score]
best_word[:score] = score_word(word)
best_word[:word] = word
elsif score_word(word) == best_word[:score]
if word.length == 10 && best_word[:word].length != 10
best_word[:word] = word
elsif word.length < best_word[:word].length && best_word[:word].length != 10
best_word[:word] = word
end
end
end
best_word
end


def is_in_english_dict?(input)
csv_file = 'assets/dictionary-english.csv'
CSV.read(csv_file, "r").each do |line|
return true if line.include? input.downcase
end
return false
end
1 change: 1 addition & 0 deletions wave-5-game.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'csv'
require_relative 'lib/adagrams'

def display_welcome_message
Expand Down