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 by Jackie and Jane #12

Open
wants to merge 5 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
145 changes: 145 additions & 0 deletions lib/adagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
require 'pry'
require 'csv'

# WAVE 1
# The draw_letters method starts with a fresh pool of tiles and returns
# an array of 10 letters (the "hand").
def draw_letters

Choose a reason for hiding this comment

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

Small issue here, each letter has an equal chance to be drawn. Instead an A should be 9 times more likely than a J.

Instead you'll need an Array like:

["A","A","A","A","A","A","A","A","A","B","B"...]

And draw from that array, removing letters as they're drawn.

# letter_pool is an array of hashes that represent available tiles in the game
# in each hash, keys represent the letter on the tile
# the value in each hash represents the number of tiles with that letter
letter_pool = [
{"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 = []
while (hand.length < 10) do
pull = letter_pool[rand(26)]
if (pull.values.reduce > 0) # if the selected letter has tiles in the pool
letter = pull.keys.reduce # fetch the letter
hand << letter # add the selected letter to the hand
pull[letter] -= 1 # decrement the tiles available for selected letter
end
end
return hand
end

# WAVE 2
# The uses_available_letters method accepts a word from the user and returns
# true if that word uses only letters available in hand, false if not.
def uses_available_letters?(input, letters_in_hand)
# Create a copy of the hand to use within this method so the original hand
# remains unaltered.
new_letters_in_hand = letters_in_hand.map do |letter|
letter
end

# To account for duplicate letters, delete letters from the hand as they are
# matched with letters from user input.
word = input.upcase.split("")
word.each do |letter|
if new_letters_in_hand.include?(letter)
new_letters_in_hand.delete_at(new_letters_in_hand.index letter)
else
return false # returns false if any letter user entered isn't in hand
end
end
return true # returns true only after all letters confirmed in hand
end

# WAVE 3
# The score_word method calculates the score for word by summing up the score
# of each tile.
def score_word(word)
score = 0
word.upcase.split("").each do |letter|
if "AEIOULNRST".include? letter
score += 1
elsif "DG".include? letter
score += 2
elsif "BCMP".include? letter
score += 3
elsif "FHVWY".include? letter
score += 4
elsif "K".include? letter
score += 5
elsif "JX".include? letter
score += 8
elsif "QZ".include? letter
score += 10
end
end
if word.length >= 7
score += 8
end
return score
end

# WAVE 4
# The highest_score_from method identifies the highest scoring word played
# by the user, using tie-breaking logic when necessary.
def highest_score_from(words)
highest = {
word: "",
score: 0
}

words.each do |word|
current_score = score_word(word)
if current_score > highest[:score]
highest[:score] = current_score
highest[:word] = word
elsif current_score == highest[:score]
if word.length == 10 && highest[:word].length != 10
highest[:score] = current_score
highest[:word] = word
elsif word.length < highest[:word].length && highest[:word].length != 10
highest[:score] = current_score
highest[:word] = word
end
end
end
return highest
end

# WAVE 5
# The is_in_english_dict? method checks to make sure the user entered a word
# that is in the English dictionary by comparing against a csv list of words.
def is_in_english_dict?(input)
# Map csv words to an array
dictionary_words = CSV.open('assets/dictionary-english.csv', headers: true).map do |row|
row["Word"]
end

downcased_input = input.downcase
if dictionary_words.include? downcased_input
return true
else
return false
end
end
2 changes: 1 addition & 1 deletion wave-5-game.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'lib/adagrams'
require 'pry'

def display_welcome_message
puts "Welcome to Adagrams!"
Expand Down Expand Up @@ -71,7 +72,6 @@ def run_game
end
user_input_word = get_user_input
end

score = score_word(user_input_word)
played_words << user_input_word

Expand Down