Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d6de977
This is ready for code.
rishallen Mar 8, 2016
ce36441
added constant LETTERS to scoring.rb and tried to make a test in scor…
DStorck Mar 8, 2016
b68f6aa
added a test that is testing the letter and value.
rishallen Mar 8, 2016
2220eb1
created letter.value method in scoring.rb, and the test we wrote befo…
DStorck Mar 8, 2016
b2b4b27
connected the Scrabble module with the class Scoring. Wrote test and …
rishallen Mar 8, 2016
dae4b75
added test in scoring_spec.rb to test word value
DStorck Mar 8, 2016
2f912c6
added the score method that gets the score for each word.
rishallen Mar 8, 2016
ac32fa1
we wrote a test and a method to find value of passed word AND IT WORKS
DStorck Mar 8, 2016
7a1cb32
created some functions that were not needed. Wrote a test for highest…
rishallen Mar 9, 2016
3f38b2f
created 2 methods, both take in words and scores and stores them in a…
rishallen Mar 9, 2016
1f65c12
added methods to scoring.rb and scoring_spec.rb that may later be use…
DStorck Mar 9, 2016
82ae8d8
added highest score method and was able to test the conditionals and …
rishallen Mar 10, 2016
6d01170
added player class and added initilize method and plays method and tw…
DStorck Mar 10, 2016
5a23572
worte a test. will continue to work on it.
rishallen Mar 10, 2016
23c0af8
modified player.rb test for plays
DStorck Mar 10, 2016
9ad10d0
Created the method total score and developed two tests that checked t…
rishallen Mar 10, 2016
715555e
polished won? method and created two tests for it. all tests are work…
DStorck Mar 10, 2016
fbd1376
2 methods created to test highest score and highest scoring word.
rishallen Mar 10, 2016
ce8f622
wrote tilebag class with draw_tiles method and test, and set up const…
DStorck Mar 10, 2016
5a36096
worked on the remaining methods for the tilebag class. The return dis…
rishallen Mar 11, 2016
b358fd7
changed how the default tiles are initialized and now we are finding …
DStorck Mar 11, 2016
8db73f2
created display all tiles method that would give an array of the lett…
rishallen Mar 11, 2016
a1e7182
We rock. we added details to tilebag method to allow for selecting m…
DStorck Mar 11, 2016
d5bc8d9
checked our tests to see if they continued to work with the methods.
rishallen Mar 11, 2016
5403477
added def tiles to player class
DStorck Mar 11, 2016
fa571f2
completed one test with new method. Will need to complete the last one.
rishallen Mar 11, 2016
3797e80
We finished the project
DStorck 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.0 -version
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scrabble
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
66 changes: 66 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# require_relative "../scrabble"

class Scrabble::Player
WINNING_SCORE = 100
attr_accessor :played_words
attr_reader :name, :their_tiles
def initialize(name, tilebag)
@name = name
@played_words = []
@tilebag = tilebag #starts up a tilebag when the player begins a game
@their_tiles = @tilebag.draw_tiles(7) #draws 7 tiles for them
end

def plays
return @played_words #shows which words they have played
end

def won?
return true if total_score > WINNING_SCORE
return false
end

def play(word)
@played_words << word
return false if won?
return Scrabble::Scoring.score(word)
end

def total_score
total_score = []
@played_words.each do |word|
total_score << Scrabble::Scoring.score(word)
end
return total_score.reduce(0, :+)
end

def highest_scoring_word
high_score_word = plays.max_by do |word|
Scrabble::Scoring.score(word)
end
return high_score_word
end

def highest_word_score
highest_scored_word = plays.max_by do |word|
Scrabble::Scoring.score(word)
end
return Scrabble::Scoring.score(highest_scored_word)
end

def tiles
return their_tiles
end
# checks to see how many tiles they need and fills them up if needed and if tiles are left in bag
def draw_tiles
num = 7 - @their_tiles.length
# fills tiles array until it has 7 letters from the given tile bag
if num > 0
new_tiles = @tilebag.draw_tiles(num)
@their_tiles << new_tiles
return @their_tiles.flatten!
end
end


end
69 changes: 69 additions & 0 deletions lib/scoring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# require_relative "../scrabble"

class Scrabble::Scoring
SEVEN_LETTER_BONUS = 50
#PLAYED_WORDS = []
#WORD_SCORE_COLLECTION =[]

LETTERS = {
%w(A E I O U L N R S T) => 1,
%w(D G) => 2,
%w(B C M P) => 3,
%w(F H V W Y) => 4,
%w(K) => 5,
%w(J X) => 8,
%w(Q Z) => 10
}

def self.letter_value(letter)
LETTERS.each do |key, value|
if key.include? letter
return value
end
end
end

def self.score(word)
word_score = 0
bonus = SEVEN_LETTER_BONUS
word_score += bonus if word.length == 7
word_array = word.split("")
word_array.each do |letter|
temp_letter_val = letter_value(letter)
word_score += temp_letter_val
end
return word_score
end

