-
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
Sara and Selam Amperes Scrabble #17
base: master
Are you sure you want to change the base?
Changes from all commits
9076a5f
51f0125
abe90cb
7e1fd57
7dd6422
3d0b672
83ff41a
31a2cbc
30354b3
dec7cc9
3d7211a
5bac91f
338afcf
1fcf3e9
dd1cc75
38c5bbb
6c5b031
ca43d65
8de122a
5080920
a8b7d84
9331fc6
3a19c3d
1785ab9
871e84d
9660334
10be9d4
b161115
4796599
209c130
6582c4c
7b66aa8
4f3d33e
d0abe56
b9a6f5e
ee935d6
11c3c40
741d474
d3d5331
14989d2
87e6b2c
fa4038c
3da20fc
4f8d78f
db8b525
a857f98
25eddac
3516183
fd768fd
568f734
f2b3ac4
819ceff
0ec3ac9
bddee42
b005cde
7d0b694
9243443
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,81 @@ | ||
require 'awesome_print' | ||
MAX_NUM_OF_TILES_ALLOWED = 7 | ||
|
||
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)) | ||
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. 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) | ||
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. You already have a |
||
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 | ||
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 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) | ||
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. NIIIICE! |
||
end | ||
|
||
end | ||
end |
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] | ||
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. 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 |
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 | ||
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. 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 |
||
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 |
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") | ||
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. 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") | ||
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. You should also test to verify that |
||
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") | ||
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. What happens with |
||
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 |
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 | ||
|
@@ -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'] | ||
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. 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 |
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 use of a constant!