Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a9ffe72
Setup directory structure and spec boilerplate;
sophiabaldonado Mar 8, 2016
a002da2
Added constant hash of scoring values.
jlikesplants Mar 8, 2016
3281ee9
Created passing test for SCORE_CHART hash.
jlikesplants Mar 8, 2016
4085b3e
Created search_score_chart method that returns the key value pair for…
sophiabaldonado Mar 8, 2016
356b572
Fixed spec to account for Scrabble module shortcut;
sophiabaldonado Mar 8, 2016
52c65a9
Restructured SCORE_CHART hash, created method to find the points for …
jlikesplants Mar 8, 2016
fd33954
Created method to create and array of points for a given word; Create…
sophiabaldonado Mar 8, 2016
4390d5b
Finished score method by adding bonus for 7 letter words. All tests p…
jlikesplants Mar 8, 2016
9813cac
Made method to find highest scoring word in an array and return the w…
jlikesplants Mar 8, 2016
b2cd6c5
Extracted methods from highest_score_from and made corresponding tests;
sophiabaldonado Mar 9, 2016
4b4759e
Extracted sorting of word-points pairs from word_pairs method to be a…
jlikesplants Mar 9, 2016
de049cd
deleted comments
jlikesplants Mar 9, 2016
c155bc8
Fixed circular require warning wooooo;
sophiabaldonado Mar 10, 2016
de4d364
Created Player class with initialize(attr reader methods for name & w…
jlikesplants Mar 10, 2016
b595750
Fixed require_relative relationships;
sophiabaldonado Mar 10, 2016
70e792f
nothing
jlikesplants Mar 10, 2016
94fd250
Actually fixed all the require_relatives lol;
sophiabaldonado Mar 10, 2016
6cd8e5a
Added ties_w_seven_letters method for wave 1 requirement; Created tot…
sophiabaldonado Mar 10, 2016
8fd3692
Finished total_score method in Player class.
jlikesplants Mar 10, 2016
83200f5
Added highest_scoring_word and highest_word_score methods to Player. …
jlikesplants Mar 10, 2016
946361e
Struggled with errors. Appears to be resolved by NOT USING CONSTANTS …
jlikesplants Mar 11, 2016
143c956
Created TileBag class initialized with tilebag array of letters; Crea…
sophiabaldonado Mar 11, 2016
978a654
Added tiles_remaining method to TileBag and added test.
jlikesplants Mar 11, 2016
83c4098
Added tiles_remaining test!
jlikesplants Mar 11, 2016
39e7586
Created a draw_tiles method in Player class that draws tiles from an …
sophiabaldonado Mar 11, 2016
af91c73
Added removes_played_letters method so that when a word is player, th…
jlikesplants 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
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
66 changes: 66 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#name: returns the value of the @name instance variable
#play(word): Adds the input word to the plays Array
# Returns false if player has already won
# Returns the score of the word
#plays: returns an Array of the words played by the player
#total_score: Returns the sum of scores of played words
#won?: If the player has over 100 points, returns true, otherwise returns false
#highest_scoring_word: Returns the highest scoring played word
#highest_word_score: Returns the highest_scoring_word score

class Scrabble::Player
attr_reader :name, :words_played, :player_tiles
attr_writer :player_tiles

def initialize(name)
@name = name
@words_played = []
@player_tiles = []
# @tilebag = Scrabble::TileBag.new
end

def won?
total_score >= 100
end

def play(word)
# adds word to array of words
@words_played << word
# Returns false if player has already won
word_score = Scrabble::Scoring.score(word)
remove_played_letters(word)
won? ? false : word_score
# scores word & returns score
end

def remove_played_letters(word)
# removes played letters from player_tiles
word_letters = Scrabble::Scoring.word_letters(word)
word_letters.length.times do
@player_tiles.delete(word_letters.find { |letter| letter})
word_letters.shift
end
end

def total_score
Scrabble::Scoring.word_scores(words_played).reduce(:+)
end

def highest_scoring_word
Scrabble::Scoring.highest_score_from(words_played, Scrabble::RWORD)
end

def highest_word_score
p words_played
Scrabble::Scoring.highest_score_from(words_played, Scrabble::RPOINTS)
end

def draw_tiles(tile_bag)
tile_bag.draw_tiles(7 - player_tiles.length).each do |letter|
@player_tiles << letter
end

end


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

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

# Take a word, upcase it, and return an array of its letters
def self.word_letters(word)
word.upcase.split(//)
end

# Finds the point value of each letter given
def self.letter_points(letter)
SCORE_CHART.fetch(letter)
end

# Finds point value of each letter in a word
# Creates an array of points for each letter
def self.word_points(word)
word_letters(word).map { |i| letter_points(i) }
end

# Sums the points from the word_points array
def self.score(word)
total = 0
# Adds a bonus for 7-letter words.
if word_points(word).length == 7 then total = 50 end
total += word_points(word).reduce(:+)
total
#=> Fixnum score of word Ex: if word = "dumpling" returns 14
end

def self.word_scores(array_of_words)
# word_scores stores the points of each word in the array_of_words
array_of_words.map { |word| score(word) }
#=> Ex: TEST_WORD_ARRAY3 => [5, 20, 7, 20, 3, 70]
end

def self.word_score_pairs(array_of_words)
# scores each word in array_of_words, then zips scores to words, creating subarray pairs - still in original TEST_WORD_ARRAY order.
array_of_words.zip(word_scores(array_of_words))
#=> Ex: TEST_WORD_ARRAY3 => [["cat", 5], ["jeez", 20], ["foot", 7], ["furzy", 20], ["see", 3], ["bcmpbcg", 70]]
end

def self.sort_pairs(array_of_words)
# takes array from word_score_pairs and sorts by points.
word_score_pairs(array_of_words).sort_by { |pair| pair[1]}
#=> Ex: TEST_WORD_ARRAY3 => [["see", 3], ["cat", 5], ["foot", 7], ["jeez", 20], ["furzy", 20], ["bcmpbcg", 70]]
end

def self.find_ties(array_of_words)
# If multiple words have the same highest-score, return the word with the least letters
sort_pairs(array_of_words).find_all { |pair| pair[1] == sort_pairs(array_of_words)[-1][1] }
#=> Ex: TEST_WORD_ARRAY3 => [["bcmpbcg", 70]]
#=> Ex: TEST_WORD_ARRAY2 => [["jeez", 20], ["furzy", 20]]
end

def self.ties_w_seven_letters(array_of_words)
find_ties(array_of_words).find_all { |word, points| word.length == 7 }
#=> Ex: TEST_WORD_ARRAY5 => [["bgeeeee", 60]]
end

# Returns the highest-valued word in a word_array.
def self.highest_score_from(array_of_words, index)
if find_ties(array_of_words).length > 1
if ties_w_seven_letters(array_of_words).length >= 1
return ties_w_seven_letters(array_of_words)[0][index]
else
least_letters = find_ties(array_of_words).min_by { |pair| pair[0].length}
return least_letters[index]
end
end
# Returns the highest-scoring word.
p sort_pairs(array_of_words)[-1][index]
# "affined"
# "bcmpbcg"
end
end
68 changes: 68 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class Scrabble::TileBag

attr_reader :tilebag

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

def draw_tiles(num)
@tilebag = @tilebag.shuffle
tiles_drawn = @tilebag.shift(num)
end

def tiles_remaining
@tilebag.length
end

end

#
# # We used this to generate tilebag array :o)
# #
# h = {
# "A" => 9,
# "N" => 6,
# "B" => 2,
# "O" => 8,
# "C" => 2,
# "P" => 2,
# "D" => 4,
# "Q" => 1,
# "E" => 12,
# "R" => 6,
# "F" => 2,
# "S" => 4,
# "G" => 3,
# "T" => 6,
# "H" => 2,
# "U" => 4,
# "I" => 9,
# "V" => 2,
# "J" => 1,
# "W" => 2,
# "K" => 1,
# "X" => 1,
# "L" => 4,
# "Y" => 2,
# "M" => 2,
# "Z" => 1
# }
#
# a = []
#
# h.each do |letter, count|
# count.times do
# a << letter
# end
# end
#
# p a
13 changes: 13 additions & 0 deletions scrabble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Scrabble

# can we put highest_score methods from player and scoring in here and import them in those classes? use super?

require_relative 'lib/scoring'
require_relative 'lib/player'
require_relative 'lib/tilebag'

RWORD = 0 # return word in methods that are double-indexed
RPOINTS = 1 # return points in methods that are double-indexed


end
98 changes: 98 additions & 0 deletions specs/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'simplecov'
SimpleCov.start

require_relative 'spec_helper'

describe Scrabble::Player do
# call Dede! It's time for Scrabble!
def dede
dede = Scrabble::Player.new("Dede")
end

it "exists" do
Scrabble::Player.wont_be_nil
end

it "should require a name parameter" do
dede = Scrabble::Player.new("Dede")
assert_equal "Dede", dede.name
end

describe "Scrabble::Player#won?" do
it "returns true for a winning score of 100 or more and false for any lower score" do
dede = Scrabble::Player.new("Dede")
dede.play("kitteh")
dede.play("cat")
dede.play("ZZZZZZZ")
assert_equal true, dede.won?
end
end


describe "Scrabble::Player#play(word)" do
it "should push the played word into the words_played array" do
dede = Scrabble::Player.new("Dede")
dede.play("dumpling")
assert dede.words_played.include?("dumpling")
end

# need to test this method for when won? is true
it "returns the correct score for a played word" do
dede = Scrabble::Player.new("Dede")
assert_equal 14, dede.play("dumpling")
end

it "removes the letters in a played word from player_tiles" do
dede = Scrabble::Player.new("Dede")
new_tilebag = Scrabble::TileBag.new
dede.draw_tiles(new_tilebag)
print "HERE #{dede.player_tiles} END"
sample_tiles = dede.player_tiles[0..2]
dede.play(sample_tiles.join)
p dede.player_tiles
end
end

describe "Scrabble::Player#total_score" do
it "returns the sum of scores in words_played" do
dede = Scrabble::Player.new("Dede")
dede.play("dumpling") # 14
dede.play("donut") # 6
dede.play("doodoo") # 8
assert_equal 28, dede.total_score
end
end

describe "Scrabble::Player#highest_scoring_word" do
it "returns the highest-scoring word in words_played" do
dede = Scrabble::Player.new("Dede")
dede.play("dumpling") # 14
dede.play("donut") # 6
dede.play("doodoo") # 8
assert_equal "dumpling", dede.highest_scoring_word
end
end

describe "Scrabble::Player#highest_word_score" do
it "returns the highest score from words_played" do
dede = Scrabble::Player.new("Dede")
dede.play("dumpling") # 14
dede.play("donut") # 6
dede.play("doodoo") # 8
assert_equal 14, dede.highest_word_score
end
end

describe "Scrabble::Player#draw_tiles" do
it "draws a number of tiles from TileBag to make player_tiles have 7 items" do
new_tilebag = Scrabble::TileBag.new
doris = Scrabble::Player.new("Doris")
doris.player_tiles = ["a", "b", "c"]
p doris.player_tiles
doris.draw_tiles(new_tilebag)
p doris.player_tiles
end
end


end
Loading