-
Notifications
You must be signed in to change notification settings - Fork 26
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
base: master
Are you sure you want to change the base?
Changes from all commits
e2c52df
224cb5d
7587430
8d37ff8
4fe7c59
a38c5e5
4f4eada
3548f89
7d32569
85f75ed
3a5b7c6
0892d57
9ae753e
62cb538
d47ddf1
985493a
7a621df
f75b789
5c19817
caace60
b113b7f
c732d94
b78233e
b889754
a4004b9
f86752a
56bc249
8675e20
d9c0355
bfd2068
7e69944
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
@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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good practice with |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't you just use the |
||
return Scrabble::Scoring.score(highest_word) | ||
end | ||
|
||
def highest_scoring_word | ||
return Scrabble::Scoring.highest_score_from(@words_played) | ||
end | ||
|
||
end # class | ||
end # module |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,115 @@ | ||
module Scrabble | ||
|
||
class Scoring | ||
SCORING_RUBRIK = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') |
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)}} # | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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