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

Alma and Dianne -- Scrabble #12

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f753289
added necessary files, began adding a constant for Scrabble tile values
dlaguerta Aug 30, 2016
4d1091c
added 3 specs to Scoring and passed
dlaguerta Aug 30, 2016
8661b7e
Wave1 methods have basic functionality, four tests
mimyal Aug 30, 2016
03b392a
fixed spec to confirm the score of word
dlaguerta Aug 30, 2016
904b463
Wave1 with one tiebreaker, 7 tests pass
mimyal Aug 30, 2016
7595ca8
First Wave finished, added 8 tests
dlaguerta Aug 31, 2016
6d44b9d
Started Wave 2, 3.5 tests
mimyal Aug 31, 2016
f5c510c
Added simplecoverage to spec_helper
mimyal Aug 31, 2016
af0c1d2
added one more test for play(word) and made it pass
dlaguerta Aug 31, 2016
e413be8
total_score method and one more test, infinite loops disrupted
mimyal Aug 31, 2016
908b9d7
added specs and methods for highest scoring word method and won? method
dlaguerta Aug 31, 2016
1f16042
Wave2 and 10 tests complete
mimyal Aug 31, 2016
74c88bb
added a new spec for Scoring, working on more specs
dlaguerta Sep 1, 2016
5862d60
added spec to raise error when words are not played
dlaguerta Sep 1, 2016
8543f9e
TileBag under construct. Stuck on #draw_tiles. Have four tests.
mimyal Sep 2, 2016
a7a00df
fixed spec for changing tile bag, now using a constant hash without a…
dlaguerta Sep 2, 2016
9fbf6ea
make sure everything is updated
dlaguerta Sep 2, 2016
bc61a0c
draw tiles now moves TILES into an 87 long array, exception error for…
mimyal Sep 2, 2016
4adb7fb
all requirements but final player method for draw_tiles finished; cur…
dlaguerta Sep 2, 2016
8820144
I think we finished yay
dlaguerta Sep 2, 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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Create a `Scrabble::Scoring` class with a __minimum of 8 specs__. The class shou
- There is a bonus for words that are seven letters. If the top score is tied between multiple words and one used all seven letters, choose the one with seven letters over the one with fewer tiles.
- If the there are multiple words that are the same score and same length, pick the first one in the supplied list.

<!--

## Wave 2
### Primary Requirements
Create a `Scrabble::Player` class with a __minimum of 11 specs__. The only required paramter for instances of the class is the player's `name`. Instances of the class should repond to the following messages:
Expand Down Expand Up @@ -110,4 +110,3 @@ Create specs for (__minimum 2__) and add to the `Player` class the following ins
### Optional Enhancements
- Create a `Scrabble::Dictionary` class that includes a method (class or instance) for searching a list of words to determine if a given word is valid (__minimum of 5 specs__).
- Create a `Scrabble::Board` class (__minimum of 15 specs__) that has a matrix (array of arrays) of tile places. Check if a word can be played on a given tile place in a certain direction (up/down or left/right).
-->
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rake/testtask'

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

task default: :test
76 changes: 76 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require_relative "scoring"

module Scrabble
class Player

attr_reader :name, :winner, :words_played, :tile_tray

def initialize(name)
@name = name
@words_played = []
@winner = false
@tile_tray = []
end

def plays
return @words_played
end

def play(word)
raise ArgumentError.new("Invalid word") if word.upcase[/[A-Z]+/] != word.upcase
if @winner == true
return false
end
word_score = Scoring.score(word) #return score
@words_played << word # dont forget this adds to instance variable every time it runs

return word_score
end

def total_score
player_score = 0
plays.each do |word|
current_score = Scoring.score(word)
player_score += current_score
end
return player_score
end

def won?
if total_score > 100
return true
else
return false
end
end #end won? method

def highest_scoring_word
best_word = Scoring.highest_score_from(plays)
return best_word
end

def highest_word_score
best_score = Scoring.score(highest_scoring_word)
return best_score
end

def tiles
return @tile_tray
end

def draw_tiles(tile_bag)
begin
result = tile_bag.draw_tiles(1)
@tile_tray << result
end until @tile_tray.length == 7
return @tile_tray

end # @todo finish this method

end





end
92 changes: 92 additions & 0 deletions lib/scoring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# @todo fix error for empty word
module Scrabble

class Scoring

TILES = {
"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
}
SEVEN_LETTER_BONUS = 50

attr_reader :word

def initialize(word)
@word = word


end

