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

Sara and Selam Amperes Scrabble #17

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
9076a5f
Added functionality to self.score method, passed all self.score tests.
SelamawitA Feb 20, 2018
51f0125
input code for highest_score_from class function and the tests as well.
SelamawitA Feb 21, 2018
abe90cb
added testing for both class functions of Scrabble::Scoring, all test…
SelamawitA Feb 21, 2018
7e1fd57
Created new player file and player_specs file. Created initialize and…
Feb 21, 2018
7dd6422
Added the play method and test
Feb 21, 2018
3d0b672
added play(word) method - capable of adding word to string of previou…
SelamawitA Feb 22, 2018
83ff41a
included tests for play(word) method
SelamawitA Feb 22, 2018
31a2cbc
updated iterative block of array of characters to upcase each letter.
SelamawitA Feb 22, 2018
30354b3
updated total_score test
Feb 22, 2018
dec7cc9
updated total_score and won? tests
Feb 22, 2018
3d7211a
added total_score() and won? method
SelamawitA Feb 22, 2018
5bac91f
updated total_score and won? tests
Feb 22, 2018
338afcf
added tests for seven letter word and winner
SelamawitA Feb 22, 2018
1fcf3e9
includes test for winner
SelamawitA Feb 22, 2018
dd1cc75
Removed @total_score variable and refactored play method to function …
SelamawitA Feb 22, 2018
38c5bbb
Updated test to align with play() that does not include @total_score …
SelamawitA Feb 22, 2018
6c5b031
added total score function method to calculate user's current score i…
SelamawitA Feb 22, 2018
ca43d65
added testing for total scores of player
SelamawitA Feb 22, 2018
8de122a
Updated won? method and test
CheerOnMars Feb 22, 2018
5080920
Created highest_scoring_word method to return the highest scoring wor…
SelamawitA Feb 22, 2018
a8b7d84
added test for highest_scoring_word method to confirm the highest sco…
SelamawitA Feb 22, 2018
9331fc6
added highest_word_score test
CheerOnMars Feb 22, 2018
3a19c3d
Created highest_word_score to calculate the numeric value of the high…
SelamawitA Feb 22, 2018
1785ab9
Merge branch 'master' of https://github.com/SelamawitA/Scrabble
SelamawitA Feb 22, 2018
871e84d
edit for highest scoring word
SelamawitA Feb 22, 2018
9660334
added files for the tilebag class and tests
CheerOnMars Feb 22, 2018
10be9d4
add require_relative file
CheerOnMars Feb 23, 2018
b161115
added title and constructor name
SelamawitA Feb 23, 2018
4796599
added array of letters for the tilebag
CheerOnMars Feb 23, 2018
209c130
created constructor and draw_tiles() method
SelamawitA Feb 23, 2018
6582c4c
added single test to confirm object could be created
SelamawitA Feb 23, 2018
7b66aa8
added logic to prevent negative tiles from being pulled from tile bag…
SelamawitA Feb 23, 2018
4f3d33e
added test for array size
SelamawitA Feb 23, 2018
d0abe56
added test for array size
SelamawitA Feb 23, 2018
b9a6f5e
updated tests to reflect the tilebag is now stored as a hash
CheerOnMars Feb 24, 2018
ee935d6
updated player_spec with wave 3 methods
CheerOnMars Feb 24, 2018
11c3c40
updated array to hash and draw_lines(num) to be able to traverse thro…
SelamawitA Feb 24, 2018
741d474
updated tests - tilebag is a hash
SelamawitA Feb 24, 2018
d3d5331
Merge branch 'master' of https://github.com/SelamawitA/Scrabble
SelamawitA Feb 24, 2018
14989d2
slight changes to method
CheerOnMars Feb 24, 2018
87e6b2c
handle merge errors. deferred to Selam's
CheerOnMars Feb 24, 2018
fa4038c
updated tiles_remaining() method to return total current tiles
SelamawitA Feb 24, 2018
3da20fc
added tiles() and draw_tiles(title_bag) methods
SelamawitA Feb 24, 2018
4f8d78f
Update player.rb
SelamawitA Feb 24, 2018
db8b525
Update scoring.rb
SelamawitA Feb 24, 2018
a857f98
Update tilebag.rb
SelamawitA Feb 24, 2018
25eddac
Update tilebag.rb
SelamawitA Feb 24, 2018
3516183
added comments
SelamawitA Feb 24, 2018
fd768fd
updated comments and indents
SelamawitA Feb 24, 2018
568f734
Merge branch 'master' of https://github.com/SelamawitA/Scrabble
SelamawitA Feb 24, 2018
f2b3ac4
cleaned up comments
SelamawitA Feb 24, 2018
819ceff
added comments and cleaned up formatting
SelamawitA Feb 24, 2018
0ec3ac9
added comments and improved formatting
SelamawitA Feb 24, 2018
bddee42
Added test for a full plaque and cleaned formatting
SelamawitA Feb 24, 2018
b005cde
Improved formatting
SelamawitA Feb 24, 2018
7d0b694
Included tests for tilebag and draw tiles
SelamawitA Feb 24, 2018
9243443
removed reference to TileBag object in draw_tiles method
SelamawitA Feb 25, 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
81 changes: 81 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'awesome_print'
MAX_NUM_OF_TILES_ALLOWED = 7

