diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..189eb029 Binary files /dev/null and b/.DS_Store differ diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..58a329cd --- /dev/null +++ b/lib/player.rb @@ -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| + 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) + return Scrabble::Scoring.score(highest_word) + end + + def highest_scoring_word + return Scrabble::Scoring.highest_score_from(@words_played) + end + + end # class +end # module diff --git a/lib/scoring.rb b/lib/scoring.rb index fb3a3f2d..6ba8e111 100644 --- a/lib/scoring.rb +++ b/lib/scoring.rb @@ -1,9 +1,115 @@ module Scrabble + class Scoring + SCORING_RUBRIK = { + '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) + end + end + puts "#{highest_word}" + return highest_word + + end + #return highest_scoring_word + + end + + def self.break_tie(incumbent, challenger) + + 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') diff --git a/lib/tilebag.rb b/lib/tilebag.rb new file mode 100644 index 00000000..f535c753 --- /dev/null +++ b/lib/tilebag.rb @@ -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)}} # + end + + puts "#{@tile_bag}" + + 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) diff --git a/specs/player_spec.rb b/specs/player_spec.rb new file mode 100644 index 00000000..047b8d60 --- /dev/null +++ b/specs/player_spec.rb @@ -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 + 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 diff --git a/specs/scoring_spec.rb b/specs/scoring_spec.rb index ab498929..446a9171 100644 --- a/specs/scoring_spec.rb +++ b/specs/scoring_spec.rb @@ -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") + + 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 diff --git a/specs/tilebag_spec.rb b/specs/tilebag_spec.rb new file mode 100644 index 00000000..215fbf9b --- /dev/null +++ b/specs/tilebag_spec.rb @@ -0,0 +1,30 @@ +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/skip_dsl' + +require_relative '../lib/tilebag.rb' + +# Get that nice colorized output +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + +describe 'tile bag test' do + + it 'it returns 7 tiles' do + new_bag = Scrabble::Tilebag.new + + tiles = new_bag.draw_tiles(7) + + tiles.length.must_equal 7 + end + + # NOT FUNCTIONING + # xit 'Can not draw more tiles than is available' do + # # ['a', 'e', 'i', 'o'] + # new_bag = Scrabble::Tilebag.new + # new_bag.draw_tiles(100) + # + # new_bag.wont_be:>, 100 + # end + + +end diff --git a/wave-1-game.rb b/wave-1-game.rb index da13d000..126ca30a 100644 --- a/wave-1-game.rb +++ b/wave-1-game.rb @@ -2,10 +2,11 @@ module Scrabble class Game - def initialize + def initialize() @words = [] end + def play start