Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
894ee0c
Created all boilerplate files and folders
digivava Mar 8, 2016
6b04c4c
Added class Scoring to scoring.rb and the module to scrabble.rb, and …
digivava Mar 8, 2016
ffba7b8
Got our first test pass which accepts Scrabble::Scoring as an object.
digivava Mar 8, 2016
fdd7086
We added the score test for Scrabble::Score.score and added self.scor…
yordanosd Mar 8, 2016
295bc58
Refactored the test we're using for checking score so it can work wit…
digivava Mar 8, 2016
ea7c831
Updated self.score method to include the ability to score word with m…
digivava Mar 8, 2016
9b79883
added tenary that will add additional 50 points to score if all 7 til…
yordanosd Mar 8, 2016
3a0675d
added self.highest_score_from(array_of_words) method to scoring class…
yordanosd Mar 8, 2016
0dad829
Added tiebreaker test method, for deciding what to do when there are …
digivava Mar 8, 2016
abca174
Added if statements that would look at the array_of_words and return …
yordanosd Mar 9, 2016
4f643f0
Fixed up the tiebreaker check with if-statements
digivava Mar 9, 2016
c39ff99
Added if-statement to handle multiple words that are same length, sam…
digivava Mar 9, 2016
5077582
left some code at the bottom of the page that simplifies the self.hig…
yordanosd Mar 10, 2016
d743526
Did necessary boilerplate for player_spec.rb, tested @name for an ins…
digivava Mar 10, 2016
0e9dad2
Added tests for the plays and play(word) methods and passed yaaay
digivava Mar 10, 2016
e54867f
we added the method won?(score) that returns false if the player has …
yordanosd Mar 10, 2016
cf16c52
changed won? to return true/false and adjusted play(word) method to a…
yordanosd Mar 10, 2016
7d4472a
completed #total_score: Returns the sum of scores of played words
yordanosd Mar 10, 2016
bb24c4c
Corrected won? method so it no longer accepts a parameter, instead, i…
digivava Mar 10, 2016
e86e1ab
Fixed test so that the #plays and play word methods are in separate d…
digivava Mar 10, 2016
ecc1241
Added highest_scoring_word and highest_word_Score methods which use h…
digivava Mar 10, 2016
682d937
Changed @words_played_array to @plays so that it matches the method name
digivava Mar 11, 2016
e496956
Added a couple more specs to test for won? method returning true or f…
digivava Mar 11, 2016
00d5e4c
we started wave 3 and created TileBag.rb and Tilebag_spec. We also go…
yordanosd Mar 11, 2016
704dc29
changed our tiles variable so that each letter has its own array
yordanosd Mar 11, 2016
947db74
completed draw_tiles(num) method and passed the test
yordanosd Mar 11, 2016
3014ce9
Fixed draw_tiles(num) method so it uses the index of the tile instead…
digivava Mar 11, 2016
48cf4a3
Added tiles and draw_tiles(tile_bags) methods to Player class
digivava Mar 11, 2016
5d4fb6d
Tests still failing but we made the tiebreaker system way more readable
digivava Mar 11, 2016
108d347
Went back to the old version of our project because we couldn't get t…
yordanosd Mar 12, 2016
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
Binary file added .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Scrabble
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['specs/*_spec.rb']
end

task default: :test
72 changes: 72 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#require_relative '../scrabble' # allows this class to access scrabble and run through irb

class Scrabble::Player
include Scrabble
MAX_TILES_ON_TRAY = 7
attr_reader :name

def initialize(player_info)
@name = player_info[:name]
@plays = []
@tiles = [] # these are the tiles that the player has on their little tray
@tilebag = Scrabble::TileBag.new
draw_tiles(@tilebag)
end

# thought about putting this in attr_accessor, but that would let the player edit it in IRB???
def plays
@plays
end

def play(word)
@plays << word
score = Scrabble::Scoring.score(word)
won? == true ? false : score
#return false if the player has already won

end

def words_and_matching_scores
total_score_hash = {}

plays.each do |word|
total_score_hash[word] = Scrabble::Scoring.score(word)
end
total_score_hash
end

def total_score
# gets value from the scores_for_each_word method
words_and_matching_scores.values.reduce(:+)
end

def won?
total_score >= 100 ? true : false
end