Choose a reason for hiding this comment

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

Good use of a constant!


module Scrabble
class Player
attr_reader :name, :players_plaque
attr_writer :players_plaque

def initialize(name)
@name = name
@array_of_words = []
@players_plaque = []
end

# Returns array of players tiles.
def tiles
return @players_plaque
end

# Draws tiles from a TileBag to fill player's plaque.
def draw_tiles(tile_bag)
if @players_plaque.length < MAX_NUM_OF_TILES_ALLOWED
add_tiles = MAX_NUM_OF_TILES_ALLOWED - @players_plaque.length
@players_plaque.concat(tile_bag.draw_tiles(add_tiles))

Choose a reason for hiding this comment

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

Very good!

end
end

# Returns an array of the words played by the user.
def plays
return @array_of_words
end

# Adds the input word to the plays array
def play(word)
player_status = false
total_score = 0
@array_of_words.each do |word_in_arr|
total_score += Scrabble::Scoring.score(word_in_arr)

Choose a reason for hiding this comment

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

You already have a total_score method inside Player why not use it?

end

if total_score < 100
word_score = Scrabble::Scoring.score(word)
player_status = word_score
@array_of_words << word
end

return player_status
end

# Returns the sum of scores of played words
def total_score
sum = 0
@array_of_words.each do |a_word|
sum+= Scrabble::Scoring.score(a_word)
end
return sum
end

# If the player has over 100 points, returns true, otherwise returns false
def won?
player_case = false

Choose a reason for hiding this comment

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

This variable name really doesn't make sense.

if total_score >= 100
player_case = true
end
return player_case
end

# Returns the highest scoring played word
def highest_scoring_word
highest_scoring_word = Scrabble::Scoring.highest_score_from(@array_of_words)

return highest_scoring_word
end

# Returns the highest_scoring_word score
def highest_word_score
return highest_scoring_value = Scrabble::Scoring.score(highest_scoring_word)

Choose a reason for hiding this comment

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

NIIIICE!

end

end
end
54 changes: 54 additions & 0 deletions lib/scoring.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,63 @@
module Scrabble
class Scoring

# Returns total score of a given word.
def self.score(word)
total = 0
array_of_characters = word.split('')
if word.match(/^[a-zA-Z]{1,7}$/) == nil
total = nil
else
array_of_characters.each do |letter|
scrabble_value = letter.upcase
case scrabble_value
when "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
scrabble_value = 1
when "D", "G"
scrabble_value = 2
when "B", "C", "M", "P"
scrabble_value = 3
when "F", "H", "V", "W", "Y"
scrabble_value = 4
when "K"
scrabble_value = 5
when "J","X"
scrabble_value = 8
when "Q", "Z"
scrabble_value = 10
end
total = scrabble_value + total
end
end
if array_of_characters.length == 7
total = total + 50
end
return total
end

