diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 00000000..f24df478 --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +scrabble diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..276cbf9e --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.0 diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..4c0a9830 --- /dev/null +++ b/Rakefile @@ -0,0 +1,9 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['spec/*-spec.rb'] +end + +task default: :test diff --git a/lib/player.rb b/lib/player.rb new file mode 100644 index 00000000..accb69ea --- /dev/null +++ b/lib/player.rb @@ -0,0 +1,62 @@ +class Player + attr_reader :name, :word + WIN_CONDITION = 100 + + def initialize(name) + @name = name + @words = [] + @word_score = 0 + end + + def plays + return @words + end + + def play(word) + if self.won? == true + return false + end + @word_score = Scoring.score(word) + @words << word + return @word_score + end + + def total_score + tally = 0 + @words.each do |word| + tally += Scoring.score(word) + end + return tally + end + + def won? + if total_score > WIN_CONDITION + return true + end + return false + end + + def highest_scoring_word + words_and_scores = { } + most = [] + @words.each do |word| + words_and_scores[word] = (Scoring.score(word)) + end + most = words_and_scores.max_by do |key, value| + value + end + return most[0] + end + + def highest_word_score + words_and_scores = { } + most = [] + @words.each do |word| + words_and_scores[Scoring.score(word)] = word + end + most = words_and_scores.max_by do |key, value| + key + end + return most[0] + end +end diff --git a/lib/scoring.rb b/lib/scoring.rb new file mode 100644 index 00000000..d999b1b9 --- /dev/null +++ b/lib/scoring.rb @@ -0,0 +1,43 @@ +class Scoring + SCORE_CHART = { + "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.check_if_letters(word) + !word.match(/[^A-Za-z]/) + end + + def self.score(word) + arr = word.downcase.split(//) + arr.length == 7 ? tally = 50 : tally = 0 + arr.each do |i| + value = SCORE_CHART[i] + tally = value + tally + end + return tally + end + + def self.highest_score_from(array_of_words) + high_word = "" + high_score = 0 + array_of_words.each do |word| + self.score(word) + if self.score(word) > high_score + high_word = word + high_score = self.score(word) + elsif self.score(word) == high_score + if word.length < high_word.length + high_word = word + high_score = self.score(word) + end + end + end + return high_word + end +end diff --git a/lib/tilebag.rb b/lib/tilebag.rb new file mode 100644 index 00000000..cc137960 --- /dev/null +++ b/lib/tilebag.rb @@ -0,0 +1,40 @@ +class TileBag + attr_reader :total_tiles + def initialize + @default_tiles = { + "A" => 9, "B" => 2, "C" => 2, + "D" => 4, "E" => 12, "F" => 2, + "G" => 3, "H" => 2, "I" => 9, + "J" => 1, "K" => 1, "L" => 4, + "M" => 2, "N" => 6, "O" => 8, + "P" => 2, "Q" => 1, "R" => 6, + "S" => 4, "T" => 6, "U" => 4, + "V" => 2, "W" => 2, "X" => 1, + "Y" => 2, "Z" => 1, + } + @total_tiles = 98 + end + + def draw_tiles(num) + your_tiles = [] + until your_tiles.length == num do + tiles_picked = @default_tiles.keys.sample + your_tiles << tiles_picked + value = @default_tiles[tiles_picked] + new_value = value - 1 + @default_tiles[tiles_picked] = new_value + end + + + #subtract tiles from @default_tiles + # sample = h.keys.sample example? + # h.select { |k,v| k == sample } + return your_tiles + end + + def tiles_remaining + @total_tiles -= self.draw_tiles(num).length + return @total_tiles + end + +end diff --git a/scrabble.rb b/scrabble.rb new file mode 100644 index 00000000..826ef672 --- /dev/null +++ b/scrabble.rb @@ -0,0 +1,14 @@ +require 'lib/scoring.rb' +require 'lib/player.rb' +require 'lib/tilebag.rb' + +module Scrabble + class Scoring + end + + class Player + end + + class TileBag + end +end diff --git a/spec/player-spec.rb b/spec/player-spec.rb new file mode 100644 index 00000000..31e9442d --- /dev/null +++ b/spec/player-spec.rb @@ -0,0 +1,97 @@ +require_relative './spec_helper' +require_relative '../lib/player' + +TEST_CASES = ["word", "butt", "fart", "red"] +WIN_CASE = ["cazique", "mezquit"] +WINNING = 100 + + +describe Player do + it "Does the Player Class exist test?" do + Player.wont_be_nil + end + + describe "Player#new(name)" do + it "Does the class initialize with a name" do + Player.new(name).wont_be_nil + end + end + + describe "#plays" do + test_player = Player.new(name) + it "Does it contain an array" do + test_player.plays.must_be_kind_of Array + end + end + + describe "#play(word)" do + + test_player = Player.new(name) + + # TEST_CASES.each do |played_word| + it "Does it add the played word?" do + test_player.play("word") + test_player.plays.must_equal(["word"]) + end + # end + end + + describe "#play(word)" do + + test_player = Player.new(name) + + TEST_CASES.each do |played_word| + it "Does it return the score of the word" do + test_player.play(played_word).must_equal(Scoring.score(played_word)) + end + end + end + + describe "#total_score" do + + test_player = Player.new(name) + + it "Does it return the total score" do + TEST_CASES.each do |word| + test_player.play(word) + end + test_player.total_score.must_equal(25) + end + end + + describe "#won?" do + + test_player = Player.new(name) + + it "Does it return true if the player won?" do + WIN_CASE.each do |word| + test_player.play(word) + end + assert test_player.won? == true + end + end + + describe "#highest_scoring_word" do + + test_player = Player.new(name) + + it "Does it return the highest scoring WORD" do + TEST_CASES.each do |word| + test_player.play(word) + end + test_player.highest_scoring_word.must_equal("word") + end + end + + describe "#highest_word_score" do + + test_player = Player.new(name) + + it "Does it return the highest word SCORE" do + TEST_CASES.each do |word| + test_player.play(word) + end + test_player.highest_word_score.must_equal(8) + end + end +end diff --git a/spec/scoring-spec.rb b/spec/scoring-spec.rb new file mode 100644 index 00000000..504ddd1f --- /dev/null +++ b/spec/scoring-spec.rb @@ -0,0 +1,52 @@ +require_relative './spec_helper' +require_relative '../lib/scoring' + +describe Scoring do + it "Does the Scoring Class exist?" do + Scoring.wont_be_nil + end + + describe "Scoring#score(word)" do + HASH_CASES = { + "cat" => 5, + "CAT" => 5, + "BOX" => 12, + "box" => 12, + "googles" => 59, + "GOOgles" => 59, + "m" => 3, + "f" => 4, + "v" => 4, + "k" => 5, + "j" => 8, + "z" => 10 + } + + HASH_CASES.each do |letters, score| + it "Does it assign a score number to each letter" do + Scoring.score(letters).must_equal(score) + end + end + end + + describe "Scoring#highest_score_from(array_of_words)" do + ARRAY_CASES = { + "cat" => ["cat"], + "box" => ["cat", "box"], + "box" => ["phone", "box", "cat"], + "purple" => ["purple", "farts", "hands", "butts"], + "hands" => ["hands", "violet"], + "hands" => ["violet", "hands"], + "jukebox" => ["jukebox", "cazique", "mezquit"], + "mezquit" => ["mezquit", "jukebox", "cazique"], + "buzzcut" => ["jukebox", "buzzcut"], + "buzzcut" => ["buzzcut", "jukebox"] + } + + ARRAY_CASES.each do |key, value| + it "Does it return the highest score" do + Scoring.highest_score_from(value).must_equal(key) + end + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..3950a123 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,10 @@ +require 'simplecov' +SimpleCov.start + +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' +require 'minitest/reporters' + +# give us some really pretty output :) +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/spec/tilebag-spec.rb b/spec/tilebag-spec.rb new file mode 100644 index 00000000..d5dedc94 --- /dev/null +++ b/spec/tilebag-spec.rb @@ -0,0 +1,36 @@ +require_relative './spec_helper' +require_relative '../lib/tilebag' + + +describe TileBag do + it "Does the TileBag Class exist test?" do + TileBag.wont_be_nil + end + + describe "#draw_tiles" do + it "Does it return number of random tiles" do + test_tiles = TileBag.new + tile_list = test_tiles.draw_tiles(7) + tile_list.length.must_equal(7) + end + end + + describe "#tiles_remaining" do + it "Does it return tiles remaining" do + test_tiles = TileBag.new + test_tiles.tiles_remaining.must_equal(91) + end + end +end + + + +#TRYING TO FIGURE OUT IF IT IS SUBTRACTING CORRECT TILES. CURRENTLY UNABLE TO MAKE IT WORK. WILL COME BACK. +# describe "#draw_tiles" do +# +# it "Does it subtract the correct tiles from tile chosen?" do +# test_tiles = TileBag.new +# test_tiles.draw_tiles(2).must_equal(3) +# +# end +# end