def self.score(word) #won't raise error for '' whyyyy
invalid_words = word.upcase[/[^A-Z]/]
current_word = word.upcase

raise ArgumentError.new("Invalid word") if
invalid_words == current_word
score = 0
word_array = word.upcase.split(//)
word_array.each do |letter|
score += Scrabble::Scoring::TILES[letter]
end
if word_array.length !=7
return score
else

return score += SEVEN_LETTER_BONUS
end
end

def self.highest_score_from(array_of_words)
max_score = 0
max_words = []
winning_word = 'PIZZAZZas'
array_of_words.each do |word|
current_score = score(word)
if current_score == max_score
max_words << word
elsif current_score > max_score
max_words = [word]
max_score = current_score
else
#karion
end
end #each
max_words.each do |word|
if word.length < winning_word.length
winning_word = word
elsif word.length == 7
winning_word = word
return winning_word
else
# karion
end
end
return winning_word # will return the first if tie breaker
end
end

end
64 changes: 64 additions & 0 deletions lib/tile_bag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module Scrabble
class TileBag

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

attr_reader :game_tile_bag

def initialize
@game_tile_bag = TILES_BAG.clone

end

def draw_tiles(num)
sum_of_all_tiles = @game_tile_bag.values.reduce(:+)
if sum_of_all_tiles < num
raise Exception.new("Cannot draw more tiles than currently in bag.")
end
new_player_tiles = 0
alphabet_array = []
@game_tile_bag.map do |key, value|
value.times do
alphabet_array << key
# =>["A", "A", "A", "F", "F", "G"]
end
end

new_player_tiles = alphabet_array.sample(num) #array of drawn letters
new_player_tiles.each do |letter|
@game_tile_bag[letter] -= 1
end
return new_player_tiles #array of letters
end #draw_tiles



end
end
125 changes: 125 additions & 0 deletions specs/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
require_relative "../lib/player.rb"
require_relative 'spec_helper'
module Scrabble

describe Player do
describe "#initialize" do
let(:player) { Player.new(:name) }

it "can create an instance of Player" do
player.must_be_instance_of(Player)
end

it "must respond to name attribute" do
player.must_respond_to(:name)
end

end

describe "#plays" do
let(:player) { Player.new(:name) }
it "should return an array" do
player.plays.must_be_instance_of(Array)
end

it "should return an array of words/string" do
player.plays.each{|word| word.must_be_instance_of(String)}
end
end

describe "#play(word)" do
let(:player) { Player.new("Alma") }

it "should raise an error if word is not provided at all" do
proc{player.play('')}.must_raise(ArgumentError)

end

it "should append words to @words_played" do
player.play("dog")
player.play("foot")
"dog".must_equal(player.plays.first)
"foot".must_equal(player.plays.last)
end

it "method returns the score of a word from Scoring" do
player.play("imposter").must_equal(12)
end
end #play(word)

describe "#total_score" do
let(:player) { Player.new("Alma") }

it "should check total score from plays array" do
player.play("dog") #returns score = 5
player.play("foot") #returns => 7
player.play("coffee") #returns => 14
player.total_score.must_equal(26)
end

end #end describe total score

describe "#won?" do
let(:player) { Player.new("Alma") }

it "should return true if total player score is greater than 100" do
player.play("pizzazz")
player.play("coffee")
player.total_score #will return sum
player.won?.must_equal(true)
end
end #end won? method

describe "#highest_scoring_word" do
let(:player) { Player.new("Alma") }

it "should return the highest scoring word player played" do
player.play("pizzazz")
player.play("coffee")
player.highest_scoring_word.must_equal("pizzazz")
end
end #end highest scoring word method

describe "#highest_word_score" do
let(:player) { Player.new("Alma") }

it "should return the score of the highest scoring word player played" do
player.play("pizzazz")
player.play("coffee")
player.highest_word_score.must_equal(95)
end
end

describe "#tiles" do
let(:player) { Player.new("Alma") }

it "should return an array of letters" do
player.tiles.must_be_instance_of(Array)
end

it "should limit the tray to a max of 7" do
player.tiles.length.must_be(:<=, 7)
end
end

describe "#draw_tiles(tile_bag)" do
let(:player) { Player.new("Alma") }
it "should draw tiles until tray has 7 tiles" do
player.draw_tiles(TileBag.new)
player.tile_tray.length.must_equal(7)
end
end
end




#
# it "should not allow player to draw more tiles than tray_max" do
#
# end
# player need to care about their own tray



end
Loading