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

Beylul & Danielle #14

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
31516d3
Created initial module/class and data structure and added specs files.
dschrimm Aug 29, 2016
d7141db
added the Rakefile, added one red test
Bella8 Aug 29, 2016
0156214
Added a spec and score method appears to accurately return total score.
dschrimm Aug 30, 2016
11ff60f
Added an ArgummentError and updated the test
Bella8 Aug 30, 2016
fde8a5d
Added 7 letter bonus feature and completed super baseline highest_sco…
dschrimm Aug 30, 2016
2f679de
Modified the highst score method
Bella8 Aug 30, 2016
134f680
Added tie logic to highest_score_from
dschrimm Aug 30, 2016
d63e9bc
Added more tests that check the correct input for method highest_scor…
Bella8 Aug 31, 2016
b5fa027
started player and player specs. initialize, plays, and play methods.
dschrimm Aug 31, 2016
603bc0d
Added the total_score method and tests
Bella8 Aug 31, 2016
abbde38
Add won and associated methods and specs.
dschrimm Aug 31, 2016
eb5c285
Modified the specs/spec_helper.rb
Bella8 Aug 31, 2016
82bdb29
Added highst_scoring_word and highst_word_score methods and tests
Bella8 Aug 31, 2016
b86a1ce
Working version of Wave 2
dschrimm Aug 31, 2016
5130558
Removed unnecessary plays method.
dschrimm Aug 31, 2016
1a6aba3
added gitignore
dschrimm Aug 31, 2016
cbe3889
Created the tilebag file and the specs tilebag file. Added one test a…
Bella8 Sep 1, 2016
acb407d
Add draw-tiles method and change list of available tiles to array
dschrimm Sep 1, 2016
5585516
Added draw_tiles(tile_bag) method and more tests
Bella8 Sep 1, 2016
7c8b9ae
Additional player methods tiles and draw_tiles and associated specs
dschrimm Sep 2, 2016
041a4da
Added more tests and fixed failing three failing tests.
Bella8 Sep 2, 2016
5d5f001
Accounted for checking tiles for double letter words
dschrimm Sep 2, 2016
0d3d760
Shorten letter initializer processs
dschrimm Sep 2, 2016
cbe1f28
Cleaned up the programs
Bella8 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
coverage/

*.gem
*.rbc
/.config
Expand Down
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
97 changes: 97 additions & 0 deletions player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
require_relative 'scoring'
require_relative 'tilebag'
module Scrabble
class Player
attr_reader :name
attr_accessor :plays, :total, :highest_word, :tiles

def initialize(name)
@name = name
@plays = []
@total = 0
@highest_word = ""
@tiles = []
end

def play(word)
# check if each letter of the word is included in the tiles list
a = word.scan /\w/
check_tiles = @tiles.clone
a.each do |i|
# if it's included, remove from temporary list to account for duplicate letters in a word
if check_tiles.include?(i)
check_tiles.delete_at(check_tiles.index(i))
# exit method and don't change list of tiles
else
puts "it's not a valid word"
return nil
# end
end
end

# update list of remaining tiles
@tiles = check_tiles
# add word to list of player's played words
@plays << word
# calculate and add word score
word_score = Scrabble::Scoring.score(word)
@total += word_score

if won?
return false
else
return word_score
end
end

def total_score
return @total
end

def won?
if @total > 100
return true
else
return false
end
end

def highest_scoring_word
@highest_word = Scrabble::Scoring.highest_score_from(@plays)
return @highest_word
end

def highest_word_score
highest_word = highest_scoring_word
highest_score = Scrabble::Scoring.score(highest_word)
return highest_score
end

# @tiles is an array of an individual player.
def tiles
if @tiles.length <= 7
return @tiles
else
puts "uh oh, something went wrong"
end
end

def draw_tiles(tile_bag)
# calculate number of tiles needed to fill rack
tiles_needed = 7 - @tiles.length
# if there are enough tiles to fill rack, fill it
if tiles_needed < tile_bag.tiles_remaining
tiles_needed.times do
@tiles << tile_bag.draw_tiles(1)
end
# if there are not enough tiles to fill the rack, take as many as are left
else
until tile_bag.tiles_remaining == 0
@tiles << tile_bag.draw_tiles(1)
end
end
@tiles.flatten!
return tiles
end
end
end
68 changes: 68 additions & 0 deletions scoring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Scrabble
class Scoring
attr_accessor :words
#letters and their corsponding scores in a hash
LETTERS = {1 => %w(A E I O U L N R S T), 2 => %w(D G), 3 => %w(B C M P), 4 => %w(F H V W Y), 5 => ["K"], 8 => %w(J X), 10 => %w(Q Z) }

def initialize
#
@words = []
end

def self.score(word)
word.upcase!
current_score = 0
word_letters = word.split("")

word_letters.each do |i|
if !LETTERS.values.join.include?(i)
raise ArgumentError.new("Invalid input")
end
LETTERS.values.length.times do |j|
if LETTERS.values[j].include?(i)
current_score += LETTERS.keys[j]
break
end
end
end

if word_letters.length == 7
current_score += 50
end

# @words << word
return current_score
end

