-
Notifications
You must be signed in to change notification settings - Fork 26
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
Samantha and Kiera - Scrabble - Octos #22
base: master
Are you sure you want to change the base?
Conversation
…er- and lower-case letters
…method to calculate total score, and cleaned up the ternary statement to add 50 points to a 7 letter word
…of an array of words
… paspassing tests
ScrabbleWhat We're Looking For
Good job overall! The big learning goals I'm looking to see demonstrated on this project are:
It doesn't look like you got an opportunity to exercise the third, since |
# LETTERS = { | ||
# 1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"], | ||
# 2 => ["D", "G"], | ||
# 3 => ["B", "C", "M", "P"], |
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.
I like the idea of using a hash for this, but I think I would switch the keys and the values. I would go with something like:
LETTERS = {
"A" => 1
"B" => 3,
"C" => 3,
"D" => 2,
# ...
}
Then to get the score for a letter, you can say LETTERS[letter]
.
case letter | ||
when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" | ||
total_score += 1 | ||
when "D", "G" |
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.
Using a case
statement certainly solves this problem, but it's a one-off solution. If you ever need to use letter scores anywhere else, you'll have to recreate this data. Keeping it in a data structure makes it much more amenable to change.
# } | ||
def self.score(word) # 'dog' | ||
word = word.upcase | ||
word_array = word.split('') #['D', 'O', 'G'] |
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.
I like that you're using comments here to keep track of what format the word will be in at each stage. This also makes the code easy to understand when you're reading it for the first time.
|
||
if word_array.length > 7 || word_array.length < 1 | ||
return total_score = nil | ||
else |
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.
On line 20, you don't need to set total_score
, you can say return nil
.
total_score += 10 | ||
else | ||
total_score = nil | ||
end # case letter |
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.
On line 46, this will cause problems if there's a bad letter in the middle of a word, like 't3st'
. Once you've set total_score = nil
, trying to add to it will result in an error. Instead you should return nil
here, bail out of the method entirely.
def won? | ||
if total_score >= 100 | ||
return true | ||
else |
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.
Since >=
will always return true
or false
, this could be shortened to:
def won?
return total_score >= 100
end
num.times do | ||
random_num = rand(26) # Choose a letter at random | ||
@letter_array[random_num] | ||
@tile_bag << @letter_array[random_num][0] #Add letter to tile bag |
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.
By this logic, you're as likely to draw an e
as you are a z
! Maybe this is fine? The way you've set up your data structures, it would be difficult to do it any other way.
Instead, you might keep all the letters in the bag in an array. You could seed the array with code like:
def initialize
letters = {
# the hash you had before
}
@tiles = []
letters.each do |letter, count|
count.times do
@tiles << letter
end
end
end
Then you could pull a random tile with sample
and get a nice even distribution.
@letter_array[random_num][1] -= 1 # Reduce number of tiles by 1 | ||
if @letter_array[random_num][1] == 0 # Delete letter from list if 0 tiles left | ||
@letter_array.delete_at(random_num) | ||
end |
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.
There's a bug here! You remove items from @letter_array
, but always do rand(26)
to figure out what index to use, which means that once the bag is getting low on letters you're likely to go past the end of what you have left.
end | ||
|
||
it 'returns the highest word if there are two words' do | ||
end | ||
array_of_words = ["dog", "fish"] | ||
Scrabble::Scoring.highest_score_from(array_of_words).must_equal "fish" |
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.
Like we discussed in class, it would probably be wise to check multiple orderings of these words, that is, both ['dog', 'fish']
and ['fish', 'dog']
. You'd be surprised how often this sort of thing turns up a bug.
describe "#won?" do | ||
it'if player has 100 or more points return true' do | ||
|
||
player = Scrabble::Player.new("player") |
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.
Good job catching both sides of this condition. I would be interested in looking at 100 points specifically: what does your program do at 99 points, at 100, and at 101? Where is the cutoff? This is the sort of question tests are well-situated to answer.
Scrabble
Congratulations! You're submitting your assignment.
Comprehension Questions
score
method in theScoring
class a class method instead of an instance method?Enumerable
mixin? If so, where and why was it helpful?