# Returns highest score from an array of words.
def self.highest_score_from(array_of_words)
max = 0
if array_of_words.size == 0
highest_score = nil
elsif array_of_words.size == 1
highest_score = array_of_words[0]

Choose a reason for hiding this comment

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

What about if the only word is invalid?

end
array_of_words.each do |word|
if Scrabble::Scoring.score(word) > max
max = Scrabble::Scoring.score(word)
highest_score = word
elsif Scrabble::Scoring.score(word) == max && word.length == 7
max = Scrabble::Scoring.score(word)
highest_score = word
elsif Scrabble::Scoring.score(word) == max && highest_score.length !=7
if highest_score.length > word.length
highest_score = word
end
end
end

return highest_score
end
end
end
65 changes: 65 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'awesome_print'

module Scrabble
class TileBag
attr_reader :tile_bag

def initialize
@tile_bag =
{ A: ['A']*9,
B: ['B']*2,
C: ['C']*2,
D: ['D']*4,
E: ['E']*12,
F: ['F']*2,
G: ['G']*3,
H: ['H']*2,
I: ['I']*9,
J: ['J']*1,
K: ['K']*1,
L: ['L']*4,
M: ['M']*2,
N: ['N']*6,
O: ['O']*8,
P: ['P']*2,
Q: ['Q']*1,
R: ['R']*6,
S: ['S']*4,
T: ['T']*6,
U: ['U']*4,
V: ['V']*2,
W: ['W']*2,
X: ['X']*1,
Y: ['Y']*2,
Z: ['Z']*1 }
end

# Returns a collection of random tiles, removes the tiles from the default set or returns an empty array and displays a message to the user.
def draw_tiles(num)
random_tiles = []
if num <= tiles_remaining && num > 1
num.times do
count = 0
random_number = rand(0...tiles_remaining)
@tile_bag.map{|letter_type,letter|
letter.each do |single_letter|
if count == random_number

Choose a reason for hiding this comment

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

The structure you've got here, means that each letter has an equal chance of being drawn. Also after a letter is exhausted you'll continue to pop from the letter array.

random_tiles << single_letter
letter.pop
end
count+=1
end}
end
else
puts "There are not enough tiles"
end
return random_tiles
end

# Returns the number of tiles remaining in the bag
def tiles_remaining
tiles_remaining = @tile_bag.sum{|letter_type,character| character.length}
return tiles_remaining
end
end
end
128 changes: 128 additions & 0 deletions specs/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require 'minitest/autorun'
require 'minitest/pride'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative '../lib/player'
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new


describe 'Player' do

describe 'name' do
it 'returns the value of the @name instance variable' do
player_name = Scrabble::Player.new ("Ada")
player_name.name.must_equal "Ada"
end
end

describe 'tiles' do
it 'is a collection of letters that the player can play (max 7) and starts at 0' do
player = Scrabble::Player.new ("Ada")
player.tiles.must_be_instance_of Array
player.tiles.length.must_equal 0
end

end

describe 'draw_tiles' do
it 'Fills a partially populated plaque' do
player_name = Scrabble::Player.new("Ada")
tile_bag = Scrabble::TileBag.new
player_name.players_plaque = ['C','D','E']
player_name.draw_tiles(tile_bag)
player_name.tiles.length.must_equal 7
end

it 'Will not fill in a fully populated plaque' do
player_name = Scrabble::Player.new("Ada")
tile_bag = Scrabble::TileBag.new
testing_arr = ['A','B','C','D','E','F','G']
player_name.players_plaque = testing_arr
player_name.draw_tiles(tile_bag)
player_name.players_plaque.must_equal(testing_arr)
end

end

describe 'plays' do
it 'returns an Array' do
player_name = Scrabble::Player.new ("Ada")
player_name.plays.must_be_instance_of Array
end

end

describe 'play' do
it 'adds the word into the array of words' do
player_name = Scrabble::Player.new ("Ada")
player_name.play("pop")
player_name.plays.must_include "pop"
end