def self.highest_score_from(array_of_words)
if array_of_words.class != Array || array_of_words == []
raise ArgumentError.new("Invalid input")
end
word_scores= {}
array_of_words.each do |i|
letter_score = score(i)
if word_scores.keys.include?(letter_score)
word_scores[letter_score] << i
else
word_scores[letter_score] = [i]
end
end
max_score = word_scores.keys.max
if word_scores[max_score].length == 1
return word_scores[max_score][0]
else
shortest_letters = 7
shortest_word = ""
word_scores[max_score].each do |arry_of_vals|
if arry_of_vals.length < shortest_letters
shortest_letters = arry_of_vals.length
shortest_word = arry_of_vals
elsif arry_of_vals.length == 7
return arry_of_vals
end
end
return shortest_word
end
end
end
end
134 changes: 134 additions & 0 deletions specs/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
require_relative 'spec_helper'
require_relative '../player'
require_relative '../tilebag'

describe Scrabble::Player do

p = Scrabble::Player.new("name")

describe "initialize" do
it "should be an instance of Player" do
p.must_be_instance_of(Scrabble::Player)
end

it "should initialize with only one parameter (name)" do
p.must_respond_to(:name)
end
end

describe "plays" do
it "should return an Array of the words played by the player" do
p.tiles = ["c","a","t","b","i","r","d"]
p.plays = []
p.play("cat")
p.play("bird")
p.plays.must_equal(["CAT", "BIRD"])
end
end

describe "play" do
it "should add the input word to the plays Array" do
p.tiles = ["d","o","g","b","i","r","d"]
p.plays = []
p.play("dog")
p.plays.must_equal(["DOG"])
end

it "should check availability of double-instances of letters in list" do
p.total = 0
p.tiles = ["h", "e", "l", "o"]
p.play("hello").must_equal(nil)
end

it "should return false if player has already won" do
p.tiles = ["a", "i", "h"]
p.total = 101
p.play("hi").must_equal(false)
end

it "should check if the the word is inculded in the tile bag, if not tell the player
he cant use the word and return nil" do
r = Scrabble::Player.new("name")
r.tiles = ["h", "e", "l", "l", "o","c", "w"]
r.play("son").must_equal(nil)
end

it "should add the word played into the the plays array" do
s = Scrabble::Player.new("name")
s.tiles = ["h", "e", "l", "l", "o","c", "w"]
s.play("cow")
s.plays.must_equal(["COW"])
end

it "should delete the letters included in the word used from the tile rack" do
r = Scrabble::Player.new("name")
r.tiles = ["h", "e", "l", "l", "o","c", "w"]
r.play("cow")
r.tiles.must_equal(["h", "e", "l", "l"])
end

it "should return word_score if player has not already won" do
p.tiles = ["h","i"]
p.total = 0
p.play("hi").must_equal(5)
end
end

describe "#total_score" do
it "should return the total score of played words" do
w = Scrabble::Player.new("name")
w.tiles = ["c","a","t","b","i","r"]
w.plays = []
w.total = 0
w.play("cat")
w.tiles = ["b", "i", "r", "d","a","n","d"]
w.play("birdand")
w.tiles = ["a", "n", "d","h","o","m","e"]
w.play("home")
w.total_score.must_equal(75)
end
end

describe "won?" do
it "should return true if player has over 100 points" do
p.total = 101
p.won?.must_equal(true)
end

it "should return false is player does not have over 100 points" do
p.total = 100
p.won?.must_equal(false)
end
end

describe "#highest_scoring_word" do
it "should return the highest scoring played word" do
@plays = []
p.plays = ["car", "birdand", "home"]
p.highest_scoring_word.must_equal("BIRDAND")
end
end

describe "#highest_word_score" do
it "should return the highst scoring word score" do
@plays = []
p.plays = ["car", "birdand", "home"]
p.highest_word_score.must_equal(61)
end
end

describe "#tiles" do
it "should have at most seven tiles at a time" do
p.tiles.length.must_be :<=, 7
end
end

describe "#draw_tiles" do
it "should fill the tiles array to 7 when there are tiles remaining in the tile bag" do
q = Scrabble::Player.new("name")
r = Scrabble::TileBag.new
q.draw_tiles(r)
q.tiles.length.must_equal(7)
end
end
end
45 changes: 45 additions & 0 deletions specs/scoring_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require_relative 'spec_helper'
require_relative '../scoring'

describe Scrabble::Scoring do

describe "#scoring" do

it "should return the total score value for the given word" do
Scrabble::Scoring.score("DOG").must_equal(5)
end

it "should raise an error when an invalid parameter is supplied" do
proc {Scrabble::Scoring.score("123")}.must_raise(ArgumentError)
end

it "should give a 50 point bonus for seven letter words" do
Scrabble::Scoring.score("bunnies").must_equal(59)
end

end

describe "#highest_score_from" do

it "should choose the shortest word in case of a tie" do
Scrabble::Scoring.highest_score_from(["aeoiu", "dog", "deee"]).must_equal("DOG")
end

it "should choose the 7 letter word over a shorter word in case of a tie" do
Scrabble::Scoring.highest_score_from(["ddda", "eeeeeee"]).must_equal("EEEEEEE")
end

it "should raise an error when an invalid parameter is supplied" do
proc {Scrabble::Scoring.highest_score_from({})}.must_raise(ArgumentError)
end
it "should choose the first word if their lengths are the same" do
Scrabble::Scoring.highest_score_from(["aaaa", "eeee"]).must_equal("AAAA")
end

it "should have a length of at least one string." do
proc {Scrabble::Scoring.highest_score_from([])}.must_raise(ArgumentError)
end

end

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

require 'minitest'
require 'minitest/spec'
require "minitest/autorun"
require "minitest/reporters"
require 'minitest/pride'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
Loading