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

Cara & Emilce -- Scrabble -- Octos #15

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e2c52df
Passes first test
MississippiBrenn Feb 20, 2018
224cb5d
Added all letters and corresponding scores
Feb 20, 2018
7587430
2nd test passes
MississippiBrenn Feb 20, 2018
8d37ff8
Uncommented test
MississippiBrenn Feb 20, 2018
4fe7c59
Sorry, small & irrelevant changes
Feb 20, 2018
a38c5e5
Merge branch 'master' of https://github.com/emilcecarlisa/Scrabble
Feb 20, 2018
4f4eada
Passing the bad characters test
Feb 20, 2018
3548f89
Started writing our own tests.
MississippiBrenn Feb 20, 2018
7d32569
Merging changes
Feb 20, 2018
85f75ed
MERGING
Feb 21, 2018
3a5b7c6
Resolved MERGING
Feb 21, 2018
0892d57
Passed all tests. Wave 1 complete
Feb 21, 2018
9ae753e
Created player file and class
Feb 21, 2018
62cb538
Added player_spec file
Feb 21, 2018
d47ddf1
Verifying files available
Feb 21, 2018
985493a
Wave 2 first test passing
Feb 21, 2018
7a621df
End of day Wed
MississippiBrenn Feb 22, 2018
f75b789
Added method to sum the word scores
Feb 22, 2018
5c19817
Added sum test.
Feb 22, 2018
caace60
Added won method
MississippiBrenn Feb 22, 2018
b113b7f
Added the highest scoring word method and test
Feb 23, 2018
c732d94
All tests are passing at once Wave 2
MississippiBrenn Feb 23, 2018
b78233e
Added the Tilebag initialize
Feb 23, 2018
b889754
Wave 3, with one tess
MississippiBrenn Feb 24, 2018
a4004b9
Added functionality to draw random tiles and delete them from the til…
Feb 24, 2018
f86752a
Working on file to pass
MississippiBrenn Feb 27, 2018
56bc249
All wave 1 tests are passing
MississippiBrenn Feb 27, 2018
8675e20
Updated scoring tests and method
Feb 27, 2018
d9c0355
err
Feb 27, 2018
bfd2068
Adjusting player_spec tests(returns words played)
Feb 27, 2018
7e69944
highest word/highest score tests running
Feb 27, 2018
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.
69 changes: 69 additions & 0 deletions lib/player.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require_relative 'scoring.rb'
# require_relative 'wave-2-game.rb'
require 'pry'


module Scrabble
class Player
attr_reader :name, :words_played



def initialize(name)
@name = name
@words_played = []
end


def plays #(word)
return @words_played
# @words_played.push(word)
# return @words_played
end

def play(word)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem here, even if I've won it pushes the new word into @words_played

@words_played.push(word)

if won?
return false
else
return Scrabble::Scoring.score(word)
# self.plays(word)
# return a
end
end

def total_score()
word_scores = @words_played.map do |word|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good practice with .map and .inject

Scrabble::Scoring.score(word)
end
# @words_played.inject(0) do |sum, word|
# word_score = Scrabble::Scoring.score(word)
# #sum += word_score
# return sum
# end

# return sum
return word_scores.inject(:+)
end # method

def won?
if total_score > 100
has_won = true
else
has_won = false
end
return has_won
end

def highest_word_score
highest_word = Scrabble::Scoring.highest_score_from(@words_played)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you just use the highest_scoring_word here?

return Scrabble::Scoring.score(highest_word)
end

def highest_scoring_word
return Scrabble::Scoring.highest_score_from(@words_played)
end

end # class
end # module
110 changes: 108 additions & 2 deletions lib/scoring.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,115 @@
module Scrabble

class Scoring
SCORING_RUBRIK = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of a constant, and a good choice of data structure!

'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
}
def self.score(word)


word_score = 0

#for each has key

word.downcase.each_char do |letter|
return nil unless SCORING_RUBRIK.include?(letter)
word_score += SCORING_RUBRIK[letter]
end


if word.length == 7
word_score += 50
elsif word.length > 7
return nil
elsif word.length == 0
return nil
end
return word_score
end


def self.highest_score_from(array_of_words)

if array_of_words.empty?
return nil
else
highest_score = -1
highest_word = array_of_words.first
array_of_words.each do |word|
score = self.score(word)
if score > highest_score
highest_word = word
highest_score = score

elsif score == highest_score
# highest_word =
self.break_tie(highest_word, word)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, a helper method!

end
end
puts "#{highest_word}"
return highest_word

end
#return highest_scoring_word

end

def self.break_tie(incumbent, challenger)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done here

if incumbent.length == 7
return incumbent
elsif challenger.length == 7
return challenger
end

if challenger.length > incumbent.length
return incumbent
elsif challenger.length < incumbent.length
return challenger
elsif incumbent.length == challenger.length
return incumbent
end