def highest_scoring_word
highest_score[0]
end

def highest_score
high_score = words_and_matching_scores.max_by do |word, score|
score
end
high_score
end

def highest_word_score
highest_score[1]
end

# thought about putting this in attr_accessor, but that would let the player edit it in IRB???
def tiles
@tiles
end

def draw_tiles(tile_bag)
num = MAX_TILES_ON_TRAY - tiles.length
@tiles = tile_bag.draw_tiles(num)
end

end
62 changes: 62 additions & 0 deletions lib/scoring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#require_relative '../scrabble' # allows this class to access scrabble and run through irb

class Scrabble::Scoring
include Scrabble
LETTER_POINTS = {
1 => ["A", "E", "I", "O", "U", "L", "N", "R", "S", "T"],
2 => ["D", "G"],
3 => ["B", "C", "M", "P"],
4 => ["F", "H", "V", "W", "Y"],
5 => ["K"],
8 => ["J", "X"],
10 => ["Q", "Z"]
}

# returns the score value for a single given word
def self.score(word)
score = 0
tiles = 0

word_array = word.upcase.split("")

word_array.each do |letter|
LETTER_POINTS.each do |points, letters_array|
if letters_array.include? letter
score += points
tiles += 1
end
end
end
# if all seven tiles are used the below tenary will add 50 points to the users score else return score
tiles == 7 ? score += 50 : score
score
end

def self.highest_score_from(array_of_words) # array_of_words = ["cats", "fee", "no"]

array_of_word_values = []
array_of_words.each do |word|
array_of_word_values << [word, self.score(word), word.length] # = [["cats", 7, 4], ["fee", 7, 3], ["no", 2, 2]]
end

highest_score_array = array_of_word_values[0] # ["cats", 7, 4]

array_of_word_values.each do |word_array| # ["cats", 7, 4]
# compares the score of the words
if word_array[1] == highest_score_array[1] # compare score
# compares the length of the words
if word_array[2] < highest_score_array[2]
highest_score_array[2] == 7 ? highest_score_array : highest_score_array = word_array
#highest_score_array = word_array # replace the highest score to the word_array being referenced
elsif word_array[2] >= highest_score_array[2]
if highest_score_array[2] < 7
word_array[2] == 7 ? highest_score_array = word_array : highest_score_array
end
end
elsif word_array[1] > highest_score_array[1]
highest_score_array = word_array
end
end
return highest_score_array[0]
end
end
58 changes: 58 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# require_relative './scrabble.rb'

class Scrabble::TileBag

include Scrabble

attr_reader :default_tiles

def initialize
@default_tiles = [["A", 1], ["A", 1], ["A", 1], ["A", 1], ["A", 1], ["A", 1], ["A", 1], ["A", 1], ["A", 1],
["B", 1], ["B", 1],
["C", 1], ["C", 1],
["D", 1], ["D", 1], ["D", 1], ["D", 1],
["E", 1], ["E", 1], ["E", 1], ["E", 1], ["E", 1], ["E", 1], ["E", 1],
["E", 1], ["E", 1], ["E", 1], ["E", 1], ["E", 1],
["F", 1], ["F", 1],
["G", 1], ["G", 1],["G", 1],
["H", 1], ["H", 1],
["I", 1], ["I", 1], ["I", 1], ["I", 1], ["I", 1], ["I", 1], ["I", 1], ["I", 1], ["I", 1],
["J", 1],
["K", 1],
["L", 1], ["L", 1], ["L", 1], ["L", 1],
["M", 1], ["M", 1],
["N", 1], ["N", 1], ["N", 1], ["N", 1], ["N", 1], ["N", 1],
["O", 1], ["O", 1], ["O", 1], ["O", 1], ["O", 1], ["O", 1], ["O", 1], ["O", 1],
["P", 1], ["P", 1],
["Q", 1],
["R", 1], ["R", 1], ["R", 1], ["R", 1], ["R", 1], ["R", 1],
["S", 1], ["S", 1], ["S", 1], ["S", 1],
["T", 1], ["T", 1], ["T", 1], ["T", 1], ["T", 1], ["T", 1],
["U", 1], ["U", 1], ["U", 1], ["U", 1],
["V", 1], ["V", 1],
["W", 1], ["W", 1],
["X", 1],
["Y", 1], ["Y", 1],
["Z", 1]]
end

