-
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
Scrabble- Octos- Brandy & Ari! #12
base: master
Are you sure you want to change the base?
Changes from all commits
39fbde3
593d58f
c3138c0
89614e3
c7342b5
1294c1a
a1a57d9
9a81913
f04ce9d
a849892
8cbbead
c4ac6c0
f0492a4
f854a1d
9f1d3a0
45130dc
b3434ec
fa7f749
a11da9c
17e66a9
74fd199
a1972cd
46f5d09
7dd1e85
311425a
2609a16
2858d43
d86610a
040342b
7ea30f3
ba78889
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require_relative '../lib/tilebag' | ||
|
||
|
||
module Scrabble | ||
class Player | ||
|
||
attr_reader :name, :player_score, :plays, :tiles | ||
|
||
def initialize(player_name) | ||
@name = player_name | ||
@player_score = 0 #This is total_score | ||
@plays = [] | ||
@tiles = [] | ||
end | ||
|
||
|
||
def play(word) | ||
if @player_score > 100 | ||
return false | ||
end | ||
|
||
@plays << word | ||
|
||
score = Scrabble::Scoring.score(word) | ||
@player_score += score.to_i | ||
|
||
return score | ||
end | ||
|
||
def won? | ||
@player_score > 100 ? true : false | ||
end | ||
|
||
def highest_scoring_word | ||
|
||
highest_scoring_words = [] | ||
max = 0 | ||
|
||
@plays.each do |word| | ||
score = Scrabble::Scoring.score(word) | ||
if score.to_i > max | ||
highest_scoring_words << word | ||
max = score | ||
end | ||
end | ||
return highest_scoring_words[-1] | ||
|
||
end | ||
|
||
def highest_word_score | ||
max = 0 | ||
|
||
@plays.each do |word| | ||
score = Scrabble::Scoring.score(word) | ||
if score.to_i > max | ||
max = score | ||
end | ||
end | ||
return max | ||
end | ||
|
||
|
||
def draw_tiles(tile_bag) | ||
|
||
until @tiles.length == 7 | ||
new_tile = tile_bag.draw_tiles(1) | ||
@tiles << new_tile | ||
end | ||
|
||
end | ||
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,72 @@ | ||
module Scrabble | ||
class Scoring | ||
|
||
def self.score(word) | ||
if word =~ /[\W]/ || word =~ /[\s+]/ || word == "" || word =~ /.{8,}$/ | ||
return nil | ||
elsif word.length == 7 | ||
total_score = 50 | ||
else | ||
total_score = 0 | ||
end | ||
|
||
letters_groups = [/[AaEeIiOoUuLlNnRrSsTt]/, /[DdGg]/, /[BbCcMmPp]/, /[FfHhVvWwYy]/, /[Kk]/, /[JjXx]/, /[QqZz]/] | ||
point_options = [1, 2, 3, 4, 5, 8, 10] | ||
letters_groups.each_with_index do |group, index| | ||
matches = [] | ||
|
||
matches = word.scan(group) | ||
|
||
score = matches.length * point_options[index] | ||
total_score += score | ||
end | ||
return total_score | ||
end | ||
|
||
# # WE WANTED TO CREATE A METHOD THAT RETURNED ALL OF THE WORDS WITH THE HIGHEST SCORE RATHER THAN THE FIRST OCCURANCE. | ||
|
||
def self.highest_score_from(array_of_words) | ||
return nil if array_of_words.empty? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this initial "short circuit" of the method to not do any more logic |
||
array_of_scores = [] | ||
words_with_max = [] | ||
array_of_words.each do |word| | ||
array_of_scores << score(word) | ||
end | ||
|
||
# # .max on array_of_scores if we want just one, no duplicates | ||
# # Maybe we could do array_of_scores.rindex(array_of_scores.max) which would return the index for the max score which is equal to the index for the corresponding word in array_of_words | ||
|
||
array_of_scores.each_with_index do |score, index| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be a great place to use an enumerable method! |
||
if score == array_of_scores.max | ||
words_with_max << array_of_words[index] | ||
end | ||
end | ||
|
||
if words_with_max.length > 1 | ||
|
||
# # .Sort on lengths for words_with_max | ||
# # But if we do refactoring mentioned above, words_with_max won't be created | ||
# # Could possibly do .length on array_of_words[index from array of scores] | ||
|
||
words_with_max.each {|word| return word if word.length == 7} | ||
|
||
least_letters = [] | ||
start_value = words_with_max[0].length | ||
least_letters << words_with_max[0] | ||
|
||
words_with_max.each do |word| | ||
if word == words_with_max[0] | ||
next | ||
else | ||
if start_value > word.length | ||
least_letters << word | ||
end | ||
end | ||
end | ||
return least_letters[-1] | ||
end | ||
|
||
return words_with_max[0] | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Scrabble | ||
class Tilebag | ||
|
||
TILE_BAG_LETTERS = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", | ||
"T", "U", "V", "W", "X", "Y", "Z"] | ||
|
||
TILE_BAG_COUNT = [9, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 1, 2, 1] | ||
|
||
attr_reader :bag, :tiles_remaining | ||
|
||
def initialize | ||
@bag = [] | ||
TILE_BAG_COUNT.each_with_index do |count, index| | ||
count.times do | ||
tile = TILE_BAG_LETTERS[index] | ||
@bag << tile | ||
end | ||
end | ||
|
||
@tiles_remaining = 96 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be much better to dynamically grab this # from the collection rather than hard coding it so that if one of the letter counts changed above, you wouldn't need to update this value. |
||
|
||
end | ||
|
||
def draw_tiles(num) | ||
|
||
if @tiles_remaining >= num | ||
@tiles_remaining -= num | ||
picked_tiles = @bag.sample(num) | ||
|
||
picked_tiles.each do |tile| | ||
@bag.delete_at(@bag.index(tile)) | ||
end | ||
|
||
return picked_tiles | ||
end | ||
|
||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though it is functionally the same - i'd recommend using the |
||
end | ||
|
||
end | ||
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.
Cool use of scan