#return highest score from array_of_tiles
#in case of tie :
# -better to use fewer tiles
# -if top scores is between multiple words and one with 7, one using 7 win_words
# - multiple words with same score and same length, pick first
def self.highest_score_from(word_array)
by_scores = word_array.group_by { |word| self.score(word) }
win_words = by_scores.max[1]
if win_words.length == 1
return win_words[0]
elsif win_words.any? { |word| word.length == 7 }
seven_letter_words = win_words.select { |word| word.length == 7}
return seven_letter_words.first
else
min_word = win_words.min_by { |word| word.length }
return min_word
end
end

end

#we were experimenting with several versions of this method.
#=======================================================================
# def self.score(word)
# value_array = []
# x = (word).split("")
# x.each do |letter|
# value_array << letter_value(letter)
# end
# total = value_array.inject(:+)
# return word, total
# end
82 changes: 82 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# require_relative '../scrabble'
class Scrabble::Tilebag
attr_accessor :default_tiles

def initialize
@default_tiles = new_bag
end

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


#returns an array of all actual tiles, to allow user to pick more than one of the same letter tile
#we incorporated this into our draw_tiles method because .sample was not letting that happen
def display_all_tiles
array = []
@default_tiles.each do |key, value|
array << (key * value).split("")
end
return array.flatten
end

#check to see if the numbers of tiles player is trying to draw are available,
# then removes them from tile bag and returns new tiles
# ex - if player tries to draw 6 and there are only 4, it gives them those 4 tiles
def draw_tiles(num)
if num <= tiles_remaining
our_tiles = display_all_tiles.sample(num)
tile_removal(our_tiles)
return our_tiles
else
print "Sorry, there are only #{tiles_remaining} left."
our_tiles = display_all_tiles.sample(tiles_remaining)
tile_removal(our_tiles)
return our_tiles
end
end

#added up number of tiles using INJECT!
def tiles_remaining
@default_tiles.values.inject(0, :+)
end

#deletes tiles from tile_bag, when there are no tiles left, it remove that key/value pair from bag
def tile_removal(array_of_tiles)
array_of_tiles.each do |letter|
if @default_tiles.keys.include?(letter)
@default_tiles[letter] -= 1
end
@default_tiles.delete_if {|key, value| value == 0 }
end
end

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



end

require_relative "./lib/scoring"
require_relative './lib/player'
require_relative './lib/tilebag'
109 changes: 109 additions & 0 deletions specs/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
require_relative './spec_helper'
require_relative '../scrabble'
require_relative '../lib/player'



describe Scrabble::Player do

before do
@our_tile_bag = Scrabble::Tilebag.new
end

it "this does exist" do
Scrabble::Player.wont_be_nil
end


describe "InitializeInstanceVariable" do
it "should create instance of player name" do
Scrabble::Player.new("Tim", @our_tile_bag).must_be_instance_of(Scrabble::Player)
end
end

describe "ReturnWordArray" do
it "should return an Array of the words played by player" do
Scrabble::Player.new("Tim", @our_tile_bag).plays.must_equal([])
end
end

describe "PlayWordsMethodExists" do
it "should input a new word to the plays array" do
risha = Scrabble::Player.new("risha", @our_tile_bag)
risha.play("RED")
risha.plays.must_equal(["RED"])
end
end

describe "PlayWordReturnValue" do
it "should return score of word, if player has not won" do
risha = Scrabble::Player.new("risha", @our_tile_bag)
risha.play("RED").must_equal(4)
end
end

describe "PlayWordReturnValue" do
it "should return false if player has won" do
risha = Scrabble::Player.new("risha", @our_tile_bag)
risha.play("ZZZZZZZZZZZZ").must_equal(false)
end
end

describe "TotalScore" do
it "should return the total score of the played words array" do
risha = Scrabble::Player.new("risha", @our_tile_bag)
risha.play("RED")
risha.play("HOTDOG")
risha.total_score.must_equal(15)
end
end

describe "Won?Methodreturn_True" do
it "should return true if player has over 100 points" do
risha = Scrabble::Player.new("risha", @our_tile_bag)
risha.play("ZZZZZZZZZZZZ")
risha.won?.must_equal(true)
end
end

describe "Won?Methodreturn_false" do
it "should return false if player has fewer than 100 points" do
risha = Scrabble::Player.new("risha", @our_tile_bag)
risha.play("RED")
risha.won?.must_equal(false)
end
end

describe "HighestScoringWord" do
it "should return the highest scoring word" do
risha = Scrabble::Player.new("risha", @our_tile_bag)
risha.play("RED")
risha.play("HOTDOG")
risha.highest_scoring_word.must_equal("HOTDOG")
end
end

describe "HighestWordScore" do
it "should return the highest score" do
risha = Scrabble::Player.new("risha", @our_tile_bag)
risha.play("RED")
risha.play("HOTDOG")
risha.highest_word_score.must_equal(11)
end
end

describe "PlayerClass-tiles_method" do
it "should return a collection of tiles player can play(max7)" do
puppet = Scrabble::Player.new("puppet", @our_tile_bag)
puppet.tiles.length.must_equal(7)
end
end

describe "DrawTiles" do
it "should not do anything if bag is full" do
betty = Scrabble::Player.new("betty", @our_tile_bag)
betty.draw_tiles.must_be_nil
end
end

end
Loading