forked from AdaGold/adagrams
-
Notifications
You must be signed in to change notification settings - Fork 27
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
jacquelynoelle
wants to merge
5
commits into
Ada-C10:master
Choose a base branch
from
jacquelynoelle:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
And draw from that array, removing letters as they're drawn.