end
end
end

# def highest_word_score
# word_scores = []
#
# @words_played.each do |word|
# score = Scrabble::Scoring.score(word)
# word_scores << score
# end
#
# return word_scores.max
# end

end # class Scoring
end # module Scrabble


puts score_word = Scrabble::Scoring.score('dog')
40 changes: 40 additions & 0 deletions lib/tilebag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Scrabble
class Tilebag

attr_reader :tile_bag

BAG = { 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 }

def initialize
#creates array which takes (letter, value) as index & tilebag as iterator
@tile_bag = BAG.each_with_object([]) { |(letter, value),tile_bag|
#iterate through the letters val times and shovel the
#letter to the individual letter tilebag
value.times { (tile_bag << letter.to_s)}} #

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small indentation error. The above loops also look awkward. They are however an awesome way to turn the hash above into an array.

end

puts "#{@tile_bag}"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

puts is unneeded here:


def draw_tiles(num)

players_tiles = []

num.times do
random_number = rand(0...tile_bag.length)

new_tile = tile_bag.delete_at(random_number)
if new_tile == nil
puts "No more tiles! "
else players_tiles << new_tile
end
end # times

return players_tiles

end # draw_tiles
end # class # Tilebag
end # module Scrabble


# x = Scrabble::Tilebag.new
# puts x.draw_tiles(3)
87 changes: 87 additions & 0 deletions specs/player_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'

require_relative '../lib/player.rb'

# Get that nice colorized output
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe 'player tests' do

it 'it takes a players name' do
# assemble
new_player = Scrabble::Player.new("Jill")
# act/assert
new_player.name.must_equal "Jill"

end

#this will only pass if there is nothing in the player_words variable to begin with
it 'returns words played' do
#act
new_word = 'bird'
#assert
player_d = Scrabble::Player.new('player_d')
player_d.play(new_word)
player_d.plays.must_equal ['bird']

end

it "returns false if has won == true" do
#assert
player_d = Scrabble::Player.new('player_d')

player_d.play('zzzzzz')
player_d.play('zzzzzz')

player_d.play('pie').must_equal false

end

it "returns score of a play/word " do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a test for what happens if you keep playing after you won?

word = 'pie'
#act
player_d = Scrabble::Player.new('player_d')

player_d.play(word).must_equal 5

# puts player_d.play(word)
end

it "sums the scores for player" do
player_d = Scrabble::Player.new('player_d')

player_d.play('dino') # 5
player_d.play('pie') # 5
player_d.play('sock') # 10
player_d.plays

player_d.total_score.must_equal 20
end

it "tells you if you won" do
player_d = Scrabble::Player.new('player_d')
player_d.play( 'zzzzzzz')

player_d.won?.must_equal true
end

it "returns the highest scoring word" do
player_d = Scrabble::Player.new('player_d')
player_d.play( 'apple')
player_d.play( 'zzzzzzz')

player_d.highest_scoring_word.must_equal 'zzzzzzz'
end

it "returns highest word score" do
player_d = Scrabble::Player.new('player_d')
player_d.play( 'zzzzzzz')
player_d.play( 'apple')

player_d.highest_word_score.must_equal 120
end


end
32 changes: 31 additions & 1 deletion specs/scoring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

require_relative '../lib/scoring'


# Get that nice colorized output
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe 'Scoring' do

describe 'score' do
it 'correctly scores simple words' do
Scrabble::Scoring.score('dog').must_equal 5
Scrabble::Scoring.score('cat').must_equal 5
Scrabble::Scoring.score('pig').must_equal 6

end

it 'adds 50 points for a 7-letter word' do
Expand Down Expand Up @@ -41,22 +44,49 @@
end

describe 'highest_score_from' do

it 'returns nil if no words were passed' do
#arrange
words = []
#act & #assert
Scrabble::Scoring.highest_score_from(words).must_be_nil

end

it 'returns the only word in a length-1 array' do
words = []
words << "pearl"
Scrabble::Scoring.highest_score_from(words).must_equal "pearl"
end

it 'returns the highest word if there are two words' do
words = []
words.push("zebra", "otter")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest also varying the order of the words since that makes a difference in a tie.


Scrabble::Scoring.highest_score_from(words).must_equal "zebra"
end

it 'if tied, prefer a word with 7 letters' do
words = []
words.push("mum", "agenda")

Scrabble::Scoring.highest_score_from(words).must_equal "agenda"
end

it 'if tied and no word has 7 letters, prefers the word with fewer letters' do
words = []
words.push("cat", "long")

Scrabble::Scoring.highest_score_from(words).must_equal "cat"
end

it 'returns the first word of a tie with same letter count' do
it 'returns the first word of a tie with same letter (count)' do
words = []
words.push("longa", "pie", "longe")

Scrabble::Scoring.highest_score_from(words).must_equal "longa"


end
end
end
Loading