def draw_tiles(num)

array_of_selected_tiles =[]

# picks a random number that will be used as the index value for selecting a tile,
# and then it adds the tile that has that index to array_of_selected_words, then
# it will delete the tile at that index
num.times do
selected_tile_index = rand(0..default_tiles.length)
array_of_selected_tiles << @default_tiles[selected_tile_index]
@default_tiles.delete_at(selected_tile_index)
end
array_of_selected_tiles
end

def tiles_remaining
default_tiles.length
end

end
12 changes: 12 additions & 0 deletions scrabble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

# using this module as a namespace, not a mixin
module Scrabble


end

# putting this at the end because otherwise it will read scoring.rb before it creates
# the Scrabble module, meaning the code in scoring.rb won't work
require_relative './lib/scoring'
require_relative './lib/player'
require_relative './lib/tilebag'
130 changes: 130 additions & 0 deletions specs/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
require_relative './spec_helper'

describe Scrabble::Player do
it "should be an object we have access to" do
Scrabble::Player.wont_be_nil
end
end

describe "Scrabble::Player#name" do
# returns the value of the @name instance variable
it "should return the value 'Joe' when player instance is named Joe" do
joe = Scrabble::Player.new(name: "Joe")
joe.name.must_equal ("Joe")
end
end

describe "Scrabble::Player#plays" do
joe = Scrabble::Player.new(name: "Joe")

it "should return the score '5' for the word 'cat'" do
joe.play("cat").must_equal 5
end

end

describe "Scrabble::Player#play(word)" do
mary = Scrabble::Player.new(name: "Mary")

it "should return false if player has already won" do
# starts mary off with a bunch of words already played, then adds "cat"
array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]

# create an each method that will play the words in the array one by one
array_of_words.each do |word_played|
mary.play(word_played)
end

mary.play("cat").must_equal false
end
end

describe "Scrabble::Player#total_score" do
jane = Scrabble::Player.new(name: "Jane")
it %!should return '243' for array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]! do
array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]

# create an each method that will play the words in the array one by one
array_of_words.each do |word_played|
jane.play(word_played)
end

jane.total_score.must_equal 243
end
end

describe "Scrabble::Player#highest_scoring_word" do
april = Scrabble::Player.new(name: "April")

# it should return the highest scoring played WORD
it %!should return "frog" for the words played "frog" and "nose"! do
april.play("frog")
april.play("nose")
april.highest_scoring_word.must_equal "frog"
end

it %!should return 'aaaaaad' for array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]! do
fox = Scrabble::Player.new(name: "Fox")
array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]
# create an each method that will play the words in the array one by one
array_of_words.each do |word_played|
fox.play(word_played)
end

fox.highest_scoring_word.must_equal "aaaaaad"
end


end

describe "Scrabble::Player#highest_word_score" do
jody = Scrabble::Player.new(name: "Jody")

# it should return the highest scoring played WORD
it %!should return "8" for the words played "frog" and "nose"! do
jody.play("frog")
jody.play("nose")
jody.highest_word_score.must_equal 8
end

it %!should return '58' for array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]! do
fox = Scrabble::Player.new(name: "Fox")
array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]
# create an each method that will play the words in the array one by one
array_of_words.each do |word_played|
fox.play(word_played)
end

fox.highest_word_score.must_equal 58
end

end

describe "Scrabble::Player#won?" do

it %!should return 'true' for array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]! do
fox = Scrabble::Player.new(name: "Fox")
array_of_words = ["nose", "aaaaaad", "eeeeeed", "frog", "rrrrrrr", "qqqqqj"]
# create an each method that will play the words in the array one by one
array_of_words.each do |word_played|
fox.play(word_played)
end

fox.won?.must_equal true
end

it %!should return "false" for the words played "frog" and "nose"! do
jody = Scrabble::Player.new(name: "Jody")
jody.play("frog")
jody.play("nose")
jody.won?.must_equal false
end
end

describe "Scrabble::Player#tiles" do
bill = Scrabble::Player.new(name: "Bill")
# should return a collection of letters that the player can play (max 7) -- that's their own personal little tray of letters
it "should return 7 letters" do
bill.tiles.length.must_equal 7
end
end
Loading