it 'Returns false if player has already won' do
player_name = Scrabble::Player.new ("Ada")
player_name.play("ZZZZZZZ")
must_be_false = player_name.play("POP")
must_be_false.must_equal false
end

it 'Returns score of word if < 100 ' do
player_name = Scrabble::Player.new ("Ada")

Choose a reason for hiding this comment

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

What about playing invalid words?

must_return_word = player_name.play("lawl")
must_return_word.must_equal 7
end

end

describe 'total_score' do
it 'Returns a total score to the user' do
player_name = Scrabble::Player.new("Ada")

Choose a reason for hiding this comment

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

You should also test to verify that total_sore doesn't keep going up after the player wins.

player_name.play('hi')
player_name.play('bye')
player_name.play('cry')
player_name.play('why')
player_name.total_score.must_equal 33
end
end

describe 'won?' do
it 'Returns true if player has won' do
player_name = Scrabble::Player.new ("Ada")
player_name.play("ZZZZZZZ")
player_name.won?.must_equal true
end

it 'Returns false if player has not won' do
player_name = Scrabble::Player.new ("Ada")

Choose a reason for hiding this comment

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

What happens with won? if the player never played a word?

player_name.play("pop")
player_name.play("lawl")
player_name.won?.must_equal false
end

end

describe 'highest_scoring_word' do
it 'Returns highest scoring word in an array of words for a single player' do
player_name = Scrabble::Player.new("Ada")
player_name.play('hi')
player_name.play('bye')
player_name.play('why')
player_name.play('cry')

player_name.highest_scoring_word.must_equal 'why'
end
end

describe 'highest_word_score' do
it "Returns the score of the highest scoring word" do
player_name = Scrabble::Player.new("Ada")
player_name.play('hi')
player_name.play('bye')
player_name.play('ZZZZZZZ')
player_name.play('cry')

player_name.highest_word_score.must_equal 120
end
end
end
20 changes: 19 additions & 1 deletion specs/scoring_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
require 'minitest/autorun'
require 'minitest/pride'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative '../lib/scoring'

# Get that nice colorized output
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe 'Scoring' do
Expand Down Expand Up @@ -42,21 +42,39 @@

describe 'highest_score_from' do
it 'returns nil if no words were passed' do
empt_array = []
empt_array_of_words = Scrabble::Scoring.highest_score_from(empt_array)
empt_array_of_words.must_be_nil
end

it 'returns the only word in a length-1 array' do
single_arr = ['word']
solo_arr_of_words = Scrabble::Scoring.highest_score_from(single_arr)
solo_arr_of_words.must_equal 'word'
end

it 'returns the highest word if there are two words' do
random_arr = ['zzebra','medium']

Choose a reason for hiding this comment

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

You should also vary up the order in these tests so that you can ensure that the order isn't determining the winner (unless it's supposed to).

      random_arr = ['zzebra','medium']
      highest_of_words = Scrabble::Scoring.highest_score_from(random_arr)
      highest_of_words.must_equal 'zzebra'

      random_arr = ['medium','zzebra']
      highest_of_words = Scrabble::Scoring.highest_score_from(random_arr)
      highest_of_words.must_equal 'zzebra'

highest_of_words = Scrabble::Scoring.highest_score_from(random_arr)
highest_of_words.must_equal 'zzebra'
end

it 'if tied, prefer a word with 7 letters' do
equivalent_words = ['zzebra','zebrakk']
tied_words = Scrabble::Scoring.highest_score_from(equivalent_words)
tied_words.must_equal 'zebrakk'
end

it 'if tied and no word has 7 letters, prefers the word with fewer letters' do
equivalent_words = ['zebra','kebrak']
tied_values = Scrabble::Scoring.highest_score_from(equivalent_words)
tied_values.must_equal 'zebra'
end

it 'returns the first word of a tie with same letter count' do
tied_words = ['zebra','qebra']
tied_values = Scrabble::Scoring.highest_score_from(tied_words)
tied_values.must_equal 'zebra'
end
end
end
Loading