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

Scrabble- Octos- Brandy & Ari! #12

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
39fbde3
Completed self.score
arrriiii Feb 21, 2018
593d58f
Wave 1 complete
brandyaustinseattle Feb 21, 2018
c3138c0
.score and .highest_score_from complete with passing test
brandyaustinseattle Feb 21, 2018
89614e3
Created Player class
arrriiii Feb 21, 2018
c7342b5
Created player test file
arrriiii Feb 21, 2018
1294c1a
Started player.rb and have all tests passing in player_spec.rb
brandyaustinseattle Feb 22, 2018
a1a57d9
Started player.rb and have all tests passing in player_spec.rb
brandyaustinseattle Feb 22, 2018
9a81913
Worked on player.rb and have all tests passing in player_spec.rb
brandyaustinseattle Feb 22, 2018
f04ce9d
Merge branch 'master' of https://github.com/Ari-1/Scrabble
brandyaustinseattle Feb 22, 2018
a849892
Still working on player.rb and player_spec.rb; Added remaining instru…
brandyaustinseattle Feb 22, 2018
8cbbead
No changes made
arrriiii Feb 22, 2018
c4ac6c0
Update current file
arrriiii Feb 22, 2018
f0492a4
Still working on player.rb and player_spec.rb; Added remaining instru…
brandyaustinseattle Feb 22, 2018
f854a1d
Merge branch 'master' of https://github.com/Ari-1/Scrabble
brandyaustinseattle Feb 22, 2018
9f1d3a0
Attempting to sync with updated instructions
brandyaustinseattle Feb 22, 2018
45130dc
Merge branch 'master' of https://github.com/Ada-C9/Scrabble
brandyaustinseattle Feb 22, 2018
b3434ec
scoring.rb and scoring_spec.rb complete, player.rb and player_spec.rb…
brandyaustinseattle Feb 22, 2018
fa7f749
"Update Wave 2 & 3"
arrriiii Feb 22, 2018
a11da9c
Completed test to check if player has won? method
arrriiii Feb 22, 2018
17e66a9
Completed tests for highest scoring word
arrriiii Feb 22, 2018
74fd199
Fixing highest score method
arrriiii Feb 22, 2018
a1972cd
Fixed and created highest word score
arrriiii Feb 22, 2018
46f5d09
Update player.rb
arrriiii Feb 22, 2018
7dd1e85
Completed initialize for Tilebag
arrriiii Feb 22, 2018
311425a
removed extra files
arrriiii Feb 22, 2018
2609a16
Updated picked tiles from bag test
arrriiii Feb 23, 2018
2858d43
Completed draw_tiles method
arrriiii Feb 23, 2018
d86610a
Completed testing for draw_tiles
arrriiii Feb 23, 2018
040342b
Add comments to player.rb
arrriiii Feb 23, 2018
7ea30f3
Finished Scrabble Project
arrriiii Feb 23, 2018
ba78889
Update scoring.rb
brandyaustinseattle Feb 26, 2018
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
73 changes: 73 additions & 0 deletions lib/player.rb
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
63 changes: 63 additions & 0 deletions lib/scoring.rb
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)

Choose a reason for hiding this comment

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

Cool use of scan


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?

Choose a reason for hiding this comment

The 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|

Choose a reason for hiding this comment

The 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
41 changes: 41 additions & 0 deletions lib/tilebag.rb
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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

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

Though it is functionally the same - i'd recommend using the else here to return nil since it is easier to read.

end

end
end
Loading